In my previous post on the subject I showed how to configure the Arduino Ethernet shield, in such a way you can save a lot of memory. Now it's time to use the shield for something useful.
Arduino is mostly used to take data from sensors and to drive actuators to perform actions (such as motors, servos, relays, etc.). In this post we are going to use Arduino to collect some data (e.g. temperatures) and store them on a computer disk for subsequent analysis. As a physics teacher, in fact, I am interested in using Arduino as a laboratory tool and in physics you always collect data from experiments to be analysed offline.
Then, suppose you want to perform the following calorimetry experiment: take a resistor and wrap it in a waterproof material; then connect its leads to a voltage generator and make current flow through it. If R is the resistance of the device and V the voltage across its leads, the Ohm's Law states that the current flowing is I=V/R. The resistor dissipates heat, because of the Joule's effect, as W=RI2, where W is the amount of energy per unit time. If you plunge the resistor into water, the energy released by the resistor causes the heating of the water and you expect that the temperature of the water raises linearly with time.
You can perform this experiment using an LM35 connected to an Arduino to measure the water temperature versus time (the sensor leads must be made waterproof, of course, e.g. using some heat-shrink tubing). An Ethernet shield can then be used to send data to a computer.
Let' start looking at the Arduino sketch, shown below.
#include <Ethernet.h> #include <SPI.h> #define PORT 5000 #define LM35PIN A0 byte mac[] = {0x5e, 0xa4, 0x18, 0xf0, 0x8a, 0xf6}; IPAddress arduinoIP(192, 168, 1, 67); IPAddress dnsIP(192, 168, 1, 1); IPAddress gatewayIP(192, 168, 1, 1); IPAddress subnetIP(255, 255, 255, 0); EthernetServer server(PORT); boolean notYetConnected; void setup() { Ethernet.begin(mac, arduinoIP, dnsIP, gatewayIP, subnetIP); notYetConnected = true; } void loop() { int i = 0; EthernetClient client = server.available(); if (client) { if (notYetConnected) { client.println("Welcome!"); notYetConnected = false; } if (client.available()) { unsigned long now = millis(); int lm35 = analogRead(LM35PIN); now += millis(); double T = 5000.*lm35/10240.; server.print(0.5*now); server.print(" "); server.println(T); } } }
The first include
directives are needed to use the Ethernet shield. Then we define two symbols: PORT
is used to send data over the Internet, LM35PIN
represents the Arduino pin to which the LM35 sensor is connected.
Besides the addresses used to configure the Ethernet shield (see my previous post), a number of data members are defined: in particular, the EthernetServer
object called server
is instantiated, listening on port PORT
. This creates an object in the Arduino memory that connects to the Internet and waits for signals on the given port, represented as an integer (5000 in the example).
The setup()
method just initialise variables and configure the Ethernet shield. The most interesting part is in the loop()
method. Here we instantiate (create) an object called client
of class EthernetClient
. Such an object is returned by the server
object that continuously polls the port to which is connected. If no client is connected, the server returns NULL. Then, as soon as client
is found to be not NULL, and available for communication, we can send and receive data to/from it.
Before sending data we must get them: first of all we obtain the current time as the number of milliseconds elapsed since the beginning of the execution of the sketch (we don't care about the absolute time of the event). This time is returned by the function millis()
and is represented as an unsigned long integer, i.e. a binary code of 32 bits. With 32 bits, the highest number that can be represented is 232-1= 4294967295. Dividing this number by 86400 (the number of seconds in a day) and by 1000 we get about 50: this is the number of days during which the millis()
can work without reaching the overflow condition. In other words, there is plenty of time to perform our experiment.
Then we get the reading from the LM35 sensor using analogRead
and measure the time again. Averaging the last time with the one previously measured provides a better estimate of the time of reading. Note that, in between, we just read raw data, in such a way we minimise the time spent in data acquisition and obtain the time with as much precision as possible. Computing the temperature in degrees is made after getting the time: the analog pin reading is a 10 bits binary number: its highest value (1024) corresponds to an input of 5 V, i.e. 5000 mV. The actual temperature, in Celsius, can be obtained reading the output voltage of the sensor divided by 10.
To transmit data to a remote client, it's enough to call the print
method of the server
object. We then print the time reading, a blank and the actual temperature. Without any delay in the loop()
, the sketch will read temperatures at a rate of one measurement every few milliseconds (quite fast, indeed).
In order to collect those data on a computer you need an Internet client that connects to the Arduino port 5000, writes some data on that port to announce it (knock, knock) and waits for data. An example of such a program in C is below.
#include <stdio.h> #include <string.h> #include <arpa/inet.h> #define PORT 5000 #define ADDRESS "192.168.1.67" int main() { int len; int i; /* create socket */ int sock = socket(AF_INET , SOCK_STREAM , 0); if (sock <= 0) { printf("Can't create socket. Error"); return -1; } struct sockaddr_in server; server.sin_addr.s_addr = inet_addr(ADDRESS); server.sin_family = AF_INET; server.sin_port = htons(PORT); /* connect */ if (connect(sock , (struct sockaddr *)&server , sizeof(server)) < 0) { printf("can't connect to the server. Error"); return -1; } printf("CONNECTED: hit return to start DAQ\n"); char message[255]; scanf("%s", message); send(sock, message, strlen(message), 0); /* read */ while (1) { unsigned char c; recv(sock, &c, sizeof(unsigned char), 0); printf("%c", c); } return 0; }
Briefly, we first create a so-called socket to make a connection between the client and the server (the Arduino). We must connect the socket to the same port to which the server is listening at. Once connected, with scanf
we just read a string from the keyboard and send it to the server. This way the server answer and data acquisition starts. Data sent from the server are read with the recv
statement (one character at a time). In the above example the reading loop lasts forever and just print characters on screen. You can, of course, write data on a file until some event happens (e.g. key pressed, maximum number of data received, etc.).
On the Farnell Element14 site you can find all the products mentioned above (just follow links) and many others, with which you can realise much more experiments using a similar technique.
The content of this post can be found in my blog, too, and will be available as a chapter in a free publication of mine: Arduino Scientific Programming.
Top Comments