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

Dependencies:   DHT WIZnetInterface mbed

Committer:
embeddist
Date:
Tue Sep 22 11:56:10 2015 +0000
Revision:
0:ac90825a7703
This post shows how to connect IoT platform to data.sparkfun.com (Cloud service).

Who changed what in which revision?

UserRevisionLine numberNew contents of line
embeddist 0:ac90825a7703 1 #include "mbed.h"
embeddist 0:ac90825a7703 2 #include "DHT.h"
embeddist 0:ac90825a7703 3 #include "EthernetInterface.h"
embeddist 0:ac90825a7703 4
embeddist 0:ac90825a7703 5 /*
embeddist 0:ac90825a7703 6 *Input Pins, Misc
embeddist 0:ac90825a7703 7 */
embeddist 0:ac90825a7703 8 DHT sensor(D4, DHT11);
embeddist 0:ac90825a7703 9 DigitalIn triggerPin(D3);
embeddist 0:ac90825a7703 10
embeddist 0:ac90825a7703 11 EthernetInterface eth;
embeddist 0:ac90825a7703 12 TCPSocketConnection sock;
embeddist 0:ac90825a7703 13
embeddist 0:ac90825a7703 14
embeddist 0:ac90825a7703 15 /*
embeddist 0:ac90825a7703 16 * if you don't want to use DNS (and reduce your sketch size)
embeddist 0:ac90825a7703 17 * use the numeric IP instead of the name for the server:
embeddist 0:ac90825a7703 18 * IPAddress server(54,86,132,254); // numeric IP for data.sparkfun.com
embeddist 0:ac90825a7703 19 */
embeddist 0:ac90825a7703 20 //char server[] = "data.sparkfun.com"; // name address for data.spark
embeddist 0:ac90825a7703 21
embeddist 0:ac90825a7703 22 /*
embeddist 0:ac90825a7703 23 * Phant Stuffs
embeddist 0:ac90825a7703 24 */
embeddist 0:ac90825a7703 25 char publicKey[] = "NJxQ73DlZWTaJNyon1bL";//"insert_your_publicKey"
embeddist 0:ac90825a7703 26 char privateKey[] = "5dEXwN0qJPI72PjkG8gx";//"insert_your_privateKey";
embeddist 0:ac90825a7703 27 uint8_t NUM_FIELDS = 2;
embeddist 0:ac90825a7703 28 char fieldNames1[] = "hum";
embeddist 0:ac90825a7703 29 char fieldNames2[] = "temp";
embeddist 0:ac90825a7703 30
embeddist 0:ac90825a7703 31 void post_data(void);
embeddist 0:ac90825a7703 32
embeddist 0:ac90825a7703 33 int main()
embeddist 0:ac90825a7703 34 {
embeddist 0:ac90825a7703 35 float h = 0.0f, c = 0.0f, f = 0.0f, k = 0.0f, dp = 0.0f, dpf = 0.0f;
embeddist 0:ac90825a7703 36 float val, val_lightPin, val_tempPin;
embeddist 0:ac90825a7703 37 int http_cmd_sz=800;
embeddist 0:ac90825a7703 38 char http_cmd[http_cmd_sz];
embeddist 0:ac90825a7703 39 int buffer_sz=300;
embeddist 0:ac90825a7703 40 char buffer[buffer_sz];
embeddist 0:ac90825a7703 41 int returnCode = 0;
embeddist 0:ac90825a7703 42 // Enter a MAC address for your controller below.
embeddist 0:ac90825a7703 43 uint8_t mac_addr[6] = {0x00, 0x08, 0xDC, 0x00, 0x01, 0x02};
embeddist 0:ac90825a7703 44
embeddist 0:ac90825a7703 45 printf("initializing Ethernet\r\n");
embeddist 0:ac90825a7703 46 // initializing MAC address
embeddist 0:ac90825a7703 47 eth.init(mac_addr);
embeddist 0:ac90825a7703 48
embeddist 0:ac90825a7703 49 // Check Ethenret Link
embeddist 0:ac90825a7703 50 if(eth.link() == true) printf("- Ethernet PHY Link-Done \r\n");
embeddist 0:ac90825a7703 51 else printf("- Ethernet PHY Link- Fail\r\n");
embeddist 0:ac90825a7703 52
embeddist 0:ac90825a7703 53 // Start Ethernet connecting: Trying to get an IP address using DHCP
embeddist 0:ac90825a7703 54 if (eth.connect()<0) printf("Fail - Ethernet Connecing");
embeddist 0:ac90825a7703 55
embeddist 0:ac90825a7703 56 // Print your local IP address:
embeddist 0:ac90825a7703 57 printf("IP=%s\n\r",eth.getIPAddress());
embeddist 0:ac90825a7703 58 printf("MASK=%s\n\r",eth.getNetworkMask());
embeddist 0:ac90825a7703 59 printf("GW=%s\n\r",eth.getGateway());
embeddist 0:ac90825a7703 60
embeddist 0:ac90825a7703 61 while(1)
embeddist 0:ac90825a7703 62 {
embeddist 0:ac90825a7703 63 if(triggerPin ==0)
embeddist 0:ac90825a7703 64 {
embeddist 0:ac90825a7703 65 sensor.readData();
embeddist 0:ac90825a7703 66 c = sensor.ReadTemperature(CELCIUS);
embeddist 0:ac90825a7703 67 h = sensor.ReadHumidity();
embeddist 0:ac90825a7703 68 printf("Temperature in Celcius: %4.2f", c);
embeddist 0:ac90825a7703 69 printf("Humidity is %4.2f\n", h, dp, dpf);
embeddist 0:ac90825a7703 70
embeddist 0:ac90825a7703 71 sock.connect("data.sparkfun.com", 80);
embeddist 0:ac90825a7703 72
embeddist 0:ac90825a7703 73 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",
embeddist 0:ac90825a7703 74 publicKey, privateKey, fieldNames1, h, fieldNames2, c);
embeddist 0:ac90825a7703 75 sock.send_all(http_cmd, http_cmd_sz-1);
embeddist 0:ac90825a7703 76
embeddist 0:ac90825a7703 77 while ( (returnCode = sock.receive(buffer, buffer_sz-1)) > 0)
embeddist 0:ac90825a7703 78 {
embeddist 0:ac90825a7703 79 buffer[returnCode] = '\0';
embeddist 0:ac90825a7703 80 printf("Received %d chars from server:\n\r%s\n", returnCode, buffer);
embeddist 0:ac90825a7703 81 }
embeddist 0:ac90825a7703 82
embeddist 0:ac90825a7703 83 sock.close();
embeddist 0:ac90825a7703 84 }
embeddist 0:ac90825a7703 85
embeddist 0:ac90825a7703 86 wait(2);
embeddist 0:ac90825a7703 87 }
embeddist 0:ac90825a7703 88 }
embeddist 0:ac90825a7703 89
embeddist 0:ac90825a7703 90
embeddist 0:ac90825a7703 91