Skip to content

Instantly share code, notes, and snippets.

@Informatic
Created January 11, 2016 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Informatic/5c71516137ccff82a7f0 to your computer and use it in GitHub Desktop.
Save Informatic/5c71516137ccff82a7f0 to your computer and use it in GitHub Desktop.
#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