Created
August 7, 2016 08:29
-
-
Save jackrobotics/69196a01e39c2d74c73ff85c3139de2c to your computer and use it in GitHub Desktop.
antoTime.ino
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 <AntoIO.h> | |
AntoIO anto("JackRoboticS", "p7erLWvOQsdRAwVylmdF8eR0OX1VMNSHfk1GRzCX", "TIME"); | |
int day,month,year,hours,minutes,seconds; | |
bool bIsConnected = false; | |
void setup() { | |
// SSID and Password of your WiFi access point. | |
const char* ssid = "HONEYLab"; | |
const char* pwd = "@HONEYLab"; | |
Serial.begin(115200); | |
delay(10); | |
Serial.println(); | |
Serial.println(); | |
Serial.print("Anto library version: "); | |
Serial.println(anto.getVersion()); | |
Serial.print("Connecting to "); | |
Serial.println(ssid); | |
// Connect to your WiFi access point | |
if (!anto.begin(ssid, pwd)) { | |
Serial.println("Connection failed!!"); | |
// Stop everything. | |
while (1); | |
} | |
Serial.println(); | |
Serial.println("WiFi connected"); | |
Serial.println("Connecting to MQTT broker"); | |
// register callback functions | |
anto.mqtt.onConnected(connectedCB); | |
anto.mqtt.onDisconnected(disconnectedCB); | |
anto.mqtt.onData(dataCB); | |
anto.mqtt.onPublished(publishedCB); | |
// Connect to Anto.io MQTT broker | |
anto.mqtt.connect(); | |
} | |
void loop() { | |
delay(5000); | |
if (!bIsConnected) anto.mqtt.connect(); | |
} | |
void connectedCB() | |
{ | |
bIsConnected = true; | |
Serial.println("Connected to MQTT Broker"); | |
// If the connection is establised, subscribe channels | |
// by using sub(channel, QOS) | |
// where QOS is 0, 1, or 2 | |
anto.sub("TIME", 0); | |
} | |
/* | |
disconnectedCB(): a callback function called when the connection to the MQTT broker is broken. | |
*/ | |
void disconnectedCB() | |
{ | |
bIsConnected = false; | |
Serial.println("Disconnected to MQTT Broker"); | |
} | |
/* | |
dataCB(): a callback function called when there a message from the subscribed channel. | |
*/ | |
void dataCB(String& topic, String& msg) | |
{ | |
day = msg.substring(0, 2).toInt(), | |
month = msg.substring(3, 5).toInt(), | |
year = msg.substring(6, 10).toInt(), | |
hours = msg.substring(11, 13).toInt(), | |
minutes = msg.substring(14, 16).toInt(), | |
seconds = msg.substring(17, 19).toInt(); | |
//Serial.println("======================================================"); | |
Serial.print(day);Serial.print("/"); | |
Serial.print(month);Serial.print("/"); | |
Serial.print(year);Serial.print(" "); | |
Serial.print(hours);Serial.print(":"); | |
Serial.print(minutes);Serial.print(":"); | |
Serial.println(seconds); | |
} | |
/* | |
publishedCB(): a callback function called when the message is published. | |
*/ | |
void publishedCB(void) | |
{ | |
Serial.println("published"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment