1+ /* **************************************************
2+ Adafruit MQTT Library ESP8266 Adafruit IO Anonymous Time Query
3+
4+ Must use the latest version of ESP8266 Arduino from:
5+ https://github.com/esp8266/Arduino
6+
7+ Works great with Adafruit's Huzzah ESP board & Feather
8+ ----> https://www.adafruit.com/product/2471
9+ ----> https://www.adafruit.com/products/2821
10+
11+ Adafruit invests time and resources providing this open source code,
12+ please support Adafruit and open-source hardware by purchasing
13+ products from Adafruit!
14+
15+ Written by Adafruit Industries.
16+ MIT license, all text above must be included in any redistribution
17+ ****************************************************/
18+ #include < ESP8266WiFi.h>
19+ #include " Adafruit_MQTT.h"
20+ #include " Adafruit_MQTT_Client.h"
21+
22+ /* ************************ WiFi Access Point *********************************/
23+ #define WLAN_SSID " network"
24+ #define WLAN_PASS " password"
25+
26+ /* ************************ Adafruit.io Setup *********************************/
27+ #define AIO_SERVER " io.adafruit.com"
28+ #define AIO_SERVERPORT 8883
29+
30+ WiFiClientSecure client;
31+ Adafruit_MQTT_Client mqtt (&client, AIO_SERVER, AIO_SERVERPORT);
32+
33+ Adafruit_MQTT_Subscribe timefeed = Adafruit_MQTT_Subscribe(&mqtt, " time/seconds" );
34+
35+ // set timezone offset from UTC
36+ int timeZone = -4 ; // UTC - 4 eastern daylight time (nyc)
37+ int interval = 4 ; // trigger every X hours
38+
39+ int last_min = -1 ;
40+
41+ void timecallback (uint32_t current) {
42+ // adjust to local time zone
43+ current += (timeZone * 60 * 60 );
44+ int curr_hour = (current / 60 / 60 ) % 24 ;
45+ int curr_min = (current / 60 ) % 60 ;
46+ int curr_sec = (current) % 60 ;
47+
48+ Serial.print (" Time: " );
49+ Serial.print (curr_hour); Serial.print (' :' );
50+ Serial.print (curr_min); Serial.print (' :' );
51+ Serial.println (curr_sec);
52+
53+ // only trigger on minute change
54+ if (curr_min != last_min) {
55+ last_min = curr_min;
56+
57+ Serial.println (" This will print out every minute!" );
58+ }
59+ }
60+
61+ void setup () {
62+
63+ Serial.begin (115200 );
64+ delay (10 );
65+
66+ Serial.print (F (" \n Adafruit IO anonymous Time Demo" ));
67+
68+ WiFi.begin (WLAN_SSID, WLAN_PASS);
69+ while (WiFi.status () != WL_CONNECTED) {
70+ delay (500 );
71+ Serial.print (F (" ." ));
72+ }
73+ Serial.println (F (" WiFi connected." ));
74+
75+ timefeed.setCallback (timecallback);
76+ mqtt.subscribe (&timefeed);
77+
78+ }
79+
80+ void loop () {
81+
82+ // Ensure the connection to the MQTT server is alive (this will make the first
83+ // connection and automatically reconnect when disconnected). See the MQTT_connect
84+ // function definition further below.
85+ MQTT_connect ();
86+
87+ // wait 10 seconds for subscription messages
88+ // since we have no other tasks in this example.
89+ mqtt.processPackets (10000 );
90+
91+ // keep the connection alive
92+ mqtt.ping ();
93+
94+ }
95+
96+ // Function to connect and reconnect as necessary to the MQTT server.
97+ // Should be called in the loop function and it will take care if connecting.
98+ void MQTT_connect () {
99+ int8_t ret;
100+
101+ // Stop if already connected.
102+ if (mqtt.connected ()) {
103+ return ;
104+ }
105+
106+ Serial.print (" Connecting to MQTT... " );
107+
108+ uint8_t retries = 3 ;
109+ while ((ret = mqtt.connect ()) != 0 ) { // connect will return 0 for connected
110+ Serial.println (mqtt.connectErrorString (ret));
111+ Serial.println (" Retrying MQTT connection in 5 seconds..." );
112+ mqtt.disconnect ();
113+ delay (5000 ); // wait 5 seconds
114+ retries--;
115+ if (retries == 0 ) {
116+ // basically die and wait for WDT to reset me
117+ while (1 );
118+ }
119+ }
120+
121+ Serial.println (" MQTT Connected!" );
122+ }
0 commit comments