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.
Dependencies: 4DGL-uLCD-SE PinDetect mbed ttmath
main.cpp
00001 #include "mbed.h" 00002 #include <ttmath.h> 00003 #include "PinDetect.h" 00004 #include "uLCD_4DGL.h" 00005 00006 Serial pc(USBTX, USBRX); 00007 Serial esp(p28, p27); // tx, rx 00008 DigitalOut reset(p26); 00009 DigitalOut led1(LED1); 00010 DigitalOut led4(LED4); 00011 Timer t; 00012 00013 uLCD_4DGL uLCD(p9,p10,p11); // serial tx, serial rx, reset pin; 00014 00015 PinDetect pb1(p17); 00016 PinDetect pb2(p18); 00017 PinDetect pb3(p19); 00018 PinDetect pb4(p20); 00019 00020 // Global count variable 00021 00022 int volatile hours=12; 00023 int volatile minutes=0; 00024 bool volatile alarmSet=false; 00025 int volatile alarmTimeSet = 0; 00026 int volatile timeDisp=0; //real time 00027 bool volatile AMPM=false; //False AM, true PM 00028 00029 00030 int count,ended,timeout; 00031 char buf[2024]; 00032 char snd[1024]; 00033 char ssid[32] = "MEwifi"; // enter WiFi router ssid inside the quotes 00034 char pwd [32] = "fxyh5453"; // enter WiFi router password inside the quotes 00035 bool timeReady = false; 00036 00037 char curTime[20]; 00038 00039 void SendCMD(),getreply(),ESPconfig(),ESPsetbaudrate(), getTime(); 00040 int timeFromResponse(); 00041 void dev_recv() 00042 { 00043 led1 = !led1; 00044 while(esp.readable()) { 00045 pc.putc(esp.getc()); 00046 } 00047 } 00048 00049 void pc_recv() 00050 { 00051 led4 = !led4; 00052 while(pc.readable()) { 00053 esp.putc(pc.getc()); 00054 } 00055 } 00056 00057 void pb1_hit_callback (void) { 00058 count++; 00059 hours++; 00060 if(hours>12){ hours= 1; AMPM=!AMPM;} 00061 00062 00063 //uLCD.cls(); 00064 //if(AMPM) 00065 //uLCD.printf("\nAlarm Time %d : %d PM\nTime %d\nAlarm Time %d",hours, minutes, secondsTime, alarmTimeSet); 00066 //else 00067 //uLCD.printf("\nAlarm Time %d : %d AM\nTime %d\nAlarm Time %d",hours, minutes, secondsTime, alarmTimeSet); 00068 } 00069 00070 void pb2_hit_callback (void) { 00071 count++; 00072 minutes++; 00073 if(minutes>59) minutes= 0; 00074 //uLCD.cls(); 00075 //if(AMPM) 00076 //uLCD.printf("\nAlarm Time %d : %d PM\nTime %d\nAlarm Time %d",hours, minutes, secondsTime, alarmTimeSet); 00077 //else 00078 //uLCD.printf("\nAlarm Time %d : %d AM\nTime %d\nAlarm Time %d",hours, minutes, secondsTime, alarmTimeSet); 00079 } 00080 00081 void pb3_hit_callback (void) { 00082 alarmSet=true; 00083 alarmTimeSet=60*60*hours+60*minutes; 00084 if(AMPM) alarmTimeSet += 60*60*12; 00085 //uLCD.cls(); 00086 //uLCD.printf("\nAlarm Time %d : %d\nTime %d\nAlarm Turned On",hours, minutes, secondsTime); 00087 } 00088 00089 void pb4_hit_callback (void) { 00090 alarmSet=false; 00091 //uLCD.cls(); 00092 //uLCD.printf("\nAlarm Time %d : %d\nTime %d\nAlarm Turned Off",hours, minutes, secondsTime); 00093 } 00094 00095 int main() 00096 { 00097 pb1.mode(PullUp); 00098 pb2.mode(PullUp); 00099 pb3.mode(PullUp); 00100 pb4.mode(PullUp); 00101 00102 wait(.001); 00103 // Setup Interrupt callback function for a pb hit 00104 pb1.attach_deasserted(&pb1_hit_callback); 00105 pb2.attach_deasserted(&pb2_hit_callback); 00106 pb3.attach_deasserted(&pb3_hit_callback); 00107 pb4.attach_deasserted(&pb4_hit_callback); 00108 00109 pb1.setSampleFrequency(); 00110 pb2.setSampleFrequency(); 00111 pb3.setSampleFrequency(); 00112 pb4.setSampleFrequency(); 00113 00114 reset=0; //hardware reset for 8266 00115 pc.baud(9600); // set what you want here depending on your terminal program speed 00116 pc.printf("\f\n\r-------------ESP8266 Hardware Reset-------------\n\r"); 00117 wait(0.5); 00118 reset=1; 00119 timeout=2; 00120 getreply(); 00121 00122 esp.baud(9600); // change this to the new ESP8266 baudrate if it is changed at any time. 00123 00124 //ESPsetbaudrate(); //****************** include this routine to set a different ESP8266 baudrate ****************** 00125 00126 ESPconfig(); //****************** include Config to set the ESP8266 configuration *********************** 00127 getTime(); 00128 pc.printf("%s", buf); 00129 00130 timeDisp = timeFromResponse(); 00131 set_time(timeDisp); 00132 00133 pc.printf("%d", timeDisp); 00134 timeDisp = timeDisp%86400; 00135 while (1) { 00136 time_t secondsTime = time(NULL)%86400; 00137 uLCD.cls(); 00138 if(AMPM) 00139 uLCD.printf("\nAlarm Time %d : %d PM\nTime %d\nAlarm Time %d",hours, minutes, secondsTime, alarmTimeSet); 00140 else 00141 uLCD.printf("\nAlarm Time %d : %d AM\nTime %d\nAlarm Time %d",hours, minutes, secondsTime, alarmTimeSet); 00142 //timeDisp++; 00143 if(alarmSet&&int(alarmTimeSet/60)==int(secondsTime/60)){ 00144 uLCD.cls(); 00145 uLCD.printf("ALARM"); 00146 } 00147 00148 wait(.5); 00149 } 00150 00151 } 00152 00153 // Sets new ESP8266 baurate, change the esp.baud(xxxxx) to match your new setting once this has been executed 00154 void ESPsetbaudrate() 00155 { 00156 strcpy(snd, "AT+CIOBAUD=115200\r\n"); // change the numeric value to the required baudrate 00157 SendCMD(); 00158 } 00159 00160 // +++++++++++++++++++++++++++++++++ This is for ESP8266 config only, run this once to set up the ESP8266 +++++++++++++++ 00161 void ESPconfig() 00162 { 00163 00164 wait(5); 00165 pc.printf("\f---------- Starting ESP Config ----------\r\n\n"); 00166 strcpy(snd,".\r\n.\r\n"); 00167 SendCMD(); 00168 wait(1); 00169 pc.printf("---------- Reset & get Firmware ----------\r\n"); 00170 strcpy(snd,"node.restart()\r\n"); 00171 SendCMD(); 00172 timeout=5; 00173 getreply(); 00174 pc.printf(buf); 00175 00176 00177 // set CWMODE to 1=Station,2=AP,3=BOTH, default mode 1 (Station) 00178 pc.printf("\n---------- Setting Mode ----------\r\n"); 00179 strcpy(snd, "wifi.setmode(wifi.STATION)\r\n"); 00180 SendCMD(); 00181 timeout=4; 00182 getreply(); 00183 pc.printf(buf); 00184 00185 wait(1); 00186 00187 strcpy(snd, "wifi.sta.config(\""); 00188 strcat(snd, ssid); 00189 strcat(snd, "\",\""); 00190 strcat(snd, pwd); 00191 strcat(snd, "\")\r\n"); 00192 SendCMD(); 00193 timeout=10; 00194 getreply(); 00195 pc.printf(buf); 00196 00197 wait(1); 00198 00199 pc.printf("\n---------- Get IP's ----------\r\n"); 00200 strcpy(snd, "print(wifi.sta.getip())\r\n"); 00201 SendCMD(); 00202 timeout=3; 00203 getreply(); 00204 pc.printf(buf); 00205 00206 wait(1); 00207 00208 pc.printf("\n---------- Get Connection Status ----------\r\n"); 00209 strcpy(snd, "print(wifi.sta.status())\r\n"); 00210 SendCMD(); 00211 timeout=5; 00212 getreply(); 00213 00214 } 00215 void getTime() { 00216 wait(1); 00217 strcpy(snd,"sk=net.createConnection(net.TCP, 0)\r\n"); 00218 SendCMD(); 00219 timeout=3; 00220 getreply(); 00221 00222 strcpy(snd,"sk:on(\"receive\", function(sck, c) print(c) end )\r\n"); 00223 SendCMD(); 00224 timeout=3; 00225 getreply(); 00226 00227 strcpy(snd,"sk:connect(80,\"api.timezonedb.com\")\r\n"); 00228 SendCMD(); 00229 timeout=3; 00230 getreply(); 00231 wait(1); 00232 00233 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"); 00234 SendCMD(); 00235 timeout=17; 00236 getreply(); 00237 00238 timeReady = true; 00239 00240 } 00241 void SendCMD() 00242 { 00243 esp.printf("%s", snd); 00244 } 00245 00246 void getreply() 00247 { 00248 memset(buf, '\0', sizeof(buf)); 00249 t.start(); 00250 ended=0; 00251 count=0; 00252 while(!ended) { 00253 if(esp.readable()) { 00254 buf[count] = esp.getc(); 00255 count++; 00256 } 00257 if(t.read() > timeout) { 00258 ended = 1; 00259 t.stop(); 00260 t.reset(); 00261 } 00262 } 00263 } 00264 00265 int timeFromResponse() { 00266 char * timestamp; 00267 timestamp = strstr(buf, "\"timestamp\":"); 00268 00269 timestamp = timestamp + 12; 00270 int count = 0; 00271 00272 while(timestamp[count] != ',') { 00273 count++; 00274 } 00275 00276 strncpy(curTime, timestamp, count); 00277 return atoi(curTime); 00278 }
Generated on Fri Jul 22 2022 03:19:41 by
1.7.2