Displays the current weather, date, and time

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

Welcome to the Smart Weather Clock, which uses WiFi and Bluetooth to give the user a clear date, time, and weather for the day.

This wiki serves to document

  • What components you'll need
  • How these components will be connected
  • How the code gets all the information it needs

Components

LCD Screen

Components / 4D Systems 128 by 128 Smart Color LCD uLCD-144-G2
128 by 128 Color Smart LCD with serial interface and SD card slot

The LCD screen is a 128x128 pixel full color LCD by 4D Systems. A wonderful introduction can be found here.

In the simplest case, we'll only use one screen, but we can (and perhaps should) augment the design to include two.

Import library4DGL-uLCD-SE

Fork of 4DGL lib for uLCD-144-G2. Different command values needed. See https://mbed.org/users/4180_1/notebook/ulcd-144-g2-128-by-128-color-lcd/ for instructions and demo code.

You can buy it from sparkfun.

5V Adapter

The adapter allows us to feed much more power to the MBED and, critically, decouples us from a computer. It's important to remember that on the barrel jack adapter (i.e., the part that goes in the breadboard), the pin closest to the jack is 5V, and the back pin is Ground.

You can get the adapter here, and the barrel jack adapter here

Bluetooth UART Module

This module allows us to interface with the mbed via bluetooth and the ever-helpful AdaFruit Bluetooth App. A great introduction can be found here.

We'll use it to allow the user to set some defaults and API keys. This is stored in persistent flash storage on the LPC 1768 (See the Local File System, but you'll need an SD card if you're using a different MBED device.

You can get the Bluetooth module here.

WiFi Module

The WiFi module is also from AdaFruit, like the Bluetooth module. This allows us to interface with different WiFi networks for internet connectivity. A nice introduction is here.

You can get the WiFi module here.

WiFi Interface!

The current WiFi interface (found here) has some major problems - we're using an augmented version in our code!

Connections

5V Input for MBED

Before you connect the MBED to 5V from the wall, Make sure you have connected it correctly! If you don't, you could brick the MBED.

Thankfully, this is easy to test - when you think you have hooked up the 5V Jack correctly, try lighting a single LED (with a 10K resistor in series). If it lights up - you're good!

Alternatively, you could just plug in the MBED via USB, and just not have the MBED run on the +5V. You'll still need the Wall Adapter, however.

WiFi Module

The WiFi Module has two Grounds - you must connect both!

MBED5V Barrel Jack
Vin+5V (back pin)
GndGnd (pin closest to jack)
MBEDuLCD Cable
5V5V
GndGnd
P09TX
P10RX
P11Reset
MBEDBluetooth UART
5VVin
GndGnd
GndCTS
P27TXO
P28RXI
MBEDWiFi Module
5VV+
GndGnd
GndGnd
P13RX
P14TX
P15Reset

Code

The code works in a fairly logical order:

  1. Connect to the given WiFi, and check that we have all of our API keys
  2. Get the current location via the IP API
  3. Get the current time via the TimeZoneDB
  4. Get the current weather via the OpenWeatherMap API
  5. Continually update the weather, once per minute

Some caveats:

  • Memory seems to be an issue. If I had to guess, it would be the MbedJSONValue JSON parser - instantiating a third one seems to always lead to an MBED OS Fault or an MBED Memory Allocation fault. More insight is needed here.
  • The off-the-shelf ESP8226 Interface doesn't handle receiving correctly - I had to augment the library with recv and recv2. It works in most simple practical use cases.
Committer:
alexhrao
Date:
Sun Mar 31 03:08:51 2019 +0000
Revision:
3:7bf41989ff8f
Parent:
2:f7d19812bdc5
Child:
4:55f0c303f56a
Minor changes

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