XML for Arduino Sensor Data

If the things in the internet of things (IOT) are viewed as providing web services then the developed protocols and standards for online web services can be implemented on embedded devices, like the Arduino and allow for seamless integration with other web apps. Two standout methods for accessing and providing web services are REST and XML. REST is used to issue the ‘command’ to the web service and can be as simple as a URL. XML can be used to represent the information that the service provides and is an easily machine readable format.
To demonstrate this concept a simple application was developed using the arduino platform

. The arduino connects to the internet using the arduino Ethernet Shield and the application returns the value read at analog pins ‘A0’ and ‘A1’, a pot is connected to pin A0 as shown below:

Arduino XML Sensor ConnectionArduino XML Sensor (Pot) Connection

The pot acts as a simulator of an arduino sensor for demo purposes. The returned data of the arduino sensor is delivered as an xml file when the ip address of the arduino is accessed, for example from a regular web browser.

Arduino Example Code:

# -*- coding: utf-8 -*-
#include
#include

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,2, 95);

// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);

void setup()
{
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
}

void loop()
{
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank) {
client.println("HTTP/1.1 200 OK");
//client.println("Content-Type: text/html");
client.println();
client.print("");
//for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
client.print("");
//////////////////////////////////////////////////////////
client.print("");
client.print("");
client.print("analogChannel0");
client.print("");

client.print("");
client.print("sensor");
client.print("");

client.print("");
client.print("analog");
client.print("");

client.print("");
client.print(analogRead(0));
client.print("");
client.print("");
//////////////////////////////////////////////////////////

client.print("");
client.print("");
client.print("analogChannel1");
client.print("");

client.print("");
client.print("sensor");
client.print("");

client.print("");
client.print("analog");
client.print("");

client.print("");
client.print(analogRead(1));
client.print("");
client.print("");
//////////////////////////////////////////////////////////
client.print("");
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
}
else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
}
}

Example Browser Reply:

If you would like to implement this arduino project but don’t have all the parts you can get the ones you don’t have on amazon using the following links:

Also if you liked this article please leave a comment and don’t be shy to share it with others who may be interested in using the arduino ethernet shield with various arduino sensors to view data on the web using an implementation of xml for arduino.

Arduino Home Automation Projects – Book

Arduino Bluetooth Temperature Sensor Project

Return Home