Skip to content

Commit 384e7ca

Browse files
mitra42256dpi
authored andcommitted
Create ESP8266DevelopmentBoard.ino
Example for ESP8266 - just WiFi library has different name
1 parent 4288378 commit 384e7ca

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// This example uses an ESP8266 Development Board
2+
// to connect to shiftr.io.
3+
//
4+
// You can check on your device after a successful
5+
// connection here: https://www.shiftr.io/try.
6+
//
7+
// by Mitra Ardron - trivial change from Joël Gähwiler's
8+
// https://github.com/256dpi/arduino-mqtt/examples/ESP32DevelopmentBoard
9+
// tested on Lolin D1 mini
10+
11+
#include <ESP8266WiFi.h>
12+
#include <MQTT.h>
13+
14+
const char ssid[] = "Zest";
15+
const char pass[] = "#zestubud";
16+
17+
WiFiClient net;
18+
MQTTClient client;
19+
20+
unsigned long lastMillis = 0;
21+
22+
void connect() {
23+
Serial.print("checking wifi...");
24+
while (WiFi.status() != WL_CONNECTED) {
25+
Serial.print(".");
26+
delay(1000);
27+
}
28+
29+
Serial.print("\nconnecting...");
30+
while (!client.connect("arduino", "public", "public")) {
31+
Serial.print(".");
32+
delay(1000);
33+
}
34+
35+
Serial.println("\nconnected!");
36+
37+
client.subscribe("/hello");
38+
// client.unsubscribe("/hello");
39+
}
40+
41+
void messageReceived(String &topic, String &payload) {
42+
Serial.println("incoming: " + topic + " - " + payload);
43+
44+
// Note: Do not use the client in the callback to publish, subscribe or
45+
// unsubscribe as it may cause deadlocks when other things arrive while
46+
// sending and receiving acknowledgments. Instead, change a global variable,
47+
// or push to a queue and handle it in the loop after calling `client.loop()`.
48+
}
49+
50+
void setup() {
51+
Serial.begin(460800);
52+
WiFi.begin(ssid, pass);
53+
54+
// Note: Local domain names (e.g. "Computer.local" on OSX) are not supported
55+
// by Arduino. You need to set the IP address directly.
56+
client.begin("public.cloud.shiftr.io", net);
57+
client.onMessage(messageReceived);
58+
59+
connect();
60+
}
61+
62+
void loop() {
63+
client.loop();
64+
delay(10); // <- fixes some issues with WiFi stability
65+
66+
if (!client.connected()) {
67+
connect();
68+
}
69+
70+
// publish a message roughly every second.
71+
if (millis() - lastMillis > 1000) {
72+
lastMillis = millis();
73+
client.publish("/hello", "world");
74+
}
75+
}

0 commit comments

Comments
 (0)