commit 03ec0d6d716fd65d5b6eddf8bf6c488fe9416cee Author: kim Date: Wed Oct 8 06:01:48 2025 +0000 Upload files to "main" diff --git a/main/GH.ino b/main/GH.ino new file mode 100644 index 0000000..cf5ffe3 --- /dev/null +++ b/main/GH.ino @@ -0,0 +1,272 @@ +#include +#include +#include +#include +#include + +#ifndef STASSID +#define STASSID "lab" +#define STAPSK "ksg1019!" +#endif + +const char *ssid = STASSID; +const char *password = STAPSK; + +#define seconds() (millis()/1000) + +#define DHTPIN0 D0 +#define HostName "GH" + +float erratio, t1, t2, h1, h2; + +struct dataStruct{ + float temp_GH, humidity_GH, soil_humidity_GH, temp_Env, humidity_Env, soil_humidity_Env; + uint8_t control, temp_c, vent_c, irrigate_c, received; + char which[4]; +}Data; + +struct UdataStruct{ + float temp_Env, humidity_Env, soil_humidity_Env; + uint8_t received; + char which[4]; +}UData; + + +#define Heater D3 +#define Ventilator D4 +#define Reset D5 + +DHTesp dht0; +WiFiUDP Udp; +WiFiUDP Udp2; + +unsigned int localPort = 8888; +char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; +char packetBuffer2[UDP_TX_PACKET_MAX_SIZE]; + +void control(void); +void printData(void); +void getData(void); +void getControl(void); +void UdpSend(void); + +IPAddress ip(192,168,0,200); +IPAddress gateway(192,168,0,1); +IPAddress subnet(255,255,255,0); + +long int st; +String temp; + +void setup() +{ + Data.temp_c = 1; + Data.vent_c = 35; + Data.irrigate_c = 60; + + pinMode(Heater, OUTPUT); + pinMode(Ventilator, OUTPUT); + pinMode(Reset, OUTPUT); + + 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("."); + } + + 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(); + + Serial.print("Connected to "); + Serial.println(ssid); + Serial.print("IP address: "); + Serial.println(WiFi.localIP()); + //Serial.printf("UDP server on port %d\n", localPort); + Udp.begin(localPort); + Udp2.begin(8889); + st = millis(); +} + + + +void loop() +{ + + long int now = millis(); + ArduinoOTA.handle(); + getUdata(); + getControl(); + control(); + if(now - st > 4000){ + getData(); + delay(50); + control(); + UdpSend(); + st = now; + } +} + +void UdpSend(){ + delay(50); + if(bitRead(Data.received, 4)) { + bitClear(Data.received, 4); + + Udp.beginPacket("192.168.0.225", 8888); + Udp.write((byte*) &Data, sizeof(Data)); + Udp.endPacket(); + + Udp.beginPacket("192.168.0.202", 8888); + Udp.write((byte*) &Data, sizeof(Data)); + Udp.endPacket(); + + Udp.beginPacket("192.168.0.203", 8888); + Udp.write((byte*) &Data, sizeof(Data)); + Udp.endPacket(); + } + } + + void getUdata(){ + while(Udp2.parsePacket()){ + Udp2.read((byte*) &UData, sizeof(UData)); + Data.temp_Env = UData.temp_Env; + Data.humidity_Env = UData.humidity_Env; + Data.soil_humidity_Env = UData.soil_humidity_Env; + if( bitRead(UData.received, 6) ) bitSet(Data.received, 6); + else bitClear(Data.received, 6); + if( bitRead(UData.received, 4) ) bitSet(Data.received, 4); + } + } + +void control(){ + if ( ! bitRead(Data.control, 3) ) { + if ( (Data.temp_c - 1) > Data.temp_GH ) bitSet(Data.control, 1); + if ( Data.temp_GH > Data.temp_c ) bitClear(Data.control, 1); + if ( Data.irrigate_c > Data.soil_humidity_GH ) bitSet(Data.control, 0); + if ( Data.soil_humidity_GH > (Data.irrigate_c + 20)) bitClear(Data.control, 0); + if ( Data.irrigate_c > Data.soil_humidity_Env ) bitSet(Data.control, 2); + if ( Data.soil_humidity_Env > (Data.irrigate_c + 20)) bitClear(Data.control, 2); + } + else { + if ( Data.temp_GH > (Data.temp_c + 5 ) ) bitClear(Data.control, 1); + if ( (Data.temp_c - 5 ) > Data.temp_GH ) bitSet(Data.control, 1); + if ( Data.irrigate_c - 30 > Data.soil_humidity_GH ) bitSet(Data.control, 0); + if ( Data.soil_humidity_GH > (Data.irrigate_c + 30)) bitClear(Data.control, 0); + if ( Data.irrigate_c - 30 > Data.soil_humidity_Env ) bitSet(Data.control, 2); + if ( Data.soil_humidity_Env > (Data.irrigate_c + 30) ) bitClear(Data.control, 2); + } + + if (bitRead(Data.control, 1)) digitalWrite(Heater, LOW); + else digitalWrite(Heater, HIGH); + + if(bitRead(Data.control, 4 )) { + delay(1000); + Serial.println("Resetting...."); + Data.control &= B01111; + digitalWrite(Reset, LOW); + delay(1000); + } +} + +void getData(){ + int soil; + strcpy(Data.which, "GH"); + t1 = dht0.getTemperature(); + Data.temp_GH = t1; + h1 = dht0.getHumidity(); + Data.humidity_GH = h1; + soil = analogRead(A0); + Data.soil_humidity_GH = map(soil,900, 400, 0, 100); + //Data.soil_humidity_GH = soil/10; + if (isnan(t1) || isnan(h1)) bitSet(Data.received, 7); + else bitClear(Data.received, 7); + bitClear(Data.received,0); +} + +void getControl(){ + while(Udp.parsePacket()){ + Udp.read(packetBuffer, UDP_TX_PACKET_MAX_SIZE); + switch (*packetBuffer) { + case 'i': bitSet(Data.control, 0); break; + case 'h': bitSet(Data.control, 1); break; + case 'v': bitSet(Data.control, 2); break; + case 'C': bitSet(Data.control, 3); break; + case 'I': bitClear(Data.control, 0); break; + case 'H': bitClear(Data.control, 1); break; + case 'V': bitClear(Data.control, 2); break; + case 'c': bitClear(Data.control, 3); break; + case '+': Data.temp_c += 1; break; + case '-': Data.temp_c -= 1; break; + case '1': Data.vent_c += 1; break; + case '2': Data.vent_c -= 1; break; + case '3': Data.irrigate_c += 1; break; + case '4': Data.irrigate_c -= 1; break; + case 'r': bitSet(Data.control, 4); break; + } + UdpSend(); + } +} + +void printData(){ + Serial.print(Data.which); + Serial.print(":"); + Serial.print(Data.temp_GH); + Serial.print(":"); + Serial.print(Data.humidity_GH ); + Serial.print(":"); + Serial.print(Data.soil_humidity_GH); + Serial.print(":"); + Serial.print(Data.temp_GH); + Serial.print(":"); + Serial.print(Data.humidity_GH ); + Serial.print(":"); + Serial.print(Data.soil_humidity_GH); + Serial.print(":"); + Serial.print(Data.temp_c); + Serial.print(":"); + Serial.print(Data.vent_c); + Serial.print(":"); + Serial.print(Data.irrigate_c); + Serial.print(":"); + Serial.print(Data.control); + Serial.print(":"); + Serial.println(Data.received); +}