Upload files to "json"
This commit is contained in:
@@ -0,0 +1,292 @@
|
||||
#include <DHTesp.h>
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <WiFiClient.h>
|
||||
#include <ArduinoOTA.h>
|
||||
#include <ArduinoJson.h>
|
||||
#include <PubSubClient.h>
|
||||
#include <WiFiUdp.h>
|
||||
|
||||
#define DHTPIN0 D0
|
||||
#define HostName "GH"
|
||||
#define Heater D3
|
||||
#define Ventilator D4
|
||||
#define Reset D5
|
||||
|
||||
#ifndef STASSID
|
||||
#define STASSID "lab"
|
||||
#define STAPSK "ksg1019!"
|
||||
#endif
|
||||
|
||||
const char *ssid = STASSID;
|
||||
const char *password = STAPSK;
|
||||
long int start_time;
|
||||
char msg[500];
|
||||
float temp_GH, humidity_GH, soil_moisture_GH=60.00 , temp_Env, humidity_Env, soil_moisture_Env=60.00;
|
||||
uint8_t Control, temp_c, vent_c, irrigate_c, error_code;
|
||||
char which[4];
|
||||
unsigned int localPort = 8888;
|
||||
char packetBuffer[UDP_TX_PACKET_MAX_SIZE];
|
||||
//char packetBuffer2[UDP_TX_PACKET_MAX_SIZE];
|
||||
|
||||
DynamicJsonDocument doc(500);
|
||||
|
||||
JsonArray data = doc.createNestedArray("data");
|
||||
JsonObject data_obj = data.createNestedObject();
|
||||
JsonArray gh = data_obj.createNestedArray("Green House");
|
||||
JsonObject gh_obj = gh.createNestedObject();
|
||||
JsonArray env = data_obj.createNestedArray("Environment");
|
||||
JsonObject env_obj = env.createNestedObject();
|
||||
JsonArray sunroom = data_obj.createNestedArray("SunRoom");
|
||||
JsonObject sunroom_obj = sunroom.createNestedObject();
|
||||
JsonArray control_arr = doc.createNestedArray("Control");
|
||||
JsonObject control_obj = control_arr.createNestedObject();
|
||||
|
||||
StaticJsonDocument<200> Received_JSON;
|
||||
|
||||
DHTesp dht0;
|
||||
WiFiUDP Udp;
|
||||
|
||||
IPAddress ip(192,168,0,200);
|
||||
IPAddress gateway(192,168,0,1);
|
||||
IPAddress subnet(255,255,255,0);
|
||||
|
||||
const char* mqtt_server = "192.168.0.228";
|
||||
WiFiClient mqttClient;
|
||||
PubSubClient client(mqttClient);
|
||||
|
||||
void control(void);
|
||||
void getData(void);
|
||||
void getControl(void);
|
||||
void buildJson(void);
|
||||
void reconnect(void);
|
||||
void parseJson(char*, byte*, unsigned int);
|
||||
void OTA_setup(void);
|
||||
|
||||
void setup()
|
||||
{
|
||||
temp_c = 1;
|
||||
vent_c = 35;
|
||||
irrigate_c = 50;
|
||||
|
||||
pinMode(Heater, OUTPUT);
|
||||
pinMode(Ventilator, OUTPUT);
|
||||
pinMode(Reset, OUTPUT);
|
||||
pinMode(A0, INPUT);
|
||||
|
||||
digitalWrite(Heater, HIGH);
|
||||
digitalWrite(Ventilator, HIGH);
|
||||
digitalWrite(Reset, HIGH);
|
||||
|
||||
dht0.setup(DHTPIN0, DHTesp::DHT22);
|
||||
|
||||
Serial.begin(115200);
|
||||
delay(1000);
|
||||
|
||||
WiFi.config(ip, gateway, subnet);
|
||||
WiFi.mode(WIFI_STA);
|
||||
WiFi.begin(ssid, password);
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(500); Serial.print(".");
|
||||
}
|
||||
Serial.print("Connected to ");
|
||||
Serial.println(ssid);
|
||||
Serial.print("IP address: ");
|
||||
Serial.println(WiFi.localIP());
|
||||
|
||||
client.setServer(mqtt_server, 1883);
|
||||
client.setCallback(parseJson);
|
||||
|
||||
OTA_setup();
|
||||
start_time = millis();
|
||||
Udp.begin(localPort);
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
if (!client.connected()) {
|
||||
reconnect();
|
||||
}
|
||||
client.loop();
|
||||
|
||||
long int now_time = millis();
|
||||
ArduinoOTA.handle();
|
||||
getControl();
|
||||
control();
|
||||
if(now_time - start_time > 4900){
|
||||
client.subscribe("Env. Data");
|
||||
delay(50);
|
||||
client.subscribe("SunRoom. Data");
|
||||
delay(50);
|
||||
getData();
|
||||
control();
|
||||
buildJson();
|
||||
//serializeJsonPretty(doc, Serial);
|
||||
serializeJson(doc, msg);
|
||||
if(bitRead(error_code, 4)) {
|
||||
bitClear(error_code, 4);
|
||||
client.publish("GH", msg);
|
||||
}
|
||||
start_time = now_time;
|
||||
}
|
||||
}
|
||||
|
||||
void control(){
|
||||
if ( ! bitRead(Control, 3) ) {
|
||||
if ( (temp_c - 1) > temp_GH ) bitSet(Control, 1);
|
||||
if ( temp_GH > temp_c ) bitClear(Control, 1);
|
||||
if ( irrigate_c > soil_moisture_GH ) bitSet(Control, 0);
|
||||
if ( soil_moisture_GH > (irrigate_c + 20)) bitClear(Control, 0);
|
||||
if ( irrigate_c > soil_moisture_Env ) bitSet(Control, 2);
|
||||
if ( soil_moisture_Env > (irrigate_c + 20)) bitClear(Control, 2);
|
||||
}
|
||||
else {
|
||||
if ( temp_GH > (temp_c + 5 ) ) bitClear(Control, 1);
|
||||
if ( (temp_c - 5 ) > temp_GH ) bitSet(Control, 1);
|
||||
if ( irrigate_c - 30 > soil_moisture_GH ) bitSet(Control, 0);
|
||||
if ( soil_moisture_GH > (irrigate_c + 30)) bitClear(Control, 0);
|
||||
if ( irrigate_c - 30 > soil_moisture_Env ) bitSet(Control, 2);
|
||||
if ( soil_moisture_Env > (irrigate_c + 30) ) bitClear(Control, 2);
|
||||
}
|
||||
|
||||
if (bitRead(Control, 1)) digitalWrite(Heater, LOW);
|
||||
else digitalWrite(Heater, HIGH);
|
||||
|
||||
if(bitRead(Control, 4 )) {
|
||||
delay(1000);
|
||||
//Serial.println("Resetting....");
|
||||
Control &= B01111;
|
||||
digitalWrite(Reset, LOW);
|
||||
delay(1000);
|
||||
}
|
||||
}
|
||||
|
||||
void getData(){
|
||||
int soil;
|
||||
float h1, t1;
|
||||
|
||||
strcpy(which, "GH");
|
||||
t1 = dht0.getTemperature();
|
||||
delay(50);
|
||||
h1 = dht0.getHumidity();
|
||||
delay(50);
|
||||
soil = analogRead(A0);
|
||||
delay(50);
|
||||
soil_moisture_GH = map(soil, 900, 400, 0, 100);
|
||||
temp_GH = t1;
|
||||
humidity_GH = h1;
|
||||
if (isnan(t1) || isnan(h1)) bitSet(error_code, 7);
|
||||
else bitClear(error_code, 7);
|
||||
bitClear(error_code,0);
|
||||
}
|
||||
|
||||
|
||||
void getControl(){
|
||||
while(Udp.parsePacket()){
|
||||
Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE);
|
||||
switch (*packetBuffer) {
|
||||
case 'i': bitSet(Control, 0); break;
|
||||
case 'h': bitSet(Control, 1); break;
|
||||
case 'v': bitSet(Control, 2); break;
|
||||
case 'C': bitSet(Control, 3); break;
|
||||
case 'I': bitClear(Control, 0); break;
|
||||
case 'H': bitClear(Control, 1); break;
|
||||
case 'V': bitClear(Control, 2); break;
|
||||
case 'c': bitClear(Control, 3); break;
|
||||
case '+': temp_c += 1; break;
|
||||
case '-': temp_c -= 1; break;
|
||||
case '1': vent_c += 1; break;
|
||||
case '2': vent_c -= 1; break;
|
||||
case '3': irrigate_c += 1; break;
|
||||
case '4': irrigate_c -= 1; break;
|
||||
case 'r': bitSet(Control, 4); break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void buildJson(){
|
||||
doc["Title"] = "Gardner";
|
||||
doc["Time"] = "time";
|
||||
gh_obj["Temperature(C)"] = temp_GH;
|
||||
gh_obj["Humidity(%)"] = humidity_GH;
|
||||
gh_obj["Soil Moisture(%)"] = soil_moisture_GH;
|
||||
control_obj["Control Bit(8)"] = Control;
|
||||
control_obj["Heater Trigger Temperature(C)"] = temp_c;
|
||||
control_obj["Ventilator Trigger Temperature(C)"] = vent_c;
|
||||
control_obj["Irrigator Trigger Humidity(%)"] = irrigate_c ;
|
||||
doc["Error Code"][0] = error_code;
|
||||
doc["Sent From"] = which;
|
||||
}
|
||||
|
||||
void parseJson(char* topic, byte* payload, unsigned int length) {
|
||||
deserializeJson(Received_JSON, payload);
|
||||
if( Received_JSON["Sent From"] == "Env" ) {
|
||||
env_obj["Temperature(C)"] = Received_JSON["Temperature"];
|
||||
env_obj["Humidity(%)"] = Received_JSON["Humidity"];
|
||||
env_obj["Soil Moisture(%)"] = Received_JSON["Soil Mositure"];
|
||||
soil_moisture_Env = Received_JSON["Soil Mositure"];
|
||||
uint8_t error = Received_JSON["Error Code"];
|
||||
if( bitRead(error, 6) ) bitSet(error_code, 6);
|
||||
else bitClear(error_code, 6);
|
||||
if( bitRead(error, 4) ) bitSet(error_code, 4);
|
||||
}
|
||||
if( Received_JSON["Sent From"] == "SunRoom" ) {
|
||||
sunroom_obj["Temperature(C)"] = Received_JSON["Temperature"];
|
||||
sunroom_obj["Humidity(%)"] = Received_JSON["Humidity"];
|
||||
sunroom_obj["Soil Moisture(%)"] = Received_JSON["Soil Mositure"];
|
||||
uint8_t error = Received_JSON["Error Code"];
|
||||
if( bitRead(error, 5) ) bitSet(error_code, 5);
|
||||
else bitClear(error_code, 5);
|
||||
if( bitRead(error, 4) ) bitSet(error_code, 4);
|
||||
}
|
||||
}
|
||||
|
||||
void reconnect() {
|
||||
while (!client.connected()) {
|
||||
//Serial.print("Attempting MQTT connection...");
|
||||
if (client.connect("mqttClient")) {
|
||||
//Serial.println("connected");
|
||||
}
|
||||
else {
|
||||
//Serial.print("failed, rc=");
|
||||
//Serial.print(client.state());
|
||||
//Serial.println(" try again in 5 seconds");
|
||||
delay(5000);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void OTA_setup(){
|
||||
ArduinoOTA.setHostname(HostName);
|
||||
ArduinoOTA.setPassword("7788");
|
||||
ArduinoOTA.onStart([]() {
|
||||
String type;
|
||||
if (ArduinoOTA.getCommand() == U_FLASH) {
|
||||
type = "sketch";
|
||||
} else { // U_SPIFFS
|
||||
type = "filesystem";
|
||||
}
|
||||
Serial.println("Start updating " + type);
|
||||
});
|
||||
ArduinoOTA.onEnd([]() {
|
||||
Serial.println("\nEnd");
|
||||
});
|
||||
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
|
||||
Serial.printf(".");if (progress == 100) Serial.printf("\n");
|
||||
});
|
||||
ArduinoOTA.onError([](ota_error_t error) {
|
||||
Serial.printf("Error[%u]: ", error);
|
||||
if (error == OTA_AUTH_ERROR) {
|
||||
Serial.println("Auth Failed");
|
||||
} else if (error == OTA_BEGIN_ERROR) {
|
||||
Serial.println("Begin Failed");
|
||||
} else if (error == OTA_CONNECT_ERROR) {
|
||||
Serial.println("Connect Failed");
|
||||
} else if (error == OTA_RECEIVE_ERROR) {
|
||||
Serial.println("Receive Failed");
|
||||
} else if (error == OTA_END_ERROR) {
|
||||
Serial.println("End Failed");
|
||||
}
|
||||
});
|
||||
ArduinoOTA.begin();
|
||||
}
|
||||
Reference in New Issue
Block a user