|
|
|
@ -17,8 +17,13 @@ void setupEnergyMonitor();
|
|
|
|
|
// const char* test_client_cert = ""; //to verify the client
|
|
|
|
|
void setup()
|
|
|
|
|
{
|
|
|
|
|
Serial.begin(9600);
|
|
|
|
|
|
|
|
|
|
eicDataQueue = xQueueCreate( 50, sizeof( xformerMonitorData ) );
|
|
|
|
|
if (eicDataQueue == 0)
|
|
|
|
|
{
|
|
|
|
|
printf("Failed to create queue= %p\n", eicDataQueue);
|
|
|
|
|
}
|
|
|
|
|
// configure time
|
|
|
|
|
// TODO: make dst and timezone configurable
|
|
|
|
|
int timezone = 3;
|
|
|
|
@ -32,12 +37,12 @@ void setup()
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
// Start the DS18B20 sensors
|
|
|
|
|
monitorTempSensors.cabinet.begin();
|
|
|
|
|
monitorTempSensors.oil.begin();
|
|
|
|
|
// monitorTempSensors.cabinet.begin();
|
|
|
|
|
// monitorTempSensors.oil.begin();
|
|
|
|
|
|
|
|
|
|
// Get each DS18B20 sensors' address
|
|
|
|
|
monitorTempSensors.oil.getAddress(oilTempSensorAddr, 0);
|
|
|
|
|
monitorTempSensors.cabinet.getAddress(cabinetTempSensorAddr, 0);
|
|
|
|
|
// // Get each DS18B20 sensors' address
|
|
|
|
|
// monitorTempSensors.oil.getAddress(oilTempSensorAddr, 0);
|
|
|
|
|
// monitorTempSensors.cabinet.getAddress(cabinetTempSensorAddr, 0);
|
|
|
|
|
|
|
|
|
|
setupMQTTClient();
|
|
|
|
|
|
|
|
|
@ -50,7 +55,7 @@ void setup()
|
|
|
|
|
NULL, /* Task input parameter */
|
|
|
|
|
0, /* Priority of the task */
|
|
|
|
|
&taskReadEIC, /* Task handle. */
|
|
|
|
|
0); /* Core where the task should run */
|
|
|
|
|
1); /* Core where the task should run */
|
|
|
|
|
|
|
|
|
|
xTaskCreatePinnedToCore(
|
|
|
|
|
sendSensorDataOverMQTT, /* Function to implement the task */
|
|
|
|
@ -59,7 +64,7 @@ void setup()
|
|
|
|
|
NULL, /* Task input parameter */
|
|
|
|
|
0, /* Priority of the task */
|
|
|
|
|
&taskSendData, /* Task handle. */
|
|
|
|
|
1); /* Core where the task should run */
|
|
|
|
|
0); /* Core where the task should run */
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void connect()
|
|
|
|
@ -115,11 +120,11 @@ void loop()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// mqttClient.publish("xfmormermon", "buffer");
|
|
|
|
|
// // mqttClient.publish("xfmormermon", "buffer");
|
|
|
|
|
|
|
|
|
|
// // publish a message roughly every second.
|
|
|
|
|
Serial.println("Sleeping 10s");
|
|
|
|
|
delay(10000);
|
|
|
|
|
// Serial.println("Sleeping 10s");
|
|
|
|
|
// delay(10000);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void setupMQTTClient()
|
|
|
|
@ -161,8 +166,9 @@ void setupEnergyMonitor()
|
|
|
|
|
// readEICData: reads the EIC and inserts data into queue
|
|
|
|
|
void readEICData(void *pvParameters)
|
|
|
|
|
{
|
|
|
|
|
Serial.print("Task1 running on core ");
|
|
|
|
|
Serial.print("Task0 running on core ");
|
|
|
|
|
Serial.println(xPortGetCoreID());
|
|
|
|
|
delay(2000);
|
|
|
|
|
|
|
|
|
|
// Attach interrupt for reading data every one second
|
|
|
|
|
readEICTimer = timerBegin(0, 80, true);
|
|
|
|
@ -180,6 +186,7 @@ void sendSensorDataOverMQTT(void *pvParameters)
|
|
|
|
|
{
|
|
|
|
|
Serial.print("Task1 running on core ");
|
|
|
|
|
Serial.println(xPortGetCoreID());
|
|
|
|
|
delay(3000);
|
|
|
|
|
StaticJsonDocument<512> mqttJsonData;
|
|
|
|
|
JsonObject tempObj = mqttJsonData.createNestedObject("temps");
|
|
|
|
|
JsonObject powerObj = mqttJsonData.createNestedObject("power");
|
|
|
|
@ -189,6 +196,7 @@ void sendSensorDataOverMQTT(void *pvParameters)
|
|
|
|
|
int emptySpaces = uxQueueSpacesAvailable(eicDataQueue);
|
|
|
|
|
for (;;)
|
|
|
|
|
{
|
|
|
|
|
// Serial.println("hello from Sensor data");
|
|
|
|
|
if (messagesWaiting > 2)
|
|
|
|
|
{
|
|
|
|
|
xQueueReceive(eicDataQueue, &mqttSensorData, portMAX_DELAY);
|
|
|
|
@ -215,11 +223,11 @@ void sendSensorDataOverMQTT(void *pvParameters)
|
|
|
|
|
|
|
|
|
|
char buffer[512];
|
|
|
|
|
size_t n = serializeJson(mqttJsonData, buffer);
|
|
|
|
|
delay(50);
|
|
|
|
|
mqttClient.publish("xfmormermon/", buffer, n);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
delay(1000); // <- fixes some issues with WiFi stability
|
|
|
|
|
mqttClient.loop();
|
|
|
|
|
delay(10); // <- fixes some issues with WiFi stability
|
|
|
|
|
|
|
|
|
|
if (!mqttClient.connected())
|
|
|
|
|
{
|
|
|
|
@ -241,12 +249,12 @@ void IRAM_ATTR ReadData(){
|
|
|
|
|
if (timesEnteredISR == 60)
|
|
|
|
|
{
|
|
|
|
|
timesEnteredISR = 0;
|
|
|
|
|
monitorTempSensors.cabinet.requestTemperatures();
|
|
|
|
|
monitorTempSensors.oil.requestTemperatures();
|
|
|
|
|
// monitorTempSensors.cabinet.requestTemperatures();
|
|
|
|
|
// monitorTempSensors.oil.requestTemperatures();
|
|
|
|
|
// get cabinet temp sensor data
|
|
|
|
|
sensorData.temps.cabinet = monitorTempSensors.cabinet.getTempC(cabinetTempSensorAddr);
|
|
|
|
|
// get oil temp sensor data
|
|
|
|
|
sensorData.temps.oil = monitorTempSensors.oil.getTempC(oilTempSensorAddr);
|
|
|
|
|
// sensorData.temps.cabinet = monitorTempSensors.cabinet.getTempC(cabinetTempSensorAddr);
|
|
|
|
|
// // get oil temp sensor data
|
|
|
|
|
// sensorData.temps.oil = monitorTempSensors.oil.getTempC(oilTempSensorAddr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -260,5 +268,5 @@ void IRAM_ATTR ReadData(){
|
|
|
|
|
// sensorData.energy.exp =
|
|
|
|
|
|
|
|
|
|
xQueueSend(eicDataQueue, &sensorData, portMAX_DELAY);
|
|
|
|
|
|
|
|
|
|
Serial.println("hello from ISR");
|
|
|
|
|
}
|