Part 1 of our ECE 4180 Final Project
Dependencies: 4DGL-uLCD-SE EthernetInterface MODSERIAL NTPClient mbed-rtos mbed
weather.cpp@0:9fa2874be5e4, 2015-05-01 (annotated)
- Committer:
- mohit1234
- Date:
- Fri May 01 18:07:34 2015 +0000
- Revision:
- 0:9fa2874be5e4
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mohit1234 | 0:9fa2874be5e4 | 1 | #include <string> |
mohit1234 | 0:9fa2874be5e4 | 2 | #include "weather.h" |
mohit1234 | 0:9fa2874be5e4 | 3 | #include "EthernetInterface.h" |
mohit1234 | 0:9fa2874be5e4 | 4 | #include "rtos.h" |
mohit1234 | 0:9fa2874be5e4 | 5 | #include <sstream> |
mohit1234 | 0:9fa2874be5e4 | 6 | #include "mbed.h" |
mohit1234 | 0:9fa2874be5e4 | 7 | #include <vector> |
mohit1234 | 0:9fa2874be5e4 | 8 | #include <functional> |
mohit1234 | 0:9fa2874be5e4 | 9 | #include <iostream> |
mohit1234 | 0:9fa2874be5e4 | 10 | |
mohit1234 | 0:9fa2874be5e4 | 11 | using namespace std; |
mohit1234 | 0:9fa2874be5e4 | 12 | |
mohit1234 | 0:9fa2874be5e4 | 13 | weatherInfo currentWeather; |
mohit1234 | 0:9fa2874be5e4 | 14 | |
mohit1234 | 0:9fa2874be5e4 | 15 | void split(const string& s, char c, |
mohit1234 | 0:9fa2874be5e4 | 16 | vector<string>& v) |
mohit1234 | 0:9fa2874be5e4 | 17 | { |
mohit1234 | 0:9fa2874be5e4 | 18 | string::size_type i = 0; |
mohit1234 | 0:9fa2874be5e4 | 19 | string::size_type j = s.find(c); |
mohit1234 | 0:9fa2874be5e4 | 20 | |
mohit1234 | 0:9fa2874be5e4 | 21 | while (j != string::npos) { |
mohit1234 | 0:9fa2874be5e4 | 22 | v.push_back(s.substr(i, j-i)); |
mohit1234 | 0:9fa2874be5e4 | 23 | i = ++j; |
mohit1234 | 0:9fa2874be5e4 | 24 | j = s.find(c, j); |
mohit1234 | 0:9fa2874be5e4 | 25 | |
mohit1234 | 0:9fa2874be5e4 | 26 | if (j == string::npos) |
mohit1234 | 0:9fa2874be5e4 | 27 | v.push_back(s.substr(i, s.length())); |
mohit1234 | 0:9fa2874be5e4 | 28 | } |
mohit1234 | 0:9fa2874be5e4 | 29 | } |
mohit1234 | 0:9fa2874be5e4 | 30 | |
mohit1234 | 0:9fa2874be5e4 | 31 | weatherInfo getWeatherInfo() |
mohit1234 | 0:9fa2874be5e4 | 32 | { |
mohit1234 | 0:9fa2874be5e4 | 33 | TCPSocketConnection sock; |
mohit1234 | 0:9fa2874be5e4 | 34 | sock.connect("ece4180.cloudapp.net", 80); |
mohit1234 | 0:9fa2874be5e4 | 35 | //sock.connect("google.com", 80); |
mohit1234 | 0:9fa2874be5e4 | 36 | |
mohit1234 | 0:9fa2874be5e4 | 37 | char http_cmd[] = "GET /weather.php HTTP/1.0\n\n"; |
mohit1234 | 0:9fa2874be5e4 | 38 | sock.send_all(http_cmd, sizeof(http_cmd)-1); |
mohit1234 | 0:9fa2874be5e4 | 39 | |
mohit1234 | 0:9fa2874be5e4 | 40 | char buffer[300]; |
mohit1234 | 0:9fa2874be5e4 | 41 | int ret; |
mohit1234 | 0:9fa2874be5e4 | 42 | while (true) { |
mohit1234 | 0:9fa2874be5e4 | 43 | ret = sock.receive(buffer, sizeof(buffer)-1); |
mohit1234 | 0:9fa2874be5e4 | 44 | if (ret <= 0) |
mohit1234 | 0:9fa2874be5e4 | 45 | break; |
mohit1234 | 0:9fa2874be5e4 | 46 | buffer[ret] = '\0'; |
mohit1234 | 0:9fa2874be5e4 | 47 | } |
mohit1234 | 0:9fa2874be5e4 | 48 | sock.close(); |
mohit1234 | 0:9fa2874be5e4 | 49 | |
mohit1234 | 0:9fa2874be5e4 | 50 | vector<string> v; |
mohit1234 | 0:9fa2874be5e4 | 51 | string s(buffer); |
mohit1234 | 0:9fa2874be5e4 | 52 | |
mohit1234 | 0:9fa2874be5e4 | 53 | split(s, '|', v); |
mohit1234 | 0:9fa2874be5e4 | 54 | |
mohit1234 | 0:9fa2874be5e4 | 55 | currentWeather.zipcode = v[1].c_str(); |
mohit1234 | 0:9fa2874be5e4 | 56 | currentWeather.tempC = atof(v[2].c_str()); |
mohit1234 | 0:9fa2874be5e4 | 57 | currentWeather.tempF = (9.0*currentWeather.tempC/5.0) + 32.0; |
mohit1234 | 0:9fa2874be5e4 | 58 | currentWeather.condition = v[3]; |
mohit1234 | 0:9fa2874be5e4 | 59 | currentWeather.clouds = atof(v[4].c_str()); |
mohit1234 | 0:9fa2874be5e4 | 60 | currentWeather.windSpeedKm = atof(v[5].c_str()); |
mohit1234 | 0:9fa2874be5e4 | 61 | currentWeather.windSpeedMi = currentWeather.windSpeedKm/1.609344; |
mohit1234 | 0:9fa2874be5e4 | 62 | |
mohit1234 | 0:9fa2874be5e4 | 63 | return currentWeather; |
mohit1234 | 0:9fa2874be5e4 | 64 | } |