Created
May 17, 2017 13:26
-
-
Save anonymous/83caa0e2163151a34c742039fbf3f85e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ESP8266WiFi.h> | |
#include <ESP8266mDNS.h> | |
#include <WiFiUdp.h> | |
#include <ArduinoOTA.h> | |
#include <PubSubClient.h> | |
#include <NTPClient.h> | |
#include "DHT.h" | |
DHT dht1(5, DHT22); | |
const char* ssid = ""; | |
const char* password = ""; | |
const char* mqtt_server = "mqtt.mcs.mediatek.com"; | |
WiFiUDP ntpUDP; | |
NTPClient timeClient(ntpUDP, "time1.google.com", 28800, 10000); | |
WiFiClient espClient; | |
PubSubClient client(espClient); | |
long lastMsg = 0; | |
void setup() { | |
Serial.begin(115200); | |
pinMode(2, OUTPUT); | |
dht1.begin(); | |
WiFi.mode(WIFI_STA); | |
WiFi.begin(ssid, password); | |
while (WiFi.waitForConnectResult() != WL_CONNECTED) { | |
Serial.println("Connection Failed! Rebooting..."); | |
delay(3000); | |
ESP.restart(); | |
} | |
// Port defaults to 8266 | |
ArduinoOTA.setPort(8266); | |
// Hostname defaults to esp8266-[ChipID] | |
ArduinoOTA.setHostname("8266-mcs"); | |
// No authentication by default | |
ArduinoOTA.setPassword((const char *)"123"); | |
ArduinoOTA.onStart([]() { | |
Serial.println("Start"); | |
}); | |
ArduinoOTA.onEnd([]() { | |
Serial.println("\nEnd"); | |
}); | |
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { | |
Serial.printf("Progress: %u%%\r", (progress / (total / 100))); | |
}); | |
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.println("Ready"); | |
Serial.print("IP address: "); | |
Serial.println(WiFi.localIP()); | |
client.setServer(mqtt_server, 1883); | |
client.setCallback(callback); | |
timeClient.begin(); | |
timeClient.update(); | |
} | |
void loop() { | |
ArduinoOTA.handle(); | |
if (!client.connected()) { | |
reconnect(); | |
} | |
client.loop(); | |
long now = millis(); | |
if (now - lastMsg > 10000) { | |
timeClient.update(); | |
lastMsg = now; | |
tsensor(); | |
hsensor(); | |
} | |
} | |
void reconnect() { | |
while (!client.connected()) { | |
Serial.println("Attempting MQTT connection..."); | |
// Attempt to connect | |
String a = "client-" + String(timeClient.getEpochTime()); | |
int str_len = a.length() + 1; | |
char char_array[str_len]; | |
a.toCharArray(char_array, str_len); | |
if (client.connect(char_array)) { | |
Serial.println("connected"); | |
client.subscribe("mcs/DdWfy5zH/cH1TDARpFr0K9NKX/switch4"); | |
} else { | |
Serial.print("failed, rc="); | |
Serial.print(client.state()); | |
Serial.println("try again in 5 seconds"); | |
// Wait 5 seconds before retrying | |
delay(5000); | |
} | |
} | |
} | |
void callback(char* topic, byte* payload, unsigned int length) { | |
String cmd = ""; | |
Serial.print("topic:["); | |
Serial.print(topic); | |
Serial.print("] : "); | |
for (int i = 0; i < length; i++) { | |
Serial.print((char)payload[i]); | |
cmd = cmd + String((char)payload[i]); | |
} | |
Serial.println(); | |
int commaPosition; | |
commaPosition = cmd.indexOf(','); | |
cmd = cmd.substring(commaPosition + 1, cmd.length()); | |
commaPosition = cmd.indexOf(','); | |
cmd = cmd.substring(commaPosition + 1, cmd.length()); | |
Serial.println(cmd); | |
if (cmd == "0") { | |
digitalWrite(2, 1); | |
String a = ",," + String(timeClient.getFormattedTime()) + " get 0"; | |
int str_len = a.length() + 1; | |
char char_array[str_len]; | |
a.toCharArray(char_array, str_len); | |
client.publish("mcs/DdWfy5zH/cH1TDARpFr0K9NKX/text4", char_array); | |
} | |
if (cmd == "1") { | |
digitalWrite(2, 0); | |
String a = ",," + String(timeClient.getFormattedTime()) + " get 1"; | |
int str_len = a.length() + 1; | |
char char_array[str_len]; | |
a.toCharArray(char_array, str_len); | |
client.publish("mcs/DdWfy5zH/cH1TDARpFr0K9NKX/text4", char_array); | |
} | |
} | |
void tsensor() | |
{ | |
float t1 = dht1.readTemperature(); | |
String a = ",," + String(t1); | |
int str_len = a.length() + 1; | |
char char_array[str_len]; | |
a.toCharArray(char_array, str_len); | |
client.publish("mcs/DdWfy5zH/cH1TDARpFr0K9NKX/tt", char_array); | |
} | |
void hsensor() | |
{ | |
float h1 = dht1.readHumidity(); | |
String a = ",," + String(h1); | |
int str_len = a.length() + 1; | |
char char_array[str_len]; | |
a.toCharArray(char_array, str_len); | |
client.publish("mcs/DdWfy5zH/cH1TDARpFr0K9NKX/hh", char_array); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment