Objective
The objective of this tutorial is to send temperature and light data from a sky mote connected to the USB port to a mysql database. This tutorial assumes that you already have mysql, python and contiki-3.0 running in your computer. The computer must have a linux distribution, i used ubuntu 14.04.Step 1: Setting up python
In order to communicate the sky mote with the mysql database a serial communication is needed. Later i will provide a python code that is in charge of getting the data from the sky mote and the save it into the mysql database. Python need an extension called pySerial in order to be able to communicate with the serial port (USB). You can download the extension (pyserial-3.1.1.tar.gz) in the following link:https://pypi.python.org/pypi/pyserial
Then, extract the archive, cd to the extracted folder and install the pySerial using the command.
1
| python setup.py install |
http://sourceforge.net/projects/mysql-python/?source=dlp
Then, extract the archive, cd to the extracted folder and install the MySQLdb using the command.
1
| python setup.py install |
Step 2: Loading code to sky mote
The following code reads the temperature and light information from the sky mote, and then displays this information in the serial port (USB). This is a C code using Contiki-3.0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
| #include "contiki.h"#include "/home/doctorado/contiki-3.0/dev/sht11/sht11-sensor.h"#include "/home/doctorado/contiki-3.0/platform/sky/dev/light-sensor.h"#include "/home/doctorado/contiki-3.0/core/dev/leds.h"#include <stdio.h>//Declare the processPROCESS(send_sensor_info_process, "Print the Sensors Information");//Make the process start when the module is loadedAUTOSTART_PROCESSES(&send_sensor_info_process);/*---------------------------------------------------------------------------*/static intget_light(void){ return 10 * light_sensor.value(LIGHT_SENSOR_PHOTOSYNTHETIC) / 7;}/*---------------------------------------------------------------------------*/static intget_temp(void){ return ((sht11_sensor.value(SHT11_SENSOR_TEMP) / 10) - 396) / 10;}/*---------------------------------------------------------------------------*/int sequence = 0;//Define the process codePROCESS_THREAD(send_sensor_info_process, ev, data){ static struct etimer et; PROCESS_BEGIN(); while(1){ etimer_set(&et, CLOCK_SECOND * 5); SENSORS_ACTIVATE(light_sensor); SENSORS_ACTIVATE(sht11_sensor); PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et)); printf("Data\t", get_temp()); printf("%d\t", sequence++); printf("%d\t", get_temp()); printf("%d\n", get_light()); etimer_reset(&et); SENSORS_DEACTIVATE(light_sensor); SENSORS_DEACTIVATE(sht11_sensor); } PROCESS_END();}</stdio.h> |
1
| /home/doctorado/contiki-3.0/examples/sky |
1
| make TARGET=sky MOTES=/dev/ttyUSB0 TemperatureSky.upload && make login |
1
2
| Data 0 28 182Data 1 28 174 |
Step 3: Creating a database in Mysql
To create a database and a table in Mysql which is appropiate for this application you must write the following commands in mysql. The first command creates a database called mote1, and the second command creates a table named weatherData.
1
2
3
| CREATE DATABASE mote1;USE mote1;create table weatherData (id INT NOT NULL PRIMARY KEY AUTO_INCREMENT,secuencia INT(11) NOT NULL,tempC INT(4) NOT NULL,light INT(4) NOT NULL); |
Step 4: Creating a python code to read the temperature and light data and save it into mysql
The following python code reads the serial port (USB) and saves the temperature and light data into mysql, specifically python saves the information in the database named mote1 and in the table called weatherData.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
| #!/usr/bin/pythonimport serial import MySQLdb#establish connection to MySQL. You'll have to change this for your database.#dbConn = MySQLdb.connect("localhost","database_username","password","database_name") or die ("could not connect to database")dbConn = MySQLdb.connect("localhost","root","1234","mote1") or die ("could not connect to database")#open a cursor to the database#cursor = dbConn.cursor()#device = '/dev/tty.usbmodem1411' #this will have to be changed to the serial port you are usingdevice = '/dev/ttyUSB0' #this will have to be changed to the serial port you are usingtry: print "Trying...",device arduino = serial.Serial(device, 115200) except: print "Failed to connect on",device while True: #open a cursor to the database cursor = dbConn.cursor() try: data = arduino.readline() #read the data from the arduino pieces = data.split("\t") #split the data by the tab print "pieces[0]=",pieces[0] print "pieces[1]=",pieces[1] print "pieces[2]=",pieces[2] print "pieces[2]=",pieces[3] #Here we are going to insert the data into the Database try: cursor.execute("INSERT INTO weatherData (secuencia,tempC,light) VALUES (%s,%s,%s)", (pieces[1],pieces[2],pieces[3])) dbConn.commit() #commit the insert cursor.close() #close the cursor except MySQLdb.IntegrityError: print "failed to insert data" finally: cursor.close() #close just incase it failed except: print "Failed to get data from Arduino!" |
1
| python serial-mysql.py |
1
2
3
4
5
6
7
8
9
| Dataes[0]= pieces[1]= 0pieces[2]= 28pieces[2]= 181Dataes[0]= �pieces[1]= 1pieces[2]= 28pieces[2]= 175 |
1
| select * from weatherData; |
No comments:
Post a Comment