-
-
Save Informatic/5c71516137ccff82a7f0 to your computer and use it in GitHub Desktop.
This file contains 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 <user_config.h> | |
#include <common_config.h> | |
#include <SmingCore/SmingCore.h> | |
bool state = false; | |
MqttClient mqtt(MQTT_BROKER, MQTT_PORT, *[](String topic, String message) { | |
Serial.printf("*** %d: message received @ %s: %s\n", millis(), topic.c_str(), message.c_str()); | |
}); | |
Timer keepaliveTimer; | |
String deviceName; | |
void startMqttClient() | |
{ | |
Serial.println("*** Connecting to MQTT as " + deviceName); | |
mqtt.connect(deviceName); | |
mqtt.subscribe("testing"); | |
keepaliveTimer.initializeMs(5000, *[] { | |
Serial.printf("*** %d: publishing...\n", millis()); | |
mqtt.publish("testing", "echo " + String(millis())); | |
Serial.printf("*** %d: published\n", millis()); | |
}).start(); | |
} | |
void init() | |
{ | |
deviceName = "echo-" + WifiStation.getMAC().substring(6, 12); | |
Serial.begin(SERIAL_BAUD_RATE); // 115200 by default | |
Serial.systemDebugOutput(false); // Debug output to serial | |
Serial.println("*** Starting " + deviceName + " ..."); | |
WifiStation.config(WIFI_SSID, WIFI_PWD); | |
WifiStation.enable(true); | |
WifiAccessPoint.enable(false); | |
WifiStation.waitConnection(*[] { | |
Serial.println("*** Connection succeeded"); | |
startMqttClient(); | |
}, 20, *[] { | |
Serial.println("*** Connection failed"); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment