![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
mac
Dependencies: Adafruit_GFX WIZnetInterface mbed
Fork of Weather_Forecast_Helloworld_WIZwiki-W7500 by
main.cpp@2:06e2b95bd224, 2015-09-04 (annotated)
- Committer:
- joon874
- Date:
- Fri Sep 04 01:51:35 2015 +0000
- Revision:
- 2:06e2b95bd224
- Parent:
- 0:5d0d5244d683
- Child:
- 3:246564796e88
Changed OLED display lines
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
joon874 | 0:5d0d5244d683 | 1 | |
joon874 | 0:5d0d5244d683 | 2 | #include "mbed.h" |
joon874 | 0:5d0d5244d683 | 3 | #include "EthernetInterface.h" |
joon874 | 0:5d0d5244d683 | 4 | #include "Adafruit_SSD1306.h" |
joon874 | 0:5d0d5244d683 | 5 | |
joon874 | 0:5d0d5244d683 | 6 | /* |
joon874 | 0:5d0d5244d683 | 7 | * Sheild LED |
joon874 | 0:5d0d5244d683 | 8 | */ |
joon874 | 0:5d0d5244d683 | 9 | // DigitalOut rled(D9); |
joon874 | 0:5d0d5244d683 | 10 | // DigitalOut gled(D10); |
joon874 | 0:5d0d5244d683 | 11 | // DigitalOut bled(D11); |
joon874 | 0:5d0d5244d683 | 12 | |
joon874 | 0:5d0d5244d683 | 13 | /* |
joon874 | 0:5d0d5244d683 | 14 | * W7500 onboard LED |
joon874 | 0:5d0d5244d683 | 15 | */ |
joon874 | 0:5d0d5244d683 | 16 | DigitalOut rled(LED1); |
joon874 | 0:5d0d5244d683 | 17 | DigitalOut gled(LED2); |
joon874 | 0:5d0d5244d683 | 18 | DigitalOut bled(LED3); |
joon874 | 0:5d0d5244d683 | 19 | |
joon874 | 0:5d0d5244d683 | 20 | |
joon874 | 0:5d0d5244d683 | 21 | class I2CPreInit : public I2C |
joon874 | 0:5d0d5244d683 | 22 | { |
joon874 | 0:5d0d5244d683 | 23 | public: |
joon874 | 0:5d0d5244d683 | 24 | I2CPreInit(PinName sda, PinName scl) : I2C(sda, scl) |
joon874 | 0:5d0d5244d683 | 25 | { |
joon874 | 0:5d0d5244d683 | 26 | frequency(100000); |
joon874 | 0:5d0d5244d683 | 27 | start(); |
joon874 | 0:5d0d5244d683 | 28 | }; |
joon874 | 0:5d0d5244d683 | 29 | }; |
joon874 | 0:5d0d5244d683 | 30 | |
joon874 | 0:5d0d5244d683 | 31 | I2CPreInit gI2C(PA_10,PA_9); |
joon874 | 0:5d0d5244d683 | 32 | |
joon874 | 0:5d0d5244d683 | 33 | /* |
joon874 | 0:5d0d5244d683 | 34 | * set OLED |
joon874 | 0:5d0d5244d683 | 35 | */ |
joon874 | 0:5d0d5244d683 | 36 | Adafruit_SSD1306_I2c gOled(gI2C,NC,0x78,64,128); |
joon874 | 0:5d0d5244d683 | 37 | |
joon874 | 0:5d0d5244d683 | 38 | |
joon874 | 0:5d0d5244d683 | 39 | /* |
joon874 | 0:5d0d5244d683 | 40 | * LED desplay functions |
joon874 | 0:5d0d5244d683 | 41 | */ |
joon874 | 0:5d0d5244d683 | 42 | void clean(){ |
joon874 | 0:5d0d5244d683 | 43 | rled = 0; |
joon874 | 0:5d0d5244d683 | 44 | gled = 0; |
joon874 | 0:5d0d5244d683 | 45 | bled = 1; |
joon874 | 0:5d0d5244d683 | 46 | } |
joon874 | 0:5d0d5244d683 | 47 | void clouds(){ |
joon874 | 0:5d0d5244d683 | 48 | rled = 0; |
joon874 | 0:5d0d5244d683 | 49 | gled = 0; |
joon874 | 0:5d0d5244d683 | 50 | bled = 0; |
joon874 | 0:5d0d5244d683 | 51 | } |
joon874 | 0:5d0d5244d683 | 52 | void thunderstorm(){ |
joon874 | 0:5d0d5244d683 | 53 | while(1){ |
joon874 | 0:5d0d5244d683 | 54 | rled = 0; |
joon874 | 0:5d0d5244d683 | 55 | gled = 0; |
joon874 | 0:5d0d5244d683 | 56 | bled = 0; |
joon874 | 0:5d0d5244d683 | 57 | wait(0.5); |
joon874 | 0:5d0d5244d683 | 58 | rled = 0; |
joon874 | 0:5d0d5244d683 | 59 | gled = 0; |
joon874 | 0:5d0d5244d683 | 60 | bled = 0; |
joon874 | 0:5d0d5244d683 | 61 | wait(0.5); |
joon874 | 0:5d0d5244d683 | 62 | }; |
joon874 | 0:5d0d5244d683 | 63 | } |
joon874 | 0:5d0d5244d683 | 64 | void rain(){ |
joon874 | 0:5d0d5244d683 | 65 | rled = 1; |
joon874 | 0:5d0d5244d683 | 66 | gled = 1; |
joon874 | 0:5d0d5244d683 | 67 | bled = 0; |
joon874 | 0:5d0d5244d683 | 68 | } |
joon874 | 0:5d0d5244d683 | 69 | void mist(){ |
joon874 | 0:5d0d5244d683 | 70 | while(1){ |
joon874 | 0:5d0d5244d683 | 71 | rled = 1; |
joon874 | 0:5d0d5244d683 | 72 | gled = 1; |
joon874 | 0:5d0d5244d683 | 73 | bled = 0; |
joon874 | 0:5d0d5244d683 | 74 | wait(0.5); |
joon874 | 0:5d0d5244d683 | 75 | rled = 1; |
joon874 | 0:5d0d5244d683 | 76 | gled = 1; |
joon874 | 0:5d0d5244d683 | 77 | bled = 1; |
joon874 | 0:5d0d5244d683 | 78 | wait(0.5); |
joon874 | 0:5d0d5244d683 | 79 | }; |
joon874 | 0:5d0d5244d683 | 80 | } |
joon874 | 0:5d0d5244d683 | 81 | void haze(){ |
joon874 | 0:5d0d5244d683 | 82 | rled = 0; |
joon874 | 0:5d0d5244d683 | 83 | gled = 1; |
joon874 | 0:5d0d5244d683 | 84 | bled = 0; |
joon874 | 0:5d0d5244d683 | 85 | } |
joon874 | 0:5d0d5244d683 | 86 | void fog(){ |
joon874 | 0:5d0d5244d683 | 87 | while(1){ |
joon874 | 0:5d0d5244d683 | 88 | rled = 1; |
joon874 | 0:5d0d5244d683 | 89 | gled = 0; |
joon874 | 0:5d0d5244d683 | 90 | bled = 0; |
joon874 | 0:5d0d5244d683 | 91 | wait(1.0); |
joon874 | 0:5d0d5244d683 | 92 | rled = 1; |
joon874 | 0:5d0d5244d683 | 93 | gled = 1; |
joon874 | 0:5d0d5244d683 | 94 | bled = 0; |
joon874 | 0:5d0d5244d683 | 95 | wait(1.0); |
joon874 | 0:5d0d5244d683 | 96 | }; |
joon874 | 0:5d0d5244d683 | 97 | } |
joon874 | 0:5d0d5244d683 | 98 | void error(){ |
joon874 | 0:5d0d5244d683 | 99 | rled = 1; |
joon874 | 0:5d0d5244d683 | 100 | gled = 1; |
joon874 | 0:5d0d5244d683 | 101 | bled = 1; |
joon874 | 0:5d0d5244d683 | 102 | } |
joon874 | 0:5d0d5244d683 | 103 | |
joon874 | 0:5d0d5244d683 | 104 | /* |
joon874 | 0:5d0d5244d683 | 105 | * main |
joon874 | 0:5d0d5244d683 | 106 | */ |
joon874 | 0:5d0d5244d683 | 107 | int main() { |
joon874 | 0:5d0d5244d683 | 108 | |
joon874 | 0:5d0d5244d683 | 109 | int phy_link; |
joon874 | 0:5d0d5244d683 | 110 | printf("Wait a second...\r\n"); |
joon874 | 0:5d0d5244d683 | 111 | uint8_t mac_addr[6] = {0x00, 0x08, 0xDC, 0x03, 0x04, 0x02}; |
joon874 | 0:5d0d5244d683 | 112 | |
joon874 | 0:5d0d5244d683 | 113 | EthernetInterface eth; |
joon874 | 0:5d0d5244d683 | 114 | |
joon874 | 0:5d0d5244d683 | 115 | eth.init(mac_addr); //Use DHCP |
joon874 | 0:5d0d5244d683 | 116 | |
joon874 | 0:5d0d5244d683 | 117 | while(1){ |
joon874 | 0:5d0d5244d683 | 118 | |
joon874 | 0:5d0d5244d683 | 119 | eth.connect(); |
joon874 | 0:5d0d5244d683 | 120 | |
joon874 | 0:5d0d5244d683 | 121 | /* |
joon874 | 0:5d0d5244d683 | 122 | * phy link |
joon874 | 0:5d0d5244d683 | 123 | */ |
joon874 | 0:5d0d5244d683 | 124 | do{ |
joon874 | 0:5d0d5244d683 | 125 | phy_link = eth.ethernet_link(); |
joon874 | 0:5d0d5244d683 | 126 | printf("..."); |
joon874 | 0:5d0d5244d683 | 127 | wait(2); |
joon874 | 0:5d0d5244d683 | 128 | }while(!phy_link); |
joon874 | 0:5d0d5244d683 | 129 | printf("\r\n"); |
joon874 | 0:5d0d5244d683 | 130 | |
joon874 | 0:5d0d5244d683 | 131 | printf("IP Address is %s\r\n\r\n", eth.getIPAddress()); |
joon874 | 0:5d0d5244d683 | 132 | |
joon874 | 0:5d0d5244d683 | 133 | /* |
joon874 | 0:5d0d5244d683 | 134 | * TCP socket connect to openweather server |
joon874 | 0:5d0d5244d683 | 135 | */ |
joon874 | 0:5d0d5244d683 | 136 | TCPSocketConnection sock; |
joon874 | 0:5d0d5244d683 | 137 | sock.connect("api.openweathermap.org", 80); |
joon874 | 0:5d0d5244d683 | 138 | |
joon874 | 0:5d0d5244d683 | 139 | |
joon874 | 0:5d0d5244d683 | 140 | /* |
joon874 | 0:5d0d5244d683 | 141 | * GET method, to request weather forecast |
joon874 | 0:5d0d5244d683 | 142 | */ |
joon874 | 0:5d0d5244d683 | 143 | char http_cmd[] = "GET /data/2.5/weather?q=Seoul,kr HTTP/1.0\n\n"; |
joon874 | 0:5d0d5244d683 | 144 | //char http_cmd[] = "GET /data/2.5/weather?q=London,uk HTTP/1.0\n\n"; |
joon874 | 0:5d0d5244d683 | 145 | //char http_cmd[] = "GET /data/2.5/weather?q=Berlin,de HTTP/1.0\n\n"; |
joon874 | 0:5d0d5244d683 | 146 | sock.send_all(http_cmd, sizeof(http_cmd)-1); |
joon874 | 0:5d0d5244d683 | 147 | |
joon874 | 0:5d0d5244d683 | 148 | /* |
joon874 | 0:5d0d5244d683 | 149 | * get data into buffer |
joon874 | 0:5d0d5244d683 | 150 | */ |
joon874 | 0:5d0d5244d683 | 151 | char buffer[1024]; |
joon874 | 0:5d0d5244d683 | 152 | int ret; |
joon874 | 0:5d0d5244d683 | 153 | while (true) { |
joon874 | 0:5d0d5244d683 | 154 | ret = sock.receive(buffer, sizeof(buffer)-1); |
joon874 | 0:5d0d5244d683 | 155 | if (ret <= 0) |
joon874 | 0:5d0d5244d683 | 156 | break; |
joon874 | 0:5d0d5244d683 | 157 | buffer[ret] = '\0'; |
joon874 | 0:5d0d5244d683 | 158 | printf("Received %d chars from server: %s\n", ret, buffer); |
joon874 | 0:5d0d5244d683 | 159 | } |
joon874 | 0:5d0d5244d683 | 160 | printf("\r\n\r\n"); |
joon874 | 0:5d0d5244d683 | 161 | |
joon874 | 0:5d0d5244d683 | 162 | /* |
joon874 | 0:5d0d5244d683 | 163 | * parsing current date, weather, city, tempurature |
joon874 | 0:5d0d5244d683 | 164 | */ |
joon874 | 0:5d0d5244d683 | 165 | char *date; |
joon874 | 0:5d0d5244d683 | 166 | char *weather; |
joon874 | 0:5d0d5244d683 | 167 | char *city; |
joon874 | 0:5d0d5244d683 | 168 | char *temper; |
joon874 | 0:5d0d5244d683 | 169 | |
joon874 | 0:5d0d5244d683 | 170 | char cur_date[17] = {0}; |
joon874 | 0:5d0d5244d683 | 171 | char weather_con[15] = {0}; |
joon874 | 0:5d0d5244d683 | 172 | char city_name[10] = {0}; |
joon874 | 0:5d0d5244d683 | 173 | char temper_data[3] = {0}; |
joon874 | 0:5d0d5244d683 | 174 | |
joon874 | 0:5d0d5244d683 | 175 | int temp; |
joon874 | 0:5d0d5244d683 | 176 | int num100, num10, num1; |
joon874 | 0:5d0d5244d683 | 177 | |
joon874 | 0:5d0d5244d683 | 178 | /* |
joon874 | 0:5d0d5244d683 | 179 | * parding date |
joon874 | 0:5d0d5244d683 | 180 | */ |
joon874 | 0:5d0d5244d683 | 181 | date = strstr(buffer, "Date"); |
joon874 | 0:5d0d5244d683 | 182 | for(int x=0;x<17;x++){ |
joon874 | 0:5d0d5244d683 | 183 | cur_date[x] = date[x+6]; |
joon874 | 0:5d0d5244d683 | 184 | } |
joon874 | 0:5d0d5244d683 | 185 | |
joon874 | 0:5d0d5244d683 | 186 | /* |
joon874 | 0:5d0d5244d683 | 187 | * parsing weather condition |
joon874 | 0:5d0d5244d683 | 188 | */ |
joon874 | 0:5d0d5244d683 | 189 | weather = strstr(buffer, "main"); |
joon874 | 0:5d0d5244d683 | 190 | for(int i=0; i<15;i++){ |
joon874 | 0:5d0d5244d683 | 191 | weather_con[i] = weather[i+7]; |
joon874 | 0:5d0d5244d683 | 192 | if(weather_con[i] == 34){ |
joon874 | 0:5d0d5244d683 | 193 | weather_con[i] = 0; |
joon874 | 0:5d0d5244d683 | 194 | break; |
joon874 | 0:5d0d5244d683 | 195 | } |
joon874 | 0:5d0d5244d683 | 196 | } |
joon874 | 0:5d0d5244d683 | 197 | |
joon874 | 0:5d0d5244d683 | 198 | /* |
joon874 | 0:5d0d5244d683 | 199 | * parsing city name |
joon874 | 0:5d0d5244d683 | 200 | */ |
joon874 | 0:5d0d5244d683 | 201 | city = strstr(buffer, "name"); |
joon874 | 0:5d0d5244d683 | 202 | for(int j=0; j<10;j++){ |
joon874 | 0:5d0d5244d683 | 203 | city_name[j] = city[j+7]; |
joon874 | 0:5d0d5244d683 | 204 | if(city_name[j] == 34){ |
joon874 | 0:5d0d5244d683 | 205 | city_name[j] = 0; |
joon874 | 0:5d0d5244d683 | 206 | break; |
joon874 | 0:5d0d5244d683 | 207 | } |
joon874 | 0:5d0d5244d683 | 208 | } |
joon874 | 0:5d0d5244d683 | 209 | |
joon874 | 0:5d0d5244d683 | 210 | /* |
joon874 | 0:5d0d5244d683 | 211 | * parsing current tempurature |
joon874 | 0:5d0d5244d683 | 212 | */ |
joon874 | 0:5d0d5244d683 | 213 | temper = strstr(buffer, "temp"); |
joon874 | 0:5d0d5244d683 | 214 | for(int k=0; k<3;k++){ |
joon874 | 0:5d0d5244d683 | 215 | temper_data[k] = temper[k+6]; |
joon874 | 0:5d0d5244d683 | 216 | } |
joon874 | 0:5d0d5244d683 | 217 | |
joon874 | 0:5d0d5244d683 | 218 | /* |
joon874 | 0:5d0d5244d683 | 219 | * kelvin to celius converter |
joon874 | 0:5d0d5244d683 | 220 | */ |
joon874 | 0:5d0d5244d683 | 221 | num100 = temper_data[0] - 48 - 2; |
joon874 | 0:5d0d5244d683 | 222 | num10 = temper_data[1] - 48 - 7; |
joon874 | 0:5d0d5244d683 | 223 | num1 = temper_data[2] - 48 - 3; |
joon874 | 0:5d0d5244d683 | 224 | |
joon874 | 0:5d0d5244d683 | 225 | temp = num100*100 + num10*10 + num1; |
joon874 | 0:5d0d5244d683 | 226 | |
joon874 | 0:5d0d5244d683 | 227 | /* |
joon874 | 0:5d0d5244d683 | 228 | * printf |
joon874 | 0:5d0d5244d683 | 229 | */ |
joon874 | 0:5d0d5244d683 | 230 | printf("city name : %s\r\n", city_name); |
joon874 | 0:5d0d5244d683 | 231 | printf("weather : %s\r\n", weather_con); |
joon874 | 0:5d0d5244d683 | 232 | printf("temperature : %d\r\n\r\n", temp); |
joon874 | 0:5d0d5244d683 | 233 | |
joon874 | 0:5d0d5244d683 | 234 | /* |
joon874 | 0:5d0d5244d683 | 235 | * OLED Display |
joon874 | 0:5d0d5244d683 | 236 | */ |
joon874 | 0:5d0d5244d683 | 237 | gOled.begin(); |
joon874 | 0:5d0d5244d683 | 238 | gOled.clearDisplay(); |
joon874 | 0:5d0d5244d683 | 239 | |
joon874 | 2:06e2b95bd224 | 240 | gOled.printf("%s\n\n", cur_date); |
joon874 | 0:5d0d5244d683 | 241 | gOled.printf("City : %s\n", city_name); |
joon874 | 0:5d0d5244d683 | 242 | gOled.printf("Weather : %s\n", weather_con); |
joon874 | 0:5d0d5244d683 | 243 | gOled.printf("Temper : %d\n", temp); |
joon874 | 0:5d0d5244d683 | 244 | gOled.display(); |
joon874 | 0:5d0d5244d683 | 245 | gOled.setTextCursor(0,0); |
joon874 | 0:5d0d5244d683 | 246 | |
joon874 | 0:5d0d5244d683 | 247 | /* |
joon874 | 0:5d0d5244d683 | 248 | * LED display rely on weather condition |
joon874 | 0:5d0d5244d683 | 249 | */ |
joon874 | 0:5d0d5244d683 | 250 | if(strcmp(weather_con,"Clouds")==0) clouds(); |
joon874 | 0:5d0d5244d683 | 251 | else if(strcmp(weather_con,"Rain")==0) rain(); |
joon874 | 0:5d0d5244d683 | 252 | else if(strcmp(weather_con,"Thunderstorm")==0) thunderstorm(); |
joon874 | 0:5d0d5244d683 | 253 | else if(strcmp(weather_con,"Clean")==0) clean(); |
joon874 | 0:5d0d5244d683 | 254 | else if(strcmp(weather_con,"Mist")==0) mist(); |
joon874 | 0:5d0d5244d683 | 255 | else if(strcmp(weather_con,"Haze")==0) haze(); |
joon874 | 0:5d0d5244d683 | 256 | else if(strcmp(weather_con,"Fog")==0) fog(); |
joon874 | 0:5d0d5244d683 | 257 | else error(); |
joon874 | 0:5d0d5244d683 | 258 | |
joon874 | 0:5d0d5244d683 | 259 | |
joon874 | 0:5d0d5244d683 | 260 | sock.close(); |
joon874 | 0:5d0d5244d683 | 261 | |
joon874 | 0:5d0d5244d683 | 262 | eth.disconnect(); |
joon874 | 0:5d0d5244d683 | 263 | |
joon874 | 0:5d0d5244d683 | 264 | /* |
joon874 | 0:5d0d5244d683 | 265 | * everytime in delay, request weather forecast |
joon874 | 0:5d0d5244d683 | 266 | */ |
joon874 | 0:5d0d5244d683 | 267 | wait(60.0); |
joon874 | 0:5d0d5244d683 | 268 | |
joon874 | 0:5d0d5244d683 | 269 | }; |
joon874 | 0:5d0d5244d683 | 270 | |
joon874 | 0:5d0d5244d683 | 271 | } |