W7500 OLED

Dependencies:   mbed WIZ_TCC_W7500OLED WIZnetInterface

Committer:
jcm931213
Date:
Tue Oct 31 01:11:54 2017 +0000
Revision:
1:feb9f603f054
Parent:
0:03e145bf4b2f
Child:
2:19f4e538d1cd
edit blank space

Who changed what in which revision?

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