Edison Carrick / Mbed 2 deprecated Wifi_Alarm_Sunrise_Simulator

Dependencies:   mbed

Fork of huzzah_helloWorld by ECE 4180 Team Who

Committer:
ecarrick
Date:
Fri Mar 18 15:59:13 2016 +0000
Revision:
4:80b814538fe6
Parent:
3:072c63f44572
spelling error

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jhunter029 0:57cec3469a80 1 #include "mbed.h"
ecarrick 1:e4753022f351 2 #include <string>
ecarrick 1:e4753022f351 3 #include <sstream>
ecarrick 1:e4753022f351 4 #include <stdlib.h>
ecarrick 1:e4753022f351 5
jhunter029 0:57cec3469a80 6 Serial pc(USBTX, USBRX);
jhunter029 0:57cec3469a80 7 Serial esp(p28, p27); // tx, rx
jhunter029 0:57cec3469a80 8 DigitalOut reset(p26);
jhunter029 0:57cec3469a80 9 DigitalOut led1(LED1);
ecarrick 1:e4753022f351 10 DigitalOut led2(LED2);
ecarrick 1:e4753022f351 11 DigitalOut led3(LED3);
jhunter029 0:57cec3469a80 12 DigitalOut led4(LED4);
ecarrick 1:e4753022f351 13 DigitalIn pb(p10);
ecarrick 1:e4753022f351 14 PwmOut light(p21);
ecarrick 1:e4753022f351 15
ecarrick 1:e4753022f351 16 Ticker alarmChecker;
ecarrick 1:e4753022f351 17 bool alarmFired = false;
ecarrick 3:072c63f44572 18 int alarmHour = 8; // hard configured start alarm time
ecarrick 3:072c63f44572 19 int alarmMin = 15;
ecarrick 3:072c63f44572 20 int alarmDur = 15; // alarm will execute at 8:00am and brighten for 15 min
ecarrick 1:e4753022f351 21 string data;
jhunter029 0:57cec3469a80 22 Timer t;
ecarrick 1:e4753022f351 23 time_t rtc;
ecarrick 1:e4753022f351 24 struct tm *tp;
ecarrick 1:e4753022f351 25
jhunter029 0:57cec3469a80 26
jhunter029 0:57cec3469a80 27 int count,ended,timeout;
jhunter029 0:57cec3469a80 28 char buf[2024];
jhunter029 0:57cec3469a80 29 char snd[1024];
ecarrick 1:e4753022f351 30 char val[2];
jhunter029 0:57cec3469a80 31
ecarrick 1:e4753022f351 32 char ssid[32] = "ssid"; // enter WiFi router ssid inside the quotes
ecarrick 2:f01835b2e4f7 33 char pwd [32] = "Password"; // enter WiFi router password inside the quotes
jhunter029 0:57cec3469a80 34
ecarrick 1:e4753022f351 35 void SendCMD(),getreply(),ESPhttp(),ESPsetbaudrate(), sendConfig(), ESPreset(), sendNTPFunction(), updateNTP(), getUtime(), startSunrise(float d);
ecarrick 1:e4753022f351 36
ecarrick 3:072c63f44572 37 // check the time in seconds, compare to the alarm time
ecarrick 3:072c63f44572 38 // if the alarm has fired, then don't re-fire once done
ecarrick 3:072c63f44572 39 // wait until the next day (midnight) to reset the fire status
ecarrick 1:e4753022f351 40 void alarmCheck(){
ecarrick 1:e4753022f351 41 rtc = time(NULL);
ecarrick 1:e4753022f351 42 tp = localtime(&rtc);
ecarrick 1:e4753022f351 43 led1=1;
ecarrick 1:e4753022f351 44 if(!alarmFired){
ecarrick 1:e4753022f351 45 printf("hr: %d min:%d\r\n", tp->tm_hour, tp->tm_min);
ecarrick 1:e4753022f351 46 int daystamp = tp->tm_hour * 3600 + tp->tm_min * 60;
ecarrick 1:e4753022f351 47 int alarmstamp = alarmHour * 3600 + alarmMin * 60 - (alarmDur * 60);
ecarrick 3:072c63f44572 48 pc.printf("%d - %d = %d\r\n", alarmstamp, daystamp, alarmstamp - daystamp);
ecarrick 1:e4753022f351 49 if( (alarmstamp - daystamp) <= 0){
ecarrick 1:e4753022f351 50 // time to start the wake up sequence
ecarrick 1:e4753022f351 51 startSunrise((float)(alarmDur*60));
ecarrick 1:e4753022f351 52 alarmFired = true;
ecarrick 1:e4753022f351 53 }
ecarrick 1:e4753022f351 54 } else{
ecarrick 3:072c63f44572 55 if(tp->tm_hour == 0) // wait until midnight
ecarrick 1:e4753022f351 56 alarmFired = false;
ecarrick 1:e4753022f351 57 }
ecarrick 1:e4753022f351 58 wait(1);
ecarrick 1:e4753022f351 59 led1=0;
ecarrick 1:e4753022f351 60
ecarrick 1:e4753022f351 61 }
ecarrick 1:e4753022f351 62
ecarrick 1:e4753022f351 63 void flush(){
ecarrick 1:e4753022f351 64 while(esp.readable()) {
ecarrick 1:e4753022f351 65 char tmp = esp.getc();
ecarrick 1:e4753022f351 66 data += tmp;
ecarrick 1:e4753022f351 67 pc.putc(tmp);
ecarrick 1:e4753022f351 68 }
ecarrick 1:e4753022f351 69 }
jhunter029 0:57cec3469a80 70 void dev_recv()
jhunter029 0:57cec3469a80 71 {
jhunter029 0:57cec3469a80 72 led1 = !led1;
ecarrick 1:e4753022f351 73 timeout=2;
ecarrick 1:e4753022f351 74 getreply();
ecarrick 1:e4753022f351 75 pc.printf("%s\n\n", buf);
ecarrick 1:e4753022f351 76 char * location = strstr(buf, "GET /?");
ecarrick 1:e4753022f351 77 if(location != NULL){
ecarrick 1:e4753022f351 78 //pc.printf("%d\n", location - buf);
ecarrick 1:e4753022f351 79 char * hour = strstr(buf, "h=");
ecarrick 1:e4753022f351 80 char * min = strstr(buf, "m=");
ecarrick 1:e4753022f351 81 char * dur = strstr(buf, "d=");
ecarrick 1:e4753022f351 82 if(hour != NULL && min != NULL && dur != NULL){
ecarrick 1:e4753022f351 83 strncpy(val, hour+2,2);
ecarrick 1:e4753022f351 84 alarmHour = atoi(val) %24;
ecarrick 1:e4753022f351 85 strncpy(val, min+2,2);
ecarrick 1:e4753022f351 86 alarmMin = atoi(val) %60;
ecarrick 1:e4753022f351 87 strncpy(val, dur+2,2);
ecarrick 1:e4753022f351 88 alarmDur = atoi(val);
ecarrick 1:e4753022f351 89
ecarrick 1:e4753022f351 90 pc.printf("New config: hrs %d min %d duration %d", alarmHour, alarmMin, alarmDur);
ecarrick 1:e4753022f351 91 sendConfig();
ecarrick 1:e4753022f351 92
ecarrick 1:e4753022f351 93 }
jhunter029 0:57cec3469a80 94 }
jhunter029 0:57cec3469a80 95 }
jhunter029 0:57cec3469a80 96
jhunter029 0:57cec3469a80 97 void pc_recv()
jhunter029 0:57cec3469a80 98 {
jhunter029 0:57cec3469a80 99 led4 = !led4;
jhunter029 0:57cec3469a80 100 while(pc.readable()) {
jhunter029 0:57cec3469a80 101 esp.putc(pc.getc());
jhunter029 0:57cec3469a80 102 }
jhunter029 0:57cec3469a80 103 }
jhunter029 0:57cec3469a80 104
jhunter029 0:57cec3469a80 105
jhunter029 0:57cec3469a80 106 int main()
jhunter029 0:57cec3469a80 107 {
ecarrick 1:e4753022f351 108
ecarrick 1:e4753022f351 109 pb.mode(PullDown);
ecarrick 1:e4753022f351 110 light.period(0.005); //200Hz to eliminate flicker
jhunter029 0:57cec3469a80 111 reset=0; //hardware reset for 8266
ecarrick 1:e4753022f351 112 pc.baud(115200); // set what you want here depending on your terminal program speed
jhunter029 0:57cec3469a80 113 pc.printf("\f\n\r-------------ESP8266 Hardware Reset-------------\n\r");
jhunter029 0:57cec3469a80 114 wait(0.5);
jhunter029 0:57cec3469a80 115 reset=1;
ecarrick 1:e4753022f351 116 timeout=1;
jhunter029 0:57cec3469a80 117 getreply();
ecarrick 1:e4753022f351 118
ecarrick 1:e4753022f351 119 // RESET wifi module and and connect to the wifi
ecarrick 1:e4753022f351 120 ESPreset();
ecarrick 1:e4753022f351 121
ecarrick 1:e4753022f351 122 // send the NTP function to wifi module
ecarrick 1:e4753022f351 123 sendNTPFunction();
ecarrick 1:e4753022f351 124
ecarrick 2:f01835b2e4f7 125 rtc = time(NULL);
ecarrick 2:f01835b2e4f7 126 tp = localtime(&rtc);
ecarrick 1:e4753022f351 127
ecarrick 1:e4753022f351 128
ecarrick 1:e4753022f351 129 while(tp->tm_year ==0){
ecarrick 1:e4753022f351 130 updateNTP();
ecarrick 1:e4753022f351 131 wait(3);
ecarrick 1:e4753022f351 132 getUtime();
ecarrick 1:e4753022f351 133 wait(1);
ecarrick 1:e4753022f351 134 }
ecarrick 1:e4753022f351 135
ecarrick 1:e4753022f351 136 led3=1;
ecarrick 1:e4753022f351 137
ecarrick 3:072c63f44572 138 // test the light to make sure it works
ecarrick 1:e4753022f351 139 for(float i=0.0;i<1.0;i+=0.1){
ecarrick 1:e4753022f351 140 light=i;
ecarrick 1:e4753022f351 141 wait(0.5);
ecarrick 1:e4753022f351 142 }
ecarrick 1:e4753022f351 143 light = 0;
jhunter029 0:57cec3469a80 144
ecarrick 3:072c63f44572 145 // set up the HTTP server
ecarrick 1:e4753022f351 146 ESPhttp();
ecarrick 1:e4753022f351 147
ecarrick 3:072c63f44572 148 // uncomment to be able to send commands to the wifi module directly through the PC COM port
ecarrick 1:e4753022f351 149 //pc.attach(&pc_recv, Serial::RxIrq);
ecarrick 4:80b814538fe6 150 // set up an interrupt to listen for data sent by the wifi module
ecarrick 3:072c63f44572 151 // allows the configuration to be updated.
jhunter029 0:57cec3469a80 152 esp.attach(&dev_recv, Serial::RxIrq);
jhunter029 0:57cec3469a80 153
ecarrick 3:072c63f44572 154 // print the time to console to verify updated
ecarrick 1:e4753022f351 155 rtc = time(NULL);
ecarrick 1:e4753022f351 156 pc.printf("%s\r\n", ctime(&rtc));
ecarrick 1:e4753022f351 157 led1=0;led2=0;led3=0;led4=0;
ecarrick 1:e4753022f351 158 tp = localtime(&rtc);
ecarrick 1:e4753022f351 159 printf("hr: %d min:%d\r\n", tp->tm_hour, tp->tm_min);
ecarrick 3:072c63f44572 160
ecarrick 4:80b814538fe6 161 // set up an interrupt checker to check the alarm status every minute.
ecarrick 1:e4753022f351 162 alarmChecker.attach(&alarmCheck, 60);
jhunter029 0:57cec3469a80 163 while(1) {
ecarrick 3:072c63f44572 164 sleep(); // low power sleep to conserve power inbetween checks
jhunter029 0:57cec3469a80 165 }
jhunter029 0:57cec3469a80 166
jhunter029 0:57cec3469a80 167 }
ecarrick 1:e4753022f351 168
ecarrick 3:072c63f44572 169 // brighten the light over the duration period length
ecarrick 3:072c63f44572 170 // keep the light on for 2 minutes, then shut it off
ecarrick 1:e4753022f351 171 void startSunrise(float d){
ecarrick 1:e4753022f351 172 for(float i=0.0; i < 1.0; i += (1.0/d)){
ecarrick 1:e4753022f351 173 light = i;
ecarrick 1:e4753022f351 174 pc.printf("%f\n", i);
ecarrick 1:e4753022f351 175 wait(1);
ecarrick 1:e4753022f351 176 }
ecarrick 1:e4753022f351 177 light = 1.0;
ecarrick 1:e4753022f351 178
ecarrick 1:e4753022f351 179 wait(120);
ecarrick 1:e4753022f351 180 light = 0;
ecarrick 1:e4753022f351 181
ecarrick 1:e4753022f351 182 }
jhunter029 0:57cec3469a80 183
jhunter029 0:57cec3469a80 184 // Sets new ESP8266 baurate, change the esp.baud(xxxxx) to match your new setting once this has been executed
ecarrick 3:072c63f44572 185 // This is an important configuration change. By setting the last argument to zero, it stops the module from echoing
ecarrick 3:072c63f44572 186 // commands back, allowing us to send commands and read only the value
jhunter029 0:57cec3469a80 187 void ESPsetbaudrate()
jhunter029 0:57cec3469a80 188 {
ecarrick 1:e4753022f351 189 strcpy(snd, "uart.setup(0, 9600, 8, 0, 1, 0)\r\n"); // change the numeric value to the required baudrate
jhunter029 0:57cec3469a80 190 SendCMD();
ecarrick 1:e4753022f351 191 timeout=2;
ecarrick 1:e4753022f351 192 getreply();
ecarrick 1:e4753022f351 193 pc.printf(buf);
jhunter029 0:57cec3469a80 194 }
jhunter029 0:57cec3469a80 195
ecarrick 1:e4753022f351 196 void ESPreset(){
jhunter029 0:57cec3469a80 197 pc.printf("---------- Reset & get Firmware ----------\r\n");
jhunter029 0:57cec3469a80 198 strcpy(snd,"node.restart()\r\n");
jhunter029 0:57cec3469a80 199 SendCMD();
ecarrick 1:e4753022f351 200 timeout=2;
jhunter029 0:57cec3469a80 201 getreply();
jhunter029 0:57cec3469a80 202 pc.printf(buf);
ecarrick 1:e4753022f351 203
ecarrick 1:e4753022f351 204 ESPsetbaudrate();
ecarrick 1:e4753022f351 205
ecarrick 1:e4753022f351 206
jhunter029 0:57cec3469a80 207 pc.printf("\n---------- Connecting to AP ----------\r\n");
ecarrick 2:f01835b2e4f7 208 //pc.printf("ssid = %s pwd = %s\r\n",ssid,pwd);
jhunter029 0:57cec3469a80 209 strcpy(snd, "wifi.sta.config(\"");
jhunter029 0:57cec3469a80 210 strcat(snd, ssid);
jhunter029 0:57cec3469a80 211 strcat(snd, "\",\"");
jhunter029 0:57cec3469a80 212 strcat(snd, pwd);
jhunter029 0:57cec3469a80 213 strcat(snd, "\")\r\n");
jhunter029 0:57cec3469a80 214 SendCMD();
ecarrick 1:e4753022f351 215 timeout=3;
jhunter029 0:57cec3469a80 216 getreply();
jhunter029 0:57cec3469a80 217 pc.printf(buf);
jhunter029 0:57cec3469a80 218
jhunter029 0:57cec3469a80 219 wait(5);
jhunter029 0:57cec3469a80 220
jhunter029 0:57cec3469a80 221 pc.printf("\n---------- Get IP's ----------\r\n");
jhunter029 0:57cec3469a80 222 strcpy(snd, "print(wifi.sta.getip())\r\n");
jhunter029 0:57cec3469a80 223 SendCMD();
ecarrick 1:e4753022f351 224 timeout=2;
jhunter029 0:57cec3469a80 225 getreply();
jhunter029 0:57cec3469a80 226 pc.printf(buf);
jhunter029 0:57cec3469a80 227
jhunter029 0:57cec3469a80 228 wait(1);
jhunter029 0:57cec3469a80 229
jhunter029 0:57cec3469a80 230 pc.printf("\n---------- Get Connection Status ----------\r\n");
jhunter029 0:57cec3469a80 231 strcpy(snd, "print(wifi.sta.status())\r\n");
ecarrick 1:e4753022f351 232 pc.printf("%s", snd);
jhunter029 0:57cec3469a80 233 SendCMD();
ecarrick 1:e4753022f351 234 timeout=2;
jhunter029 0:57cec3469a80 235 getreply();
jhunter029 0:57cec3469a80 236 pc.printf(buf);
ecarrick 1:e4753022f351 237
ecarrick 1:e4753022f351 238
ecarrick 1:e4753022f351 239 }
jhunter029 0:57cec3469a80 240
ecarrick 1:e4753022f351 241 void ESPhttp()
ecarrick 1:e4753022f351 242 {
ecarrick 1:e4753022f351 243
jhunter029 0:57cec3469a80 244 pc.printf("\n---------- Setting up http server ----------\r\n");
ecarrick 1:e4753022f351 245
ecarrick 1:e4753022f351 246 sendConfig();
ecarrick 1:e4753022f351 247
ecarrick 1:e4753022f351 248 strcpy(snd, "srv=net.createServer(net.TCP)\r\n");
jhunter029 0:57cec3469a80 249 SendCMD();
jhunter029 0:57cec3469a80 250 wait(1);
jhunter029 0:57cec3469a80 251 strcpy(snd, "srv:listen(80,function(conn)\r\n");
jhunter029 0:57cec3469a80 252 SendCMD();
jhunter029 0:57cec3469a80 253 wait(1);
jhunter029 0:57cec3469a80 254 strcpy(snd, "conn:on(\"receive\",function(conn,payload)\r\n");
jhunter029 0:57cec3469a80 255 SendCMD();
jhunter029 0:57cec3469a80 256 wait(1);
ecarrick 1:e4753022f351 257
jhunter029 0:57cec3469a80 258 strcpy(snd, "print(payload)\r\n");
jhunter029 0:57cec3469a80 259 SendCMD();
jhunter029 0:57cec3469a80 260 wait(1);
jhunter029 0:57cec3469a80 261
ecarrick 1:e4753022f351 262
jhunter029 0:57cec3469a80 263 strcpy(snd, "conn:send(\"<!DOCTYPE html>\")\r\n");
jhunter029 0:57cec3469a80 264 SendCMD();
jhunter029 0:57cec3469a80 265 wait(1);
jhunter029 0:57cec3469a80 266
jhunter029 0:57cec3469a80 267 strcpy(snd, "conn:send(\"<html>\")\r\n");
jhunter029 0:57cec3469a80 268 SendCMD();
jhunter029 0:57cec3469a80 269 wait(1);
jhunter029 0:57cec3469a80 270
ecarrick 1:e4753022f351 271 strcpy(snd, "conn:send(\"<h1>Configure Sunrise Alarm</h1><br><br><form method='get'>Wake up at: <input name=h value='\")\r\n");
ecarrick 1:e4753022f351 272 SendCMD();
ecarrick 1:e4753022f351 273 wait(1);
ecarrick 1:e4753022f351 274
ecarrick 1:e4753022f351 275 strcpy(snd, "conn:send(hour)\r\n");
ecarrick 1:e4753022f351 276 SendCMD();
ecarrick 1:e4753022f351 277 wait(1);
ecarrick 1:e4753022f351 278
ecarrick 1:e4753022f351 279 strcpy(snd, "conn:send(\"'> Hours and <input name=m value='\")\r\n");
ecarrick 1:e4753022f351 280 SendCMD();
jhunter029 0:57cec3469a80 281 wait(1);
jhunter029 0:57cec3469a80 282
ecarrick 1:e4753022f351 283 strcpy(snd, "conn:send(min)\r\n");
ecarrick 1:e4753022f351 284 SendCMD();
ecarrick 1:e4753022f351 285 wait(1);
ecarrick 1:e4753022f351 286
ecarrick 1:e4753022f351 287 strcpy(snd, "conn:send(\"'> minutes <br> <br> Start sunrise <input name=d value='\")\r\n");
ecarrick 1:e4753022f351 288 SendCMD();
ecarrick 1:e4753022f351 289 wait(1);
ecarrick 1:e4753022f351 290
ecarrick 1:e4753022f351 291 strcpy(snd, "conn:send(dur)\r\n");
ecarrick 1:e4753022f351 292 SendCMD();
ecarrick 1:e4753022f351 293 wait(1);
ecarrick 1:e4753022f351 294
ecarrick 1:e4753022f351 295 strcpy(snd, "conn:send(\"'> minutes before <input type=submit value='submit'></form>\")\r\n");
jhunter029 0:57cec3469a80 296 SendCMD();
jhunter029 0:57cec3469a80 297 wait(1);
jhunter029 0:57cec3469a80 298
jhunter029 0:57cec3469a80 299 strcpy(snd, "conn:send(\"</html>\")\r\n");
jhunter029 0:57cec3469a80 300 SendCMD();
jhunter029 0:57cec3469a80 301 wait(1);
jhunter029 0:57cec3469a80 302
jhunter029 0:57cec3469a80 303 strcpy(snd, "end)\r\n");
jhunter029 0:57cec3469a80 304 SendCMD();
jhunter029 0:57cec3469a80 305 wait(1);
jhunter029 0:57cec3469a80 306
jhunter029 0:57cec3469a80 307 strcpy(snd, "conn:on(\"sent\",function(conn) conn:close() end)\r\n");
jhunter029 0:57cec3469a80 308 SendCMD();
jhunter029 0:57cec3469a80 309 wait(1);
jhunter029 0:57cec3469a80 310 strcpy(snd, "end)\r\n");
jhunter029 0:57cec3469a80 311 SendCMD();
ecarrick 1:e4753022f351 312
ecarrick 1:e4753022f351 313 flush();
ecarrick 1:e4753022f351 314
ecarrick 1:e4753022f351 315 pc.printf("\r\nDONE\r\n");
jhunter029 0:57cec3469a80 316 }
jhunter029 0:57cec3469a80 317
jhunter029 0:57cec3469a80 318 void SendCMD()
jhunter029 0:57cec3469a80 319 {
ecarrick 1:e4753022f351 320 pc.printf("%s", snd);
jhunter029 0:57cec3469a80 321 esp.printf("%s", snd);
jhunter029 0:57cec3469a80 322 }
jhunter029 0:57cec3469a80 323
jhunter029 0:57cec3469a80 324 void getreply()
jhunter029 0:57cec3469a80 325 {
jhunter029 0:57cec3469a80 326 memset(buf, '\0', sizeof(buf));
jhunter029 0:57cec3469a80 327 t.start();
jhunter029 0:57cec3469a80 328 ended=0;
jhunter029 0:57cec3469a80 329 count=0;
jhunter029 0:57cec3469a80 330 while(!ended) {
jhunter029 0:57cec3469a80 331 if(esp.readable()) {
jhunter029 0:57cec3469a80 332 buf[count] = esp.getc();
jhunter029 0:57cec3469a80 333 count++;
jhunter029 0:57cec3469a80 334 }
jhunter029 0:57cec3469a80 335 if(t.read() > timeout) {
jhunter029 0:57cec3469a80 336 ended = 1;
jhunter029 0:57cec3469a80 337 t.stop();
jhunter029 0:57cec3469a80 338 t.reset();
jhunter029 0:57cec3469a80 339 }
jhunter029 0:57cec3469a80 340 }
jhunter029 0:57cec3469a80 341 }
jhunter029 0:57cec3469a80 342
ecarrick 1:e4753022f351 343 void sendConfig()
ecarrick 1:e4753022f351 344 {
ecarrick 1:e4753022f351 345 stringstream strs;
ecarrick 1:e4753022f351 346 stringstream strs1;
ecarrick 1:e4753022f351 347 stringstream strs2;
ecarrick 1:e4753022f351 348
ecarrick 1:e4753022f351 349 strcpy(snd, "hour=");
ecarrick 1:e4753022f351 350 SendCMD();
ecarrick 1:e4753022f351 351 strs << alarmHour;
ecarrick 1:e4753022f351 352 string temp_str = strs.str();
ecarrick 1:e4753022f351 353 char const* pchar = temp_str.c_str();
ecarrick 1:e4753022f351 354 strcpy(snd, pchar);
ecarrick 1:e4753022f351 355 SendCMD();
ecarrick 1:e4753022f351 356 strcpy(snd, "\r\n");
ecarrick 1:e4753022f351 357 SendCMD();
ecarrick 1:e4753022f351 358 wait(1);
ecarrick 1:e4753022f351 359
ecarrick 1:e4753022f351 360
ecarrick 1:e4753022f351 361 strcpy(snd, "min=");
ecarrick 1:e4753022f351 362 SendCMD();
ecarrick 1:e4753022f351 363 strs1 << alarmMin;
ecarrick 1:e4753022f351 364 temp_str = strs1.str();
ecarrick 1:e4753022f351 365 pchar = temp_str.c_str();
ecarrick 1:e4753022f351 366 strcpy(snd, pchar);
ecarrick 1:e4753022f351 367 SendCMD();
ecarrick 1:e4753022f351 368 strcpy(snd, "\r\n");
ecarrick 1:e4753022f351 369 SendCMD();
ecarrick 1:e4753022f351 370 wait(1);
ecarrick 1:e4753022f351 371
ecarrick 1:e4753022f351 372
ecarrick 1:e4753022f351 373
ecarrick 1:e4753022f351 374 strcpy(snd, "dur=");
ecarrick 1:e4753022f351 375 SendCMD();
ecarrick 1:e4753022f351 376 strs2 << alarmDur;
ecarrick 1:e4753022f351 377 temp_str = strs2.str();
ecarrick 1:e4753022f351 378 pchar = temp_str.c_str();
ecarrick 1:e4753022f351 379 strcpy(snd, pchar);
ecarrick 1:e4753022f351 380 SendCMD();
ecarrick 1:e4753022f351 381 strcpy(snd, "\r\n");
ecarrick 1:e4753022f351 382 SendCMD();
ecarrick 1:e4753022f351 383 wait(1);
ecarrick 1:e4753022f351 384 }
ecarrick 1:e4753022f351 385
ecarrick 1:e4753022f351 386 void sendNTPFunction()
ecarrick 1:e4753022f351 387 {
ecarrick 2:f01835b2e4f7 388 wait(5);
ecarrick 2:f01835b2e4f7 389
ecarrick 2:f01835b2e4f7 390 pc.printf("\n---------- Setting up NTp Client on Wifi Module ----------\r\n");
ecarrick 2:f01835b2e4f7 391
ecarrick 1:e4753022f351 392 strcpy(snd, "timezone = -4\r\n");
ecarrick 1:e4753022f351 393 SendCMD();
ecarrick 1:e4753022f351 394 wait(2);
ecarrick 1:e4753022f351 395 strcpy(snd, "request=string.char(227,0,6,236,0,0,0,0,0,0,0,0,49,78,49,52,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)\r\n");
ecarrick 1:e4753022f351 396 SendCMD();
ecarrick 1:e4753022f351 397 wait(4);
ecarrick 1:e4753022f351 398
ecarrick 1:e4753022f351 399 strcpy(snd, "time = 0\r\n");
ecarrick 1:e4753022f351 400 SendCMD();
ecarrick 1:e4753022f351 401 wait(1);
ecarrick 1:e4753022f351 402 strcpy(snd, "sk=net.createConnection(net.UDP, 0)\r\n");
ecarrick 1:e4753022f351 403 SendCMD();
ecarrick 1:e4753022f351 404 wait(1);
ecarrick 1:e4753022f351 405
ecarrick 1:e4753022f351 406 strcpy(snd, "sk:on(\"receive\", function(sck, payload)\r\n");
ecarrick 1:e4753022f351 407 SendCMD();
ecarrick 1:e4753022f351 408 wait(1);
ecarrick 1:e4753022f351 409 strcpy(snd, "print(\"YAY!\")\r\n");
ecarrick 1:e4753022f351 410 SendCMD();
ecarrick 1:e4753022f351 411 wait(1);
ecarrick 1:e4753022f351 412 strcpy(snd, "local highw,loww,ntpstamp\r\n");
ecarrick 1:e4753022f351 413 SendCMD();
ecarrick 1:e4753022f351 414 wait(1);
ecarrick 1:e4753022f351 415 strcpy(snd, "highw = payload:byte(41) * 256 + payload:byte(42)\r\n");
ecarrick 1:e4753022f351 416 SendCMD();
ecarrick 1:e4753022f351 417 wait(1);
ecarrick 1:e4753022f351 418 strcpy(snd, "loww = payload:byte(43) * 256 + payload:byte(44)\r\n");
ecarrick 1:e4753022f351 419 SendCMD();
ecarrick 1:e4753022f351 420 wait(1);
ecarrick 1:e4753022f351 421 strcpy(snd, "ntpstamp=( highw * 65536 + loww ) + ( timezone* 3600)\r\n");
ecarrick 1:e4753022f351 422 SendCMD();
ecarrick 1:e4753022f351 423 wait(1);
ecarrick 1:e4753022f351 424 strcpy(snd, "time = ntpstamp - 1104494400 - 1104494400\r\n");
ecarrick 1:e4753022f351 425 SendCMD();
ecarrick 1:e4753022f351 426 wait(1);
ecarrick 1:e4753022f351 427 strcpy(snd, "print(time)\r\n");
ecarrick 1:e4753022f351 428 SendCMD();
ecarrick 1:e4753022f351 429 wait(1);
ecarrick 1:e4753022f351 430
ecarrick 1:e4753022f351 431 strcpy(snd, "sck:close()\r\n");
ecarrick 1:e4753022f351 432 SendCMD();
ecarrick 1:e4753022f351 433 wait(1);
ecarrick 1:e4753022f351 434
ecarrick 1:e4753022f351 435 strcpy(snd, "end )\r\n");
ecarrick 1:e4753022f351 436 SendCMD();
ecarrick 1:e4753022f351 437 wait(1);
ecarrick 1:e4753022f351 438
ecarrick 1:e4753022f351 439 flush();
ecarrick 1:e4753022f351 440 wait(15);
ecarrick 1:e4753022f351 441 //updateNTP();
ecarrick 1:e4753022f351 442 }
ecarrick 3:072c63f44572 443 // this function executes the connection to the NTP server
ecarrick 3:072c63f44572 444 // saving the time stamp into a local variable on the wifi module to be read
ecarrick 1:e4753022f351 445 void updateNTP(){
ecarrick 1:e4753022f351 446 flush();
ecarrick 1:e4753022f351 447 strcpy(snd, "sk:connect(123,\"104.232.3.3\")\r\n");
ecarrick 1:e4753022f351 448 SendCMD();
ecarrick 1:e4753022f351 449 wait(3);
ecarrick 1:e4753022f351 450 strcpy(snd, "sk:send(request)\r\n");
ecarrick 1:e4753022f351 451 SendCMD();
ecarrick 1:e4753022f351 452 wait(4);
ecarrick 1:e4753022f351 453 timeout=3;
ecarrick 1:e4753022f351 454 getreply();
ecarrick 1:e4753022f351 455 pc.printf(buf);
ecarrick 1:e4753022f351 456 //getUtime();
ecarrick 1:e4753022f351 457 }
ecarrick 3:072c63f44572 458 // This function calls the wifi module to output the time stamp and save it to the local clock
ecarrick 3:072c63f44572 459 // This should be executed right after updateNTP to reduce the delay in getting the clock value
ecarrick 3:072c63f44572 460 // and setting the clock
ecarrick 1:e4753022f351 461 void getUtime(){
ecarrick 1:e4753022f351 462 flush();
ecarrick 1:e4753022f351 463 strcpy(snd, "print(time)\r\n");
ecarrick 1:e4753022f351 464 SendCMD();
ecarrick 1:e4753022f351 465 wait(1);
ecarrick 1:e4753022f351 466 timeout=5;
ecarrick 1:e4753022f351 467 getreply();
ecarrick 1:e4753022f351 468 pc.printf(buf);
ecarrick 1:e4753022f351 469 int utime = atoi(buf);
ecarrick 1:e4753022f351 470 if(utime != NULL)
ecarrick 1:e4753022f351 471 set_time(utime);
ecarrick 1:e4753022f351 472 time_t seconds = time(NULL);
ecarrick 1:e4753022f351 473 pc.printf("Time as a basic string = %s", ctime(&seconds));
ecarrick 1:e4753022f351 474 }