sdfghjjh
Dependencies: Adafruit_GFX WIZnetInterface mbed
Fork of Weather_Forecast_Helloworld_WIZwiki-W7500 by
main.cpp@14:28b09c3f456b, 2017-06-13 (annotated)
- Committer:
- jcm931213
- Date:
- Tue Jun 13 01:22:22 2017 +0000
- Revision:
- 14:28b09c3f456b
- Parent:
- 13:6396875b2889
blank code; remove LED
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 | // I2C Class |
joon874 | 11:d6285226f186 | 6 | I2C i2c(PA_10,PA_9); |
joon874 | 11:d6285226f186 | 7 | |
joon874 | 11:d6285226f186 | 8 | // OLED Class |
joon874 | 11:d6285226f186 | 9 | Adafruit_SSD1306_I2c gOled(i2c,NC,0x78,64,128); |
joon874 | 11:d6285226f186 | 10 | |
joon874 | 11:d6285226f186 | 11 | // Declare Ethernet Class |
joon874 | 11:d6285226f186 | 12 | EthernetInterface eth; |
joon874 | 11:d6285226f186 | 13 | |
joon874 | 11:d6285226f186 | 14 | // Declare TCP Connection Class |
joon874 | 11:d6285226f186 | 15 | TCPSocketConnection sock; |
joon874 | 0:5d0d5244d683 | 16 | |
joon874 | 0:5d0d5244d683 | 17 | |
joon874 | 11:d6285226f186 | 18 | |
joon874 | 0:5d0d5244d683 | 19 | int main() { |
joon874 | 11:d6285226f186 | 20 | |
joon874 | 0:5d0d5244d683 | 21 | int phy_link; |
joon874 | 0:5d0d5244d683 | 22 | printf("Wait a second...\r\n"); |
joon874 | 0:5d0d5244d683 | 23 | |
joon874 | 11:d6285226f186 | 24 | //--------- Have to modify the mac address------------- |
joon874 | 11:d6285226f186 | 25 | uint8_t mac_addr[6] = {0x00, 0x08, 0xDC, 0xff, 0xff, 0x25}; |
joon874 | 11:d6285226f186 | 26 | |
jcm931213 | 14:28b09c3f456b | 27 | |
jcm931213 | 14:28b09c3f456b | 28 | //Ethernet Init using DHCP |
jcm931213 | 14:28b09c3f456b | 29 | |
jcm931213 | 14:28b09c3f456b | 30 | ///////////////////// |
joon874 | 0:5d0d5244d683 | 31 | |
joon874 | 0:5d0d5244d683 | 32 | while(1){ |
joon874 | 11:d6285226f186 | 33 | |
jcm931213 | 14:28b09c3f456b | 34 | // wait phy link... |
jcm931213 | 14:28b09c3f456b | 35 | |
jcm931213 | 14:28b09c3f456b | 36 | //////////////////// |
jcm931213 | 14:28b09c3f456b | 37 | |
joon874 | 11:d6285226f186 | 38 | printf("\r\n"); |
joon874 | 12:b390072ae7ea | 39 | |
joon874 | 12:b390072ae7ea | 40 | eth.connect(); |
joon874 | 11:d6285226f186 | 41 | |
joon874 | 13:6396875b2889 | 42 | printf("IP Address is %s\r\n\r\n", eth.getIPAddress()); |
joon874 | 13:6396875b2889 | 43 | printf("MASK Address is %s\r\n\r\n", eth.getNetworkMask()); |
joon874 | 13:6396875b2889 | 44 | printf("GATEWAY Address is %s\r\n\r\n", eth.getGateway()); |
joon874 | 13:6396875b2889 | 45 | printf("MAC Address is %s\r\n\r\n", eth.getMACAddress()); |
joon874 | 11:d6285226f186 | 46 | |
joon874 | 11:d6285226f186 | 47 | // TCP socket connect to openweather server |
joon874 | 11:d6285226f186 | 48 | //TCPSocketConnection sock; |
jcm931213 | 14:28b09c3f456b | 49 | |
jcm931213 | 14:28b09c3f456b | 50 | /////////////////////////////////////////// |
jcm931213 | 14:28b09c3f456b | 51 | |
joon874 | 11:d6285226f186 | 52 | |
joon874 | 11:d6285226f186 | 53 | // GET method, to request weather forecast |
joon874 | 11:d6285226f186 | 54 | char http_cmd[] = "GET /data/2.5/weather?q=London,uk&appid=a0ca47dd7f6066404629b3e1ad728981 HTTP/1.0\n\n"; |
jcm931213 | 14:28b09c3f456b | 55 | |
joon874 | 11:d6285226f186 | 56 | |
jcm931213 | 14:28b09c3f456b | 57 | // send data(request) |
jcm931213 | 14:28b09c3f456b | 58 | |
jcm931213 | 14:28b09c3f456b | 59 | |
jcm931213 | 14:28b09c3f456b | 60 | ////////////////////////////////////////// |
jcm931213 | 14:28b09c3f456b | 61 | |
joon874 | 11:d6285226f186 | 62 | |
joon874 | 11:d6285226f186 | 63 | // get data into buffer |
joon874 | 11:d6285226f186 | 64 | char buffer[2048]; |
joon874 | 11:d6285226f186 | 65 | int ret; |
jcm931213 | 14:28b09c3f456b | 66 | |
jcm931213 | 14:28b09c3f456b | 67 | // receive data |
jcm931213 | 14:28b09c3f456b | 68 | |
joon874 | 11:d6285226f186 | 69 | while (true) { |
jcm931213 | 14:28b09c3f456b | 70 | // break the while(), when received tha data |
jcm931213 | 14:28b09c3f456b | 71 | // save the data to ret |
jcm931213 | 14:28b09c3f456b | 72 | |
jcm931213 | 14:28b09c3f456b | 73 | |
jcm931213 | 14:28b09c3f456b | 74 | //////////////////////////////// |
joon874 | 11:d6285226f186 | 75 | buffer[ret] = '\0'; |
joon874 | 11:d6285226f186 | 76 | printf("Received %d chars from server: %s\n", ret, buffer); |
joon874 | 11:d6285226f186 | 77 | } |
joon874 | 11:d6285226f186 | 78 | printf("\r\n\r\n"); |
joon874 | 11:d6285226f186 | 79 | |
joon874 | 11:d6285226f186 | 80 | // parsing current date, weather, city, tempurature |
joon874 | 11:d6285226f186 | 81 | char *date; |
joon874 | 11:d6285226f186 | 82 | char *weather; |
joon874 | 11:d6285226f186 | 83 | char *city; |
joon874 | 11:d6285226f186 | 84 | char *temper; |
joon874 | 11:d6285226f186 | 85 | |
joon874 | 11:d6285226f186 | 86 | char cur_date[17] = {0}; |
joon874 | 11:d6285226f186 | 87 | char weather_con[15] = {0}; |
joon874 | 11:d6285226f186 | 88 | char city_name[10] = {0}; |
joon874 | 11:d6285226f186 | 89 | char temper_data[3] = {0}; |
joon874 | 0:5d0d5244d683 | 90 | |
joon874 | 11:d6285226f186 | 91 | int temp; |
joon874 | 11:d6285226f186 | 92 | int num100, num10, num1; |
joon874 | 11:d6285226f186 | 93 | |
joon874 | 11:d6285226f186 | 94 | //parding date |
joon874 | 11:d6285226f186 | 95 | date = strstr(buffer, "Date"); |
joon874 | 11:d6285226f186 | 96 | for(int x=0;x<17;x++){ |
joon874 | 11:d6285226f186 | 97 | cur_date[x] = date[x+6]; |
joon874 | 11:d6285226f186 | 98 | } |
joon874 | 11:d6285226f186 | 99 | |
joon874 | 11:d6285226f186 | 100 | // parsing weather condition |
joon874 | 11:d6285226f186 | 101 | weather = strstr(buffer, "main"); |
joon874 | 11:d6285226f186 | 102 | for(int i=0; i<15;i++){ |
joon874 | 11:d6285226f186 | 103 | weather_con[i] = weather[i+7]; |
joon874 | 11:d6285226f186 | 104 | if(weather_con[i] == 34){ |
joon874 | 11:d6285226f186 | 105 | weather_con[i] = 0; |
joon874 | 11:d6285226f186 | 106 | break; |
joon874 | 11:d6285226f186 | 107 | } |
joon874 | 11:d6285226f186 | 108 | } |
joon874 | 11:d6285226f186 | 109 | |
joon874 | 11:d6285226f186 | 110 | // parsing city name |
joon874 | 11:d6285226f186 | 111 | city = strstr(buffer, "name"); |
joon874 | 11:d6285226f186 | 112 | for(int j=0; j<10;j++){ |
joon874 | 11:d6285226f186 | 113 | city_name[j] = city[j+7]; |
joon874 | 11:d6285226f186 | 114 | if(city_name[j] == 34){ |
joon874 | 11:d6285226f186 | 115 | city_name[j] = 0; |
joon874 | 11:d6285226f186 | 116 | break; |
joon874 | 11:d6285226f186 | 117 | } |
joon874 | 11:d6285226f186 | 118 | } |
joon874 | 0:5d0d5244d683 | 119 | |
joon874 | 11:d6285226f186 | 120 | //parsing current tempurature |
joon874 | 11:d6285226f186 | 121 | temper = strstr(buffer, "temp"); |
joon874 | 11:d6285226f186 | 122 | for(int k=0; k<3;k++){ |
joon874 | 11:d6285226f186 | 123 | temper_data[k] = temper[k+6]; |
joon874 | 11:d6285226f186 | 124 | } |
joon874 | 11:d6285226f186 | 125 | |
joon874 | 11:d6285226f186 | 126 | //kelvin to celius converter |
joon874 | 11:d6285226f186 | 127 | num100 = temper_data[0]- 48; |
joon874 | 11:d6285226f186 | 128 | num10 = temper_data[1] - 48; |
joon874 | 11:d6285226f186 | 129 | num1 = temper_data[2]- 48; |
joon874 | 11:d6285226f186 | 130 | temp = (num100*100 + num10*10 + num1) - 273; |
joon874 | 11:d6285226f186 | 131 | |
joon874 | 11:d6285226f186 | 132 | // Debug message |
joon874 | 11:d6285226f186 | 133 | printf("city name : %s\r\n", city_name); |
joon874 | 11:d6285226f186 | 134 | printf("weather : %s\r\n", weather_con); |
joon874 | 11:d6285226f186 | 135 | printf("temperature : %d\r\n\r\n", temp); |
joon874 | 11:d6285226f186 | 136 | |
joon874 | 11:d6285226f186 | 137 | // OLED Display |
joon874 | 11:d6285226f186 | 138 | gOled.begin(); |
joon874 | 11:d6285226f186 | 139 | gOled.clearDisplay(); |
joon874 | 11:d6285226f186 | 140 | gOled.printf("%s\n\n", cur_date); |
joon874 | 11:d6285226f186 | 141 | gOled.printf("City : %s\n", city_name); |
joon874 | 11:d6285226f186 | 142 | gOled.printf("Weather : %s\n", weather_con); |
joon874 | 11:d6285226f186 | 143 | gOled.printf("Temper : %d\n", temp); |
joon874 | 11:d6285226f186 | 144 | gOled.display(); |
joon874 | 11:d6285226f186 | 145 | gOled.setTextCursor(0,0); |
joon874 | 0:5d0d5244d683 | 146 | |
joon874 | 0:5d0d5244d683 | 147 | |
joon874 | 11:d6285226f186 | 148 | //Disconnection |
joon874 | 11:d6285226f186 | 149 | sock.close(); |
joon874 | 11:d6285226f186 | 150 | eth.disconnect(); |
joon874 | 0:5d0d5244d683 | 151 | |
joon874 | 11:d6285226f186 | 152 | /* |
joon874 | 11:d6285226f186 | 153 | * everytime in delay, request weather forecast |
joon874 | 11:d6285226f186 | 154 | */ |
joon874 | 11:d6285226f186 | 155 | wait(20.0); |
joon874 | 0:5d0d5244d683 | 156 | |
joon874 | 0:5d0d5244d683 | 157 | }; |
joon874 | 0:5d0d5244d683 | 158 | |
joon874 | 0:5d0d5244d683 | 159 | } |