/* Basic ESP8266 MQTT example โปรแกรมสำหรับ ESP8266 โดยใช้ mqtt_server broker.mqtt-dashboard.com */ #include #include #include #include StaticJsonDocument<200> doc; String message; int s01, s02, s03, s04, s05, s06, s07, s08, s09, s10, s11, s12; // Update these with values suitable for your network. const char* mqtt_server = "broker.mqtt-dashboard.com"; WiFiClient espClient; PubSubClient client(espClient); unsigned long lastMsg = 0; #define MSG_BUFFER_SIZE (200) int timezone = 7 * 3600; //ตั้งค่า TimeZone ตามเวลาประเทศไทย int dst = 0; //กำหนดค่า Date Swing Time char msg[MSG_BUFFER_SIZE]; String data ; #define ConfigWiFi_Pin D2 #define ESP_AP_NAME "ESP8266 sakConfig 204" int th,tw, tm, ts, td, tn, ty; float vsec; int value = 0; String vx1 = "204"; #define vtopic "saksri/muk" void setup() { pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output pinMode(ConfigWiFi_Pin, INPUT_PULLUP); pinMode(D0, OUTPUT); pinMode(D5, OUTPUT); pinMode(D6, OUTPUT); Serial.begin(115200); Serial.println("Starting..."); WiFiManager wifiManager; if (digitalRead(ConfigWiFi_Pin) == LOW) // Press button { //reset saved settings wifiManager.resetSettings(); // go to ip 192.168.4.1 to config } wifiManager.autoConnect(ESP_AP_NAME); while (WiFi.status() != WL_CONNECTED) { delay(250); Serial.print("."); } /* WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(250); Serial.print("."); } */ Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); configTime(timezone, dst, "pool.ntp.org", "time.nist.gov"); //ดึงเวลาจาก Server Serial.println("\nLoading time"); while (!time(nullptr)) { Serial.print("*"); delay(1000); wdt_enable(WDTO_2S); } client.setServer(mqtt_server, 1883); client.setCallback(callback); } void callback(char* topic, byte* payload, unsigned int length) { Serial.print("Message arrived ["); Serial.print(topic); Serial.print("] "); message = ""; for (int i = 0; i < length; i++) { Serial.print((char)payload[i]); message = message + (char)payload[i]; } Serial.print("message="); Serial.println(message); // Switch on the LED if an 1 was received as first character if (message == "11") { digitalWrite(BUILTIN_LED, LOW); // Turn the LED on (Note that LOW is the voltage level // but actually the LED is on; this is because // it is active low on the ESP-01) } else { digitalWrite(BUILTIN_LED, HIGH); // Turn the LED off by making the voltage HIGH } if (message == vx1 + "H00") { if (s01 == 1) { data = vx1 + "1ON"; data.toCharArray(msg, (data.length() + 1)); client.publish(vtopic, msg); } if (s01 == 0) { data = vx1 + "1OFF"; data.toCharArray(msg, (data.length() + 1)); client.publish(vtopic, msg); } if (s02 == 1) { data = vx1 + "2ON"; data.toCharArray(msg, (data.length() + 1)); client.publish(vtopic, msg); } if (s02 == 0) { data = vx1 + "2OFF"; data.toCharArray(msg, (data.length() + 1)); client.publish(vtopic, msg); } if (s03 == 1) { data = vx1 + "3ON"; data.toCharArray(msg, (data.length() + 1)); client.publish(vtopic, msg); } if (s03 == 0) { data = vx1 + "3OFF"; data.toCharArray(msg, (data.length() + 1)); client.publish(vtopic, msg); } if (s04 == 1) { data = vx1 + "4ON"; data.toCharArray(msg, (data.length() + 1)); client.publish(vtopic, msg); } if (s04 == 0) { data = vx1 + "4OFF"; data.toCharArray(msg, (data.length() + 1)); client.publish(vtopic, msg); } } if (message == vx1 + "AON") { s01 = 1; data = vx1 + "1ON"; data.toCharArray(msg, (data.length() + 1)); client.publish(vtopic, msg); digitalWrite(D5,0); } if (message == vx1 + "AOFF") { s01 = 0; data = vx1 + "1OFF"; data.toCharArray(msg, (data.length() + 1)); client.publish(vtopic, msg); digitalWrite(D5,1); } if (message == vx1 + "BON") { s02 = 1; data = vx1 + "2ON"; data.toCharArray(msg, (data.length() + 1)); client.publish(vtopic, msg); digitalWrite(D6,0); } if (message == vx1 + "BOFF") { s02 = 0; data = vx1 + "2OFF"; data.toCharArray(msg, (data.length() + 1)); client.publish(vtopic, msg); digitalWrite(D6,1); } if (message == vx1 + "CON") { s03 = 1; data = vx1 + "3ON"; data.toCharArray(msg, (data.length() + 1)); client.publish(vtopic, msg); } if (message == vx1 + "COFF") { s03 = 0; data = vx1 + "3OFF"; data.toCharArray(msg, (data.length() + 1)); client.publish(vtopic, msg); } if (message == vx1 + "DON") { s04 = 1; data = vx1 + "4ON"; data.toCharArray(msg, (data.length() + 1)); client.publish(vtopic, msg); } if (message == vx1 + "DOFF") { s04 = 0; data = vx1 + "4OFF"; data.toCharArray(msg, (data.length() + 1)); client.publish(vtopic, msg); } Serial.print("s="); Serial.print(s01); Serial.print(s02); Serial.print(s03); Serial.println(s04); } void reconnect() { // Loop until we're reconnected while (!client.connected()) { Serial.print("Attempting MQTT connection..."); // Create a random client ID String clientId = "ESP8266Client-"; clientId += String(random(0xffff), HEX); // Attempt to connect if (client.connect(clientId.c_str())) { Serial.println("connected"); // Once connected, publish an announcement... // client.publish("outTopic", "hello world"); // ... and resubscribe // client.subscribe("saksri/muk"); client.subscribe(vtopic); } else { Serial.print("failed, rc="); Serial.print(client.state()); Serial.println(" try again in 5 seconds"); // Wait 5 seconds before retrying delay(5000); } } } void loop() { if (!client.connected()) { reconnect(); } client.loop(); time_t now = time(nullptr); struct tm* p_tm = localtime(&now); if (millis()-vsec>3000) { vsec = millis(); ts = p_tm->tm_sec; tm = p_tm->tm_min; th = p_tm->tm_hour; tw = p_tm->tm_wday; td = p_tm->tm_mday; tn = p_tm->tm_mon + 1; ty = p_tm->tm_year ; String vty=String(ty); String vtn=String(tn); String vtd=String(td); String vtw=String(tw); String vth=String(th); String vtm=String(tm); String vts=String(ts); //snprintf (msg, MSG_BUFFER_SIZE, "hello88 world #%ld", value); // Serial.print("Publish3333 message: "); //Serial.println(msg); //client.publish("saksri/muk", msg); data = vx1 + "D1"+vth+":"+vtm+":"+vts; data.toCharArray(msg, (data.length() + 1)); client.publish(vtopic, msg); if (digitalRead(D0)==0) digitalWrite(D0,1); else digitalWrite(D0,0); } }