Beginning Sensor Networks with Arduino and Raspberry Pi by Charles Bell
Author:Charles Bell
Language: eng
Format: epub, pdf
ISBN: 9781430258247
Publisher: Apress
Now that you have the necessary libraries downloaded and copied, open a new Arduino project and name it Arduino_Xively. Start the file with the following includes. You need the SPI and Ethernet headers for the Ethernet card. The Xively library uses the HTTPClient header. Finally, you include the Xively header:
#include <SPI.h>
#include <Ethernet.h>
#include <HttpClient.h>
#include <Xively.h>
To use the Ethernet shield, you must also declare a MAC address. The IP address for the Ethernet shield is requested via DHCP. You define the MAC address as an array. This can be a random set of values, provided they are in the range 0x00–0xFF. You can use what is shown here:
byte mac_addr[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
Next you define your Xively API key and feed ID. You also define the pin number for your TMP36 sensor. This example uses pin 0. You can choose whatever pin you want to use—just change this define, and the rest of the code will point to the correct pin:
char xivelyKey[] = "<YOUR_KEY_HERE>";
#define FEED_NUMBER <YOUR_FEED_HERE>
#define SENSOR_PIN 0
Recall that when I discussed how to use the Xively library, I said that you need to set up an array for your feed with each channel (also called a datastream) in its own element. In this project, you need two elements: one for temperature values in Celsius and another in Fahrenheit. Notice the names used in the following code. These names must match those defined as channels for your device on Xively. Spelling counts, so make sure they match what you defined in Xively:
char sensor1_name[] = "celsius";
char sensor2_name[] = "fahrenheit";
XivelyDatastream datastreams[] = {
XivelyDatastream(sensor1_name, strlen(sensor1_name), DATASTREAM_FLOAT),
XivelyDatastream(sensor2_name, strlen(sensor2_name), DATASTREAM_FLOAT),
};
Recall also that you must define an instance of the feed class initializing the class with your feed number, the array of datastreams (channels), and the number of datastreams. Following that, you need to create an instance of the EthernetClient class from the HTTPClient library and pass that to a new instance of the XivelyClient class. The following statements effect these implementations:
XivelyFeed feed(FEED_NUMBER, datastreams, 2 /* number of datastreams */);
EthernetClient client;
XivelyClient xivelyclient(client);
You create a method to read the sensor and return the temperature in Celsius and Fahrenheit. Name the method get_temperature(), and use the code from Chapter 3 to complete it. I leave this as an exercise for you but include the correct solution in a moment.
As in the previous projects, you also need a record_sample() method that writes the samples to Xively. Create a new method, and complete it as follows. Notice that here you call the put() method for the Xively client to send the samples to Xively. You need only pass an instance of your feed and your API key. That’s it—it’s that easy:
void record_sample(float tempc, float tempf) {
datastreams[0].setFloat(tempc);
datastreams[1].setFloat(tempf);
Download
Beginning Sensor Networks with Arduino and Raspberry Pi by Charles Bell.pdf
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Coding Theory | Localization |
Logic | Object-Oriented Design |
Performance Optimization | Quality Control |
Reengineering | Robohelp |
Software Development | Software Reuse |
Structured Design | Testing |
Tools | UML |
Deep Learning with Python by François Chollet(12554)
Hello! Python by Anthony Briggs(9903)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9783)
The Mikado Method by Ola Ellnestam Daniel Brolund(9767)
Dependency Injection in .NET by Mark Seemann(9327)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8281)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7754)
Grails in Action by Glen Smith Peter Ledbrook(7684)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7549)
Becoming a Dynamics 365 Finance and Supply Chain Solution Architect by Brent Dawson(6993)
Microservices with Go by Alexander Shuiskov(6760)
Practical Design Patterns for Java Developers by Miroslav Wengner(6667)
Test Automation Engineering Handbook by Manikandan Sambamurthy(6617)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6397)
Angular Projects - Third Edition by Aristeidis Bampakos(6022)
The Art of Crafting User Stories by The Art of Crafting User Stories(5553)
NetSuite for Consultants - Second Edition by Peter Ries(5483)
Demystifying Cryptography with OpenSSL 3.0 by Alexei Khlebnikov(5289)
Kotlin in Action by Dmitry Jemerov(5047)
