Ian Kilburn / Embedded_web_wiznet

Dependencies:   WIZnet_Library mbed

Fork of Embedded_web_wiznet by IPN ESIME ZACATENCO

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 //w5100
00002 #include "mbed.h"
00003 #include "WIZnetInterface.h"
00004 #include <string>
00005 
00006 #define ECHO_SERVER_PORT   80
00007 
00008 
00009 WIZnetInterface eth(SPI_MOSI, SPI_MISO, SPI_SCK,SPI_CS,PB_4); // spi, cs, reset
00010 Serial pc(SERIAL_TX,SERIAL_RX);
00011 // theres a conflict with LED1 on the Nucleo board it uses the same pin as SPI_SCK!
00012 //DigitalOut led(LED1);
00013 // This is the chip select for the sd card which shares the SPI bus on the Arduino shield.
00014 DigitalOut SD_CS(PB_5);
00015 
00016 
00017 void f_ethernet_init(void);
00018 
00019 const char * IP_Addr    = "192.168.1.210";
00020 const char * IP_Subnet  = "255.255.255.0";
00021 const char * IP_Gateway = "192.168.1.1";
00022 char data[8];
00023 
00024 int ret;
00025 bool status;
00026 
00027 
00028 char paq_en[64];
00029 #define THING_SPEAK_IP_STR      "184.106.153.149" /* thingspeak.com IP Address */
00030 #define THING_SPEAK_IP_PORT     80 /* port number */
00031 #define THING_SPEAK_KEY_STR     "94XG0UQQXJ28F1QM" /* API key */
00032 #define THING_SPEAK_CHANNEL     21931 /* channel ID */
00033 #define THING_SPEAK_LABEL_STR   "field1"
00034 char * str0 = "POST /update HTTP/1.1\n";
00035 char * str1 = "Host: api.thingspeak.com\n";
00036 char * str2 = "Connection: close\n";
00037 char * str3 = "X-THINGSPEAKAPIKEY: ";
00038 char * str4 = "Content-Type: application/x-www-form-urlencoded\n";
00039 char * write_key = "94XG0UQQXJ28F1QM";
00040 char * str5 = "Content-Length: ";
00041 
00042 void updateThingSpeak(char * tsData)
00043 {
00044 }    
00045 
00046 int main()
00047 {
00048 // force the chip select for the SD card high to avoid collisions. We're not using the sd card for this program    
00049     char buffer[64];
00050     char data_entry[64];
00051     SD_CS=1;
00052     uint8_t mac[]={0x90,0xa2,0xDa,0x0d,0x42,0xe0};
00053     int attempt=0;    
00054     int entry=42;
00055     int length;
00056     f_ethernet_init();
00057     TCPSocketConnection client;
00058 //
00059     
00060     while (attempt < 4){   
00061     pc.printf("\nWaiting for connection to ThingSpeak server...\n");    
00062     ret=client.connect(THING_SPEAK_IP_STR,THING_SPEAK_IP_PORT);
00063     if (!ret) {
00064             pc.printf("\nConnected to ThingSpeak server\n");
00065             }
00066        else {
00067                        pc.printf("\nConnection attempt to ThingSpeak server failed\n");
00068                        attempt++;
00069             }
00070     if (client.is_connected() ) // try to send data
00071     { 
00072 // update function
00073      client.send(str0,strlen(str0));
00074      pc.printf("%s >%d",str0,strlen(str0));
00075      client.send(str1,strlen(str1));
00076      pc.printf("%s >%d",str1,strlen(str1));
00077      client.send(str2,strlen(str2));
00078      pc.printf("%s >%d",str2,strlen(str2));
00079      sprintf(buffer,"%s %s\n",str3,write_key);
00080      client.send(buffer,strlen(buffer));
00081      pc.printf("%s >%d",buffer,strlen(buffer));
00082      client.send(str4,strlen(str4));
00083      pc.printf("%s >%d",str4,strlen(str4));
00084      sprintf(data_entry,"field1=%d\n",entry);
00085      length=strlen(data_entry);
00086      sprintf(buffer,"Content-Length: %d\n\n",length);
00087      pc.printf("%s >%d",buffer,strlen(buffer));
00088      pc.printf("%s >%d",data_entry,strlen(data_entry));
00089      client.send(buffer,strlen(buffer));
00090      client.send(data_entry,strlen(data_entry));
00091      entry++;
00092      wait(20.0);
00093     }   
00094 }
00095 }
00096 
00097 void f_ethernet_init()
00098 {
00099     uint8_t mac[]={0x90,0xa2,0xDa,0x0d,0x42,0xe0};
00100     // mbed_mac_address((char *)mac); 
00101     pc.printf("\tStarting Ethernet Server ...\n\r");
00102     wait(1.0);
00103     ret = eth.init(mac);
00104     if(!ret)
00105     {
00106         pc.printf("Initialized, MAC= %s\n\r",eth.getMACAddress());
00107     }    
00108     else
00109     {
00110         pc.printf("Communication Failure  ... Restart devices ...\n\r");    
00111     }
00112     pc.printf("Connected");
00113     wait(0.5);
00114     pc.printf(".");
00115     wait(0.5);
00116     pc.printf(".\n\r");
00117     wait(0.5);
00118     ret = eth.connect();
00119     if(!ret)
00120     {
00121         pc.printf("Connection Established!\n\n\r");
00122         wait(1);
00123         pc.printf("IP=%s\n\rMASK=%s\n\rGW=%s\n\r",eth.getIPAddress(), eth.getNetworkMask(), eth.getGateway());
00124     }    
00125     else
00126     {
00127         pc.printf("Communication Failure  ... Restart devices ...\n\r"); 
00128     }
00129 }  
00130