Fork from Alex

Dependencies:   mbed MbedJSONValue mbed-rtos 4DGL-uLCD-SE ESP8266NodeMCUInterface

Committer:
alexhrao
Date:
Sun Mar 31 00:53:59 2019 +0000
Revision:
2:f7d19812bdc5
Parent:
1:0620ba35d2e8
Child:
3:7bf41989ff8f
Add recv dependency

Who changed what in which revision?

UserRevisionLine numberNew contents of line
alexhrao 0:4ffa136585a2 1 #include "mbed.h"
alexhrao 1:0620ba35d2e8 2 #include "uLCD_4DGL.h"
alexhrao 1:0620ba35d2e8 3 #include "ESP8266Interface.h"
alexhrao 1:0620ba35d2e8 4 #include "TCPSocketConnection.h"
alexhrao 1:0620ba35d2e8 5 #include "rtos.h"
alexhrao 1:0620ba35d2e8 6
alexhrao 1:0620ba35d2e8 7 // We need this for being able to reset the MBED (similar to watch dogs)
alexhrao 1:0620ba35d2e8 8 extern "C" void mbed_reset();
alexhrao 1:0620ba35d2e8 9
alexhrao 1:0620ba35d2e8 10 // LCD Screen
alexhrao 1:0620ba35d2e8 11 uLCD_4DGL uLCD(p9, p10, p11);
alexhrao 1:0620ba35d2e8 12 Mutex lcd_lock;
alexhrao 1:0620ba35d2e8 13
alexhrao 1:0620ba35d2e8 14 // File System
alexhrao 1:0620ba35d2e8 15 LocalFileSystem fs("local");
alexhrao 1:0620ba35d2e8 16
alexhrao 1:0620ba35d2e8 17 // Bluetooth
alexhrao 1:0620ba35d2e8 18 RawSerial pc(USBTX, USBRX);
alexhrao 1:0620ba35d2e8 19 RawSerial dev(p28,p27);
alexhrao 1:0620ba35d2e8 20 Thread dev_thread;
alexhrao 1:0620ba35d2e8 21
alexhrao 1:0620ba35d2e8 22 // Error LED
alexhrao 1:0620ba35d2e8 23 DigitalOut err_led(LED1);
alexhrao 1:0620ba35d2e8 24
alexhrao 1:0620ba35d2e8 25 // WiFi
alexhrao 1:0620ba35d2e8 26 ESP8266Interface wifi(p13, p14, p15, 9600, 10000);
alexhrao 1:0620ba35d2e8 27
alexhrao 1:0620ba35d2e8 28 // Time
alexhrao 1:0620ba35d2e8 29 Thread time_thread;
alexhrao 1:0620ba35d2e8 30
alexhrao 1:0620ba35d2e8 31 // Weather
alexhrao 1:0620ba35d2e8 32 Thread weather_thread;
alexhrao 1:0620ba35d2e8 33 char* forecast_link;
alexhrao 0:4ffa136585a2 34
alexhrao 1:0620ba35d2e8 35 void time_updater() {
alexhrao 1:0620ba35d2e8 36 // We're not an interrupt, so take as much time as we need. Infinite loop
alexhrao 1:0620ba35d2e8 37 // but wait 1 second between each loop
alexhrao 1:0620ba35d2e8 38 struct tm* ltm;
alexhrao 1:0620ba35d2e8 39 time_t now;
alexhrao 1:0620ba35d2e8 40
alexhrao 1:0620ba35d2e8 41 now = time(NULL);
alexhrao 1:0620ba35d2e8 42 ltm = localtime(&now);
alexhrao 1:0620ba35d2e8 43
alexhrao 1:0620ba35d2e8 44 // Buffer for time string. Max length is 23:59 + \0
alexhrao 1:0620ba35d2e8 45 int max_time_len = 6;
alexhrao 1:0620ba35d2e8 46 char ftime[max_time_len];
alexhrao 1:0620ba35d2e8 47
alexhrao 1:0620ba35d2e8 48 int min = -1;
alexhrao 1:0620ba35d2e8 49
alexhrao 1:0620ba35d2e8 50 while (true) {
alexhrao 1:0620ba35d2e8 51 // if the minute has changed, update.
alexhrao 1:0620ba35d2e8 52 now = time(NULL);
alexhrao 1:0620ba35d2e8 53 ltm = localtime(&now);
alexhrao 1:0620ba35d2e8 54 if(ltm->tm_min != min) {
alexhrao 1:0620ba35d2e8 55 // Get the new time
alexhrao 1:0620ba35d2e8 56 strftime(ftime, max_time_len, "%H:%M", ltm);
alexhrao 1:0620ba35d2e8 57 // Update time! Lock the lcd mutex
alexhrao 1:0620ba35d2e8 58 lcd_lock.lock();
alexhrao 1:0620ba35d2e8 59 uLCD.text_width(2);
alexhrao 1:0620ba35d2e8 60 uLCD.text_height(2);
alexhrao 1:0620ba35d2e8 61 uLCD.text_string(ftime, 0, 2, FONT_8X8, GREEN);
alexhrao 1:0620ba35d2e8 62 // Done updating - unlock!
alexhrao 1:0620ba35d2e8 63 lcd_lock.unlock();
alexhrao 1:0620ba35d2e8 64 min = ltm->tm_min;
alexhrao 1:0620ba35d2e8 65 }
alexhrao 1:0620ba35d2e8 66 // Wait 1 second
alexhrao 1:0620ba35d2e8 67 Thread::wait(1.0f);
alexhrao 1:0620ba35d2e8 68 }
alexhrao 1:0620ba35d2e8 69 }
alexhrao 1:0620ba35d2e8 70
alexhrao 1:0620ba35d2e8 71 void weather_updater() {
alexhrao 1:0620ba35d2e8 72 // We can take as long as we want
alexhrao 1:0620ba35d2e8 73 while (true) {
alexhrao 1:0620ba35d2e8 74 // get the weather
alexhrao 1:0620ba35d2e8 75 // we'll always want to update the LCD - don't worry about the previous
alexhrao 1:0620ba35d2e8 76 // weather
alexhrao 1:0620ba35d2e8 77 int curr_temp = 0;
alexhrao 1:0620ba35d2e8 78 int high_temp = 0;
alexhrao 1:0620ba35d2e8 79 int low_temp = 0;
alexhrao 1:0620ba35d2e8 80 char buf[12];
alexhrao 1:0620ba35d2e8 81 sprintf(buf, "%d %d/%d", curr_temp, high_temp, low_temp);
alexhrao 1:0620ba35d2e8 82 // lock
alexhrao 1:0620ba35d2e8 83 lcd_lock.lock();
alexhrao 1:0620ba35d2e8 84 uLCD.text_width(2);
alexhrao 1:0620ba35d2e8 85 uLCD.text_height(2);
alexhrao 1:0620ba35d2e8 86 // include null!
alexhrao 1:0620ba35d2e8 87 uLCD.text_string(buf, 0, 5, FONT_7X8, WHITE);
alexhrao 1:0620ba35d2e8 88 // done! unlock
alexhrao 1:0620ba35d2e8 89 lcd_lock.unlock();
alexhrao 1:0620ba35d2e8 90 Thread::wait(3600);
alexhrao 1:0620ba35d2e8 91 }
alexhrao 1:0620ba35d2e8 92 }
alexhrao 1:0620ba35d2e8 93
alexhrao 1:0620ba35d2e8 94 void dev_recv() {
alexhrao 1:0620ba35d2e8 95 // Continually check if we have stuff...
alexhrao 1:0620ba35d2e8 96 char buf[1024];
alexhrao 1:0620ba35d2e8 97 int ind = 0;
alexhrao 1:0620ba35d2e8 98 while (true) {
alexhrao 1:0620ba35d2e8 99 while (true) {
alexhrao 1:0620ba35d2e8 100 // get stuff. If we encounter \r or \n, that's a complete command!
alexhrao 1:0620ba35d2e8 101 char tmp = dev.getc();
alexhrao 1:0620ba35d2e8 102 if (tmp == '\n' || tmp == '\r') {
alexhrao 1:0620ba35d2e8 103 break;
alexhrao 1:0620ba35d2e8 104 }
alexhrao 1:0620ba35d2e8 105 buf[ind++] = tmp;
alexhrao 1:0620ba35d2e8 106 Thread::wait(0.01);
alexhrao 1:0620ba35d2e8 107 }
alexhrao 1:0620ba35d2e8 108 buf[ind] = '\0';
alexhrao 1:0620ba35d2e8 109 // read command and respond
alexhrao 1:0620ba35d2e8 110 if (strcmp(buf, "reset") == 0) {
alexhrao 1:0620ba35d2e8 111 dev.printf("Are you sure? y/[n]\n");
alexhrao 1:0620ba35d2e8 112 }
alexhrao 1:0620ba35d2e8 113 buf[0] = '\0';
alexhrao 1:0620ba35d2e8 114 ind = 0;
alexhrao 1:0620ba35d2e8 115 //if (strcmp(buf, "reset") != 0) {
alexhrao 1:0620ba35d2e8 116 Thread::wait(0.01);
alexhrao 1:0620ba35d2e8 117 }
alexhrao 2:f7d19812bdc5 118 }
alexhrao 0:4ffa136585a2 119
alexhrao 0:4ffa136585a2 120 int main() {
alexhrao 1:0620ba35d2e8 121 // Set up bluetooth
alexhrao 1:0620ba35d2e8 122 dev.baud(9600);
alexhrao 1:0620ba35d2e8 123 pc.baud(9600);
alexhrao 2:f7d19812bdc5 124
alexhrao 1:0620ba35d2e8 125 // Tell user we're initializing...
alexhrao 1:0620ba35d2e8 126 lcd_lock.lock();
alexhrao 1:0620ba35d2e8 127 uLCD.cls();
alexhrao 1:0620ba35d2e8 128 uLCD.text_height(2);
alexhrao 1:0620ba35d2e8 129 uLCD.text_string("PLEASE WAIT", 0, 2, FONT_7X8, WHITE);
alexhrao 1:0620ba35d2e8 130 lcd_lock.unlock();
alexhrao 2:f7d19812bdc5 131
alexhrao 1:0620ba35d2e8 132 // Need to get wifi settings. If we don't have local file, then ask!
alexhrao 1:0620ba35d2e8 133 FILE* fid = NULL;//fopen("/local/settings.txt", "r");
alexhrao 1:0620ba35d2e8 134 char ssid[256];
alexhrao 1:0620ba35d2e8 135 char pass[256];
alexhrao 2:f7d19812bdc5 136 char api_key[256];
alexhrao 2:f7d19812bdc5 137 if (true) {
alexhrao 1:0620ba35d2e8 138
alexhrao 2:f7d19812bdc5 139 } else if (fid != NULL) {
alexhrao 2:f7d19812bdc5 140 // Read WiFi Settings
alexhrao 2:f7d19812bdc5 141 char settings_buf[1024];
alexhrao 2:f7d19812bdc5 142 // Guaranteed to be 256, 256, 512 AT MOST + two new lines
alexhrao 2:f7d19812bdc5 143 //
alexhrao 2:f7d19812bdc5 144 fgets(settings_buf, 1024, fid);
alexhrao 2:f7d19812bdc5 145 // find \n
alexhrao 2:f7d19812bdc5 146 int settings_ind = 0;
alexhrao 2:f7d19812bdc5 147 int counter = 0;
alexhrao 2:f7d19812bdc5 148 while (settings_buf[counter] != '\n') {
alexhrao 2:f7d19812bdc5 149 ssid[settings_ind++] = settings_buf[counter++];
alexhrao 2:f7d19812bdc5 150 }
alexhrao 2:f7d19812bdc5 151 ssid[settings_ind] = '\0';
alexhrao 2:f7d19812bdc5 152 settings_ind = 0;
alexhrao 2:f7d19812bdc5 153 counter++;
alexhrao 2:f7d19812bdc5 154 while (settings_buf[counter] != '\n') {
alexhrao 2:f7d19812bdc5 155 pass[settings_ind++] = settings_buf[counter++];
alexhrao 2:f7d19812bdc5 156 }
alexhrao 2:f7d19812bdc5 157 pass[settings_ind] = '\0';
alexhrao 2:f7d19812bdc5 158 settings_ind = 0;
alexhrao 2:f7d19812bdc5 159 counter++;
alexhrao 2:f7d19812bdc5 160 while (settings_buf[counter] != '\n') {
alexhrao 2:f7d19812bdc5 161 api_key[settings_ind++] = settings_buf[counter++];
alexhrao 2:f7d19812bdc5 162 }
alexhrao 2:f7d19812bdc5 163 api_key[settings_ind] = '\0';
alexhrao 2:f7d19812bdc5 164 fclose(fid);
alexhrao 1:0620ba35d2e8 165 } else {
alexhrao 1:0620ba35d2e8 166 lcd_lock.lock();
alexhrao 1:0620ba35d2e8 167 uLCD.cls();
alexhrao 1:0620ba35d2e8 168 uLCD.text_height(2);
alexhrao 1:0620ba35d2e8 169 uLCD.text_width(2);
alexhrao 1:0620ba35d2e8 170 uLCD.text_string("SEE", 0, 2, FONT_7X8, RED);
alexhrao 1:0620ba35d2e8 171 uLCD.text_string("DEVICE", 0, 5, FONT_7X8, RED);
alexhrao 1:0620ba35d2e8 172 lcd_lock.unlock();
alexhrao 2:f7d19812bdc5 173
alexhrao 1:0620ba35d2e8 174 // Ask!
alexhrao 1:0620ba35d2e8 175 dev.printf("Please provide the name of a WiFi Network\n");
alexhrao 2:f7d19812bdc5 176
alexhrao 1:0620ba35d2e8 177 // Wait for them to respond
alexhrao 1:0620ba35d2e8 178 while (!dev.readable()) {
alexhrao 1:0620ba35d2e8 179 wait(0.001);
alexhrao 1:0620ba35d2e8 180 }
alexhrao 1:0620ba35d2e8 181 int ind = 0;
alexhrao 2:f7d19812bdc5 182
alexhrao 1:0620ba35d2e8 183 // Read response
alexhrao 1:0620ba35d2e8 184 while (ind < 255) {
alexhrao 1:0620ba35d2e8 185 char tmp = dev.getc();
alexhrao 1:0620ba35d2e8 186 if (tmp == '\n' || tmp == '\r') {
alexhrao 1:0620ba35d2e8 187 break;
alexhrao 1:0620ba35d2e8 188 }
alexhrao 1:0620ba35d2e8 189 ssid[ind++] = tmp;
alexhrao 1:0620ba35d2e8 190 wait(0.01);
alexhrao 1:0620ba35d2e8 191 }
alexhrao 1:0620ba35d2e8 192 ssid[ind] = '\0';
alexhrao 1:0620ba35d2e8 193
alexhrao 1:0620ba35d2e8 194 // flush device
alexhrao 1:0620ba35d2e8 195 while (dev.readable()) {
alexhrao 1:0620ba35d2e8 196 dev.getc();
alexhrao 1:0620ba35d2e8 197 wait(0.01);
alexhrao 1:0620ba35d2e8 198 }
alexhrao 1:0620ba35d2e8 199
alexhrao 2:f7d19812bdc5 200 // Get the password
alexhrao 1:0620ba35d2e8 201 dev.printf("Please provide the password\n");
alexhrao 1:0620ba35d2e8 202 while (!dev.readable()) {
alexhrao 1:0620ba35d2e8 203 wait(0.001);
alexhrao 1:0620ba35d2e8 204 }
alexhrao 1:0620ba35d2e8 205 ind = 0;
alexhrao 1:0620ba35d2e8 206 while (ind < 255) {
alexhrao 1:0620ba35d2e8 207 char tmp = dev.getc();
alexhrao 1:0620ba35d2e8 208 if (tmp == '\n' || tmp == '\r') {
alexhrao 1:0620ba35d2e8 209 break;
alexhrao 1:0620ba35d2e8 210 }
alexhrao 1:0620ba35d2e8 211 pass[ind++] = tmp;
alexhrao 1:0620ba35d2e8 212 wait(0.01);
alexhrao 1:0620ba35d2e8 213 }
alexhrao 1:0620ba35d2e8 214 pass[ind] = '\0';
alexhrao 2:f7d19812bdc5 215
alexhrao 2:f7d19812bdc5 216 // Get the API key
alexhrao 2:f7d19812bdc5 217 dev.printf("Please provide the API key\n");
alexhrao 2:f7d19812bdc5 218 while (!dev.readable()) {
alexhrao 2:f7d19812bdc5 219 wait(0.001);
alexhrao 2:f7d19812bdc5 220 }
alexhrao 2:f7d19812bdc5 221 ind = 0;
alexhrao 2:f7d19812bdc5 222 while (ind < 255) {
alexhrao 2:f7d19812bdc5 223 char tmp = dev.getc();
alexhrao 2:f7d19812bdc5 224 if (tmp == '\n' || tmp == '\r') {
alexhrao 2:f7d19812bdc5 225 break;
alexhrao 2:f7d19812bdc5 226 }
alexhrao 2:f7d19812bdc5 227 api_key[ind++] = tmp;
alexhrao 2:f7d19812bdc5 228 wait(0.01);
alexhrao 2:f7d19812bdc5 229 }
alexhrao 2:f7d19812bdc5 230 api_key[ind] = '\0';
alexhrao 1:0620ba35d2e8 231 // Because this is a simple proof of concept, we store the password in
alexhrao 1:0620ba35d2e8 232 // plaintext. It should be noted, however, that you **should never do
alexhrao 1:0620ba35d2e8 233 // this in "real life"**
alexhrao 1:0620ba35d2e8 234 //fid = fopen("/local/settings.txt", "w");
alexhrao 2:f7d19812bdc5 235 //fprintf(fid, "%s\n%s\n%s\n", ssid, pass, api_key);
alexhrao 1:0620ba35d2e8 236 //fclose(fid);
alexhrao 1:0620ba35d2e8 237 }
alexhrao 1:0620ba35d2e8 238
alexhrao 1:0620ba35d2e8 239 bool res = wifi.init();
alexhrao 1:0620ba35d2e8 240 //dev.printf("Reset: %d\n", res);
alexhrao 1:0620ba35d2e8 241 // Set up the WiFi Access Point
alexhrao 1:0620ba35d2e8 242 dev.printf("Connecting to WiFi %s with Password %s\n", ssid, pass);
alexhrao 1:0620ba35d2e8 243 res = wifi.connect("Alex's iPhone", "mbedlookhere");
alexhrao 2:f7d19812bdc5 244 if (!res) {
alexhrao 2:f7d19812bdc5 245 dev.printf("Connection Failed... Resetting Device\n");
alexhrao 2:f7d19812bdc5 246 err_led = 1;
alexhrao 2:f7d19812bdc5 247 mbed_reset();
alexhrao 2:f7d19812bdc5 248 }
alexhrao 2:f7d19812bdc5 249 dev.printf("Connected with IP Address: %s\n", wifi.getIPAddress());
alexhrao 2:f7d19812bdc5 250
alexhrao 2:f7d19812bdc5 251 // Get the time & location
alexhrao 2:f7d19812bdc5 252 dev.printf("Getting the current time & location...\n");
alexhrao 2:f7d19812bdc5 253
alexhrao 2:f7d19812bdc5 254 set_time(1256729737);
alexhrao 2:f7d19812bdc5 255
alexhrao 2:f7d19812bdc5 256 // Clear the LCD Screen first
alexhrao 2:f7d19812bdc5 257 lcd_lock.lock();
alexhrao 2:f7d19812bdc5 258 uLCD.cls();
alexhrao 2:f7d19812bdc5 259 lcd_lock.unlock();
alexhrao 2:f7d19812bdc5 260
alexhrao 2:f7d19812bdc5 261 // Now that we know what time it is, set up our Time Thread
alexhrao 2:f7d19812bdc5 262 time_thread.start(time_updater);
alexhrao 2:f7d19812bdc5 263
alexhrao 2:f7d19812bdc5 264 // Make the request to get the forecast link
alexhrao 1:0620ba35d2e8 265 TCPSocketConnection tcp;
alexhrao 2:f7d19812bdc5 266 int tcp_res = tcp.connect("alexhrao.com", 80);
alexhrao 1:0620ba35d2e8 267 dev.printf("Connection: %d\n", tcp_res);
alexhrao 2:f7d19812bdc5 268 //int sent = tcp.send("GET / HTTP/1.1\r\nHost: alexhrao.com\r\nUser-Agent: curl/7.54.0\r\nAccept: */*", 73);
alexhrao 2:f7d19812bdc5 269 tcp.set_blocking(true, 10000);
alexhrao 2:f7d19812bdc5 270 char* cmd = "GET / HTTP/1.1\r\nHost: alexhrao.com\r\n\r\n";
alexhrao 2:f7d19812bdc5 271 int sent = tcp.send_all(cmd, strlen(cmd));
alexhrao 2:f7d19812bdc5 272 dev.printf("Sent %d\n", sent);
alexhrao 2:f7d19812bdc5 273 char buf[128];
alexhrao 2:f7d19812bdc5 274 for (int i = 0; i < 32; i++) {
alexhrao 2:f7d19812bdc5 275 buf[i] = 0;
alexhrao 2:f7d19812bdc5 276 }
alexhrao 2:f7d19812bdc5 277 // wifi.send(cmd, strlen(cmd));
alexhrao 2:f7d19812bdc5 278 // wifi.recv(buf, 2048);
alexhrao 2:f7d19812bdc5 279 //wait(15);
alexhrao 2:f7d19812bdc5 280 //tcp.receive(buf, 16);
alexhrao 2:f7d19812bdc5 281 int to_rec = 7;
alexhrao 2:f7d19812bdc5 282 //wifi.getc();
alexhrao 2:f7d19812bdc5 283 while (true) {
alexhrao 2:f7d19812bdc5 284 wifi.recv(buf, &to_rec);
alexhrao 2:f7d19812bdc5 285 dev.printf("To Receive: %d\n", to_rec);
alexhrao 2:f7d19812bdc5 286 to_rec = 7;
alexhrao 2:f7d19812bdc5 287 buf[7] = '\0';
alexhrao 2:f7d19812bdc5 288 dev.printf("%s", buf);
alexhrao 2:f7d19812bdc5 289 wait(15);
alexhrao 2:f7d19812bdc5 290 }
alexhrao 2:f7d19812bdc5 291 wifi.recv(buf, &to_rec);
alexhrao 2:f7d19812bdc5 292 dev.printf("%s\n", buf);
alexhrao 2:f7d19812bdc5 293 wifi.recv(buf, &to_rec);
alexhrao 2:f7d19812bdc5 294 int recv2 = tcp.receive_all(buf, 4);
alexhrao 2:f7d19812bdc5 295 dev.printf("Received (%d):\n%s\n", recv2, buf);
alexhrao 1:0620ba35d2e8 296 // Where are we & What time is it?
alexhrao 1:0620ba35d2e8 297 // Create the TCP connection, request from ipstack. Get the request,
alexhrao 1:0620ba35d2e8 298 // and store the current time (FORMAT?)
alexhrao 1:0620ba35d2e8 299 set_time(1256729737);
alexhrao 1:0620ba35d2e8 300 // Now that we know what time it is, set up our Time Thread
alexhrao 1:0620ba35d2e8 301 time_thread.start(time_updater);
alexhrao 1:0620ba35d2e8 302 // Now, make a single request to nws and get the forecast link - we can
alexhrao 1:0620ba35d2e8 303 // store this link for later!
alexhrao 1:0620ba35d2e8 304
alexhrao 1:0620ba35d2e8 305 // Start up weather service!
alexhrao 1:0620ba35d2e8 306 weather_thread.start(weather_updater);
alexhrao 1:0620ba35d2e8 307
alexhrao 1:0620ba35d2e8 308 //weather_ticker.attach(&weather_tick, 900000.0f);
alexhrao 1:0620ba35d2e8 309 // Listen on bluetooth.
alexhrao 1:0620ba35d2e8 310 dev_thread.start(dev_recv);
alexhrao 1:0620ba35d2e8 311 while(true) {
alexhrao 1:0620ba35d2e8 312 err_led = !err_led;
alexhrao 1:0620ba35d2e8 313 //dev.printf("Connection Status: %d\n", wifi.is_connected());
alexhrao 1:0620ba35d2e8 314 wait(0.5);
alexhrao 0:4ffa136585a2 315 }
alexhrao 0:4ffa136585a2 316 }