addd ssd1306 lib
This commit is contained in:
65
P1_gateway_FW/lib/MQTT/examples/ArduinoYun/ArduinoYun.ino
Normal file
65
P1_gateway_FW/lib/MQTT/examples/ArduinoYun/ArduinoYun.ino
Normal file
@@ -0,0 +1,65 @@
|
||||
// This example uses an Arduino Yun or a Yun-Shield
|
||||
// and the MQTTClient to connect to shiftr.io.
|
||||
//
|
||||
// You can check on your device after a successful
|
||||
// connection here: https://shiftr.io/try.
|
||||
//
|
||||
// by Joël Gähwiler
|
||||
// https://github.com/256dpi/arduino-mqtt
|
||||
|
||||
#include <Bridge.h>
|
||||
#include <BridgeClient.h>
|
||||
#include <MQTT.h>
|
||||
|
||||
BridgeClient net;
|
||||
MQTTClient client;
|
||||
|
||||
unsigned long lastMillis = 0;
|
||||
|
||||
void connect() {
|
||||
Serial.print("connecting...");
|
||||
while (!client.connect("arduino", "try", "try")) {
|
||||
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() {
|
||||
Bridge.begin();
|
||||
Serial.begin(115200);
|
||||
|
||||
// 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("broker.shiftr.io", net);
|
||||
client.onMessage(messageReceived);
|
||||
|
||||
connect();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
client.loop();
|
||||
|
||||
if (!client.connected()) {
|
||||
connect();
|
||||
}
|
||||
|
||||
// publish a message roughly every second.
|
||||
if (millis() - lastMillis > 1000) {
|
||||
lastMillis = millis();
|
||||
client.publish("/hello", "world");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user