dweet.io_CloudService

Dependencies:   HTTPClient WIZnetInterface mbed

Fork of dweetIoExample by WIZnet

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "EthernetInterface.h"
00003 #include "HTTPClient.h"
00004 
00005 /*
00006     SET DHCP
00007 */
00008 #define USE_DHCP    1
00009 //--------- Have to modify the mac address-------------
00010 unsigned char MAC_Addr[6] = {0x00,0x08,0xDC,0x12,0x34,0x56};
00011 
00012 /* IP SET */
00013 //char IP_Addr[] = "192.168.0.100";
00014 //char IP_Subnet[] = "255.255.255.0";
00015 //char IP_Gateway[] = "192.168.0.1";
00016 
00017 Serial pc(USBTX, USBRX);
00018 
00019 EthernetInterface ethernet;
00020 
00021 AnalogIn Sensor(A0);
00022 
00023 int main() {
00024 
00025 
00026   ///  mbed_mac_address((char *)MAC_Addr); //Use mbed mac addres
00027     pc.baud(9600);
00028     #if USE_DHCP
00029     int ret = ethernet.init(MAC_Addr);
00030     #else
00031     int ret = ethernet.init(MAC_Addr,IP_Addr,IP_Subnet,IP_Gateway);
00032     #endif
00033     if (!ret) {
00034         pc.printf("Initialized, MAC: %s\r\n", ethernet.getMACAddress());
00035         ret = ethernet.connect();
00036         if (!ret) {
00037             pc.printf("IP: %s, MASK: %s, GW: %s\r\n",
00038                       ethernet.getIPAddress(), ethernet.getNetworkMask(), ethernet.getGateway());
00039         } else {
00040             pc.printf("Error ethernet.connect() - ret = %d\r\n", ret);
00041             exit(0);
00042         }
00043     } else {
00044         pc.printf("Error ethernet.init() - ret = %d\r\n", ret);
00045         exit(0);
00046     }
00047     
00048     char str[512];
00049     char msg[128]= "";
00050     
00051     /*
00052         http://dweet.io/follow/nameYouWant
00053     */
00054     char nameYouWant[] = "nameYouWant";
00055 
00056     while(1)
00057     {
00058         sprintf(msg,"http://dweet.io/dweet/for/%s?a0=%d",nameYouWant,(int)(Sensor.read()*1000));
00059         HTTPClient http;
00060         
00061         pc.printf("Send post message to dweet.io\r\n");
00062         pc.printf("msg : %s\r\n",msg);
00063         ret = http.get(msg, str, sizeof(str));
00064         if(!ret)
00065         {
00066           pc.printf("\r\nPage fetched successfully - read %d characters\r\n", strlen(str));
00067           pc.printf("Result: %s\r\n", str);
00068         }
00069         else
00070         {
00071           pc.printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode());
00072         }
00073         wait(5);
00074     }
00075    
00076 }