Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions examples/ESP8266DevelopmentBoard/ESP8266DevelopmentBoard.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// This example uses an ESP8266 Development Board
// to connect to shiftr.io.
//
// You can check on your device after a successful
// connection here: https://www.shiftr.io/try.
//
// by Mitra Ardron - trivial change from Joël Gähwiler's
// https://github.com/256dpi/arduino-mqtt/examples/ESP32DevelopmentBoard
// tested on Lolin D1 mini

#include <ESP8266WiFi.h>
#include <MQTT.h>

const char ssid[] = "Zest";
const char pass[] = "#zestubud";

WiFiClient net;
MQTTClient client;

unsigned long lastMillis = 0;

void connect() {
Serial.print("checking wifi...");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(1000);
}

Serial.print("\nconnecting...");
while (!client.connect("arduino", "public", "public")) {
Serial.print(".");
delay(1000);
}

Serial.println("\nconnected!");

client.subscribe("/hello");
// client.unsubscribe("/hello");
}

void messageReceived(String &topic, String &payload) {
Serial.println("incoming: " + topic + " - " + payload);

// Note: Do not use the client in the callback to publish, subscribe or
// unsubscribe as it may cause deadlocks when other things arrive while
// sending and receiving acknowledgments. Instead, change a global variable,
// or push to a queue and handle it in the loop after calling `client.loop()`.
}

void setup() {
Serial.begin(460800);
WiFi.begin(ssid, pass);

// Note: Local domain names (e.g. "Computer.local" on OSX) are not supported
// by Arduino. You need to set the IP address directly.
client.begin("public.cloud.shiftr.io", net);
client.onMessage(messageReceived);

connect();
}

void loop() {
client.loop();
delay(10); // <- fixes some issues with WiFi stability

if (!client.connected()) {
connect();
}

// publish a message roughly every second.
if (millis() - lastMillis > 1000) {
lastMillis = millis();
client.publish("/hello", "world");
}
}