Mostly functional

Dependencies:   4DGL-uLCD-SE PinDetect mbed ttmath

Committer:
egnormichael
Date:
Fri Dec 08 11:54:27 2017 +0000
Revision:
0:9b9696eab6d5
Functional Alarm clock, requires a couple teaks for display

Who changed what in which revision?

UserRevisionLine numberNew contents of line
egnormichael 0:9b9696eab6d5 1 #include "mbed.h"
egnormichael 0:9b9696eab6d5 2 #include <ttmath.h>
egnormichael 0:9b9696eab6d5 3 #include "PinDetect.h"
egnormichael 0:9b9696eab6d5 4 #include "uLCD_4DGL.h"
egnormichael 0:9b9696eab6d5 5
egnormichael 0:9b9696eab6d5 6 Serial pc(USBTX, USBRX);
egnormichael 0:9b9696eab6d5 7 Serial esp(p28, p27); // tx, rx
egnormichael 0:9b9696eab6d5 8 DigitalOut reset(p26);
egnormichael 0:9b9696eab6d5 9 DigitalOut led1(LED1);
egnormichael 0:9b9696eab6d5 10 DigitalOut led4(LED4);
egnormichael 0:9b9696eab6d5 11 Timer t;
egnormichael 0:9b9696eab6d5 12
egnormichael 0:9b9696eab6d5 13 uLCD_4DGL uLCD(p9,p10,p11); // serial tx, serial rx, reset pin;
egnormichael 0:9b9696eab6d5 14
egnormichael 0:9b9696eab6d5 15 PinDetect pb1(p17);
egnormichael 0:9b9696eab6d5 16 PinDetect pb2(p18);
egnormichael 0:9b9696eab6d5 17 PinDetect pb3(p19);
egnormichael 0:9b9696eab6d5 18 PinDetect pb4(p20);
egnormichael 0:9b9696eab6d5 19
egnormichael 0:9b9696eab6d5 20 // Global count variable
egnormichael 0:9b9696eab6d5 21
egnormichael 0:9b9696eab6d5 22 int volatile hours=12;
egnormichael 0:9b9696eab6d5 23 int volatile minutes=0;
egnormichael 0:9b9696eab6d5 24 bool volatile alarmSet=false;
egnormichael 0:9b9696eab6d5 25 int volatile alarmTimeSet = 0;
egnormichael 0:9b9696eab6d5 26 int volatile timeDisp=0; //real time
egnormichael 0:9b9696eab6d5 27 bool volatile AMPM=false; //False AM, true PM
egnormichael 0:9b9696eab6d5 28
egnormichael 0:9b9696eab6d5 29
egnormichael 0:9b9696eab6d5 30 int count,ended,timeout;
egnormichael 0:9b9696eab6d5 31 char buf[2024];
egnormichael 0:9b9696eab6d5 32 char snd[1024];
egnormichael 0:9b9696eab6d5 33 char ssid[32] = "MEwifi"; // enter WiFi router ssid inside the quotes
egnormichael 0:9b9696eab6d5 34 char pwd [32] = "fxyh5453"; // enter WiFi router password inside the quotes
egnormichael 0:9b9696eab6d5 35 bool timeReady = false;
egnormichael 0:9b9696eab6d5 36
egnormichael 0:9b9696eab6d5 37 char curTime[20];
egnormichael 0:9b9696eab6d5 38
egnormichael 0:9b9696eab6d5 39 void SendCMD(),getreply(),ESPconfig(),ESPsetbaudrate(), getTime();
egnormichael 0:9b9696eab6d5 40 int timeFromResponse();
egnormichael 0:9b9696eab6d5 41 void dev_recv()
egnormichael 0:9b9696eab6d5 42 {
egnormichael 0:9b9696eab6d5 43 led1 = !led1;
egnormichael 0:9b9696eab6d5 44 while(esp.readable()) {
egnormichael 0:9b9696eab6d5 45 pc.putc(esp.getc());
egnormichael 0:9b9696eab6d5 46 }
egnormichael 0:9b9696eab6d5 47 }
egnormichael 0:9b9696eab6d5 48
egnormichael 0:9b9696eab6d5 49 void pc_recv()
egnormichael 0:9b9696eab6d5 50 {
egnormichael 0:9b9696eab6d5 51 led4 = !led4;
egnormichael 0:9b9696eab6d5 52 while(pc.readable()) {
egnormichael 0:9b9696eab6d5 53 esp.putc(pc.getc());
egnormichael 0:9b9696eab6d5 54 }
egnormichael 0:9b9696eab6d5 55 }
egnormichael 0:9b9696eab6d5 56
egnormichael 0:9b9696eab6d5 57 void pb1_hit_callback (void) {
egnormichael 0:9b9696eab6d5 58 count++;
egnormichael 0:9b9696eab6d5 59 hours++;
egnormichael 0:9b9696eab6d5 60 if(hours>12){ hours= 1; AMPM=!AMPM;}
egnormichael 0:9b9696eab6d5 61
egnormichael 0:9b9696eab6d5 62
egnormichael 0:9b9696eab6d5 63 //uLCD.cls();
egnormichael 0:9b9696eab6d5 64 //if(AMPM)
egnormichael 0:9b9696eab6d5 65 //uLCD.printf("\nAlarm Time %d : %d PM\nTime %d\nAlarm Time %d",hours, minutes, secondsTime, alarmTimeSet);
egnormichael 0:9b9696eab6d5 66 //else
egnormichael 0:9b9696eab6d5 67 //uLCD.printf("\nAlarm Time %d : %d AM\nTime %d\nAlarm Time %d",hours, minutes, secondsTime, alarmTimeSet);
egnormichael 0:9b9696eab6d5 68 }
egnormichael 0:9b9696eab6d5 69
egnormichael 0:9b9696eab6d5 70 void pb2_hit_callback (void) {
egnormichael 0:9b9696eab6d5 71 count++;
egnormichael 0:9b9696eab6d5 72 minutes++;
egnormichael 0:9b9696eab6d5 73 if(minutes>59) minutes= 0;
egnormichael 0:9b9696eab6d5 74 //uLCD.cls();
egnormichael 0:9b9696eab6d5 75 //if(AMPM)
egnormichael 0:9b9696eab6d5 76 //uLCD.printf("\nAlarm Time %d : %d PM\nTime %d\nAlarm Time %d",hours, minutes, secondsTime, alarmTimeSet);
egnormichael 0:9b9696eab6d5 77 //else
egnormichael 0:9b9696eab6d5 78 //uLCD.printf("\nAlarm Time %d : %d AM\nTime %d\nAlarm Time %d",hours, minutes, secondsTime, alarmTimeSet);
egnormichael 0:9b9696eab6d5 79 }
egnormichael 0:9b9696eab6d5 80
egnormichael 0:9b9696eab6d5 81 void pb3_hit_callback (void) {
egnormichael 0:9b9696eab6d5 82 alarmSet=true;
egnormichael 0:9b9696eab6d5 83 alarmTimeSet=60*60*hours+60*minutes;
egnormichael 0:9b9696eab6d5 84 if(AMPM) alarmTimeSet += 60*60*12;
egnormichael 0:9b9696eab6d5 85 //uLCD.cls();
egnormichael 0:9b9696eab6d5 86 //uLCD.printf("\nAlarm Time %d : %d\nTime %d\nAlarm Turned On",hours, minutes, secondsTime);
egnormichael 0:9b9696eab6d5 87 }
egnormichael 0:9b9696eab6d5 88
egnormichael 0:9b9696eab6d5 89 void pb4_hit_callback (void) {
egnormichael 0:9b9696eab6d5 90 alarmSet=false;
egnormichael 0:9b9696eab6d5 91 //uLCD.cls();
egnormichael 0:9b9696eab6d5 92 //uLCD.printf("\nAlarm Time %d : %d\nTime %d\nAlarm Turned Off",hours, minutes, secondsTime);
egnormichael 0:9b9696eab6d5 93 }
egnormichael 0:9b9696eab6d5 94
egnormichael 0:9b9696eab6d5 95 int main()
egnormichael 0:9b9696eab6d5 96 {
egnormichael 0:9b9696eab6d5 97 pb1.mode(PullUp);
egnormichael 0:9b9696eab6d5 98 pb2.mode(PullUp);
egnormichael 0:9b9696eab6d5 99 pb3.mode(PullUp);
egnormichael 0:9b9696eab6d5 100 pb4.mode(PullUp);
egnormichael 0:9b9696eab6d5 101
egnormichael 0:9b9696eab6d5 102 wait(.001);
egnormichael 0:9b9696eab6d5 103 // Setup Interrupt callback function for a pb hit
egnormichael 0:9b9696eab6d5 104 pb1.attach_deasserted(&pb1_hit_callback);
egnormichael 0:9b9696eab6d5 105 pb2.attach_deasserted(&pb2_hit_callback);
egnormichael 0:9b9696eab6d5 106 pb3.attach_deasserted(&pb3_hit_callback);
egnormichael 0:9b9696eab6d5 107 pb4.attach_deasserted(&pb4_hit_callback);
egnormichael 0:9b9696eab6d5 108
egnormichael 0:9b9696eab6d5 109 pb1.setSampleFrequency();
egnormichael 0:9b9696eab6d5 110 pb2.setSampleFrequency();
egnormichael 0:9b9696eab6d5 111 pb3.setSampleFrequency();
egnormichael 0:9b9696eab6d5 112 pb4.setSampleFrequency();
egnormichael 0:9b9696eab6d5 113
egnormichael 0:9b9696eab6d5 114 reset=0; //hardware reset for 8266
egnormichael 0:9b9696eab6d5 115 pc.baud(9600); // set what you want here depending on your terminal program speed
egnormichael 0:9b9696eab6d5 116 pc.printf("\f\n\r-------------ESP8266 Hardware Reset-------------\n\r");
egnormichael 0:9b9696eab6d5 117 wait(0.5);
egnormichael 0:9b9696eab6d5 118 reset=1;
egnormichael 0:9b9696eab6d5 119 timeout=2;
egnormichael 0:9b9696eab6d5 120 getreply();
egnormichael 0:9b9696eab6d5 121
egnormichael 0:9b9696eab6d5 122 esp.baud(9600); // change this to the new ESP8266 baudrate if it is changed at any time.
egnormichael 0:9b9696eab6d5 123
egnormichael 0:9b9696eab6d5 124 //ESPsetbaudrate(); //****************** include this routine to set a different ESP8266 baudrate ******************
egnormichael 0:9b9696eab6d5 125
egnormichael 0:9b9696eab6d5 126 ESPconfig(); //****************** include Config to set the ESP8266 configuration ***********************
egnormichael 0:9b9696eab6d5 127 getTime();
egnormichael 0:9b9696eab6d5 128 pc.printf("%s", buf);
egnormichael 0:9b9696eab6d5 129
egnormichael 0:9b9696eab6d5 130 timeDisp = timeFromResponse();
egnormichael 0:9b9696eab6d5 131 set_time(timeDisp);
egnormichael 0:9b9696eab6d5 132
egnormichael 0:9b9696eab6d5 133 pc.printf("%d", timeDisp);
egnormichael 0:9b9696eab6d5 134 timeDisp = timeDisp%86400;
egnormichael 0:9b9696eab6d5 135 while (1) {
egnormichael 0:9b9696eab6d5 136 time_t secondsTime = time(NULL)%86400;
egnormichael 0:9b9696eab6d5 137 uLCD.cls();
egnormichael 0:9b9696eab6d5 138 if(AMPM)
egnormichael 0:9b9696eab6d5 139 uLCD.printf("\nAlarm Time %d : %d PM\nTime %d\nAlarm Time %d",hours, minutes, secondsTime, alarmTimeSet);
egnormichael 0:9b9696eab6d5 140 else
egnormichael 0:9b9696eab6d5 141 uLCD.printf("\nAlarm Time %d : %d AM\nTime %d\nAlarm Time %d",hours, minutes, secondsTime, alarmTimeSet);
egnormichael 0:9b9696eab6d5 142 //timeDisp++;
egnormichael 0:9b9696eab6d5 143 if(alarmSet&&int(alarmTimeSet/60)==int(secondsTime/60)){
egnormichael 0:9b9696eab6d5 144 uLCD.cls();
egnormichael 0:9b9696eab6d5 145 uLCD.printf("ALARM");
egnormichael 0:9b9696eab6d5 146 }
egnormichael 0:9b9696eab6d5 147
egnormichael 0:9b9696eab6d5 148 wait(.5);
egnormichael 0:9b9696eab6d5 149 }
egnormichael 0:9b9696eab6d5 150
egnormichael 0:9b9696eab6d5 151 }
egnormichael 0:9b9696eab6d5 152
egnormichael 0:9b9696eab6d5 153 // Sets new ESP8266 baurate, change the esp.baud(xxxxx) to match your new setting once this has been executed
egnormichael 0:9b9696eab6d5 154 void ESPsetbaudrate()
egnormichael 0:9b9696eab6d5 155 {
egnormichael 0:9b9696eab6d5 156 strcpy(snd, "AT+CIOBAUD=115200\r\n"); // change the numeric value to the required baudrate
egnormichael 0:9b9696eab6d5 157 SendCMD();
egnormichael 0:9b9696eab6d5 158 }
egnormichael 0:9b9696eab6d5 159
egnormichael 0:9b9696eab6d5 160 // +++++++++++++++++++++++++++++++++ This is for ESP8266 config only, run this once to set up the ESP8266 +++++++++++++++
egnormichael 0:9b9696eab6d5 161 void ESPconfig()
egnormichael 0:9b9696eab6d5 162 {
egnormichael 0:9b9696eab6d5 163
egnormichael 0:9b9696eab6d5 164 wait(5);
egnormichael 0:9b9696eab6d5 165 pc.printf("\f---------- Starting ESP Config ----------\r\n\n");
egnormichael 0:9b9696eab6d5 166 strcpy(snd,".\r\n.\r\n");
egnormichael 0:9b9696eab6d5 167 SendCMD();
egnormichael 0:9b9696eab6d5 168 wait(1);
egnormichael 0:9b9696eab6d5 169 pc.printf("---------- Reset & get Firmware ----------\r\n");
egnormichael 0:9b9696eab6d5 170 strcpy(snd,"node.restart()\r\n");
egnormichael 0:9b9696eab6d5 171 SendCMD();
egnormichael 0:9b9696eab6d5 172 timeout=5;
egnormichael 0:9b9696eab6d5 173 getreply();
egnormichael 0:9b9696eab6d5 174 pc.printf(buf);
egnormichael 0:9b9696eab6d5 175
egnormichael 0:9b9696eab6d5 176
egnormichael 0:9b9696eab6d5 177 // set CWMODE to 1=Station,2=AP,3=BOTH, default mode 1 (Station)
egnormichael 0:9b9696eab6d5 178 pc.printf("\n---------- Setting Mode ----------\r\n");
egnormichael 0:9b9696eab6d5 179 strcpy(snd, "wifi.setmode(wifi.STATION)\r\n");
egnormichael 0:9b9696eab6d5 180 SendCMD();
egnormichael 0:9b9696eab6d5 181 timeout=4;
egnormichael 0:9b9696eab6d5 182 getreply();
egnormichael 0:9b9696eab6d5 183 pc.printf(buf);
egnormichael 0:9b9696eab6d5 184
egnormichael 0:9b9696eab6d5 185 wait(1);
egnormichael 0:9b9696eab6d5 186
egnormichael 0:9b9696eab6d5 187 strcpy(snd, "wifi.sta.config(\"");
egnormichael 0:9b9696eab6d5 188 strcat(snd, ssid);
egnormichael 0:9b9696eab6d5 189 strcat(snd, "\",\"");
egnormichael 0:9b9696eab6d5 190 strcat(snd, pwd);
egnormichael 0:9b9696eab6d5 191 strcat(snd, "\")\r\n");
egnormichael 0:9b9696eab6d5 192 SendCMD();
egnormichael 0:9b9696eab6d5 193 timeout=10;
egnormichael 0:9b9696eab6d5 194 getreply();
egnormichael 0:9b9696eab6d5 195 pc.printf(buf);
egnormichael 0:9b9696eab6d5 196
egnormichael 0:9b9696eab6d5 197 wait(1);
egnormichael 0:9b9696eab6d5 198
egnormichael 0:9b9696eab6d5 199 pc.printf("\n---------- Get IP's ----------\r\n");
egnormichael 0:9b9696eab6d5 200 strcpy(snd, "print(wifi.sta.getip())\r\n");
egnormichael 0:9b9696eab6d5 201 SendCMD();
egnormichael 0:9b9696eab6d5 202 timeout=3;
egnormichael 0:9b9696eab6d5 203 getreply();
egnormichael 0:9b9696eab6d5 204 pc.printf(buf);
egnormichael 0:9b9696eab6d5 205
egnormichael 0:9b9696eab6d5 206 wait(1);
egnormichael 0:9b9696eab6d5 207
egnormichael 0:9b9696eab6d5 208 pc.printf("\n---------- Get Connection Status ----------\r\n");
egnormichael 0:9b9696eab6d5 209 strcpy(snd, "print(wifi.sta.status())\r\n");
egnormichael 0:9b9696eab6d5 210 SendCMD();
egnormichael 0:9b9696eab6d5 211 timeout=5;
egnormichael 0:9b9696eab6d5 212 getreply();
egnormichael 0:9b9696eab6d5 213
egnormichael 0:9b9696eab6d5 214 }
egnormichael 0:9b9696eab6d5 215 void getTime() {
egnormichael 0:9b9696eab6d5 216 wait(1);
egnormichael 0:9b9696eab6d5 217 strcpy(snd,"sk=net.createConnection(net.TCP, 0)\r\n");
egnormichael 0:9b9696eab6d5 218 SendCMD();
egnormichael 0:9b9696eab6d5 219 timeout=3;
egnormichael 0:9b9696eab6d5 220 getreply();
egnormichael 0:9b9696eab6d5 221
egnormichael 0:9b9696eab6d5 222 strcpy(snd,"sk:on(\"receive\", function(sck, c) print(c) end )\r\n");
egnormichael 0:9b9696eab6d5 223 SendCMD();
egnormichael 0:9b9696eab6d5 224 timeout=3;
egnormichael 0:9b9696eab6d5 225 getreply();
egnormichael 0:9b9696eab6d5 226
egnormichael 0:9b9696eab6d5 227 strcpy(snd,"sk:connect(80,\"api.timezonedb.com\")\r\n");
egnormichael 0:9b9696eab6d5 228 SendCMD();
egnormichael 0:9b9696eab6d5 229 timeout=3;
egnormichael 0:9b9696eab6d5 230 getreply();
egnormichael 0:9b9696eab6d5 231 wait(1);
egnormichael 0:9b9696eab6d5 232
egnormichael 0:9b9696eab6d5 233 strcpy(snd, "sk:send(\"GET /v2/get-time-zone?key=ONL9KGITMG1G&format=json&by=zone&zone=America/New_York HTTP/1.1\\r\\nHost: api.timezonedb.com\\r\\nConnection: keep-alive\\r\\nAccept: */*\\r\\n\\r\\n\")\r\n");
egnormichael 0:9b9696eab6d5 234 SendCMD();
egnormichael 0:9b9696eab6d5 235 timeout=17;
egnormichael 0:9b9696eab6d5 236 getreply();
egnormichael 0:9b9696eab6d5 237
egnormichael 0:9b9696eab6d5 238 timeReady = true;
egnormichael 0:9b9696eab6d5 239
egnormichael 0:9b9696eab6d5 240 }
egnormichael 0:9b9696eab6d5 241 void SendCMD()
egnormichael 0:9b9696eab6d5 242 {
egnormichael 0:9b9696eab6d5 243 esp.printf("%s", snd);
egnormichael 0:9b9696eab6d5 244 }
egnormichael 0:9b9696eab6d5 245
egnormichael 0:9b9696eab6d5 246 void getreply()
egnormichael 0:9b9696eab6d5 247 {
egnormichael 0:9b9696eab6d5 248 memset(buf, '\0', sizeof(buf));
egnormichael 0:9b9696eab6d5 249 t.start();
egnormichael 0:9b9696eab6d5 250 ended=0;
egnormichael 0:9b9696eab6d5 251 count=0;
egnormichael 0:9b9696eab6d5 252 while(!ended) {
egnormichael 0:9b9696eab6d5 253 if(esp.readable()) {
egnormichael 0:9b9696eab6d5 254 buf[count] = esp.getc();
egnormichael 0:9b9696eab6d5 255 count++;
egnormichael 0:9b9696eab6d5 256 }
egnormichael 0:9b9696eab6d5 257 if(t.read() > timeout) {
egnormichael 0:9b9696eab6d5 258 ended = 1;
egnormichael 0:9b9696eab6d5 259 t.stop();
egnormichael 0:9b9696eab6d5 260 t.reset();
egnormichael 0:9b9696eab6d5 261 }
egnormichael 0:9b9696eab6d5 262 }
egnormichael 0:9b9696eab6d5 263 }
egnormichael 0:9b9696eab6d5 264
egnormichael 0:9b9696eab6d5 265 int timeFromResponse() {
egnormichael 0:9b9696eab6d5 266 char * timestamp;
egnormichael 0:9b9696eab6d5 267 timestamp = strstr(buf, "\"timestamp\":");
egnormichael 0:9b9696eab6d5 268
egnormichael 0:9b9696eab6d5 269 timestamp = timestamp + 12;
egnormichael 0:9b9696eab6d5 270 int count = 0;
egnormichael 0:9b9696eab6d5 271
egnormichael 0:9b9696eab6d5 272 while(timestamp[count] != ',') {
egnormichael 0:9b9696eab6d5 273 count++;
egnormichael 0:9b9696eab6d5 274 }
egnormichael 0:9b9696eab6d5 275
egnormichael 0:9b9696eab6d5 276 strncpy(curTime, timestamp, count);
egnormichael 0:9b9696eab6d5 277 return atoi(curTime);
egnormichael 0:9b9696eab6d5 278 }