diff --git a/Firmware/Firmware.ino b/Firmware/Firmware.ino index 6defa30..6663aa8 100644 --- a/Firmware/Firmware.ino +++ b/Firmware/Firmware.ino @@ -1,11 +1,12 @@ -#include "headers.h" //all misc. headers and functions +#include "headers.h" //all misc. headers and functions +#include "dataHandler.h" #include "MQTTFuncs.h" //MQTT related functions #include "webApp.h" //Captive Portal webpages #include //ESP32 File System #include "commHandler.h" #include "BMEHandler.h" #include "soilMoistureHandler.h" -#include "dataHandler.h" + IPAddress ipV(192, 168, 4, 1); char npkData[2024] = {"0.0,0.0,0.0"}; TaskHandle_t npkTask; @@ -130,10 +131,12 @@ void setup() //main setup functions setupBME280(); setupSoilMoisture(); - pinMode(R1,OUTPUT); - pinMode(R2,OUTPUT); - pinMode(R3,OUTPUT); - + Serial.print("MAC Address: "); + Serial.println(ss.getMacAddress()); + pinMode(R1, OUTPUT); + pinMode(R2, OUTPUT); + pinMode(R3, OUTPUT); + queue = xQueueCreate(1, sizeof(npkData)); if (queue == NULL) { @@ -283,8 +286,47 @@ void loop() // mqttPublish("smart-agri/" + String(hostName) + String("soilMoisture/"), String(getSoilMoisture())); //publish data to mqtt broker String bme = getBMEVal(); getNPKData(); + payloadVal[0] = ss.StringSeparator(bme, ';', 0); + payloadVal[1] = ss.StringSeparator(bme, ';', 1); + payloadVal[2] = ss.StringSeparator(bme, ';', 2); + payloadVal[3] = String(getSoilMoisture()); + payloadVal[4] = String("0.0"); + payloadVal[5] = String("0.0"); + payloadVal[6] = getN(); + payloadVal[7] = getP(); + payloadVal[8] = getK(); + + if (digitalRead(R1) == 1) + { + payloadVal[9] = "On"; + } + else + { + payloadVal[9] = "Off"; + } + + if (digitalRead(R2) == 1) + { + payloadVal[10] = "On"; + } + else + { + payloadVal[10] = "Off"; + } + + if (digitalRead(R3) == 1) + { + payloadVal[11] = "On"; + } + else + { + payloadVal[11] = "Off"; + } + + + payloadVal[12] = settingsMsg; - sendData(ss.StringSeparator(bme, ';', 0), ss.StringSeparator(bme, ';', 1), ss.StringSeparator(bme, ';', 2), String(getSoilMoisture()), String("0.0"), String("0.0"), getN(), getP(), getK()); + sendData(ss.StringSeparator(bme, ';', 0), ss.StringSeparator(bme, ';', 1), ss.StringSeparator(bme, ';', 2), String(getSoilMoisture()), String("0.0"), String("0.0"), getN(), getP(), getK(), payloadVal[9],payloadVal[10],payloadVal[11], settingsMsg); //send values ledState(ACTIVE_MODE); diff --git a/Firmware/MQTTFuncs.h b/Firmware/MQTTFuncs.h index 4f33217..a1acd8c 100644 --- a/Firmware/MQTTFuncs.h +++ b/Firmware/MQTTFuncs.h @@ -37,6 +37,33 @@ void callback(char *topic, byte *payload, unsigned int length) Serial.print(topic); Serial.print("] "); String pLoad = ""; + if (digitalRead(R1) == 1) + { + payloadVal[9] = "On"; + } + else + { + payloadVal[9] = "Off"; + } + + if (digitalRead(R2) == 1) + { + payloadVal[10] = "On"; + } + else + { + payloadVal[10] = "Off"; + } + + if (digitalRead(R3) == 1) + { + payloadVal[11] = "On"; + } + else + { + payloadVal[11] = "Off"; + } + for (int i = 0; i < length; i++) { Serial.print((char)payload[i]); @@ -58,6 +85,7 @@ void callback(char *topic, byte *payload, unsigned int length) } else if (String(topic) == topicR) { + sendData(payloadVal[0], payloadVal[1], payloadVal[2], payloadVal[3], payloadVal[4], payloadVal[5], payloadVal[6], payloadVal[7], payloadVal[8], payloadVal[9],payloadVal[10],payloadVal[11], settingsMsg); if (pLoad.indexOf("1") >= 0) { digitalWrite(R1, !digitalRead(R1)); @@ -74,6 +102,7 @@ void callback(char *topic, byte *payload, unsigned int length) else if (String(topic) == topicS) { + sendData(payloadVal[0], payloadVal[1], payloadVal[2], payloadVal[3], payloadVal[4], payloadVal[5], payloadVal[6], payloadVal[7], payloadVal[8], payloadVal[9],payloadVal[10],payloadVal[11], pLoad); if (pLoad.indexOf("soil_sensor=") >= 0) { String temp = ss.StringSeparator(pLoad, '=', 1); @@ -137,6 +166,7 @@ bool mqttConnect() mqttClient.setServer(ip, 1883); mqttClient.setCallback(callback); + mqttClient.setBufferSize(2024); Serial.println(String("Attempting MQTT broker:") + String("smart-agri Broker")); internetStatus = "Connecting..."; diff --git a/Firmware/dataHandler.h b/Firmware/dataHandler.h index a160ca2..b29b73b 100644 --- a/Firmware/dataHandler.h +++ b/Firmware/dataHandler.h @@ -1,9 +1,9 @@ //{"FW_Version":"v1.0","macAddress":"240AC4AFDBDB9C","Environment":[{"Temperautre":29.3,"Humidity":62,"Atmospheric_Pressure":34}],"SoilParameters":[{"Soil_Moisture":0,"EC":0,"pH":0,"Nitrogen":0,"Phosphorus":0,"Potassium":0}]} #include -DynamicJsonDocument doc(1024); +DynamicJsonDocument doc(2024); -char jsonDoc[1024]; -void genJSON(String temp = String("0.0"), String humid = String("0.0"), String pressure = String("0.0"), String soil_moisture = String("0.0"), String ec = String("0.0"), String ph = String("0.0"), String nitrogen = String("0.0"), String phosphorus = String("0.0"), String potassium = String("0.0")) +char jsonDoc[2024]; +void genJSON(String temp = String("0.0"), String humid = String("0.0"), String pressure = String("0.0"), String soil_moisture = String("0.0"), String ec = String("0.0"), String ph = String("0.0"), String nitrogen = String("0.0"), String phosphorus = String("0.0"), String potassium = String("0.0"),String rel1=String("Off"), String rel2=String("Off"), String rel3=String("Off"),String msg=String("Off")) { // doc["Timestamp"] = String("12/2/2 03:11:33"); @@ -11,6 +11,10 @@ void genJSON(String temp = String("0.0"), String humid = String("0.0"), String p doc["FW_Version"] = String("v1.0"); doc["macAddress"] = ss.getMacAddress(); + doc["relay1"] = rel1; + doc["relay2"] = rel2; + doc["relay3"] = rel3; + doc["msg"] = msg; doc["Environment"][0]["Temperautre"] = temp.toFloat(); doc["Environment"][0]["Humidity"] = humid.toFloat(); doc["Environment"][0]["Atmospheric_Pressure"] = pressure.toFloat(); @@ -24,9 +28,9 @@ void genJSON(String temp = String("0.0"), String humid = String("0.0"), String p serializeJson(doc, Serial); } -void sendData(String temp = String("0.0"), String humid = String("0.0"), String pressure = String("0.0"), String soil_moisture = String("0.0"), String ec = String("0.0"), String ph = String("0.0"), String nitrogen = String("0.0"), String phosphorus = String("0.0"), String potassium = String("0.0")) +void sendData(String temp = String("0.0"), String humid = String("0.0"), String pressure = String("0.0"), String soil_moisture = String("0.0"), String ec = String("0.0"), String ph = String("0.0"), String nitrogen = String("0.0"), String phosphorus = String("0.0"), String potassium = String("0.0"),String rel1=String("Off"), String rel2=String("Off"), String rel3=String("Off"),String msg=String("Off")) { - genJSON(temp, humid, pressure, soil_moisture, ec, ph, nitrogen, phosphorus, potassium); + genJSON(temp, humid, pressure, soil_moisture, ec, ph, nitrogen, phosphorus, potassium, rel1, rel2, rel3, msg); serializeJson(doc, jsonDoc); String topicP = String("data/") + ss.getMacAddress(); Serial.print("Publishing on: "); diff --git a/Firmware/headers.h b/Firmware/headers.h index 69441d1..d0bf55a 100644 --- a/Firmware/headers.h +++ b/Firmware/headers.h @@ -6,10 +6,13 @@ String apid; String hostName = "SmartA"; String apPass; String settingsPass; +String settingsMsg=""; +String payloadVal[14]={"0.0","0.0","0.0","0.0","0.0","0.0","0.0","0.0","0.0","Off","Off","Off","null"}; #define R1 26 #define R2 25 #define R3 33 + int soil_sensorCalibValues[2]={550,0};//low,high #if defined(ARDUINO_ARCH_ESP8266)