Environmental Simulator

Dependencies:   4DGL-uLCD-SE ShiftBrite TMP36 GZ mbed

Committer:
Colmustard
Date:
Wed Nov 02 19:17:09 2016 +0000
Revision:
0:52ac517d1aba
Initial publication

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Colmustard 0:52ac517d1aba 1 // ESP8266 Static page WEB server to control Mbed
Colmustard 0:52ac517d1aba 2
Colmustard 0:52ac517d1aba 3 #include "mbed.h"
Colmustard 0:52ac517d1aba 4 #include "uLCD_4DGL.h"
Colmustard 0:52ac517d1aba 5 #include "ShiftBrite.h"
Colmustard 0:52ac517d1aba 6 #include "TMP36GZ.h"
Colmustard 0:52ac517d1aba 7
Colmustard 0:52ac517d1aba 8 uLCD_4DGL uLCD(p28, p27, p11); //On board LCD display
Colmustard 0:52ac517d1aba 9
Colmustard 0:52ac517d1aba 10 Serial pc(USBTX, USBRX);
Colmustard 0:52ac517d1aba 11 Serial esp(p13, p14); // tx, rx
Colmustard 0:52ac517d1aba 12
Colmustard 0:52ac517d1aba 13 SPI spi(p5, p6, p7); //SPI setup for ShiftBrite
Colmustard 0:52ac517d1aba 14 ShiftBrite myBrite(p15,p16,spi); //latch, enable, spi
Colmustard 0:52ac517d1aba 15
Colmustard 0:52ac517d1aba 16
Colmustard 0:52ac517d1aba 17 TMP36GZ temp_sensor(p20); // temp
Colmustard 0:52ac517d1aba 18
Colmustard 0:52ac517d1aba 19 // Standard Mbed LED definitions
Colmustard 0:52ac517d1aba 20 DigitalOut led1(LED1);
Colmustard 0:52ac517d1aba 21 DigitalOut led2(LED2);
Colmustard 0:52ac517d1aba 22 DigitalOut led3(LED3);
Colmustard 0:52ac517d1aba 23 DigitalOut led4(LED4);
Colmustard 0:52ac517d1aba 24
Colmustard 0:52ac517d1aba 25 // some test values to show on web page
Colmustard 0:52ac517d1aba 26 AnalogIn Ain1(p18);
Colmustard 0:52ac517d1aba 27 AnalogIn Ain2(p20);
Colmustard 0:52ac517d1aba 28
Colmustard 0:52ac517d1aba 29 inline float random_number()
Colmustard 0:52ac517d1aba 30 {
Colmustard 0:52ac517d1aba 31 return (rand()/(float(RAND_MAX)));
Colmustard 0:52ac517d1aba 32 }
Colmustard 0:52ac517d1aba 33
Colmustard 0:52ac517d1aba 34
Colmustard 0:52ac517d1aba 35 char ssid[32] = "TayKyr4180"; // enter WiFi router ssid inside the quotes
Colmustard 0:52ac517d1aba 36 char pwd [32] = "123456789"; // enter WiFi router password inside the quotes
Colmustard 0:52ac517d1aba 37
Colmustard 0:52ac517d1aba 38 float temperature, AdcIn, Ht;
Colmustard 0:52ac517d1aba 39 float R1=100000, R2=10000; // resistor values to give a 10:1 reduction of measured AnalogIn voltage
Colmustard 0:52ac517d1aba 40 char Vcc[10];
Colmustard 0:52ac517d1aba 41 char Temp[10];
Colmustard 0:52ac517d1aba 42
Colmustard 0:52ac517d1aba 43 // things for sending/receiving data over serial
Colmustard 0:52ac517d1aba 44 volatile int tx_in=0;
Colmustard 0:52ac517d1aba 45 volatile int tx_out=0;
Colmustard 0:52ac517d1aba 46 volatile int rx_in=0;
Colmustard 0:52ac517d1aba 47 volatile int rx_out=0;
Colmustard 0:52ac517d1aba 48 const int buffer_size = 4095;
Colmustard 0:52ac517d1aba 49 char tx_buffer[buffer_size+1];
Colmustard 0:52ac517d1aba 50 char rx_buffer[buffer_size+1];
Colmustard 0:52ac517d1aba 51 void Tx_interrupt();
Colmustard 0:52ac517d1aba 52 void Rx_interrupt();
Colmustard 0:52ac517d1aba 53 void send_line();
Colmustard 0:52ac517d1aba 54 void read_line();
Colmustard 0:52ac517d1aba 55
Colmustard 0:52ac517d1aba 56 int DataRX;
Colmustard 0:52ac517d1aba 57 int update;
Colmustard 0:52ac517d1aba 58 int count;
Colmustard 0:52ac517d1aba 59 char cmdbuff[1024];
Colmustard 0:52ac517d1aba 60 char replybuff[4096];
Colmustard 0:52ac517d1aba 61 char webdata[4096]; // This may need to be bigger depending on WEB browser used
Colmustard 0:52ac517d1aba 62 char webbuff[4096]; // Currently using 1986 characters, Increase this if more web page data added
Colmustard 0:52ac517d1aba 63 char timebuf[30];
Colmustard 0:52ac517d1aba 64 void SendCMD(),getreply(),ReadWebData(),startserver();
Colmustard 0:52ac517d1aba 65 void gettime(),setRTC(),gettemp(),getbattery();
Colmustard 0:52ac517d1aba 66 char rx_line[1024];
Colmustard 0:52ac517d1aba 67 int port =80; // set server port
Colmustard 0:52ac517d1aba 68 int SERVtimeout =5; // set server timeout in seconds in case link breaks.
Colmustard 0:52ac517d1aba 69 struct tm t;
Colmustard 0:52ac517d1aba 70 // manual set RTC values
Colmustard 0:52ac517d1aba 71 int minute =00; // 0-59
Colmustard 0:52ac517d1aba 72 int hour =12; // 2-23
Colmustard 0:52ac517d1aba 73 int dayofmonth =26; // 1-31
Colmustard 0:52ac517d1aba 74 int month =8; // 1-12
Colmustard 0:52ac517d1aba 75 int year =15; // last 2 digits
Colmustard 0:52ac517d1aba 76 int x =0;
Colmustard 0:52ac517d1aba 77 int countB =0;
Colmustard 0:52ac517d1aba 78
Colmustard 0:52ac517d1aba 79 int main()
Colmustard 0:52ac517d1aba 80 {
Colmustard 0:52ac517d1aba 81 pc.baud(115200);
Colmustard 0:52ac517d1aba 82 esp.baud(9600);
Colmustard 0:52ac517d1aba 83 led1=1,led2=0,led3=0, led4=0;
Colmustard 0:52ac517d1aba 84 // Setup a serial interrupt function to receive data
Colmustard 0:52ac517d1aba 85 esp.attach(&Rx_interrupt, Serial::RxIrq);
Colmustard 0:52ac517d1aba 86 // Setup a serial interrupt function to transmit data
Colmustard 0:52ac517d1aba 87 esp.attach(&Tx_interrupt, Serial::TxIrq);
Colmustard 0:52ac517d1aba 88 if (time(NULL) < 1420070400) {
Colmustard 0:52ac517d1aba 89 setRTC();
Colmustard 0:52ac517d1aba 90 }
Colmustard 0:52ac517d1aba 91 startserver();
Colmustard 0:52ac517d1aba 92 DataRX=0;
Colmustard 0:52ac517d1aba 93 count=0;
Colmustard 0:52ac517d1aba 94 while(1) {
Colmustard 0:52ac517d1aba 95 if(DataRX==1) {
Colmustard 0:52ac517d1aba 96 ReadWebData();
Colmustard 0:52ac517d1aba 97 esp.attach(&Rx_interrupt, Serial::RxIrq);
Colmustard 0:52ac517d1aba 98 }
Colmustard 0:52ac517d1aba 99 if(update==1) // update time, hit count, and analog levels in the HUZZAH chip
Colmustard 0:52ac517d1aba 100 {
Colmustard 0:52ac517d1aba 101 // get new values
Colmustard 0:52ac517d1aba 102 gettime();
Colmustard 0:52ac517d1aba 103 gettemp();
Colmustard 0:52ac517d1aba 104 getbattery();
Colmustard 0:52ac517d1aba 105 count++;
Colmustard 0:52ac517d1aba 106 // send new values
Colmustard 0:52ac517d1aba 107 sprintf(cmdbuff, "count,time,analog1,analog2=%d,\"%s\",\"%s\",\"%s\"\r\n",count,timebuf,Temp,Vcc);
Colmustard 0:52ac517d1aba 108 SendCMD();
Colmustard 0:52ac517d1aba 109 getreply();
Colmustard 0:52ac517d1aba 110 update=0;
Colmustard 0:52ac517d1aba 111 }
Colmustard 0:52ac517d1aba 112 }
Colmustard 0:52ac517d1aba 113 }
Colmustard 0:52ac517d1aba 114
Colmustard 0:52ac517d1aba 115 // Reads and processes GET and POST web data
Colmustard 0:52ac517d1aba 116 void ReadWebData()
Colmustard 0:52ac517d1aba 117 {
Colmustard 0:52ac517d1aba 118 wait_ms(200);
Colmustard 0:52ac517d1aba 119 esp.attach(NULL,Serial::RxIrq);
Colmustard 0:52ac517d1aba 120 DataRX=0;
Colmustard 0:52ac517d1aba 121 memset(webdata, '\0', sizeof(webdata));
Colmustard 0:52ac517d1aba 122 strcpy(webdata, rx_buffer);
Colmustard 0:52ac517d1aba 123 memset(rx_buffer, '\0', sizeof(rx_buffer));
Colmustard 0:52ac517d1aba 124 rx_in = 0;
Colmustard 0:52ac517d1aba 125 rx_out = 0;
Colmustard 0:52ac517d1aba 126 // check web data for form information
Colmustard 0:52ac517d1aba 127
Colmustard 0:52ac517d1aba 128 if(strstr(webdata, "check=Storm") != NULL ) {
Colmustard 0:52ac517d1aba 129 uLCD.media_init();
Colmustard 0:52ac517d1aba 130 uLCD.set_sector_address(0x0000, 0x0104);
Colmustard 0:52ac517d1aba 131 uLCD.display_image(0,0);
Colmustard 0:52ac517d1aba 132 // Lightning loop
Colmustard 0:52ac517d1aba 133 while(strstr(webdata, "check=Storm") != NULL && countB < 100 ){
Colmustard 0:52ac517d1aba 134 //get a new random number
Colmustard 0:52ac517d1aba 135 x = rand() % 256 + 20;
Colmustard 0:52ac517d1aba 136 myBrite.Write(125,249,255);
Colmustard 0:52ac517d1aba 137 myBrite.Brightness(x,x,x);
Colmustard 0:52ac517d1aba 138 //fast update rate for flashes
Colmustard 0:52ac517d1aba 139 wait(0.02);
Colmustard 0:52ac517d1aba 140 myBrite.Write(0,0,0);
Colmustard 0:52ac517d1aba 141 //add a random pause between flashes
Colmustard 0:52ac517d1aba 142 if (random_number()>0.9025) {
Colmustard 0:52ac517d1aba 143 myBrite.Write(0,0,0);
Colmustard 0:52ac517d1aba 144 wait(x/100);
Colmustard 0:52ac517d1aba 145 }
Colmustard 0:52ac517d1aba 146 countB = countB+1;
Colmustard 0:52ac517d1aba 147 }
Colmustard 0:52ac517d1aba 148 countB = 0;
Colmustard 0:52ac517d1aba 149 }
Colmustard 0:52ac517d1aba 150 if(strstr(webdata, "check=SunnyDay") != NULL ) {
Colmustard 0:52ac517d1aba 151 myBrite.Write(255,255,255);
Colmustard 0:52ac517d1aba 152 myBrite.Brightness(255,255,255);
Colmustard 0:52ac517d1aba 153 uLCD.media_init();
Colmustard 0:52ac517d1aba 154 uLCD.set_sector_address(0x0000, 0x00C3);
Colmustard 0:52ac517d1aba 155 uLCD.display_image(0,0);
Colmustard 0:52ac517d1aba 156 }
Colmustard 0:52ac517d1aba 157 if(strstr(webdata, "check=Dawn") != NULL ) {
Colmustard 0:52ac517d1aba 158 myBrite.Write(255,214,170);
Colmustard 0:52ac517d1aba 159 myBrite.Brightness(255,255,255);
Colmustard 0:52ac517d1aba 160 uLCD.media_init();
Colmustard 0:52ac517d1aba 161 uLCD.set_sector_address(0x0000, 0x0041);
Colmustard 0:52ac517d1aba 162 uLCD.display_image(0,0);
Colmustard 0:52ac517d1aba 163 }
Colmustard 0:52ac517d1aba 164 if(strstr(webdata, "check=Dusk") != NULL ) {
Colmustard 0:52ac517d1aba 165 myBrite.Write(255,214,170);
Colmustard 0:52ac517d1aba 166 myBrite.Brightness(255,255,255);
Colmustard 0:52ac517d1aba 167 uLCD.media_init();
Colmustard 0:52ac517d1aba 168 uLCD.set_sector_address(0x0000, 0x0082);
Colmustard 0:52ac517d1aba 169 uLCD.display_image(0,0);
Colmustard 0:52ac517d1aba 170 }
Colmustard 0:52ac517d1aba 171 if(strstr(webdata, "check=CloudyDay") != NULL ) {
Colmustard 0:52ac517d1aba 172 myBrite.Write(201,226,255);
Colmustard 0:52ac517d1aba 173 myBrite.Brightness(255,255,255);
Colmustard 0:52ac517d1aba 174 uLCD.media_init();
Colmustard 0:52ac517d1aba 175 uLCD.set_sector_address(0x0000, 0x0000);
Colmustard 0:52ac517d1aba 176 uLCD.display_image(0,0);
Colmustard 0:52ac517d1aba 177 }
Colmustard 0:52ac517d1aba 178
Colmustard 0:52ac517d1aba 179 if(strstr(webdata, "POST") != NULL ) { // set update flag if POST request
Colmustard 0:52ac517d1aba 180 update=1;
Colmustard 0:52ac517d1aba 181 }
Colmustard 0:52ac517d1aba 182 if(strstr(webdata, "GET") != NULL && strstr(webdata, "favicon") == NULL ) { // set update flag for GET request but do not want to update for favicon requests
Colmustard 0:52ac517d1aba 183 update=1;
Colmustard 0:52ac517d1aba 184 }
Colmustard 0:52ac517d1aba 185 }
Colmustard 0:52ac517d1aba 186 // Starts webserver
Colmustard 0:52ac517d1aba 187 void startserver()
Colmustard 0:52ac517d1aba 188 {
Colmustard 0:52ac517d1aba 189 gettime();
Colmustard 0:52ac517d1aba 190 gettemp();
Colmustard 0:52ac517d1aba 191 getbattery();
Colmustard 0:52ac517d1aba 192 pc.printf("++++++++++ Resetting ESP ++++++++++\r\n");
Colmustard 0:52ac517d1aba 193 strcpy(cmdbuff,"node.restart()\r\n");
Colmustard 0:52ac517d1aba 194 SendCMD();
Colmustard 0:52ac517d1aba 195 wait(2);
Colmustard 0:52ac517d1aba 196 getreply();
Colmustard 0:52ac517d1aba 197
Colmustard 0:52ac517d1aba 198 pc.printf("\n++++++++++ Starting Server ++++++++++\r\n> ");
Colmustard 0:52ac517d1aba 199
Colmustard 0:52ac517d1aba 200 // initial values
Colmustard 0:52ac517d1aba 201 sprintf(cmdbuff, "count,time,analog1,analog2=0,\"%s\",\"%s\",\"%s\"\r\n",timebuf,Temp,Vcc);
Colmustard 0:52ac517d1aba 202 SendCMD();
Colmustard 0:52ac517d1aba 203 getreply();
Colmustard 0:52ac517d1aba 204 wait(0.5);
Colmustard 0:52ac517d1aba 205
Colmustard 0:52ac517d1aba 206 //create server
Colmustard 0:52ac517d1aba 207 sprintf(cmdbuff, "srv=net.createServer(net.TCP,%d)\r\n",SERVtimeout);
Colmustard 0:52ac517d1aba 208 SendCMD();
Colmustard 0:52ac517d1aba 209 getreply();
Colmustard 0:52ac517d1aba 210 wait(0.5);
Colmustard 0:52ac517d1aba 211 strcpy(cmdbuff,"srv:listen(80,function(conn)\r\n");
Colmustard 0:52ac517d1aba 212 SendCMD();
Colmustard 0:52ac517d1aba 213 getreply();
Colmustard 0:52ac517d1aba 214 wait(0.3);
Colmustard 0:52ac517d1aba 215 strcpy(cmdbuff,"conn:on(\"receive\",function(conn,payload) \r\n");
Colmustard 0:52ac517d1aba 216 SendCMD();
Colmustard 0:52ac517d1aba 217 getreply();
Colmustard 0:52ac517d1aba 218 wait(0.3);
Colmustard 0:52ac517d1aba 219
Colmustard 0:52ac517d1aba 220 //print data to mbed
Colmustard 0:52ac517d1aba 221 strcpy(cmdbuff,"print(payload)\r\n");
Colmustard 0:52ac517d1aba 222 SendCMD();
Colmustard 0:52ac517d1aba 223 getreply();
Colmustard 0:52ac517d1aba 224 wait(0.2);
Colmustard 0:52ac517d1aba 225
Colmustard 0:52ac517d1aba 226 //web page data
Colmustard 0:52ac517d1aba 227 strcpy(cmdbuff,"conn:send('<!DOCTYPE html><html><body><h1>Lab 4 Mini Project - Environmental Simulator</h1>')\r\n");
Colmustard 0:52ac517d1aba 228 SendCMD();
Colmustard 0:52ac517d1aba 229 getreply();
Colmustard 0:52ac517d1aba 230 wait(0.4);
Colmustard 0:52ac517d1aba 231 strcpy(cmdbuff,"conn:send('Hit count: '..count..'')\r\n");
Colmustard 0:52ac517d1aba 232 SendCMD();
Colmustard 0:52ac517d1aba 233 getreply();
Colmustard 0:52ac517d1aba 234 wait(0.2);
Colmustard 0:52ac517d1aba 235 strcpy(cmdbuff,"conn:send('<br>Last hit (based on mbed RTC time): '..time..'<br><hr>')\r\n");
Colmustard 0:52ac517d1aba 236 SendCMD();
Colmustard 0:52ac517d1aba 237 getreply();
Colmustard 0:52ac517d1aba 238 wait(0.4);
Colmustard 0:52ac517d1aba 239 strcpy(cmdbuff,"conn:send('Temperature: '..analog1..' <sup>o</sup>C <br><hr>')\r\n");
Colmustard 0:52ac517d1aba 240 SendCMD();
Colmustard 0:52ac517d1aba 241 getreply();
Colmustard 0:52ac517d1aba 242 wait(0.3);
Colmustard 0:52ac517d1aba 243 strcpy(cmdbuff,"conn:send('<form method=\"POST\"')\r\n");
Colmustard 0:52ac517d1aba 244 SendCMD();
Colmustard 0:52ac517d1aba 245 getreply();
Colmustard 0:52ac517d1aba 246 wait(0.3);
Colmustard 0:52ac517d1aba 247 /*
Colmustard 0:52ac517d1aba 248 strcpy(cmdbuff,"conn:send('<form method=\"POST\"><strong> Temperature:&nbsp&nbsp<input type=\"text\" size=6 value=\"");
Colmustard 0:52ac517d1aba 249 SendCMD();
Colmustard 0:52ac517d1aba 250 getreply();
Colmustard 0:52ac517d1aba 251 wait(0.3);
Colmustard 0:52ac517d1aba 252 strcpy(cmdbuff,Temp);
Colmustard 0:52ac517d1aba 253 SendCMD();
Colmustard 0:52ac517d1aba 254 getreply();
Colmustard 0:52ac517d1aba 255 wait(0.3);
Colmustard 0:52ac517d1aba 256 */
Colmustard 0:52ac517d1aba 257
Colmustard 0:52ac517d1aba 258 strcpy(cmdbuff, "conn:send('<p><input type=\"text\" name=\"check\" value=\"\"> Lighting Conditions (choose from: Storm, Dawn, SunnyDay, CloudyDay, or Dusk)')\r\n");
Colmustard 0:52ac517d1aba 259 SendCMD();
Colmustard 0:52ac517d1aba 260 getreply();
Colmustard 0:52ac517d1aba 261 wait(0.3);
Colmustard 0:52ac517d1aba 262
Colmustard 0:52ac517d1aba 263 strcpy(cmdbuff,"conn:send('<p><input type=\"submit\" value=\"send-refresh\"></form>')\r\n");
Colmustard 0:52ac517d1aba 264 SendCMD();
Colmustard 0:52ac517d1aba 265 getreply();
Colmustard 0:52ac517d1aba 266 wait(0.3);
Colmustard 0:52ac517d1aba 267 strcpy(cmdbuff, "conn:send('<p><h2>How to use:</h2><ul><li>Select a checkbox to flip on/off</li><li>Click Send-Refresh to send data and refresh values</li></ul></body></html>')\r\n");
Colmustard 0:52ac517d1aba 268 SendCMD();
Colmustard 0:52ac517d1aba 269 getreply();
Colmustard 0:52ac517d1aba 270 wait(0.5);
Colmustard 0:52ac517d1aba 271 // end web page data
Colmustard 0:52ac517d1aba 272 strcpy(cmdbuff, "conn:on(\"sent\",function(conn) conn:close() end)\r\n"); // close current connection
Colmustard 0:52ac517d1aba 273 SendCMD();
Colmustard 0:52ac517d1aba 274 getreply();
Colmustard 0:52ac517d1aba 275 wait(0.3);
Colmustard 0:52ac517d1aba 276 strcpy(cmdbuff, "end)\r\n");
Colmustard 0:52ac517d1aba 277 SendCMD();
Colmustard 0:52ac517d1aba 278 getreply();
Colmustard 0:52ac517d1aba 279 wait(0.2);
Colmustard 0:52ac517d1aba 280 strcpy(cmdbuff, "end)\r\n");
Colmustard 0:52ac517d1aba 281 SendCMD();
Colmustard 0:52ac517d1aba 282 getreply();
Colmustard 0:52ac517d1aba 283 wait(0.2);
Colmustard 0:52ac517d1aba 284
Colmustard 0:52ac517d1aba 285 strcpy(cmdbuff, "tmr.alarm(0, 1000, 1, function()\r\n");
Colmustard 0:52ac517d1aba 286 SendCMD();
Colmustard 0:52ac517d1aba 287 getreply();
Colmustard 0:52ac517d1aba 288 wait(0.2);
Colmustard 0:52ac517d1aba 289 strcpy(cmdbuff, "if wifi.sta.getip() == nil then\r\n");
Colmustard 0:52ac517d1aba 290 SendCMD();
Colmustard 0:52ac517d1aba 291 getreply();
Colmustard 0:52ac517d1aba 292 wait(0.2);
Colmustard 0:52ac517d1aba 293 strcpy(cmdbuff, "print(\"Connecting to AP...\\n\")\r\n");
Colmustard 0:52ac517d1aba 294 SendCMD();
Colmustard 0:52ac517d1aba 295 getreply();
Colmustard 0:52ac517d1aba 296 wait(0.2);
Colmustard 0:52ac517d1aba 297 strcpy(cmdbuff, "else\r\n");
Colmustard 0:52ac517d1aba 298 SendCMD();
Colmustard 0:52ac517d1aba 299 getreply();
Colmustard 0:52ac517d1aba 300 wait(0.2);
Colmustard 0:52ac517d1aba 301 strcpy(cmdbuff, "ip, nm, gw=wifi.sta.getip()\r\n");
Colmustard 0:52ac517d1aba 302 SendCMD();
Colmustard 0:52ac517d1aba 303 getreply();
Colmustard 0:52ac517d1aba 304 wait(0.2);
Colmustard 0:52ac517d1aba 305 strcpy(cmdbuff,"print(\"IP Address: \",ip)\r\n");
Colmustard 0:52ac517d1aba 306 SendCMD();
Colmustard 0:52ac517d1aba 307 getreply();
Colmustard 0:52ac517d1aba 308 wait(0.2);
Colmustard 0:52ac517d1aba 309 strcpy(cmdbuff,"tmr.stop(0)\r\n");
Colmustard 0:52ac517d1aba 310 SendCMD();
Colmustard 0:52ac517d1aba 311 getreply();
Colmustard 0:52ac517d1aba 312 wait(0.2);
Colmustard 0:52ac517d1aba 313 strcpy(cmdbuff,"end\r\n");
Colmustard 0:52ac517d1aba 314 SendCMD();
Colmustard 0:52ac517d1aba 315 getreply();
Colmustard 0:52ac517d1aba 316 wait(0.2);
Colmustard 0:52ac517d1aba 317 strcpy(cmdbuff,"end)\r\n");
Colmustard 0:52ac517d1aba 318 SendCMD();
Colmustard 0:52ac517d1aba 319 getreply();
Colmustard 0:52ac517d1aba 320 wait(0.2);
Colmustard 0:52ac517d1aba 321
Colmustard 0:52ac517d1aba 322 pc.printf("\n\n++++++++++ Ready ++++++++++\r\n\n");
Colmustard 0:52ac517d1aba 323 }
Colmustard 0:52ac517d1aba 324
Colmustard 0:52ac517d1aba 325
Colmustard 0:52ac517d1aba 326 // ESP Command data send
Colmustard 0:52ac517d1aba 327 void SendCMD()
Colmustard 0:52ac517d1aba 328 {
Colmustard 0:52ac517d1aba 329 int i;
Colmustard 0:52ac517d1aba 330 char temp_char;
Colmustard 0:52ac517d1aba 331 bool empty;
Colmustard 0:52ac517d1aba 332 i = 0;
Colmustard 0:52ac517d1aba 333 // Start Critical Section - don't interrupt while changing global buffer variables
Colmustard 0:52ac517d1aba 334 NVIC_DisableIRQ(UART1_IRQn);
Colmustard 0:52ac517d1aba 335 empty = (tx_in == tx_out);
Colmustard 0:52ac517d1aba 336 while ((i==0) || (cmdbuff[i-1] != '\n')) {
Colmustard 0:52ac517d1aba 337 // Wait if buffer full
Colmustard 0:52ac517d1aba 338 if (((tx_in + 1) % buffer_size) == tx_out) {
Colmustard 0:52ac517d1aba 339 // End Critical Section - need to let interrupt routine empty buffer by sending
Colmustard 0:52ac517d1aba 340 NVIC_EnableIRQ(UART1_IRQn);
Colmustard 0:52ac517d1aba 341 while (((tx_in + 1) % buffer_size) == tx_out) {
Colmustard 0:52ac517d1aba 342 }
Colmustard 0:52ac517d1aba 343 // Start Critical Section - don't interrupt while changing global buffer variables
Colmustard 0:52ac517d1aba 344 NVIC_DisableIRQ(UART1_IRQn);
Colmustard 0:52ac517d1aba 345 }
Colmustard 0:52ac517d1aba 346 tx_buffer[tx_in] = cmdbuff[i];
Colmustard 0:52ac517d1aba 347 i++;
Colmustard 0:52ac517d1aba 348 tx_in = (tx_in + 1) % buffer_size;
Colmustard 0:52ac517d1aba 349 }
Colmustard 0:52ac517d1aba 350 if (esp.writeable() && (empty)) {
Colmustard 0:52ac517d1aba 351 temp_char = tx_buffer[tx_out];
Colmustard 0:52ac517d1aba 352 tx_out = (tx_out + 1) % buffer_size;
Colmustard 0:52ac517d1aba 353 // Send first character to start tx interrupts, if stopped
Colmustard 0:52ac517d1aba 354 esp.putc(temp_char);
Colmustard 0:52ac517d1aba 355 }
Colmustard 0:52ac517d1aba 356 // End Critical Section
Colmustard 0:52ac517d1aba 357 NVIC_EnableIRQ(UART1_IRQn);
Colmustard 0:52ac517d1aba 358 return;
Colmustard 0:52ac517d1aba 359 }
Colmustard 0:52ac517d1aba 360
Colmustard 0:52ac517d1aba 361 // Get Command and ESP status replies
Colmustard 0:52ac517d1aba 362 void getreply()
Colmustard 0:52ac517d1aba 363 {
Colmustard 0:52ac517d1aba 364 read_line();
Colmustard 0:52ac517d1aba 365 sscanf(rx_line,replybuff);
Colmustard 0:52ac517d1aba 366 }
Colmustard 0:52ac517d1aba 367
Colmustard 0:52ac517d1aba 368 // Read a line from the large rx buffer from rx interrupt routine
Colmustard 0:52ac517d1aba 369 void read_line() {
Colmustard 0:52ac517d1aba 370 int i;
Colmustard 0:52ac517d1aba 371 i = 0;
Colmustard 0:52ac517d1aba 372 // Start Critical Section - don't interrupt while changing global buffer variables
Colmustard 0:52ac517d1aba 373 NVIC_DisableIRQ(UART1_IRQn);
Colmustard 0:52ac517d1aba 374 // Loop reading rx buffer characters until end of line character
Colmustard 0:52ac517d1aba 375 while ((i==0) || (rx_line[i-1] != '\r')) {
Colmustard 0:52ac517d1aba 376 // Wait if buffer empty
Colmustard 0:52ac517d1aba 377 if (rx_in == rx_out) {
Colmustard 0:52ac517d1aba 378 // End Critical Section - need to allow rx interrupt to get new characters for buffer
Colmustard 0:52ac517d1aba 379 NVIC_EnableIRQ(UART1_IRQn);
Colmustard 0:52ac517d1aba 380 while (rx_in == rx_out) {
Colmustard 0:52ac517d1aba 381 }
Colmustard 0:52ac517d1aba 382 // Start Critical Section - don't interrupt while changing global buffer variables
Colmustard 0:52ac517d1aba 383 NVIC_DisableIRQ(UART1_IRQn);
Colmustard 0:52ac517d1aba 384 }
Colmustard 0:52ac517d1aba 385 rx_line[i] = rx_buffer[rx_out];
Colmustard 0:52ac517d1aba 386 i++;
Colmustard 0:52ac517d1aba 387 rx_out = (rx_out + 1) % buffer_size;
Colmustard 0:52ac517d1aba 388 }
Colmustard 0:52ac517d1aba 389 // End Critical Section
Colmustard 0:52ac517d1aba 390 NVIC_EnableIRQ(UART1_IRQn);
Colmustard 0:52ac517d1aba 391 rx_line[i-1] = 0;
Colmustard 0:52ac517d1aba 392 return;
Colmustard 0:52ac517d1aba 393 }
Colmustard 0:52ac517d1aba 394
Colmustard 0:52ac517d1aba 395
Colmustard 0:52ac517d1aba 396 // Interupt Routine to read in data from serial port
Colmustard 0:52ac517d1aba 397 void Rx_interrupt() {
Colmustard 0:52ac517d1aba 398 DataRX=1;
Colmustard 0:52ac517d1aba 399 //led3=1;
Colmustard 0:52ac517d1aba 400 // Loop just in case more than one character is in UART's receive FIFO buffer
Colmustard 0:52ac517d1aba 401 // Stop if buffer full
Colmustard 0:52ac517d1aba 402 while ((esp.readable()) && (((rx_in + 1) % buffer_size) != rx_out)) {
Colmustard 0:52ac517d1aba 403 rx_buffer[rx_in] = esp.getc();
Colmustard 0:52ac517d1aba 404 // Uncomment to Echo to USB serial to watch data flow
Colmustard 0:52ac517d1aba 405 pc.putc(rx_buffer[rx_in]);
Colmustard 0:52ac517d1aba 406 rx_in = (rx_in + 1) % buffer_size;
Colmustard 0:52ac517d1aba 407 }
Colmustard 0:52ac517d1aba 408 //led3=0;
Colmustard 0:52ac517d1aba 409 return;
Colmustard 0:52ac517d1aba 410 }
Colmustard 0:52ac517d1aba 411
Colmustard 0:52ac517d1aba 412
Colmustard 0:52ac517d1aba 413 // Interupt Routine to write out data to serial port
Colmustard 0:52ac517d1aba 414 void Tx_interrupt() {
Colmustard 0:52ac517d1aba 415 //led2=1;
Colmustard 0:52ac517d1aba 416 // Loop to fill more than one character in UART's transmit FIFO buffer
Colmustard 0:52ac517d1aba 417 // Stop if buffer empty
Colmustard 0:52ac517d1aba 418 while ((esp.writeable()) && (tx_in != tx_out)) {
Colmustard 0:52ac517d1aba 419 esp.putc(tx_buffer[tx_out]);
Colmustard 0:52ac517d1aba 420 tx_out = (tx_out + 1) % buffer_size;
Colmustard 0:52ac517d1aba 421 }
Colmustard 0:52ac517d1aba 422 //led2=0;
Colmustard 0:52ac517d1aba 423 return;
Colmustard 0:52ac517d1aba 424 }
Colmustard 0:52ac517d1aba 425
Colmustard 0:52ac517d1aba 426 void gettime()
Colmustard 0:52ac517d1aba 427 {
Colmustard 0:52ac517d1aba 428 time_t seconds = time(NULL);
Colmustard 0:52ac517d1aba 429 strftime(timebuf,50,"%H:%M:%S %a %d %b %y", localtime(&seconds));
Colmustard 0:52ac517d1aba 430 }
Colmustard 0:52ac517d1aba 431
Colmustard 0:52ac517d1aba 432 void setRTC()
Colmustard 0:52ac517d1aba 433 {
Colmustard 0:52ac517d1aba 434 t.tm_sec = (0); // 0-59
Colmustard 0:52ac517d1aba 435 t.tm_min = (minute); // 0-59
Colmustard 0:52ac517d1aba 436 t.tm_hour = (hour); // 0-23
Colmustard 0:52ac517d1aba 437 t.tm_mday = (dayofmonth); // 1-31
Colmustard 0:52ac517d1aba 438 t.tm_mon = (month-1); // 0-11 "0" = Jan, -1 added for Mbed RCT clock format
Colmustard 0:52ac517d1aba 439 t.tm_year = ((year)+100); // year since 1900, current DCF year + 100 + 1900 = correct year
Colmustard 0:52ac517d1aba 440 set_time(mktime(&t)); // set RTC clock
Colmustard 0:52ac517d1aba 441 }
Colmustard 0:52ac517d1aba 442 // Analog in example
Colmustard 0:52ac517d1aba 443 void getbattery()
Colmustard 0:52ac517d1aba 444 {
Colmustard 0:52ac517d1aba 445 AdcIn=Ain1.read();
Colmustard 0:52ac517d1aba 446 Ht = (AdcIn*3.3); // set the numeric to the exact MCU analog reference voltage for greater accuracy
Colmustard 0:52ac517d1aba 447 sprintf(Vcc,"%2.3f",Ht);
Colmustard 0:52ac517d1aba 448 }
Colmustard 0:52ac517d1aba 449 // Temperature example
Colmustard 0:52ac517d1aba 450 void gettemp()
Colmustard 0:52ac517d1aba 451 {
Colmustard 0:52ac517d1aba 452
Colmustard 0:52ac517d1aba 453 //Temp=temp_sensor.sample();
Colmustard 0:52ac517d1aba 454 sprintf(Temp,"%3.3f",temp_sensor.sample());
Colmustard 0:52ac517d1aba 455 }