This project shows how to connect Io platform to Cloud service (data.sparkfun.io).

Dependencies:   DHT WIZnetInterface mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "DHT.h"
00003 #include "EthernetInterface.h"
00004 
00005 /*
00006  *Input Pins, Misc
00007  */
00008 DHT sensor(D4, DHT11);
00009 DigitalIn  triggerPin(D3);
00010 
00011 EthernetInterface eth;
00012 TCPSocketConnection sock;
00013 
00014 
00015 /*
00016  * if you don't want to use DNS (and reduce your sketch size)
00017  * use the numeric IP instead of the name for the server:
00018  * IPAddress server(54,86,132,254);  // numeric IP for data.sparkfun.com
00019  */
00020 //char server[] = "data.sparkfun.com";    // name address for data.spark
00021 
00022 /*
00023  * Phant Stuffs
00024  */
00025 char publicKey[] = "NJxQ73DlZWTaJNyon1bL";//"insert_your_publicKey"
00026 char privateKey[] = "5dEXwN0qJPI72PjkG8gx";//"insert_your_privateKey";
00027 uint8_t NUM_FIELDS = 2;
00028 char fieldNames1[] = "hum";
00029 char fieldNames2[] = "temp";
00030 
00031 void post_data(void);
00032 
00033 int main() 
00034 {
00035     float h = 0.0f, c = 0.0f, f = 0.0f, k = 0.0f, dp = 0.0f, dpf = 0.0f;
00036     float val, val_lightPin, val_tempPin;
00037     int http_cmd_sz=800;
00038     char http_cmd[http_cmd_sz];
00039     int buffer_sz=300;  
00040     char buffer[buffer_sz];  
00041     int returnCode = 0;
00042     // Enter a MAC address for your controller below.
00043     uint8_t mac_addr[6] = {0x00, 0x08, 0xDC, 0x00, 0x01, 0x02};     
00044 
00045     printf("initializing Ethernet\r\n");
00046     // initializing MAC address
00047     eth.init(mac_addr);
00048     
00049     // Check Ethenret Link
00050     if(eth.link() == true)   printf("- Ethernet PHY Link-Done \r\n");
00051     else printf("- Ethernet PHY Link- Fail\r\n");
00052     
00053     // Start Ethernet connecting: Trying to get an IP address using DHCP
00054     if (eth.connect()<0)    printf("Fail - Ethernet Connecing");
00055     
00056     // Print your local IP address:
00057     printf("IP=%s\n\r",eth.getIPAddress());
00058     printf("MASK=%s\n\r",eth.getNetworkMask());
00059     printf("GW=%s\n\r",eth.getGateway());
00060 
00061     while(1)
00062     {
00063         if(triggerPin ==0)
00064         {
00065             sensor.readData();
00066             c   = sensor.ReadTemperature(CELCIUS);
00067             h   = sensor.ReadHumidity();
00068            printf("Temperature in Celcius: %4.2f", c);
00069            printf("Humidity is %4.2f\n", h, dp, dpf);
00070           
00071           sock.connect("data.sparkfun.com", 80);
00072     
00073           snprintf(http_cmd, http_cmd_sz,  "GET /input/%s?private_key=%s&%s=%2.2f&%s=%3.3f HTTP/1.1\r\nHost: data.sparkfun.com\r\nConection: close\r\n\r\n", 
00074                                             publicKey, privateKey, fieldNames1, h, fieldNames2, c);
00075           sock.send_all(http_cmd, http_cmd_sz-1);
00076     
00077           while ( (returnCode = sock.receive(buffer, buffer_sz-1)) > 0)
00078           {
00079               buffer[returnCode] = '\0';
00080               printf("Received %d chars from server:\n\r%s\n", returnCode, buffer);
00081           }
00082   
00083           sock.close();         
00084         }
00085         
00086         wait(2);
00087     } 
00088 }
00089 
00090 
00091