Hardware Reference
In-Depth Information
#define WEBSITE "api.xively.com"
#define API_key "yourAPIKey"
#define feedID "yourFeedID"
In the setup() function of the sketch, we can now use DHCP to get an IP address for
the sketch, as shown in the following code:
// Start the Ethernet connection
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
Ethernet.begin(mac, ip);
}
For debug purposes, we print this IP address on the Serial Monitor:
Serial.print("IP address: ");
Serial.println(Ethernet.localIP());
Now, in the loop() function of the sketch, we will make the measurements with the
DHT sensor, format the data for Xively, and send this data to the Xively server.
The first step is to make the measurements. This is done with the following lines of code:
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
We then need to format this measured data so the Xively server can understand it. Indeed,
Xively defines a specific format to receive data. All the formats are defined at ht-
tps://xively.com/dev/docs/api/communicating/data_formats/ .
In our case, we will need to put the data in a JSON string according to the Xively specific-
ations. This is done using the following piece of code:
int length = 0;
String data = "";
data = data + "\n" +
"{\"version\":\"1.0.0\",\"datastreams\" : [ {\"id\" :
\"Temperature\",\"current_value\" : \"" +
String((int)temperature) + "\"}," + "{\"id\" :
\"Humidity\",\"current_value\" : \"" +
Search WWH ::




Custom Search