Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of huzzah_helloWorld by
main.cpp@1:e4753022f351, 2016-03-18 (annotated)
- Committer:
- ecarrick
- Date:
- Fri Mar 18 05:13:25 2016 +0000
- Revision:
- 1:e4753022f351
- Parent:
- 0:57cec3469a80
- Child:
- 2:f01835b2e4f7
First commit of all code for public page viewing.
Who changed what in which revision?
| User | Revision | Line number | New 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 | 1:e4753022f351 | 18 | int alarmHour = 23; |
| ecarrick | 1:e4753022f351 | 19 | int alarmMin = 49; |
| ecarrick | 1:e4753022f351 | 20 | int alarmDur = 1; |
| 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 | 1:e4753022f351 | 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 | 1:e4753022f351 | 37 | void alarmCheck(){ |
| ecarrick | 1:e4753022f351 | 38 | rtc = time(NULL); |
| ecarrick | 1:e4753022f351 | 39 | tp = localtime(&rtc); |
| ecarrick | 1:e4753022f351 | 40 | led1=1; |
| ecarrick | 1:e4753022f351 | 41 | if(!alarmFired){ |
| ecarrick | 1:e4753022f351 | 42 | printf("hr: %d min:%d\r\n", tp->tm_hour, tp->tm_min); |
| ecarrick | 1:e4753022f351 | 43 | int daystamp = tp->tm_hour * 3600 + tp->tm_min * 60; |
| ecarrick | 1:e4753022f351 | 44 | int alarmstamp = alarmHour * 3600 + alarmMin * 60 - (alarmDur * 60); |
| ecarrick | 1:e4753022f351 | 45 | pc.printf("%d - %d = %d", alarmstamp, daystamp, alarmstamp - daystamp); |
| ecarrick | 1:e4753022f351 | 46 | if( (alarmstamp - daystamp) <= 0){ |
| ecarrick | 1:e4753022f351 | 47 | // time to start the wake up sequence |
| ecarrick | 1:e4753022f351 | 48 | startSunrise((float)(alarmDur*60)); |
| ecarrick | 1:e4753022f351 | 49 | alarmFired = true; |
| ecarrick | 1:e4753022f351 | 50 | } |
| ecarrick | 1:e4753022f351 | 51 | } else{ |
| ecarrick | 1:e4753022f351 | 52 | if(tp->tm_hour == 0) |
| ecarrick | 1:e4753022f351 | 53 | alarmFired = false; |
| ecarrick | 1:e4753022f351 | 54 | } |
| ecarrick | 1:e4753022f351 | 55 | wait(1); |
| ecarrick | 1:e4753022f351 | 56 | led1=0; |
| ecarrick | 1:e4753022f351 | 57 | |
| ecarrick | 1:e4753022f351 | 58 | } |
| ecarrick | 1:e4753022f351 | 59 | |
| ecarrick | 1:e4753022f351 | 60 | void flush(){ |
| ecarrick | 1:e4753022f351 | 61 | while(esp.readable()) { |
| ecarrick | 1:e4753022f351 | 62 | char tmp = esp.getc(); |
| ecarrick | 1:e4753022f351 | 63 | data += tmp; |
| ecarrick | 1:e4753022f351 | 64 | pc.putc(tmp); |
| ecarrick | 1:e4753022f351 | 65 | } |
| ecarrick | 1:e4753022f351 | 66 | } |
| jhunter029 | 0:57cec3469a80 | 67 | void dev_recv() |
| jhunter029 | 0:57cec3469a80 | 68 | { |
| jhunter029 | 0:57cec3469a80 | 69 | led1 = !led1; |
| ecarrick | 1:e4753022f351 | 70 | /* |
| jhunter029 | 0:57cec3469a80 | 71 | while(esp.readable()) { |
| ecarrick | 1:e4753022f351 | 72 | char tmp = esp.getc(); |
| ecarrick | 1:e4753022f351 | 73 | data += tmp; |
| ecarrick | 1:e4753022f351 | 74 | pc.putc(tmp); |
| ecarrick | 1:e4753022f351 | 75 | } |
| ecarrick | 1:e4753022f351 | 76 | */ |
| ecarrick | 1:e4753022f351 | 77 | timeout=2; |
| ecarrick | 1:e4753022f351 | 78 | getreply(); |
| ecarrick | 1:e4753022f351 | 79 | pc.printf("%s\n\n", buf); |
| ecarrick | 1:e4753022f351 | 80 | char * location = strstr(buf, "GET /?"); |
| ecarrick | 1:e4753022f351 | 81 | if(location != NULL){ |
| ecarrick | 1:e4753022f351 | 82 | //pc.printf("%d\n", location - buf); |
| ecarrick | 1:e4753022f351 | 83 | char * hour = strstr(buf, "h="); |
| ecarrick | 1:e4753022f351 | 84 | char * min = strstr(buf, "m="); |
| ecarrick | 1:e4753022f351 | 85 | char * dur = strstr(buf, "d="); |
| ecarrick | 1:e4753022f351 | 86 | if(hour != NULL && min != NULL && dur != NULL){ |
| ecarrick | 1:e4753022f351 | 87 | strncpy(val, hour+2,2); |
| ecarrick | 1:e4753022f351 | 88 | alarmHour = atoi(val) %24; |
| ecarrick | 1:e4753022f351 | 89 | strncpy(val, min+2,2); |
| ecarrick | 1:e4753022f351 | 90 | alarmMin = atoi(val) %60; |
| ecarrick | 1:e4753022f351 | 91 | strncpy(val, dur+2,2); |
| ecarrick | 1:e4753022f351 | 92 | alarmDur = atoi(val); |
| ecarrick | 1:e4753022f351 | 93 | |
| ecarrick | 1:e4753022f351 | 94 | pc.printf("New config: hrs %d min %d duration %d", alarmHour, alarmMin, alarmDur); |
| ecarrick | 1:e4753022f351 | 95 | sendConfig(); |
| ecarrick | 1:e4753022f351 | 96 | |
| ecarrick | 1:e4753022f351 | 97 | } |
| jhunter029 | 0:57cec3469a80 | 98 | } |
| jhunter029 | 0:57cec3469a80 | 99 | } |
| jhunter029 | 0:57cec3469a80 | 100 | |
| jhunter029 | 0:57cec3469a80 | 101 | void pc_recv() |
| jhunter029 | 0:57cec3469a80 | 102 | { |
| jhunter029 | 0:57cec3469a80 | 103 | led4 = !led4; |
| jhunter029 | 0:57cec3469a80 | 104 | while(pc.readable()) { |
| jhunter029 | 0:57cec3469a80 | 105 | esp.putc(pc.getc()); |
| jhunter029 | 0:57cec3469a80 | 106 | } |
| jhunter029 | 0:57cec3469a80 | 107 | } |
| jhunter029 | 0:57cec3469a80 | 108 | |
| jhunter029 | 0:57cec3469a80 | 109 | |
| jhunter029 | 0:57cec3469a80 | 110 | int main() |
| jhunter029 | 0:57cec3469a80 | 111 | { |
| ecarrick | 1:e4753022f351 | 112 | |
| ecarrick | 1:e4753022f351 | 113 | pb.mode(PullDown); |
| ecarrick | 1:e4753022f351 | 114 | light.period(0.005); //200Hz to eliminate flicker |
| jhunter029 | 0:57cec3469a80 | 115 | reset=0; //hardware reset for 8266 |
| ecarrick | 1:e4753022f351 | 116 | pc.baud(115200); // set what you want here depending on your terminal program speed |
| jhunter029 | 0:57cec3469a80 | 117 | pc.printf("\f\n\r-------------ESP8266 Hardware Reset-------------\n\r"); |
| jhunter029 | 0:57cec3469a80 | 118 | wait(0.5); |
| jhunter029 | 0:57cec3469a80 | 119 | reset=1; |
| ecarrick | 1:e4753022f351 | 120 | timeout=1; |
| jhunter029 | 0:57cec3469a80 | 121 | getreply(); |
| ecarrick | 1:e4753022f351 | 122 | |
| ecarrick | 1:e4753022f351 | 123 | // RESET wifi module and and connect to the wifi |
| ecarrick | 1:e4753022f351 | 124 | ESPreset(); |
| ecarrick | 1:e4753022f351 | 125 | |
| ecarrick | 1:e4753022f351 | 126 | // send the NTP function to wifi module |
| ecarrick | 1:e4753022f351 | 127 | sendNTPFunction(); |
| ecarrick | 1:e4753022f351 | 128 | |
| ecarrick | 1:e4753022f351 | 129 | |
| ecarrick | 1:e4753022f351 | 130 | |
| ecarrick | 1:e4753022f351 | 131 | |
| ecarrick | 1:e4753022f351 | 132 | while(tp->tm_year ==0){ |
| ecarrick | 1:e4753022f351 | 133 | updateNTP(); |
| ecarrick | 1:e4753022f351 | 134 | wait(3); |
| ecarrick | 1:e4753022f351 | 135 | getUtime(); |
| ecarrick | 1:e4753022f351 | 136 | wait(1); |
| ecarrick | 1:e4753022f351 | 137 | } |
| ecarrick | 1:e4753022f351 | 138 | |
| ecarrick | 1:e4753022f351 | 139 | led3=1; |
| ecarrick | 1:e4753022f351 | 140 | |
| ecarrick | 1:e4753022f351 | 141 | for(float i=0.0;i<1.0;i+=0.1){ |
| ecarrick | 1:e4753022f351 | 142 | light=i; |
| ecarrick | 1:e4753022f351 | 143 | wait(0.5); |
| ecarrick | 1:e4753022f351 | 144 | } |
| ecarrick | 1:e4753022f351 | 145 | light = 0; |
| jhunter029 | 0:57cec3469a80 | 146 | |
| ecarrick | 1:e4753022f351 | 147 | //ESPreset(); |
| jhunter029 | 0:57cec3469a80 | 148 | |
| ecarrick | 1:e4753022f351 | 149 | ESPhttp(); |
| ecarrick | 1:e4753022f351 | 150 | |
| ecarrick | 1:e4753022f351 | 151 | //pc.attach(&pc_recv, Serial::RxIrq); |
| jhunter029 | 0:57cec3469a80 | 152 | esp.attach(&dev_recv, Serial::RxIrq); |
| jhunter029 | 0:57cec3469a80 | 153 | |
| ecarrick | 1:e4753022f351 | 154 | rtc = time(NULL); |
| ecarrick | 1:e4753022f351 | 155 | pc.printf("%s\r\n", ctime(&rtc)); |
| ecarrick | 1:e4753022f351 | 156 | led1=0;led2=0;led3=0;led4=0; |
| ecarrick | 1:e4753022f351 | 157 | tp = localtime(&rtc); |
| ecarrick | 1:e4753022f351 | 158 | printf("hr: %d min:%d\r\n", tp->tm_hour, tp->tm_min); |
| ecarrick | 1:e4753022f351 | 159 | alarmChecker.attach(&alarmCheck, 60); |
| jhunter029 | 0:57cec3469a80 | 160 | while(1) { |
| jhunter029 | 0:57cec3469a80 | 161 | sleep(); |
| jhunter029 | 0:57cec3469a80 | 162 | } |
| jhunter029 | 0:57cec3469a80 | 163 | |
| jhunter029 | 0:57cec3469a80 | 164 | } |
| ecarrick | 1:e4753022f351 | 165 | |
| ecarrick | 1:e4753022f351 | 166 | void startSunrise(float d){ |
| ecarrick | 1:e4753022f351 | 167 | for(float i=0.0; i < 1.0; i += (1.0/d)){ |
| ecarrick | 1:e4753022f351 | 168 | light = i; |
| ecarrick | 1:e4753022f351 | 169 | pc.printf("%f\n", i); |
| ecarrick | 1:e4753022f351 | 170 | wait(1); |
| ecarrick | 1:e4753022f351 | 171 | } |
| ecarrick | 1:e4753022f351 | 172 | light = 1.0; |
| ecarrick | 1:e4753022f351 | 173 | |
| ecarrick | 1:e4753022f351 | 174 | wait(120); |
| ecarrick | 1:e4753022f351 | 175 | light = 0; |
| ecarrick | 1:e4753022f351 | 176 | |
| ecarrick | 1:e4753022f351 | 177 | } |
| jhunter029 | 0:57cec3469a80 | 178 | |
| jhunter029 | 0:57cec3469a80 | 179 | // Sets new ESP8266 baurate, change the esp.baud(xxxxx) to match your new setting once this has been executed |
| jhunter029 | 0:57cec3469a80 | 180 | void ESPsetbaudrate() |
| jhunter029 | 0:57cec3469a80 | 181 | { |
| ecarrick | 1:e4753022f351 | 182 | strcpy(snd, "uart.setup(0, 9600, 8, 0, 1, 0)\r\n"); // change the numeric value to the required baudrate |
| jhunter029 | 0:57cec3469a80 | 183 | SendCMD(); |
| ecarrick | 1:e4753022f351 | 184 | timeout=2; |
| ecarrick | 1:e4753022f351 | 185 | getreply(); |
| ecarrick | 1:e4753022f351 | 186 | pc.printf(buf); |
| jhunter029 | 0:57cec3469a80 | 187 | } |
| jhunter029 | 0:57cec3469a80 | 188 | |
| ecarrick | 1:e4753022f351 | 189 | void ESPreset(){ |
| jhunter029 | 0:57cec3469a80 | 190 | pc.printf("---------- Reset & get Firmware ----------\r\n"); |
| jhunter029 | 0:57cec3469a80 | 191 | strcpy(snd,"node.restart()\r\n"); |
| jhunter029 | 0:57cec3469a80 | 192 | SendCMD(); |
| ecarrick | 1:e4753022f351 | 193 | timeout=2; |
| jhunter029 | 0:57cec3469a80 | 194 | getreply(); |
| jhunter029 | 0:57cec3469a80 | 195 | pc.printf(buf); |
| ecarrick | 1:e4753022f351 | 196 | |
| ecarrick | 1:e4753022f351 | 197 | ESPsetbaudrate(); |
| ecarrick | 1:e4753022f351 | 198 | |
| ecarrick | 1:e4753022f351 | 199 | |
| jhunter029 | 0:57cec3469a80 | 200 | pc.printf("\n---------- Connecting to AP ----------\r\n"); |
| jhunter029 | 0:57cec3469a80 | 201 | pc.printf("ssid = %s pwd = %s\r\n",ssid,pwd); |
| jhunter029 | 0:57cec3469a80 | 202 | strcpy(snd, "wifi.sta.config(\""); |
| jhunter029 | 0:57cec3469a80 | 203 | strcat(snd, ssid); |
| jhunter029 | 0:57cec3469a80 | 204 | strcat(snd, "\",\""); |
| jhunter029 | 0:57cec3469a80 | 205 | strcat(snd, pwd); |
| jhunter029 | 0:57cec3469a80 | 206 | strcat(snd, "\")\r\n"); |
| jhunter029 | 0:57cec3469a80 | 207 | SendCMD(); |
| ecarrick | 1:e4753022f351 | 208 | timeout=3; |
| jhunter029 | 0:57cec3469a80 | 209 | getreply(); |
| jhunter029 | 0:57cec3469a80 | 210 | pc.printf(buf); |
| jhunter029 | 0:57cec3469a80 | 211 | |
| jhunter029 | 0:57cec3469a80 | 212 | wait(5); |
| jhunter029 | 0:57cec3469a80 | 213 | |
| jhunter029 | 0:57cec3469a80 | 214 | pc.printf("\n---------- Get IP's ----------\r\n"); |
| jhunter029 | 0:57cec3469a80 | 215 | strcpy(snd, "print(wifi.sta.getip())\r\n"); |
| jhunter029 | 0:57cec3469a80 | 216 | SendCMD(); |
| ecarrick | 1:e4753022f351 | 217 | timeout=2; |
| jhunter029 | 0:57cec3469a80 | 218 | getreply(); |
| jhunter029 | 0:57cec3469a80 | 219 | pc.printf(buf); |
| jhunter029 | 0:57cec3469a80 | 220 | |
| jhunter029 | 0:57cec3469a80 | 221 | wait(1); |
| jhunter029 | 0:57cec3469a80 | 222 | |
| jhunter029 | 0:57cec3469a80 | 223 | pc.printf("\n---------- Get Connection Status ----------\r\n"); |
| jhunter029 | 0:57cec3469a80 | 224 | strcpy(snd, "print(wifi.sta.status())\r\n"); |
| ecarrick | 1:e4753022f351 | 225 | pc.printf("%s", snd); |
| jhunter029 | 0:57cec3469a80 | 226 | SendCMD(); |
| ecarrick | 1:e4753022f351 | 227 | timeout=2; |
| jhunter029 | 0:57cec3469a80 | 228 | getreply(); |
| jhunter029 | 0:57cec3469a80 | 229 | pc.printf(buf); |
| ecarrick | 1:e4753022f351 | 230 | |
| ecarrick | 1:e4753022f351 | 231 | |
| ecarrick | 1:e4753022f351 | 232 | } |
| jhunter029 | 0:57cec3469a80 | 233 | |
| ecarrick | 1:e4753022f351 | 234 | // +++++++++++++++++++++++++++++++++ This is for ESP8266 config only, run this once to set up the ESP8266 +++++++++++++++ |
| ecarrick | 1:e4753022f351 | 235 | void ESPhttp() |
| ecarrick | 1:e4753022f351 | 236 | { |
| ecarrick | 1:e4753022f351 | 237 | |
| jhunter029 | 0:57cec3469a80 | 238 | pc.printf("\n---------- Setting up http server ----------\r\n"); |
| ecarrick | 1:e4753022f351 | 239 | |
| ecarrick | 1:e4753022f351 | 240 | sendConfig(); |
| ecarrick | 1:e4753022f351 | 241 | |
| ecarrick | 1:e4753022f351 | 242 | strcpy(snd, "srv=net.createServer(net.TCP)\r\n"); |
| jhunter029 | 0:57cec3469a80 | 243 | SendCMD(); |
| jhunter029 | 0:57cec3469a80 | 244 | wait(1); |
| jhunter029 | 0:57cec3469a80 | 245 | strcpy(snd, "srv:listen(80,function(conn)\r\n"); |
| jhunter029 | 0:57cec3469a80 | 246 | SendCMD(); |
| jhunter029 | 0:57cec3469a80 | 247 | wait(1); |
| jhunter029 | 0:57cec3469a80 | 248 | strcpy(snd, "conn:on(\"receive\",function(conn,payload)\r\n"); |
| jhunter029 | 0:57cec3469a80 | 249 | SendCMD(); |
| jhunter029 | 0:57cec3469a80 | 250 | wait(1); |
| ecarrick | 1:e4753022f351 | 251 | |
| jhunter029 | 0:57cec3469a80 | 252 | strcpy(snd, "print(payload)\r\n"); |
| jhunter029 | 0:57cec3469a80 | 253 | SendCMD(); |
| jhunter029 | 0:57cec3469a80 | 254 | wait(1); |
| jhunter029 | 0:57cec3469a80 | 255 | |
| ecarrick | 1:e4753022f351 | 256 | |
| jhunter029 | 0:57cec3469a80 | 257 | strcpy(snd, "conn:send(\"<!DOCTYPE html>\")\r\n"); |
| jhunter029 | 0:57cec3469a80 | 258 | SendCMD(); |
| jhunter029 | 0:57cec3469a80 | 259 | wait(1); |
| jhunter029 | 0:57cec3469a80 | 260 | |
| jhunter029 | 0:57cec3469a80 | 261 | strcpy(snd, "conn:send(\"<html>\")\r\n"); |
| jhunter029 | 0:57cec3469a80 | 262 | SendCMD(); |
| jhunter029 | 0:57cec3469a80 | 263 | wait(1); |
| jhunter029 | 0:57cec3469a80 | 264 | |
| ecarrick | 1:e4753022f351 | 265 | 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 | 266 | SendCMD(); |
| ecarrick | 1:e4753022f351 | 267 | wait(1); |
| ecarrick | 1:e4753022f351 | 268 | |
| ecarrick | 1:e4753022f351 | 269 | strcpy(snd, "conn:send(hour)\r\n"); |
| ecarrick | 1:e4753022f351 | 270 | SendCMD(); |
| ecarrick | 1:e4753022f351 | 271 | wait(1); |
| ecarrick | 1:e4753022f351 | 272 | |
| ecarrick | 1:e4753022f351 | 273 | strcpy(snd, "conn:send(\"'> Hours and <input name=m value='\")\r\n"); |
| ecarrick | 1:e4753022f351 | 274 | SendCMD(); |
| jhunter029 | 0:57cec3469a80 | 275 | wait(1); |
| jhunter029 | 0:57cec3469a80 | 276 | |
| ecarrick | 1:e4753022f351 | 277 | strcpy(snd, "conn:send(min)\r\n"); |
| ecarrick | 1:e4753022f351 | 278 | SendCMD(); |
| ecarrick | 1:e4753022f351 | 279 | wait(1); |
| ecarrick | 1:e4753022f351 | 280 | |
| ecarrick | 1:e4753022f351 | 281 | strcpy(snd, "conn:send(\"'> minutes <br> <br> Start sunrise <input name=d value='\")\r\n"); |
| ecarrick | 1:e4753022f351 | 282 | SendCMD(); |
| ecarrick | 1:e4753022f351 | 283 | wait(1); |
| ecarrick | 1:e4753022f351 | 284 | |
| ecarrick | 1:e4753022f351 | 285 | strcpy(snd, "conn:send(dur)\r\n"); |
| ecarrick | 1:e4753022f351 | 286 | SendCMD(); |
| ecarrick | 1:e4753022f351 | 287 | wait(1); |
| ecarrick | 1:e4753022f351 | 288 | |
| ecarrick | 1:e4753022f351 | 289 | strcpy(snd, "conn:send(\"'> minutes before <input type=submit value='submit'></form>\")\r\n"); |
| jhunter029 | 0:57cec3469a80 | 290 | SendCMD(); |
| jhunter029 | 0:57cec3469a80 | 291 | wait(1); |
| jhunter029 | 0:57cec3469a80 | 292 | |
| jhunter029 | 0:57cec3469a80 | 293 | strcpy(snd, "conn:send(\"</html>\")\r\n"); |
| jhunter029 | 0:57cec3469a80 | 294 | SendCMD(); |
| jhunter029 | 0:57cec3469a80 | 295 | wait(1); |
| jhunter029 | 0:57cec3469a80 | 296 | |
| jhunter029 | 0:57cec3469a80 | 297 | strcpy(snd, "end)\r\n"); |
| jhunter029 | 0:57cec3469a80 | 298 | SendCMD(); |
| jhunter029 | 0:57cec3469a80 | 299 | wait(1); |
| jhunter029 | 0:57cec3469a80 | 300 | |
| jhunter029 | 0:57cec3469a80 | 301 | strcpy(snd, "conn:on(\"sent\",function(conn) conn:close() end)\r\n"); |
| jhunter029 | 0:57cec3469a80 | 302 | SendCMD(); |
| jhunter029 | 0:57cec3469a80 | 303 | wait(1); |
| jhunter029 | 0:57cec3469a80 | 304 | strcpy(snd, "end)\r\n"); |
| jhunter029 | 0:57cec3469a80 | 305 | SendCMD(); |
| ecarrick | 1:e4753022f351 | 306 | |
| ecarrick | 1:e4753022f351 | 307 | flush(); |
| ecarrick | 1:e4753022f351 | 308 | |
| ecarrick | 1:e4753022f351 | 309 | pc.printf("\r\nDONE\r\n"); |
| jhunter029 | 0:57cec3469a80 | 310 | } |
| jhunter029 | 0:57cec3469a80 | 311 | |
| jhunter029 | 0:57cec3469a80 | 312 | void SendCMD() |
| jhunter029 | 0:57cec3469a80 | 313 | { |
| ecarrick | 1:e4753022f351 | 314 | pc.printf("%s", snd); |
| jhunter029 | 0:57cec3469a80 | 315 | esp.printf("%s", snd); |
| jhunter029 | 0:57cec3469a80 | 316 | } |
| jhunter029 | 0:57cec3469a80 | 317 | |
| jhunter029 | 0:57cec3469a80 | 318 | void getreply() |
| jhunter029 | 0:57cec3469a80 | 319 | { |
| jhunter029 | 0:57cec3469a80 | 320 | memset(buf, '\0', sizeof(buf)); |
| jhunter029 | 0:57cec3469a80 | 321 | t.start(); |
| jhunter029 | 0:57cec3469a80 | 322 | ended=0; |
| jhunter029 | 0:57cec3469a80 | 323 | count=0; |
| jhunter029 | 0:57cec3469a80 | 324 | while(!ended) { |
| jhunter029 | 0:57cec3469a80 | 325 | if(esp.readable()) { |
| jhunter029 | 0:57cec3469a80 | 326 | buf[count] = esp.getc(); |
| jhunter029 | 0:57cec3469a80 | 327 | count++; |
| jhunter029 | 0:57cec3469a80 | 328 | } |
| jhunter029 | 0:57cec3469a80 | 329 | if(t.read() > timeout) { |
| jhunter029 | 0:57cec3469a80 | 330 | ended = 1; |
| jhunter029 | 0:57cec3469a80 | 331 | t.stop(); |
| jhunter029 | 0:57cec3469a80 | 332 | t.reset(); |
| jhunter029 | 0:57cec3469a80 | 333 | } |
| jhunter029 | 0:57cec3469a80 | 334 | } |
| jhunter029 | 0:57cec3469a80 | 335 | } |
| jhunter029 | 0:57cec3469a80 | 336 | |
| ecarrick | 1:e4753022f351 | 337 | void sendConfig() |
| ecarrick | 1:e4753022f351 | 338 | { |
| ecarrick | 1:e4753022f351 | 339 | stringstream strs; |
| ecarrick | 1:e4753022f351 | 340 | stringstream strs1; |
| ecarrick | 1:e4753022f351 | 341 | stringstream strs2; |
| ecarrick | 1:e4753022f351 | 342 | |
| ecarrick | 1:e4753022f351 | 343 | strcpy(snd, "hour="); |
| ecarrick | 1:e4753022f351 | 344 | SendCMD(); |
| ecarrick | 1:e4753022f351 | 345 | strs << alarmHour; |
| ecarrick | 1:e4753022f351 | 346 | string temp_str = strs.str(); |
| ecarrick | 1:e4753022f351 | 347 | char const* pchar = temp_str.c_str(); |
| ecarrick | 1:e4753022f351 | 348 | strcpy(snd, pchar); |
| ecarrick | 1:e4753022f351 | 349 | SendCMD(); |
| ecarrick | 1:e4753022f351 | 350 | strcpy(snd, "\r\n"); |
| ecarrick | 1:e4753022f351 | 351 | SendCMD(); |
| ecarrick | 1:e4753022f351 | 352 | wait(1); |
| ecarrick | 1:e4753022f351 | 353 | |
| ecarrick | 1:e4753022f351 | 354 | |
| ecarrick | 1:e4753022f351 | 355 | strcpy(snd, "min="); |
| ecarrick | 1:e4753022f351 | 356 | SendCMD(); |
| ecarrick | 1:e4753022f351 | 357 | strs1 << alarmMin; |
| ecarrick | 1:e4753022f351 | 358 | temp_str = strs1.str(); |
| ecarrick | 1:e4753022f351 | 359 | pchar = temp_str.c_str(); |
| ecarrick | 1:e4753022f351 | 360 | strcpy(snd, pchar); |
| ecarrick | 1:e4753022f351 | 361 | SendCMD(); |
| ecarrick | 1:e4753022f351 | 362 | strcpy(snd, "\r\n"); |
| ecarrick | 1:e4753022f351 | 363 | SendCMD(); |
| ecarrick | 1:e4753022f351 | 364 | wait(1); |
| ecarrick | 1:e4753022f351 | 365 | |
| ecarrick | 1:e4753022f351 | 366 | |
| ecarrick | 1:e4753022f351 | 367 | |
| ecarrick | 1:e4753022f351 | 368 | strcpy(snd, "dur="); |
| ecarrick | 1:e4753022f351 | 369 | SendCMD(); |
| ecarrick | 1:e4753022f351 | 370 | strs2 << alarmDur; |
| ecarrick | 1:e4753022f351 | 371 | temp_str = strs2.str(); |
| ecarrick | 1:e4753022f351 | 372 | pchar = temp_str.c_str(); |
| ecarrick | 1:e4753022f351 | 373 | strcpy(snd, pchar); |
| ecarrick | 1:e4753022f351 | 374 | SendCMD(); |
| ecarrick | 1:e4753022f351 | 375 | strcpy(snd, "\r\n"); |
| ecarrick | 1:e4753022f351 | 376 | SendCMD(); |
| ecarrick | 1:e4753022f351 | 377 | wait(1); |
| ecarrick | 1:e4753022f351 | 378 | } |
| ecarrick | 1:e4753022f351 | 379 | |
| ecarrick | 1:e4753022f351 | 380 | void sendNTPFunction() |
| ecarrick | 1:e4753022f351 | 381 | { |
| ecarrick | 1:e4753022f351 | 382 | wait(15); |
| ecarrick | 1:e4753022f351 | 383 | strcpy(snd, "timezone = -4\r\n"); |
| ecarrick | 1:e4753022f351 | 384 | SendCMD(); |
| ecarrick | 1:e4753022f351 | 385 | wait(2); |
| ecarrick | 1:e4753022f351 | 386 | 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 | 387 | SendCMD(); |
| ecarrick | 1:e4753022f351 | 388 | wait(4); |
| ecarrick | 1:e4753022f351 | 389 | |
| ecarrick | 1:e4753022f351 | 390 | strcpy(snd, "time = 0\r\n"); |
| ecarrick | 1:e4753022f351 | 391 | SendCMD(); |
| ecarrick | 1:e4753022f351 | 392 | wait(1); |
| ecarrick | 1:e4753022f351 | 393 | strcpy(snd, "sk=net.createConnection(net.UDP, 0)\r\n"); |
| ecarrick | 1:e4753022f351 | 394 | SendCMD(); |
| ecarrick | 1:e4753022f351 | 395 | wait(1); |
| ecarrick | 1:e4753022f351 | 396 | |
| ecarrick | 1:e4753022f351 | 397 | strcpy(snd, "sk:on(\"receive\", function(sck, payload)\r\n"); |
| ecarrick | 1:e4753022f351 | 398 | SendCMD(); |
| ecarrick | 1:e4753022f351 | 399 | wait(1); |
| ecarrick | 1:e4753022f351 | 400 | strcpy(snd, "print(\"YAY!\")\r\n"); |
| ecarrick | 1:e4753022f351 | 401 | SendCMD(); |
| ecarrick | 1:e4753022f351 | 402 | wait(1); |
| ecarrick | 1:e4753022f351 | 403 | strcpy(snd, "local highw,loww,ntpstamp\r\n"); |
| ecarrick | 1:e4753022f351 | 404 | SendCMD(); |
| ecarrick | 1:e4753022f351 | 405 | wait(1); |
| ecarrick | 1:e4753022f351 | 406 | strcpy(snd, "highw = payload:byte(41) * 256 + payload:byte(42)\r\n"); |
| ecarrick | 1:e4753022f351 | 407 | SendCMD(); |
| ecarrick | 1:e4753022f351 | 408 | wait(1); |
| ecarrick | 1:e4753022f351 | 409 | strcpy(snd, "loww = payload:byte(43) * 256 + payload:byte(44)\r\n"); |
| ecarrick | 1:e4753022f351 | 410 | SendCMD(); |
| ecarrick | 1:e4753022f351 | 411 | wait(1); |
| ecarrick | 1:e4753022f351 | 412 | strcpy(snd, "ntpstamp=( highw * 65536 + loww ) + ( timezone* 3600)\r\n"); |
| ecarrick | 1:e4753022f351 | 413 | SendCMD(); |
| ecarrick | 1:e4753022f351 | 414 | wait(1); |
| ecarrick | 1:e4753022f351 | 415 | strcpy(snd, "time = ntpstamp - 1104494400 - 1104494400\r\n"); |
| ecarrick | 1:e4753022f351 | 416 | SendCMD(); |
| ecarrick | 1:e4753022f351 | 417 | wait(1); |
| ecarrick | 1:e4753022f351 | 418 | strcpy(snd, "print(time)\r\n"); |
| ecarrick | 1:e4753022f351 | 419 | SendCMD(); |
| ecarrick | 1:e4753022f351 | 420 | wait(1); |
| ecarrick | 1:e4753022f351 | 421 | |
| ecarrick | 1:e4753022f351 | 422 | strcpy(snd, "sck:close()\r\n"); |
| ecarrick | 1:e4753022f351 | 423 | SendCMD(); |
| ecarrick | 1:e4753022f351 | 424 | wait(1); |
| ecarrick | 1:e4753022f351 | 425 | |
| ecarrick | 1:e4753022f351 | 426 | strcpy(snd, "end )\r\n"); |
| ecarrick | 1:e4753022f351 | 427 | SendCMD(); |
| ecarrick | 1:e4753022f351 | 428 | wait(1); |
| ecarrick | 1:e4753022f351 | 429 | |
| ecarrick | 1:e4753022f351 | 430 | flush(); |
| ecarrick | 1:e4753022f351 | 431 | wait(15); |
| ecarrick | 1:e4753022f351 | 432 | //updateNTP(); |
| ecarrick | 1:e4753022f351 | 433 | } |
| ecarrick | 1:e4753022f351 | 434 | void updateNTP(){ |
| ecarrick | 1:e4753022f351 | 435 | flush(); |
| ecarrick | 1:e4753022f351 | 436 | strcpy(snd, "sk:connect(123,\"104.232.3.3\")\r\n"); |
| ecarrick | 1:e4753022f351 | 437 | SendCMD(); |
| ecarrick | 1:e4753022f351 | 438 | wait(3); |
| ecarrick | 1:e4753022f351 | 439 | strcpy(snd, "sk:send(request)\r\n"); |
| ecarrick | 1:e4753022f351 | 440 | SendCMD(); |
| ecarrick | 1:e4753022f351 | 441 | wait(4); |
| ecarrick | 1:e4753022f351 | 442 | timeout=3; |
| ecarrick | 1:e4753022f351 | 443 | getreply(); |
| ecarrick | 1:e4753022f351 | 444 | pc.printf(buf); |
| ecarrick | 1:e4753022f351 | 445 | //getUtime(); |
| ecarrick | 1:e4753022f351 | 446 | } |
| ecarrick | 1:e4753022f351 | 447 | |
| ecarrick | 1:e4753022f351 | 448 | void getUtime(){ |
| ecarrick | 1:e4753022f351 | 449 | flush(); |
| ecarrick | 1:e4753022f351 | 450 | strcpy(snd, "print(time)\r\n"); |
| ecarrick | 1:e4753022f351 | 451 | SendCMD(); |
| ecarrick | 1:e4753022f351 | 452 | wait(1); |
| ecarrick | 1:e4753022f351 | 453 | timeout=5; |
| ecarrick | 1:e4753022f351 | 454 | getreply(); |
| ecarrick | 1:e4753022f351 | 455 | pc.printf(buf); |
| ecarrick | 1:e4753022f351 | 456 | int utime = atoi(buf); |
| ecarrick | 1:e4753022f351 | 457 | if(utime != NULL) |
| ecarrick | 1:e4753022f351 | 458 | set_time(utime); |
| ecarrick | 1:e4753022f351 | 459 | time_t seconds = time(NULL); |
| ecarrick | 1:e4753022f351 | 460 | pc.printf("Time as a basic string = %s", ctime(&seconds)); |
| ecarrick | 1:e4753022f351 | 461 | } |
