![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Lan OLED
Dependencies: Adafruit_GFX WIZnetInterface mbed
Fork of Weather_Forecast_Helloworld_WIZwiki-W7500 by
main.cpp@12:b390072ae7ea, 2017-04-18 (annotated)
- Committer:
- joon874
- Date:
- Tue Apr 18 00:23:36 2017 +0000
- Revision:
- 12:b390072ae7ea
- Parent:
- 11:d6285226f186
- Child:
- 13:6396875b2889
code update
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
joon874 | 0:5d0d5244d683 | 1 | #include "mbed.h" |
joon874 | 0:5d0d5244d683 | 2 | #include "EthernetInterface.h" |
joon874 | 0:5d0d5244d683 | 3 | #include "Adafruit_SSD1306.h" |
joon874 | 11:d6285226f186 | 4 | |
joon874 | 11:d6285226f186 | 5 | // W7500 onboard LED & Init |
joon874 | 11:d6285226f186 | 6 | DigitalOut rled(LED1,1); |
joon874 | 11:d6285226f186 | 7 | DigitalOut gled(LED2,0); |
joon874 | 11:d6285226f186 | 8 | DigitalOut bled(LED3,1); |
joon874 | 0:5d0d5244d683 | 9 | |
joon874 | 11:d6285226f186 | 10 | // I2C Class |
joon874 | 11:d6285226f186 | 11 | I2C i2c(PA_10,PA_9); |
joon874 | 11:d6285226f186 | 12 | |
joon874 | 11:d6285226f186 | 13 | // OLED Class |
joon874 | 11:d6285226f186 | 14 | Adafruit_SSD1306_I2c gOled(i2c,NC,0x78,64,128); |
joon874 | 11:d6285226f186 | 15 | |
joon874 | 11:d6285226f186 | 16 | // Declare Ethernet Class |
joon874 | 11:d6285226f186 | 17 | EthernetInterface eth; |
joon874 | 11:d6285226f186 | 18 | |
joon874 | 11:d6285226f186 | 19 | // Declare TCP Connection Class |
joon874 | 11:d6285226f186 | 20 | TCPSocketConnection sock; |
joon874 | 0:5d0d5244d683 | 21 | |
joon874 | 0:5d0d5244d683 | 22 | |
joon874 | 11:d6285226f186 | 23 | // LED desplay functions |
joon874 | 0:5d0d5244d683 | 24 | void clean(){ |
joon874 | 11:d6285226f186 | 25 | rled = 0; gled = 0; bled = 0; |
joon874 | 0:5d0d5244d683 | 26 | } |
joon874 | 0:5d0d5244d683 | 27 | void clouds(){ |
joon874 | 11:d6285226f186 | 28 | rled = 1; gled = 1; bled = 1; |
joon874 | 0:5d0d5244d683 | 29 | } |
joon874 | 0:5d0d5244d683 | 30 | void thunderstorm(){ |
joon874 | 0:5d0d5244d683 | 31 | while(1){ |
joon874 | 11:d6285226f186 | 32 | rled = 0; gled = 0; bled = 0; |
joon874 | 0:5d0d5244d683 | 33 | wait(0.5); |
joon874 | 11:d6285226f186 | 34 | rled = 1; gled = 1; bled = 1; |
joon874 | 0:5d0d5244d683 | 35 | wait(0.5); |
joon874 | 0:5d0d5244d683 | 36 | }; |
joon874 | 0:5d0d5244d683 | 37 | } |
joon874 | 0:5d0d5244d683 | 38 | void rain(){ |
joon874 | 11:d6285226f186 | 39 | rled = 1; gled = 1; bled = 0; |
joon874 | 0:5d0d5244d683 | 40 | } |
joon874 | 0:5d0d5244d683 | 41 | void mist(){ |
joon874 | 0:5d0d5244d683 | 42 | while(1){ |
joon874 | 11:d6285226f186 | 43 | rled = 1; gled = 1; bled = 0; |
joon874 | 0:5d0d5244d683 | 44 | wait(0.5); |
joon874 | 11:d6285226f186 | 45 | rled = 1; gled = 1; bled = 1; |
joon874 | 0:5d0d5244d683 | 46 | wait(0.5); |
joon874 | 0:5d0d5244d683 | 47 | }; |
joon874 | 0:5d0d5244d683 | 48 | } |
joon874 | 0:5d0d5244d683 | 49 | void haze(){ |
joon874 | 11:d6285226f186 | 50 | rled = 0; gled = 1; bled = 0; |
joon874 | 0:5d0d5244d683 | 51 | } |
joon874 | 0:5d0d5244d683 | 52 | void fog(){ |
joon874 | 0:5d0d5244d683 | 53 | while(1){ |
joon874 | 11:d6285226f186 | 54 | rled = 1; gled = 0; bled = 0; |
joon874 | 0:5d0d5244d683 | 55 | wait(1.0); |
joon874 | 11:d6285226f186 | 56 | rled = 1; gled = 1; bled = 0; |
joon874 | 0:5d0d5244d683 | 57 | wait(1.0); |
joon874 | 0:5d0d5244d683 | 58 | }; |
joon874 | 0:5d0d5244d683 | 59 | } |
joon874 | 0:5d0d5244d683 | 60 | void error(){ |
joon874 | 11:d6285226f186 | 61 | rled = 1; gled = 1; bled = 1; |
joon874 | 0:5d0d5244d683 | 62 | } |
joon874 | 0:5d0d5244d683 | 63 | |
joon874 | 11:d6285226f186 | 64 | |
joon874 | 0:5d0d5244d683 | 65 | int main() { |
joon874 | 11:d6285226f186 | 66 | |
joon874 | 0:5d0d5244d683 | 67 | int phy_link; |
joon874 | 0:5d0d5244d683 | 68 | printf("Wait a second...\r\n"); |
joon874 | 0:5d0d5244d683 | 69 | |
joon874 | 11:d6285226f186 | 70 | //--------- Have to modify the mac address------------- |
joon874 | 11:d6285226f186 | 71 | uint8_t mac_addr[6] = {0x00, 0x08, 0xDC, 0xff, 0xff, 0x25}; |
joon874 | 11:d6285226f186 | 72 | |
joon874 | 11:d6285226f186 | 73 | eth.init(mac_addr); //Use DHCP |
joon874 | 0:5d0d5244d683 | 74 | |
joon874 | 0:5d0d5244d683 | 75 | while(1){ |
joon874 | 11:d6285226f186 | 76 | |
joon874 | 11:d6285226f186 | 77 | // phy link |
joon874 | 11:d6285226f186 | 78 | do{ |
joon874 | 11:d6285226f186 | 79 | phy_link = eth.ethernet_link(); |
joon874 | 11:d6285226f186 | 80 | printf("..."); |
joon874 | 11:d6285226f186 | 81 | wait(2); |
joon874 | 11:d6285226f186 | 82 | }while(!phy_link); |
joon874 | 11:d6285226f186 | 83 | printf("\r\n"); |
joon874 | 12:b390072ae7ea | 84 | |
joon874 | 12:b390072ae7ea | 85 | eth.connect(); |
joon874 | 11:d6285226f186 | 86 | |
joon874 | 11:d6285226f186 | 87 | printf("IP Address is %s\r\n\r\n", eth.getIPAddress()); |
joon874 | 11:d6285226f186 | 88 | |
joon874 | 11:d6285226f186 | 89 | // TCP socket connect to openweather server |
joon874 | 11:d6285226f186 | 90 | //TCPSocketConnection sock; |
joon874 | 11:d6285226f186 | 91 | sock.connect("api.openweathermap.org", 80); |
joon874 | 11:d6285226f186 | 92 | |
joon874 | 11:d6285226f186 | 93 | |
joon874 | 11:d6285226f186 | 94 | // GET method, to request weather forecast |
joon874 | 11:d6285226f186 | 95 | //char http_cmd[] = "GET /data/2.5/weather?q=Seoul,kr&appid=a0ca47dd7f6066404629b3e1ad728981 HTTP/1.0\n\n"; |
joon874 | 11:d6285226f186 | 96 | char http_cmd[] = "GET /data/2.5/weather?q=London,uk&appid=a0ca47dd7f6066404629b3e1ad728981 HTTP/1.0\n\n"; |
joon874 | 11:d6285226f186 | 97 | //char http_cmd[] = "GET /data/2.5/weather?q=Berlin,de&appid=a0ca47dd7f6066404629b3e1ad728981 HTTP/1.0\n\n"; |
joon874 | 11:d6285226f186 | 98 | |
joon874 | 11:d6285226f186 | 99 | sock.send_all(http_cmd, sizeof(http_cmd)-1); |
joon874 | 11:d6285226f186 | 100 | |
joon874 | 11:d6285226f186 | 101 | // get data into buffer |
joon874 | 11:d6285226f186 | 102 | char buffer[2048]; |
joon874 | 11:d6285226f186 | 103 | int ret; |
joon874 | 11:d6285226f186 | 104 | while (true) { |
joon874 | 11:d6285226f186 | 105 | ret = sock.receive(buffer, sizeof(buffer)-1); |
joon874 | 11:d6285226f186 | 106 | if (ret <= 0) |
joon874 | 11:d6285226f186 | 107 | break; |
joon874 | 11:d6285226f186 | 108 | buffer[ret] = '\0'; |
joon874 | 11:d6285226f186 | 109 | printf("Received %d chars from server: %s\n", ret, buffer); |
joon874 | 11:d6285226f186 | 110 | } |
joon874 | 11:d6285226f186 | 111 | printf("\r\n\r\n"); |
joon874 | 11:d6285226f186 | 112 | |
joon874 | 11:d6285226f186 | 113 | // parsing current date, weather, city, tempurature |
joon874 | 11:d6285226f186 | 114 | char *date; |
joon874 | 11:d6285226f186 | 115 | char *weather; |
joon874 | 11:d6285226f186 | 116 | char *city; |
joon874 | 11:d6285226f186 | 117 | char *temper; |
joon874 | 11:d6285226f186 | 118 | |
joon874 | 11:d6285226f186 | 119 | char cur_date[17] = {0}; |
joon874 | 11:d6285226f186 | 120 | char weather_con[15] = {0}; |
joon874 | 11:d6285226f186 | 121 | char city_name[10] = {0}; |
joon874 | 11:d6285226f186 | 122 | char temper_data[3] = {0}; |
joon874 | 0:5d0d5244d683 | 123 | |
joon874 | 11:d6285226f186 | 124 | int temp; |
joon874 | 11:d6285226f186 | 125 | int num100, num10, num1; |
joon874 | 11:d6285226f186 | 126 | |
joon874 | 11:d6285226f186 | 127 | //parding date |
joon874 | 11:d6285226f186 | 128 | date = strstr(buffer, "Date"); |
joon874 | 11:d6285226f186 | 129 | for(int x=0;x<17;x++){ |
joon874 | 11:d6285226f186 | 130 | cur_date[x] = date[x+6]; |
joon874 | 11:d6285226f186 | 131 | } |
joon874 | 11:d6285226f186 | 132 | |
joon874 | 11:d6285226f186 | 133 | // parsing weather condition |
joon874 | 11:d6285226f186 | 134 | weather = strstr(buffer, "main"); |
joon874 | 11:d6285226f186 | 135 | for(int i=0; i<15;i++){ |
joon874 | 11:d6285226f186 | 136 | weather_con[i] = weather[i+7]; |
joon874 | 11:d6285226f186 | 137 | if(weather_con[i] == 34){ |
joon874 | 11:d6285226f186 | 138 | weather_con[i] = 0; |
joon874 | 11:d6285226f186 | 139 | break; |
joon874 | 11:d6285226f186 | 140 | } |
joon874 | 11:d6285226f186 | 141 | } |
joon874 | 11:d6285226f186 | 142 | |
joon874 | 11:d6285226f186 | 143 | // parsing city name |
joon874 | 11:d6285226f186 | 144 | city = strstr(buffer, "name"); |
joon874 | 11:d6285226f186 | 145 | for(int j=0; j<10;j++){ |
joon874 | 11:d6285226f186 | 146 | city_name[j] = city[j+7]; |
joon874 | 11:d6285226f186 | 147 | if(city_name[j] == 34){ |
joon874 | 11:d6285226f186 | 148 | city_name[j] = 0; |
joon874 | 11:d6285226f186 | 149 | break; |
joon874 | 11:d6285226f186 | 150 | } |
joon874 | 11:d6285226f186 | 151 | } |
joon874 | 0:5d0d5244d683 | 152 | |
joon874 | 11:d6285226f186 | 153 | //parsing current tempurature |
joon874 | 11:d6285226f186 | 154 | temper = strstr(buffer, "temp"); |
joon874 | 11:d6285226f186 | 155 | for(int k=0; k<3;k++){ |
joon874 | 11:d6285226f186 | 156 | temper_data[k] = temper[k+6]; |
joon874 | 11:d6285226f186 | 157 | } |
joon874 | 11:d6285226f186 | 158 | |
joon874 | 11:d6285226f186 | 159 | //kelvin to celius converter |
joon874 | 11:d6285226f186 | 160 | num100 = temper_data[0]- 48; |
joon874 | 11:d6285226f186 | 161 | num10 = temper_data[1] - 48; |
joon874 | 11:d6285226f186 | 162 | num1 = temper_data[2]- 48; |
joon874 | 11:d6285226f186 | 163 | temp = (num100*100 + num10*10 + num1) - 273; |
joon874 | 11:d6285226f186 | 164 | |
joon874 | 11:d6285226f186 | 165 | // Debug message |
joon874 | 11:d6285226f186 | 166 | printf("city name : %s\r\n", city_name); |
joon874 | 11:d6285226f186 | 167 | printf("weather : %s\r\n", weather_con); |
joon874 | 11:d6285226f186 | 168 | printf("temperature : %d\r\n\r\n", temp); |
joon874 | 11:d6285226f186 | 169 | |
joon874 | 11:d6285226f186 | 170 | // OLED Display |
joon874 | 11:d6285226f186 | 171 | gOled.begin(); |
joon874 | 11:d6285226f186 | 172 | gOled.clearDisplay(); |
joon874 | 11:d6285226f186 | 173 | gOled.printf("%s\n\n", cur_date); |
joon874 | 11:d6285226f186 | 174 | gOled.printf("City : %s\n", city_name); |
joon874 | 11:d6285226f186 | 175 | gOled.printf("Weather : %s\n", weather_con); |
joon874 | 11:d6285226f186 | 176 | gOled.printf("Temper : %d\n", temp); |
joon874 | 11:d6285226f186 | 177 | gOled.display(); |
joon874 | 11:d6285226f186 | 178 | gOled.setTextCursor(0,0); |
joon874 | 11:d6285226f186 | 179 | |
joon874 | 11:d6285226f186 | 180 | //LED display rely on weather condition |
joon874 | 11:d6285226f186 | 181 | if(strcmp(weather_con,"Clouds")==0) clouds(); |
joon874 | 11:d6285226f186 | 182 | else if(strcmp(weather_con,"Rain")==0) rain(); |
joon874 | 11:d6285226f186 | 183 | else if(strcmp(weather_con,"Thunderstorm")==0) thunderstorm(); |
joon874 | 11:d6285226f186 | 184 | else if(strcmp(weather_con,"Clean")==0) clean(); |
joon874 | 11:d6285226f186 | 185 | else if(strcmp(weather_con,"Mist")==0) mist(); |
joon874 | 11:d6285226f186 | 186 | else if(strcmp(weather_con,"Haze")==0) haze(); |
joon874 | 11:d6285226f186 | 187 | else if(strcmp(weather_con,"Fog")==0) fog(); |
joon874 | 11:d6285226f186 | 188 | else error(); |
joon874 | 0:5d0d5244d683 | 189 | |
joon874 | 0:5d0d5244d683 | 190 | |
joon874 | 11:d6285226f186 | 191 | //Disconnection |
joon874 | 11:d6285226f186 | 192 | sock.close(); |
joon874 | 11:d6285226f186 | 193 | eth.disconnect(); |
joon874 | 0:5d0d5244d683 | 194 | |
joon874 | 11:d6285226f186 | 195 | /* |
joon874 | 11:d6285226f186 | 196 | * everytime in delay, request weather forecast |
joon874 | 11:d6285226f186 | 197 | */ |
joon874 | 11:d6285226f186 | 198 | wait(20.0); |
joon874 | 0:5d0d5244d683 | 199 | |
joon874 | 0:5d0d5244d683 | 200 | }; |
joon874 | 0:5d0d5244d683 | 201 | |
joon874 | 0:5d0d5244d683 | 202 | } |