1+ #include < time.h>
2+ #include < ESP8266WiFi.h>
3+ #include < BytebeamArduino.h>
4+
5+ // on board led pin number
6+ #define BOARD_LED 2
7+
8+ // led state variable
9+ int ledState = 0 ;
10+
11+ // wifi credentials
12+ const char * WIFI_SSID = " YOUR_WIFI_SSID" ;
13+ const char * WIFI_PASSWORD = " YOUR_WIFI_PASSWORD" ;
14+
15+ // sntp credentials
16+ const long gmtOffset_sec = 19800 ;
17+ const int daylightOffset_sec = 0 ;
18+ const char * ntpServer = " pool.ntp.org" ;
19+
20+ // function to setup the wifi with predefined credentials
21+ void setupWifi () {
22+ // set the wifi to station mode to connect to a access point
23+ WiFi.mode (WIFI_STA);
24+ WiFi.begin (WIFI_SSID , WIFI_PASSWORD);
25+
26+ Serial.println ();
27+ Serial.print (" Connecting to " + String (WIFI_SSID));
28+
29+ // wait till chip is being connected to wifi (Blocking Mode)
30+ while (WiFi.status () != WL_CONNECTED) {
31+ Serial.print (" ." );
32+ delay (250 );
33+ }
34+
35+ // now it is connected to the access point just print the ip assigned to chip
36+ Serial.println ();
37+ Serial.print (" Connected to " + String (WIFI_SSID) + " , Got IP address : " );
38+ Serial.println (WiFi.localIP ());
39+ }
40+
41+ // function to sync time from ntp server with predefined credentials
42+ void syncTimeFromNtp () {
43+ // sync the time from ntp server
44+ configTime (gmtOffset_sec, daylightOffset_sec, ntpServer);
45+
46+ struct tm timeinfo;
47+
48+ // get the current time
49+ if (!getLocalTime (&timeinfo)) {
50+ Serial.println (" Failed to obtain time" );
51+ return ;
52+ }
53+
54+ // log the time info to serial :)
55+ Serial.print (" Current time: " );
56+ Serial.println (asctime (&timeinfo));
57+ }
58+
59+ // function to setup the predefined led
60+ void setupLED () {
61+ pinMode (BOARD_LED, OUTPUT);
62+ digitalWrite (BOARD_LED, ledState);
63+ }
64+
65+ // function to toggle the predefined led
66+ void toggleLED () {
67+ ledState = !ledState;
68+ digitalWrite (BOARD_LED, ledState);
69+ }
70+
71+ // function to get the time
72+ unsigned long long getEpochMillis () {
73+ time_t now;
74+ struct tm timeinfo;
75+
76+ // get the current time i.e make sure the device is in sync with the ntp server
77+ if (!getLocalTime (&timeinfo)) {
78+ Serial.println (" failed to obtain time" );
79+ return 0 ;
80+ }
81+
82+ // get the epoch time
83+ time (&now);
84+
85+ // generate the epoch millis
86+ unsigned long long timeMillis = ((unsigned long long )now * 1000 ) + (millis () % 1000 );
87+
88+ return timeMillis;
89+ }
90+
91+ // function to publish payload to device shadow
92+ boolean publishToDeviceShadow () {
93+ static int sequence = 0 ;
94+ unsigned long long milliseconds = 0 ;
95+ char ledStatus[200 ] = " " ;
96+ char deviceShadowStream[] = " device_shadow" ;
97+
98+ const char * payload = " " ;
99+ String deviceShadowStr = " " ;
100+ StaticJsonDocument<1024 > doc;
101+
102+ // get the current epoch millis
103+ milliseconds = getEpochMillis ();
104+
105+ // make sure you got the millis
106+ if (milliseconds == 0 ) {
107+ Serial.println (" failed to get epoch millis" );
108+ return false ;
109+ }
110+
111+ // increment the sequence counter
112+ sequence++;
113+
114+ // generate the led status message string
115+ sprintf (ledStatus, " LED is %s !" , ledState == true ? " ON" : " OFF" );
116+
117+ JsonArray deviceShadowJsonArray = doc.to <JsonArray>();
118+ JsonObject deviceShadowJsonObj_1 = deviceShadowJsonArray.createNestedObject ();
119+
120+ deviceShadowJsonObj_1[" timestamp" ] = milliseconds;
121+ deviceShadowJsonObj_1[" sequence" ] = sequence;
122+ deviceShadowJsonObj_1[" Status" ] = ledStatus;
123+
124+ serializeJson (deviceShadowJsonArray, deviceShadowStr);
125+ payload = deviceShadowStr.c_str ();
126+
127+ Serial.printf (" publishing %s to %s\n " , payload, deviceShadowStream);
128+
129+ return Bytebeam.publishToStream (deviceShadowStream, payload);
130+ }
131+
132+ // handler for ToggleLED action
133+ int ToggleLED_Hanlder (char * args, char * actionId) {
134+ Serial.println (" ToggleLED Action Received !" );
135+ Serial.printf (" <--- args : %s, actionId : %s --->\n " , args, actionId);
136+
137+ // toggle the led
138+ toggleLED ();
139+
140+ // publish led state to device shadow
141+ if (!publishToDeviceShadow ()) {
142+ // publish action failed status
143+ if (!Bytebeam.publishActionFailed (actionId)) {
144+ Serial.println (" Failed to publish action failed response for Toggle LED action" );
145+ }
146+
147+ Serial.println (" Failed to publish led state to device shadow" );
148+ return -1 ;
149+ }
150+
151+ // publish action completed status
152+ if (!Bytebeam.publishActionCompleted (actionId)) {
153+ Serial.println (" Failed to publish action completed response for Toggle LED action" );
154+ return -1 ;
155+ }
156+
157+ return 0 ;
158+ }
159+
160+ void setup () {
161+ // put your setup code here, to run once:
162+ Serial.begin (9600 );
163+ Serial.println ();
164+
165+ setupWifi ();
166+ syncTimeFromNtp ();
167+
168+ // setup the gpio led
169+ setupLED ();
170+
171+ // begin the bytebeam client
172+ Bytebeam.begin ();
173+
174+ // add the handler for toggle led action
175+ Bytebeam.addActionHandler (ToggleLED_Hanlder, " ToggleLED" );
176+ }
177+
178+ void loop () {
179+ // put your main code here, to run repeatedly:
180+
181+ // bytebeam client loop
182+ Bytebeam.loop ();
183+
184+ // hold on the execution for some time
185+ delay (5000 );
186+ }
0 commit comments