Tag Archives: wireless sensor network

Reading data from multiple TelosB motes and pushing to MySQL Database

So, before starting the tutorial, I assume that you have installed TinyOS on your machine (whether it be Linux or Windows or MacOS). If not, follow this link. They have a really good step by step tutorial on how to do this.

1326546992_mote3

So, what I have is a set of TelosB motes (8 to be exact). 7 of them  placed in different parts of our laboratory, monitoring the climate data and the 8th one being used as a base station (a gateway, rather) connected to the central server machine. This Wireless Sensor Network, made of TelosB motes will be sending the climate (light, temperature, humidity) data to the base station in real-time and the base station in-turn pushes it to the MySQL database. There is provison to attach additional sensors to the TelosB motes. In my case, I have added  an additional microphone breakout board (ADMP401) to the TelosB motes which will send amplitude of sound (in dB) in addition to the climate data.

My database looks like this. The devices are identified by their device ids and corresponding data (whether temperature, light, humidity or whatever) being identified by a task number.

DB

The data can be represented in a meaningful way, by means of a real time dynamic chart using Java, JDBC and JSP/HTML5. This is beyond the scope of this tutorial and therefore I am not going into the details.

 So, how did i do this? Here it goes.

Steps

Note: All the programming was done in Linux platform, and using the Eclipse IDE. (Kepler Version)

1. Start by programming one of your motes with the BaseStation app which is already coming with the TinyOS package. Open the terminal/Cygwin and navigate to BaseStation folder. Type motelist to ensure that the mote is attached to the machine. If it displays “No Devices Found”, unplug and plug the mote again.

cd BaseStation
cd src
make telosb
make telosb install,1

This will compile and install the application onto the mote. Note that the id assigned to the device can be anything (1 in my case). It is just used to identify a particular device.

2. Now that the gateway is ready to receive messages, we should program the other motes with nesC code that will transmit the data to the base station mote. The oscilloscope application present inside the package can be edited for this purpose.

Edit OscilloscopeC.nc file which can read and transmit 5 different data sets. Initialise reading variables for all the 5 sensors.

oscilloscope_t local;
oscilloscope_t localL;
oscilloscope_t localH;
oscilloscope_t localP;
oscilloscope_t localM;

uint8_t reading;
uint8_t readingL;
uint8_t readingH;
uint8_t readingP;
uint8_t readingM;

interface AMSend as AM1;interface AMSend as AM2;
interface AMSend as AM3;interface AMSend as AM4;
interface AMSend as AM5;

Similarly add 5 Receive, Timer and Read interfaces.

Example functions are given below. (Complete code is not given. Spend some time on the already available Oscilloscope app and the functions listed here, and you will get the hang of it 🙂 )
Replicate the fired(), readDone(), sendDone() and receive() functions for all the 5 sensors.

event void T1.fired() {

local.id=81;
if ((reading == NREADINGS) )
{
if (!sendBusy && sizeof local <= call AM1.maxPayloadLength())
{
memcpy(call AM1.getPayload(&sendBuf, sizeof(local)), &local, sizeof local);
if (call AM1.send(AM_BROADCAST_ADDR, &sendBuf, sizeof local) == SUCCESS)
sendBusy = TRUE;
}
if (!sendBusy)
report_problem();

reading = 0;
/* Part 2 of cheap “time sync”: increment our count if we didn’t
jump ahead. */
if (!suppressCountChange)
local.count++;
suppressCountChange = FALSE;
}
if (call Read.read() != SUCCESS)
report_problem();
}

event void Read.readDone(error_t result, uint16_t data) {

if (result != SUCCESS)
{
data = 0xffff;
report_problem();
}
if (reading < NREADINGS)
local.readings[reading++] = data;

call T2.startOneShot(localL.interval);
}

event message_t* R1.receive(message_t* msg, void* payload, uint8_t len) {
oscilloscope_t *omsg = payload;
report_received();

if (omsg->version > local.version)
{
local.version = omsg->version;
local.interval = omsg->interval;

startTimer();
}

if (omsg->count > local.count)
{
local.count = omsg->count;
suppressCountChange = TRUE;
}

return msg;
}

3. Edit the already available OscilloscopeAppC file to map the different sensors

OscilloscopeC.AM1 -> AMSenderC;
.
.
OscilloscopeC.AM5 -> AMSenderC;

OscilloscopeC.R1 -> AMReceiverC;
.
.
OscilloscopeC.R5 -> AMReceiverC;

OscilloscopeC.T1 -> TimerMilliC;
.
.
OscilloscopeC.T5 ->TimerMilliC;
OscilloscopeC.ReadL -> PhotoSensor;
.
.
OscilloscopeC.ReadM -> Microphone;

4. Next, you need to add the database configuration details in Oscilloscope.java file

static String userid=”…”, password=”…”;
static String url = “jdbc:mysql://…….”;
static Connection con = null;
static Statement stmt = null;
con = DriverManager.getConnection(url, userid, password);
Inside the public synchronized void messageReceived(int dest_addr,
Message msg) function,  write the syntax for pushing the data to the MySQL database. For example, in my case it looked something like this:

statement.executeUpdate(“insert into solar_data ” + “values  (‘”+((omsg.get_id())%10)+”‘,'”+firstDigit(omsg.get_id())+”‘,'”+reading+”‘,'”+currentTime+”‘)”);

Repeat this for all parameters (Light, Temp, Humidity etc) by filtering them using if else blocks.

The delay interval can also be included.

And that’s it. Connect your base station TelosB to usb port. Ensure that it is detected by typing “motelist” in the terminal. Go to the java folder, make and run the app. Your motes should automatically be detected and shown in the Oscilloscope application. Basic graphs will be generated here and the data will be pushed to the MySQL database too.

Voila! You may choose to make responsive graphs using the generated data in HTML5, AngularJS etc.

Have fun exploring. Until next time.