Upload files to "socket"
This commit is contained in:
@@ -0,0 +1,264 @@
|
|||||||
|
#include <DHTesp.h>
|
||||||
|
#include <ESP8266WiFi.h>
|
||||||
|
#include <ArduinoOTA.h>
|
||||||
|
#include <ArduinoJson.h>
|
||||||
|
#include <WebSocketsClient.h>
|
||||||
|
#include <SocketIOclient.h>
|
||||||
|
#include <Hash.h>
|
||||||
|
|
||||||
|
#define HostName "GreenHouse"
|
||||||
|
#define DHTPIN0 D4
|
||||||
|
#define Heater D5
|
||||||
|
#define Irrigator D6
|
||||||
|
#define Ventilator D7
|
||||||
|
#define Reset D8
|
||||||
|
|
||||||
|
DHTesp dht0;
|
||||||
|
|
||||||
|
SocketIOclient socketIO;
|
||||||
|
|
||||||
|
void control(void);
|
||||||
|
String getData(void);
|
||||||
|
void sendData(String, String);
|
||||||
|
void socketIOEvent(socketIOmessageType_t, uint8_t *, size_t );
|
||||||
|
void OTA(void);
|
||||||
|
|
||||||
|
//const char* ssid = "olleh_WiFi_0886";
|
||||||
|
//const char* password = "0000009554";
|
||||||
|
//const char* websockets_server = "192.168.10.243";
|
||||||
|
//const char* websockets_server = "192.168.10.51";
|
||||||
|
const uint16_t websockets_port = 3000;
|
||||||
|
const char* ssid = "lab";
|
||||||
|
const char* password = "ksg1019!";
|
||||||
|
const char* websockets_server = "192.168.0.187";
|
||||||
|
|
||||||
|
IPAddress ip(192,168,0,200);
|
||||||
|
IPAddress gateway(192,168,0,1);
|
||||||
|
IPAddress subnet(255,255,255,0);
|
||||||
|
|
||||||
|
struct dataStruct{
|
||||||
|
float temp_GH, humidity_GH, soil_humidity_GH, temp_Env, humidity_Env, soil_humidity_Env;
|
||||||
|
int control, temp_c, vent_c, irrigate_gh, irrigate_env;
|
||||||
|
}Data;
|
||||||
|
|
||||||
|
String msg, Event;
|
||||||
|
|
||||||
|
void setup()
|
||||||
|
{
|
||||||
|
Data.temp_c = 1;
|
||||||
|
Data.vent_c = 35;
|
||||||
|
Data.irrigate_gh = 50;
|
||||||
|
Data.irrigate_env = 50;
|
||||||
|
Data.control = 8;
|
||||||
|
|
||||||
|
delay(1000);
|
||||||
|
dht0.setup(DHTPIN0, DHTesp::DHT22);
|
||||||
|
|
||||||
|
pinMode(Heater, OUTPUT);
|
||||||
|
pinMode(Irrigator, OUTPUT);
|
||||||
|
pinMode(Ventilator, OUTPUT);
|
||||||
|
pinMode(Reset, OUTPUT);
|
||||||
|
|
||||||
|
digitalWrite(Heater, HIGH);
|
||||||
|
digitalWrite(Irrigator, HIGH);
|
||||||
|
digitalWrite(Ventilator, HIGH);
|
||||||
|
digitalWrite(Reset, HIGH);
|
||||||
|
|
||||||
|
Serial.begin(115200);
|
||||||
|
delay(1000);
|
||||||
|
|
||||||
|
WiFi.config(ip, gateway, subnet);
|
||||||
|
WiFi.mode(WIFI_STA);
|
||||||
|
WiFi.begin(ssid, password);
|
||||||
|
|
||||||
|
while (WiFi.status() != WL_CONNECTED) {
|
||||||
|
delay(100); Serial.print(".");
|
||||||
|
}
|
||||||
|
String ip = WiFi.localIP().toString();
|
||||||
|
Serial.printf("[SETUP] WiFi Connected %s\n", ip.c_str());
|
||||||
|
|
||||||
|
OTA();
|
||||||
|
|
||||||
|
socketIO.begin(websockets_server, websockets_port, "/socket.io/?EIO=4");
|
||||||
|
delay(1000);
|
||||||
|
socketIO.onEvent(socketIOEvent);
|
||||||
|
}
|
||||||
|
|
||||||
|
void loop()
|
||||||
|
{
|
||||||
|
ArduinoOTA.handle();
|
||||||
|
socketIO.loop();
|
||||||
|
|
||||||
|
if(Event == "data") {
|
||||||
|
Serial.println(msg);
|
||||||
|
sendData("Data1", getData());
|
||||||
|
control();
|
||||||
|
Event = "";
|
||||||
|
}
|
||||||
|
|
||||||
|
if(Event == "Ok") {
|
||||||
|
Serial.println(msg);
|
||||||
|
Event = "";
|
||||||
|
}
|
||||||
|
|
||||||
|
if(Event == "control") {
|
||||||
|
String con = msg.substring(0, msg.indexOf(','));
|
||||||
|
int num = (msg.substring(msg.indexOf(',')+1, msg.length())).toInt();
|
||||||
|
|
||||||
|
if(con == "t_con") Data.temp_c = num;
|
||||||
|
if(con == "h_con") Data.irrigate_gh = num;
|
||||||
|
if(con == "v_con") Data.vent_c = num;
|
||||||
|
if(con == "control") Data.control = num;
|
||||||
|
Event = "";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void sendData(String str1, String str2)
|
||||||
|
{
|
||||||
|
DynamicJsonDocument doc(512);
|
||||||
|
JsonArray array = doc.to<JsonArray>();
|
||||||
|
String output;
|
||||||
|
|
||||||
|
array.add(str1);
|
||||||
|
array.add(str2);
|
||||||
|
serializeJson(doc, output);
|
||||||
|
socketIO.sendEVENT(output);
|
||||||
|
array.clear();
|
||||||
|
};
|
||||||
|
|
||||||
|
void socketIOEvent(socketIOmessageType_t type, uint8_t * payload, size_t length)
|
||||||
|
{
|
||||||
|
String out;
|
||||||
|
switch(type) {
|
||||||
|
case sIOtype_DISCONNECT:
|
||||||
|
Serial.printf("[IOc] Disconnected!\n");
|
||||||
|
break;
|
||||||
|
case sIOtype_CONNECT:
|
||||||
|
//Serial.printf("[IOc] Connected to url: %s\n", payload);
|
||||||
|
// join default namespace (no auto join in Socket.IO V3)
|
||||||
|
socketIO.send(sIOtype_CONNECT, "/");
|
||||||
|
break;
|
||||||
|
case sIOtype_EVENT:
|
||||||
|
//Serial.printf("[IOc] get event: %s\n", payload);
|
||||||
|
out = (char*)payload;
|
||||||
|
msg = out.substring(out.indexOf(',')+2, out.indexOf("]")-1);
|
||||||
|
Event = out.substring(2, out.indexOf(',')-1);
|
||||||
|
break;
|
||||||
|
case sIOtype_ACK:
|
||||||
|
Serial.printf("[IOc] get ack: %u\n", length);
|
||||||
|
hexdump(payload, length);
|
||||||
|
break;
|
||||||
|
case sIOtype_ERROR:
|
||||||
|
Serial.printf("[IOc] get error: %u\n", length);
|
||||||
|
hexdump(payload, length);
|
||||||
|
break;
|
||||||
|
case sIOtype_BINARY_EVENT:
|
||||||
|
Serial.printf("[IOc] get binary: %u\n", length);
|
||||||
|
hexdump(payload, length);
|
||||||
|
break;
|
||||||
|
case sIOtype_BINARY_ACK:
|
||||||
|
Serial.printf("[IOc] get binary ack: %u\n", length);
|
||||||
|
hexdump(payload, length);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void control(){
|
||||||
|
if(bitRead(Data.control, 3)) {
|
||||||
|
if( Data.irrigate_gh > Data.soil_humidity_GH) {
|
||||||
|
bitSet(Data.control, 0);
|
||||||
|
digitalWrite(Irrigator, LOW);
|
||||||
|
}
|
||||||
|
else if( (Data.irrigate_gh + 10) < Data.soil_humidity_GH ) {
|
||||||
|
bitClear(Data.control, 0);
|
||||||
|
digitalWrite(Irrigator, HIGH);
|
||||||
|
}
|
||||||
|
|
||||||
|
if( (Data.temp_c -1) > Data.temp_GH ) {
|
||||||
|
bitSet(Data.control, 1);
|
||||||
|
digitalWrite(Heater, LOW);
|
||||||
|
}
|
||||||
|
else if( (Data.temp_c +1) < Data.temp_GH ){
|
||||||
|
bitClear(Data.control, 1);
|
||||||
|
digitalWrite(Heater, HIGH);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( Data.vent_c < Data.temp_c ) {
|
||||||
|
bitSet(Data.control, 2);
|
||||||
|
digitalWrite(Ventilator, LOW);
|
||||||
|
}
|
||||||
|
else if( (Data.vent_c - 5) > Data.temp_c ) {
|
||||||
|
bitClear(Data.control, 2);
|
||||||
|
digitalWrite(Ventilator, HIGH);
|
||||||
|
}
|
||||||
|
if( Data.irrigate_env > Data.soil_humidity_Env) {
|
||||||
|
bitSet(Data.control, 4);
|
||||||
|
}
|
||||||
|
else if( (Data.irrigate_env + 10) < Data.soil_humidity_Env ) {
|
||||||
|
bitClear(Data.control, 4);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if (bitRead(Data.control, 0)) digitalWrite(Irrigator, LOW);
|
||||||
|
else digitalWrite(Irrigator, HIGH);
|
||||||
|
if (bitRead(Data.control, 1)) digitalWrite(Heater, LOW);
|
||||||
|
else digitalWrite(Heater, HIGH);
|
||||||
|
if (bitRead(Data.control, 2)) digitalWrite(Ventilator, LOW);
|
||||||
|
else digitalWrite(Ventilator, HIGH);
|
||||||
|
}
|
||||||
|
if(bitRead(Data.control, 5 )) {
|
||||||
|
delay(1000);
|
||||||
|
Serial.println("Resetting....");
|
||||||
|
digitalWrite(Reset, LOW);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
String getData(){
|
||||||
|
|
||||||
|
String s1, s3;
|
||||||
|
|
||||||
|
Data.temp_GH = dht0.getTempAndHumidity().temperature;
|
||||||
|
Data.humidity_GH = dht0.getTempAndHumidity().humidity;
|
||||||
|
Data.soil_humidity_GH = (1024 - (float)analogRead(A0))/10;
|
||||||
|
|
||||||
|
s1 = String(Data.temp_GH)+","+String(Data.humidity_GH)+","+String(Data.soil_humidity_GH)+",";
|
||||||
|
s3 = s1+String(Data.temp_c)+","+String(Data.irrigate_gh)+","+String(Data.vent_c)+","+String(Data.control);
|
||||||
|
Serial.println(s3);
|
||||||
|
return s3;
|
||||||
|
}
|
||||||
|
|
||||||
|
void OTA(){
|
||||||
|
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