Fork from Alex
Dependencies: mbed MbedJSONValue mbed-rtos 4DGL-uLCD-SE ESP8266NodeMCUInterface
Diff: main.cpp
- Revision:
- 10:16356570821e
- Parent:
- 9:299eb69af04e
diff -r 299eb69af04e -r 16356570821e main.cpp --- a/main.cpp Fri Apr 05 18:34:14 2019 +0000 +++ b/main.cpp Sat May 18 17:57:20 2019 +0000 @@ -86,7 +86,7 @@ char buf[1024]; int ind = 0; while (true) { - while (true) { + while (ind < 1023) { // get stuff. If we encounter \r or \n, that's a complete command! char tmp = dev.getc(); if (tmp == '\n' || tmp == '\r') { @@ -97,12 +97,29 @@ } buf[ind] = '\0'; // read command and respond - if (strcmp(buf, "reset") == 0) { + if (strcmp(buf, "reset wifi") == 0) { + dev.printf("Are you sure? y/[n]\n"); + if (dev.getc() == 'y') { + // Remove the WIFI.TXT file + remove("/local/wifi.txt"); + mbed_reset(); + } + } else if (strcmp(buf, "reset api") == 0) { dev.printf("Are you sure? y/[n]\n"); + if (dev.getc() == 'y') { + // Remove api_keys.txt + remove("/local/api_keys.txt"); + mbed_reset(); + } + } else if (strcmp(buf, "info") == 0) { + // Print basic info + dev.printf("WiFi Connected with Address %s\r\n", + wifi.getIPAddress()); + time_t curr_time = time(NULL); + dev.printf("Current Time: %s\r\n", ctime(&curr_time)); } buf[0] = '\0'; ind = 0; - //if (strcmp(buf, "reset") != 0) { Thread::wait(0.01); } } @@ -128,29 +145,71 @@ lcd_lock.unlock(); // Need to get wifi settings. If we don't have local file, then ask! - FILE* fid = fopen("/local/settings.txt", "r"); + FILE* fid = fopen("/local/api_keys.txt", "r"); + char settings_buf[1024]; + int settings_ind = 0; + int counter = 0; + if (fid == NULL) { + lcd_lock.lock(); + uLCD.cls(); + uLCD.text_height(2); + uLCD.text_width(2); + uLCD.text_string("SEE", 0, 2, FONT_7X8, RED); + uLCD.text_string("DEVICE", 0, 5, FONT_7X8, RED); + lcd_lock.unlock(); + // Ask + // Get the API key + dev.printf("Please provide the IP Stack API key\n"); + while (!dev.readable()) { + wait(0.001); + } + int ind = 0; + while (ind < 255) { + char tmp = dev.getc(); + if (tmp == '\n' || tmp == '\r') { + break; + } + ip_api_key[ind++] = tmp; + wait(0.01); + } + + ip_api_key[ind] = '\0'; - if (fid != NULL) { - // Read WiFi Settings - char settings_buf[1024]; - // Guaranteed to be 5 lines - // - fgets(settings_buf, 1024, fid); - // find \n - int settings_ind = 0; - int counter = 0; - while (settings_buf[counter] != '\n') { - ssid[settings_ind++] = settings_buf[counter++]; + dev.printf("Please provide the TimeZoneDB API key\n"); + while (!dev.readable()) { + wait(0.001); + } + ind = 0; + while (ind < 255) { + char tmp = dev.getc(); + if (tmp == '\r' || tmp == '\n') { + break; + } + time_api_key[ind++] = tmp; + wait(0.01); } - ssid[settings_ind] = '\0'; - settings_ind = 0; - fgets(settings_buf, 1024, fid); - counter = 0; - while (settings_buf[counter] != '\n') { - pass[settings_ind++] = settings_buf[counter++]; + time_api_key[ind] = '\0'; + + dev.printf("Please provide the OpenWeather API key\n"); + while (!dev.readable()) { + wait(0.001); } - pass[settings_ind] = '\0'; - settings_ind = 0; + ind = 0; + while (ind < 255) { + char tmp = dev.getc(); + if (tmp == '\r' || tmp == '\n') { + break; + } + weather_api_key[ind++] = tmp; + wait(0.01); + } + weather_api_key[ind] = '\0'; + // Create file + fid = fopen("/local/api_keys.txt", "w"); + fprintf(fid, "%s\n%s\n%s\n", ip_api_key, time_api_key, weather_api_key); + fclose(fid); + } else { + // Read from file fgets(settings_buf, 1024, fid); counter = 0; while (settings_buf[counter] != '\n') { @@ -172,6 +231,30 @@ } weather_api_key[settings_ind] = '\0'; fclose(fid); + } + + fid = fopen("/local/wifi.txt", "r"); + + if (fid != NULL) { + // Read WiFi Settings + // Guaranteed to be 2 lines + // + fgets(settings_buf, 1024, fid); + // find \n + settings_ind = 0; + counter = 0; + while (settings_buf[counter] != '\n') { + ssid[settings_ind++] = settings_buf[counter++]; + } + ssid[settings_ind] = '\0'; + settings_ind = 0; + fgets(settings_buf, 1024, fid); + counter = 0; + while (settings_buf[counter] != '\n') { + pass[settings_ind++] = settings_buf[counter++]; + } + pass[settings_ind] = '\0'; + fclose(fid); } else { lcd_lock.lock(); uLCD.cls(); @@ -222,61 +305,11 @@ wait(0.01); } pass[ind] = '\0'; - - // Get the API key - dev.printf("Please provide the IP Stack API key\n"); - while (!dev.readable()) { - wait(0.001); - } - ind = 0; - while (ind < 255) { - char tmp = dev.getc(); - if (tmp == '\n' || tmp == '\r') { - break; - } - ip_api_key[ind++] = tmp; - wait(0.01); - } - - ip_api_key[ind] = '\0'; - - dev.printf("Please provide the TimeZoneDB API key\n"); - while (!dev.readable()) { - wait(0.001); - } - ind = 0; - while (ind < 255) { - char tmp = dev.getc(); - if (tmp == '\r' || tmp == '\n') { - break; - } - time_api_key[ind++] = tmp; - wait(0.01); - } - time_api_key[ind] = '\0'; - - dev.printf("Please provide the OpenWeather API key\n"); - while (!dev.readable()) { - wait(0.001); - } - ind = 0; - while (ind < 255) { - char tmp = dev.getc(); - if (tmp == '\r' || tmp == '\n') { - break; - } - weather_api_key[ind++] = tmp; - wait(0.01); - } - weather_api_key[ind] = '\0'; - - - // Because this is a simple proof of concept, we store the password in // plaintext. It should be noted, however, that you **should never do // this in "real life"** - fid = fopen("/local/settings.txt", "w"); - fprintf(fid, "%s\n%s\n%s\n%s\n%s\n", ssid, pass, ip_api_key, time_api_key, weather_api_key); + fid = fopen("/local/wifi.txt", "w"); + fprintf(fid, "%s\n%s\n", ssid, pass); fclose(fid); } /*