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