Skip to content

Commit

Permalink
updated fw to save and send relay states and settings params
Browse files Browse the repository at this point in the history
  • Loading branch information
Nauman3S committed Dec 22, 2021
1 parent b635c64 commit 3732722
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 12 deletions.
56 changes: 49 additions & 7 deletions Firmware/Firmware.ino
Original file line number Diff line number Diff line change
@@ -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 <FS.h> //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;
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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);

Expand Down
30 changes: 30 additions & 0 deletions Firmware/MQTTFuncs.h
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand All @@ -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));
Expand All @@ -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);
Expand Down Expand Up @@ -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...";

Expand Down
14 changes: 9 additions & 5 deletions Firmware/dataHandler.h
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
//{"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 <ArduinoJson.h>
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");
// doc["MAC_Address"] =myMac;
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();
Expand All @@ -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: ");
Expand Down
3 changes: 3 additions & 0 deletions Firmware/headers.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 3732722

Please sign in to comment.