Created
November 15, 2015 19:06
-
-
Save chaeplin/2b69fb46479584a354b7 to your computer and use it in GitHub Desktop.
mqtt_test.ino
This file contains hidden or 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 <PubSubClient.h> | |
#include "/usr/local/src/ap_setting.h" | |
// Update these with values suitable for your network. | |
char* topic = "/30/Status/MQTT"; | |
char* subtopic = "inTopic"; | |
char* hellotopic = "HELLO"; | |
IPAddress server(192, 168, 10, 10); | |
WiFiClient wifiClient; | |
PubSubClient client(server, 1883, callback, wifiClient); | |
long lastMsg = 0; | |
char msg[50]; | |
int value = 0; | |
long lastReconnectAttempt = 0; | |
void setup() { | |
pinMode(2, OUTPUT); // Initialize the BUILTIN_LED pin as an output | |
Serial.begin(115200); | |
setup_wifi(); | |
//lastReconnectAttempt = 0; | |
} | |
void setup_wifi() { | |
delay(10); | |
// We start by connecting to a WiFi network | |
Serial.println(); | |
Serial.print("Connecting to "); | |
Serial.println(ssid); | |
WiFi.mode(WIFI_STA); | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println(""); | |
Serial.println("WiFi connected"); | |
Serial.println("IP address: "); | |
Serial.println(WiFi.localIP()); | |
} | |
void callback(char* topic, byte* payload, unsigned int length) { | |
Serial.print("Message arrived ["); | |
Serial.print(topic); | |
Serial.print("] "); | |
for (int i = 0; i < length; i++) { | |
Serial.print((char)payload[i]); | |
} | |
Serial.println(); | |
// Switch on the LED if an 1 was received as first character | |
if ((char)payload[0] == '1') { | |
digitalWrite(2, LOW); // Turn the LED on (Note that LOW is the voltage level | |
// but actually the LED is on; this is because | |
// it is acive low on the ESP-01) | |
} else { | |
digitalWrite(2, HIGH); // Turn the LED off by making the voltage HIGH | |
} | |
} | |
boolean reconnect() { | |
if (!client.connected()) { | |
if (client.connect("arduinoClient")) { | |
// Once connected, publish an announcement... | |
client.publish(hellotopic, "hello world"); | |
// ... and resubscribe | |
client.subscribe(subtopic); | |
} | |
} | |
return client.connected(); | |
} | |
void loop() | |
{ | |
if (WiFi.status() == WL_CONNECTED) { | |
if (!client.connected()) { | |
Serial.print("failed, rc="); | |
Serial.println(client.state()); | |
long now = millis(); | |
Serial.println("MQTT disconnected...."); | |
if (now - lastReconnectAttempt > 5000) { | |
lastReconnectAttempt = now; | |
if (reconnect()) { | |
Serial.println("MQTT connected...."); | |
lastReconnectAttempt = 0; | |
} | |
} | |
} else { | |
long now = millis(); | |
if (now - lastMsg > 30000) { | |
lastMsg = now; | |
Serial.println(client.state()); | |
client.publish(topic, "on"); | |
} | |
client.loop(); | |
} | |
} else { | |
setup_wifi(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
November 16, 2015 at 4:05:25 AM GMT+9d65cca6a.29a338
/30/Status/MQTT : msg.payload : string [2]
on
November 16, 2015 at 4:05:55 AM GMT+9d65cca6a.29a338
/30/Status/MQTT : msg.payload : string [2]
on
November 16, 2015 at 4:06:25 AM GMT+9d65cca6a.29a338
/30/Status/MQTT : msg.payload : string [2]
on
November 16, 2015 at 4:06:55 AM GMT+9d65cca6a.29a338
/30/Status/MQTT : msg.payload : string [2]
on