f

Dependencies:   Adafruit_GFX HTTPClient WIZnetInterface mbed

Committer:
wiznetw7500
Date:
Mon Apr 30 05:03:25 2018 +0000
Revision:
0:758720663484
fff

Who changed what in which revision?

UserRevisionLine numberNew contents of line
wiznetw7500 0:758720663484 1 #include "mbed.h"
wiznetw7500 0:758720663484 2 #include "EthernetInterface.h"
wiznetw7500 0:758720663484 3 #include "Adafruit_SSD1306.h"
wiznetw7500 0:758720663484 4 #include "HTTPClient.h"
wiznetw7500 0:758720663484 5
wiznetw7500 0:758720663484 6
wiznetw7500 0:758720663484 7 #if defined(TARGET_WIZWIKI_W7500)||defined(TARGET_WIZWIKI_W7500P)
wiznetw7500 0:758720663484 8 uint8_t mac_addr[6] = {0x00, 0x08, 0xDC, 0x53, 0xAE, 0x90};
wiznetw7500 0:758720663484 9 #endif
wiznetw7500 0:758720663484 10 // W7500 onboard LED & Init
wiznetw7500 0:758720663484 11 DigitalOut rled(LED1,1);
wiznetw7500 0:758720663484 12 DigitalOut gled(LED2,0);
wiznetw7500 0:758720663484 13 DigitalOut bled(LED3,1);
wiznetw7500 0:758720663484 14 AnalogIn Sensor(A0);
wiznetw7500 0:758720663484 15 // I2C Class
wiznetw7500 0:758720663484 16 I2C i2c(PA_10,PA_9);
wiznetw7500 0:758720663484 17
wiznetw7500 0:758720663484 18 // OLED Class
wiznetw7500 0:758720663484 19 Adafruit_SSD1306_I2c gOled(i2c,NC,0x78,64,128);
wiznetw7500 0:758720663484 20 Serial pc(USBTX, USBRX);
wiznetw7500 0:758720663484 21
wiznetw7500 0:758720663484 22 EthernetInterface eth;
wiznetw7500 0:758720663484 23
wiznetw7500 0:758720663484 24 // Declare TCP Connection Class
wiznetw7500 0:758720663484 25 TCPSocketConnection sock;
wiznetw7500 0:758720663484 26
wiznetw7500 0:758720663484 27 DigitalOut myled(D1);
wiznetw7500 0:758720663484 28
wiznetw7500 0:758720663484 29
wiznetw7500 0:758720663484 30
wiznetw7500 0:758720663484 31 int main() {
wiznetw7500 0:758720663484 32
wiznetw7500 0:758720663484 33
wiznetw7500 0:758720663484 34
wiznetw7500 0:758720663484 35 int phy_link;
wiznetw7500 0:758720663484 36 printf("Wait a second...\r\n");
wiznetw7500 0:758720663484 37
wiznetw7500 0:758720663484 38 eth.init(mac_addr); //Use DHCP
wiznetw7500 0:758720663484 39
wiznetw7500 0:758720663484 40 printf("Check Ethernet Link\r\n");
wiznetw7500 0:758720663484 41 /*while(1) //Wait link up
wiznetw7500 0:758720663484 42 {
wiznetw7500 0:758720663484 43 if(eth.link() == true)
wiznetw7500 0:758720663484 44 break;
wiznetw7500 0:758720663484 45 }*/
wiznetw7500 0:758720663484 46 printf("Link up\r\n");
wiznetw7500 0:758720663484 47
wiznetw7500 0:758720663484 48 eth.connect();
wiznetw7500 0:758720663484 49
wiznetw7500 0:758720663484 50 printf("IP Address is %s\r\n\r\n", eth.getIPAddress());
wiznetw7500 0:758720663484 51 printf("MASK Address is %s\r\n\r\n", eth.getNetworkMask());
wiznetw7500 0:758720663484 52 printf("GATEWAY Address is %s\r\n\r\n", eth.getGateway());
wiznetw7500 0:758720663484 53 printf("MAC Address is %s\r\n\r\n", eth.getMACAddress());
wiznetw7500 0:758720663484 54 while(1){
wiznetw7500 0:758720663484 55 // TCP socket connect to openweather server
wiznetw7500 0:758720663484 56 //TCPSocketConnection sock;
wiznetw7500 0:758720663484 57 char send_data[1024];
wiznetw7500 0:758720663484 58 int CDS_data=3000;
wiznetw7500 0:758720663484 59 sock.connect("api.thingspeak.com", 80);
wiznetw7500 0:758720663484 60
wiznetw7500 0:758720663484 61 sprintf(send_data, "GET http://api.thingspeak.com/update?key=0Q8S3KCULT7GUDVR&field1=%d HTTP/1.0\n\n",CDS_data);
wiznetw7500 0:758720663484 62 // GET method, to request weather forecast
wiznetw7500 0:758720663484 63 // char http_cmd[] = "GET /update?api_key=0Q8S3KCULT7GUDVR&field1=2000 HTTP/1.0\n\n";
wiznetw7500 0:758720663484 64
wiznetw7500 0:758720663484 65
wiznetw7500 0:758720663484 66 sock.send_all(send_data, sizeof(send_data)-1);
wiznetw7500 0:758720663484 67
wiznetw7500 0:758720663484 68 // get data into buffer
wiznetw7500 0:758720663484 69 char buffer[2048];
wiznetw7500 0:758720663484 70 int ret;
wiznetw7500 0:758720663484 71 while (true) {
wiznetw7500 0:758720663484 72 ret = sock.receive(buffer, sizeof(buffer)-1);
wiznetw7500 0:758720663484 73 if (ret <= 0)
wiznetw7500 0:758720663484 74 break;
wiznetw7500 0:758720663484 75 buffer[ret] = '\0';
wiznetw7500 0:758720663484 76 printf("Received %d chars from server: %s\n", ret, buffer);
wiznetw7500 0:758720663484 77 }
wiznetw7500 0:758720663484 78 printf("\r\n\r\n");
wiznetw7500 0:758720663484 79 }
wiznetw7500 0:758720663484 80 }