Local version for working on Nucleo F401 and Ethernet shield
Dependencies: WIZnet_Library mbed
Fork of Embedded_web_wiznet by
Diff: main.cpp
- Revision:
- 7:e5c9b43f5754
- Parent:
- 6:09f34aa14399
--- a/main.cpp Sun Dec 28 20:12:37 2014 +0000 +++ b/main.cpp Mon Dec 29 14:20:56 2014 +0000 @@ -1,6 +1,7 @@ -//w5500 +//w5100 #include "mbed.h" #include "WIZnetInterface.h" +#include <string> #define ECHO_SERVER_PORT 80 @@ -15,109 +16,83 @@ void f_ethernet_init(void); - - const char * IP_Addr = "192.168.1.210"; const char * IP_Subnet = "255.255.255.0"; const char * IP_Gateway = "192.168.1.1"; char data[8]; -int length; -int ret,dummy; -int lv=1; +int ret; bool status; -char a[]={"HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nPragma: no-cache\r\n\r\n"}; -char b[]={"<html>\r\n<body>\r\n"}; -char c[]={"<body bgcolor=\"green\">"}; -char c1[]={"<body bgcolor=\"000ff0\"></body>\r\n"}; -char d[]={"<title>Nucleo F401</title>\r\n"}; -char e[]={"<center><h2>Website using Wiznet</h2>\r\n"}; -char f[]={"<br /><hr></center>\r\n"}; -//char g[]={"<FONT FACE=\"Arial\" SIZE=\"8\" COLOR=\"FF0000\">"}; -//char w[]={"<meta http-equiv=\"refresh\" content=\"10; url=/\">\r\n"}; -char h[]={"<center><div STYLE=\"position:absolute;bgcolor:black; top:100px; left:200px;width:54px\">"}; +char paq_en[64]; +#define THING_SPEAK_IP_STR "184.106.153.149" /* thingspeak.com IP Address */ +#define THING_SPEAK_IP_PORT 80 /* port number */ +#define THING_SPEAK_KEY_STR "94XG0UQQXJ28F1QM" /* API key */ +#define THING_SPEAK_CHANNEL 21931 /* channel ID */ +#define THING_SPEAK_LABEL_STR "field1" +char * str0 = "POST /update HTTP/1.1\n"; +char * str1 = "Host: api.thingspeak.com\n"; +char * str2 = "Connection: close\n"; +char * str3 = "X-THINGSPEAKAPIKEY: "; +char * str4 = "Content-Type: application/x-www-form-urlencoded\n"; +char * write_key = "94XG0UQQXJ28F1QM"; +char * str5 = "Content-Length: "; -//char r[]={"HTTP/1.1 200 OK\r\n"}; -//char s[]={"Content-Type: text/html\r\n"}; -//char t[]={"Connection: close\r\n"}; -//char u[]={"Refresh: 5\r\n\r\n"}; -//char v[]={"<!DOCTYOE HTML>\r\n"}; -//char y[]={"<html>\r\n"}; - - -char paq_en[64]; - +void updateThingSpeak(char * tsData) +{ +} int main() { // force the chip select for the SD card high to avoid collisions. We're not using the sd card for this program + char buffer[64]; + char data_entry[64]; SD_CS=1; + uint8_t mac[]={0x90,0xa2,0xDa,0x0d,0x42,0xe0}; + int attempt=0; + int entry=42; + int length; f_ethernet_init(); - TCPSocketServer server; TCPSocketConnection client; - server.bind(ECHO_SERVER_PORT); - server.listen(); - pc.printf("\nWaiting for connection to client...\n"); - +// - while(lv) - { - lv=server.accept(client); - if(!lv) - { - pc.printf("Client connected:%s\n", client.get_address()); - - } - else - { - wait(10); - } + while (attempt < 4){ + pc.printf("\nWaiting for connection to ThingSpeak server...\n"); + ret=client.connect(THING_SPEAK_IP_STR,THING_SPEAK_IP_PORT); + if (!ret) { + pc.printf("\nConnected to ThingSpeak server\n"); + } + else { + pc.printf("\nConnection attempt to ThingSpeak server failed\n"); + attempt++; + } + if (client.is_connected() ) // try to send data + { +// update function + client.send(str0,strlen(str0)); + pc.printf("%s >%d",str0,strlen(str0)); + client.send(str1,strlen(str1)); + pc.printf("%s >%d",str1,strlen(str1)); + client.send(str2,strlen(str2)); + pc.printf("%s >%d",str2,strlen(str2)); + sprintf(buffer,"%s %s\n",str3,write_key); + client.send(buffer,strlen(buffer)); + pc.printf("%s >%d",buffer,strlen(buffer)); + client.send(str4,strlen(str4)); + pc.printf("%s >%d",str4,strlen(str4)); + sprintf(data_entry,"field1=%d\n",entry); + length=strlen(data_entry); + sprintf(buffer,"Content-Length: %d\n\n",length); + pc.printf("%s >%d",buffer,strlen(buffer)); + pc.printf("%s >%d",data_entry,strlen(data_entry)); + client.send(buffer,strlen(buffer)); + client.send(data_entry,strlen(data_entry)); + entry++; + wait(20.0); } - client.receive(data,sizeof(data)); - pc.printf("%c",data); - - client.send(a,sizeof(a)); - client.send(b,sizeof(b)); - client.send(c,sizeof(c)); - client.send(d,sizeof(d)); - client.send(e,sizeof(e)); - client.send(f,sizeof(f)); - - - while(1) - { - while(client.is_connected()) - { - dummy++; - - - client.send(h,sizeof(h)); - sprintf(paq_en,"<p style=\"color:white;font-size:200px\">%d</p></div></center>\r\n",dummy); - client.send(paq_en,sizeof(paq_en)); - wait(1); - client.send(h,sizeof(h)); - sprintf(paq_en,"<p style=\"color:green;font-size:200px\">%d</p></div></center>\r\n",dummy); - client.send(paq_en,sizeof(paq_en)); - - } - server.bind(ECHO_SERVER_PORT); - server.listen(); - server.accept(client); - - - client.send(a,sizeof(a)); - client.send(b,sizeof(b)); - client.send(c,sizeof(c)); - client.send(d,sizeof(d)); - client.send(e,sizeof(e)); - client.send(f,sizeof(f)); - client.send(paq_en,sizeof(paq_en)); - } - - -} +} +} void f_ethernet_init() {