Skip to content

Instantly share code, notes, and snippets.

@chaeplin
Created November 15, 2015 19:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save chaeplin/2b69fb46479584a354b7 to your computer and use it in GitHub Desktop.
Save chaeplin/2b69fb46479584a354b7 to your computer and use it in GitHub Desktop.
mqtt_test.ino
#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();
}
}
@chaeplin
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment