Analog Sensor Reading, RN-XV Wifly Connection and mySQL Database Setup
in Propeller 1
Dear forum,
I want to be able to create a temperature sensor system that reports data to the Internet. I already connected several TMP36 analog temperature sensors to an MCP3208 analog to digital converter. I also connected the propeller to a wifly RN-XV module (RN171) running firmware 4.41. I was able to set it up and send serial data to the wifly unit, and read the data by opening a TCP/IP terminal connection with Comm Operator or Tera Term by entering the wifly's DHCP ip address and port 192.168.2.10:2000. I also have a Raspberry Pi 3 running an Apache server and mysql; so I want to send the data to a mysql database in the Raspberry Pi, then post the data in the Internet by means of a dashboard (using a Python script) or by posting it in a webpage. The code below sends the data to the PST and to the Wifly module succesfully. I need help setting up the mysql connection. I am sharing the entire project as an attachment.
Please help me.
Thank you!!
I want to be able to create a temperature sensor system that reports data to the Internet. I already connected several TMP36 analog temperature sensors to an MCP3208 analog to digital converter. I also connected the propeller to a wifly RN-XV module (RN171) running firmware 4.41. I was able to set it up and send serial data to the wifly unit, and read the data by opening a TCP/IP terminal connection with Comm Operator or Tera Term by entering the wifly's DHCP ip address and port 192.168.2.10:2000. I also have a Raspberry Pi 3 running an Apache server and mysql; so I want to send the data to a mysql database in the Raspberry Pi, then post the data in the Internet by means of a dashboard (using a Python script) or by posting it in a webpage. The code below sends the data to the PST and to the Wifly module succesfully. I need help setting up the mysql connection. I am sharing the entire project as an attachment.
Please help me.
Thank you!!
CON
_clkmode = xtal1 + pll16x
_xinfreq = 5_000_000
CON
DEBUG = 0 ' Debug port sends results to Parallax Serial Terminal
W_PORT = 1 ' Wireless Module
W_RESET = 0 ' To RST pin on Wireless Module
W_TX = 15 ' To RX pin on Wireless Module
W_RX = 16 ' To TX pin on Wireless Module
W_CTS = 3 ' To RTS pin on Wireless Module
W_RTS = 4 ' To CTS pin on Wireless Module
BAUD = 9600 ' Baud Rate 9600 bps
mcp3208ratio = 0.0008056640625 'RATIO TO CALCULATE TEMPERATURE
VAR
long data[4], stack[50]
OBJ
adc : "MCP3208"
fds : "FullDuplexSerial4port"
dio : "dataIO4port"
f32 : "F32"
fstr : "FloatString"
CON
dpin = 12
cpin = 13
spin = 14
PUB main
fds.Init
fds.AddPort(W_PORT, W_RX, W_TX, W_CTS, W_RTS, 0, %000000, BAUD)
fds.AddPort(DEBUG, 31, 30, -1, -1, 0, %000000, BAUD) ' Debug to the terminal screen
fds.Start ' Start the ports
f32.start
Pause(200) ' UART startup delay
adc.start(dpin, cpin, spin, 255)
fstr.SetPrecision(4)
cognew(wiflytoPC, @stack)
PUB wiflytoPC
repeat
fds.tx(DEBUG,1) ' Clear screen on Parallax Serial Terminal
fds.tx(DEBUG, 13)
fds.Str(W_PORT, String("adc channel 0= "))
fds.Str(DEBUG, String("adc channel 0= "))
data[0] := adc.in(0)
dio.dec(W_PORT, data[0])
dio.dec(DEBUG, data[0])
fds.Str(W_PORT, String(" units"))
fds.Str(DEBUG, String(" units"))
fds.tx(W_PORT, 13)
fds.tx(DEBUG, 13)
pause(5000)
fds.Str(W_PORT, String("Temperature Sensor 1= "))
fds.Str(DEBUG, String("Temperature Sensor 1= "))
data[1] := fstr.FloatToString(f32.FMul((f32.FSub((f32.FMul(f32.FFloat(adc.in(1)), mcp3208ratio)), 0.50)), 100.0))
fds.Str(W_PORT, data[1])
fds.Str(DEBUG, data[1])
fds.Str(W_PORT, String(" degrees"))
fds.Str(DEBUG, String(" degrees"))
fds.tx(W_PORT, 13)
fds.tx(DEBUG, 13)
pause(5000)
fds.Str(W_PORT, String("Temperature Sensor 2= "))
fds.Str(DEBUG, String("Temperature Sensor 2= "))
data[2] := fstr.FloatToString(f32.FMul((f32.FSub((f32.FMul(f32.FFloat(adc.in(2)), mcp3208ratio)), 0.50)), 100.0))
fds.Str(W_PORT, data[2])
fds.Str(DEBUG, data[2])
fds.Str(W_PORT, String(" degrees"))
fds.Str(DEBUG, String(" degrees"))
fds.tx(W_PORT, 13)
fds.tx(DEBUG, 13)
pause(5000)
fds.Str(W_PORT, String("Temperature Sensor 3= "))
fds.Str(DEBUG, String("Temperature Sensor 3= "))
data[3] := fstr.FloatToString(f32.FMul((f32.FSub((f32.FMul(f32.FFloat(adc.in(3)), mcp3208ratio)), 0.50)), 100.0))
fds.Str(W_PORT, data[3])
fds.Str(DEBUG, data[3])
fds.Str(W_PORT, String(" degrees"))
fds.Str(DEBUG, String(" degrees"))
fds.tx(W_PORT, 13)
fds.tx(DEBUG, 13)
pause(5000)
PRI Pause(ms)
waitcnt(clkfreq / 1000 * ms + cnt) ' Convert to mS

Comments
PUB ReadDB SetIdle5 ser.str(1, string("**READING DB**")) ser.tx(1, CR) ex 'exit to clear anything going on or if stuck wifly ex ser.str(1, string("---")) ser.str(2, string("$$$")) Viewreply ser.str(2, string("open mywebserver.com 80")) ser.tx(2, CR) Viewreply Viewreply Viewreply ser.str(2, string("GET /readdb.php?")) ser.str(2, string("TABLE=table1")) 'DB table name ser.str(2, string("&ID=1")) 'system ID ser.str(2, string("&SERVER=xxxxxxxx.db.xxxxxx.wherethedbislocated.com")) '' DB server ser.str(2, string("&DBU=xxxxxxxdb")) ''DB username ser.str(2, string("&DBP=xxxxxxpassword")) 'DB password ser.str(2, string(" HTTP/1.1")) 'http GET terminator ser.tx(2, CR) ser.tx(2, LF) ser.str(2, string("Host: mywebserver.com")) ser.tx(2, LF) ' two LF required ser.tx(2, LF) ' CR or LF works TESTED RecWifiData 'the web page gets the db and forwards the data back to the wifly via an echo from php ParseDBReply 'parse and act on commands ser.str(1, string("**READ DB DONE**")) ser.tx(1, CR) SetIdle2 PUB ParseWifly |ii, val ii := 1 response[0] := ser.rxtime(2, 2000) 'waits here for first byte response for 2 seconds Repeat 1023 'look for more, else return Val := ser.rxtime(2, 10) 'read more, wait only 2 ms if no byte found response[ii] := Val ii++ PUB WriteDBall 'write the standard values to the DB SetIdle5 ReadContacts 'get some data from prop inputs ReadMem ser.str(1, string("**Write DB All**")) ser.tx(1, CR) FormatIP ex ex ser.str(1, string("---")) ser.str(2, string("$$$")) Viewreply ser.str(2, string("open mywebserver.com 80")) ser.tx(2, CR) Viewreply Viewreply ser.str(2, string("GET /writedball.php?")) ser.str(2, string("TABLE=table1")) 'DB table name ser.str(2, string("&ID=1")) 'system ID ser.str(2, string("&SERVER=xxxxxxxx.db.xxxxxxxx.mywebserver.com")) '' DB server ser.str(2, string("&DBU=xxxxxxxx")) ''DB username ser.str(2, string("&DBP=xxxxxxxx")) ''DB password ''/// send all status parameters ser.str(2, string("&REMIP=")) ' ser.str(2, @IP) 'send the complete IP in one string from array ser.str(2, string("&TIMER=")) ser.str(2, timer.showTimer) '' insert the running timer value here ser.str(2, string("&C1=")) ser.hex(2, C1S,1) ser.str(2, string("&C2=")) ser.hex(2, C2S,1) ser.str(2, string("&C3=")) ser.hex(2, C3S,1) ser.str(2, string("&C4=")) ser.hex(2, C4S,1) ser.str(2, string("&CM1=")) ser.hex(2, CM1S,1) ser.str(2, string("&CM2=")) ser.hex(2, CM2S,1) ser.str(2, string("&CM3=")) ser.hex(2, CM3S,1) ser.str(2, string("&CM4=")) ser.hex(2, CM4S,1) ser.str(2, string("&DVS=")) ser.hex(2, DVS,1) ser.str(2, string("&MIFI=")) ser.hex(2, MIFIselect, 1) ser.str(2, string(" HTTP/1.1")) 'http GET terminator ser.tx(2, CR) ser.tx(2, LF) ser.str(2, string("Host: mywebserver.com")) ser.tx(2, LF) ' two LF required ser.tx(2, LF) ' CR or LF works TESTED Viewreply Viewreply ser.str(1, string("**Write DB DONE**")) ser.tx(1, CR) SetIdle2read db <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Test</title> </head> <body> <br> <?php $con = mysql_connect($_REQUEST[SERVER],$_REQUEST[DBU],$_REQUEST[DBP]); if (!$con) //Send back some parameters to the device echo "A="; // See if the connection to the DB was made if (!$con) { die('Could not connect: ' . mysql_error()); } if ($con) { echo "CONNECTED"; } mysql_select_db($_REQUEST[DBU], $con); $result = mysql_query("SELECT * FROM $_REQUEST[TABLE]"); while($row = mysql_fetch_array($result)) { echo "DVRRECOFF="; echo $row['DVRRECOFF']; echo "<br>"; echo "DVRON="; echo $row['DVRPWRON']; echo "<br>"; echo "DVROFF="; echo $row['DVRPWROFF']; echo "M1BOOT="; echo $row['M1BOOT']; echo "M2BOOT="; echo $row['M2BOOT']; echo "WIFLYBOOT="; echo $row['WIFLYBOOT']; echo "<br>"; echo "<br>"; echo "CLOSED" . mysql_error(); } mysql_close($con); ?> <br> <br> </body> </html> write db <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>DB Test</title> </head> <body> <br> <?php $con = mysql_connect('$_REQUEST[SERVER]','$_REQUEST[DBU]','$_REQUEST[DBP]'); if (!$con) { die('Could not connect: ' . mysql_error()); } //echo "DBU="; //echo $_REQUEST[DBU]; //echo " "; if ($con) { echo "CONNECTED"; } //echo "DBU="; //echo $_REQUEST[DBU]; //echo " "; mysql_select_db("xxxxxxxx", $con); mysql_query("UPDATE table1 SET REMIP='$_REQUEST[REMIP]' WHERE ID='$_REQUEST[ID]'"); mysql_query("UPDATE table1 SET TIMER='$_REQUEST[TIMER]' WHERE ID='$_REQUEST[ID]'"); mysql_query("UPDATE table1 SET DVREC='$_REQUEST[DVRREC]' WHERE ID='$_REQUEST[ID]'"); mysql_query("UPDATE table1 SET DVON='$_REQUEST[DVON]' WHERE ID='$_REQUEST[ID]'"); mysql_query("UPDATE table1 SET DVOFF='$_REQUEST[DVOFF]' WHERE ID='$_REQUEST[ID]'"); mysql_query("UPDATE table1 SET CONTACT1='$_REQUEST[C1]' WHERE ID='$_REQUEST[ID]'"); mysql_query("UPDATE table1 SET CONTACT2='$_REQUEST[C2]' WHERE ID='$_REQUEST[ID]'"); mysql_query("UPDATE table1 SET CONTACT3='$_REQUEST[C3]' WHERE ID='$_REQUEST[ID]'"); mysql_query("UPDATE table1 SET CONTACT4='$_REQUEST[C4]' WHERE ID='$_REQUEST[ID]'"); mysql_query("UPDATE table1 SET CONTACT5='$_REQUEST[C5]' WHERE ID='$_REQUEST[ID]'"); //ID is in case there are multiple systems, use the entry for the specific system echo "CLOSED" . mysql_error(); mysql_close($con); ?> <br> <br> </body> </html>Dear T Chap,
First of all thank you for sharing your thoughts and suggestions and collaborating with me. I want to show my progress and be as detailed as possible so we all can benefit. I setup a mysql database with the following configuration:
Username:luxadmin
Password:jm45561031170
Database name:readings
Table name inside database readings:sensorvalues
So inside mysql I performed the following:
> CREATE DATABASE readings;
> USE readings;
> CREATE USER 'luxadmin'@'localhost' IDENTIFIED BY 'jm45561031170';
> GRANT ALL PRIVILEGES ON readings.* TO 'luxadmin'@'localhost';
> FLUSH PRIVILEGES;
> quit
$ mysql -u logger -p -h localhost
Enter password:
> USE readings;
Since I was going to read 4 sensors, and they were in integer form, I created the table with the following command:
>CREATE TABLE sensorvalues( measurement_id INT NOT NULL AUTO_INCREMENT,date DATE NOT NULL,time TIME NOT NULL,analog0 INT NOT NULL, analog1 INT NOT NULL, analog2 INT NOT NULL, analog3 INT NOT NULL, PRIMARY KEY ( measurement_id ));
So that took care of creating the database and the table were the values will be stored.
In the php side (I want to mention that this is my first time using php):
Since at this point I only want to write to the database, I went to the folder where the index.html file was (/var/www/html) and renamed it to index.php and entered the following code:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>DB Test</title> </head> <body> <br> <?php $con = mysql_connect('$_REQUEST['192.168.2.75']','$_REQUEST['luxadmin']','$_REQUEST['jm45561031170']'); if (!$con) { die('Could not connect: ' . mysql_error()); } //echo "DBU="; //echo $_REQUEST[DBU]; //echo " "; if ($con) { echo "CONNECTED"; } //echo "DBU="; //echo $_REQUEST[DBU]; //echo " "; mysql_select_db("readings", $con); mysql_query("UPDATE sensorvalues SET analog0='$_REQUEST[analog0]' WHERE ID='$_REQUEST[1]'"); mysql_query("UPDATE sensorvalues SET analog1='$_REQUEST[analog1]' WHERE ID='$_REQUEST[1]'"); mysql_query("UPDATE sensorvalues SET analog2='$_REQUEST[analog2]' WHERE ID='$_REQUEST[1]'"); mysql_query("UPDATE sensorvalues SET analog3='$_REQUEST[analog3]' WHERE ID='$_REQUEST[1]'"); //ID is in case there are multiple systems, use the entry for the specific system echo "CLOSED" . mysql_error(); mysql_close($con); ?> <br> <br> </body> </html>Finally in the propeller side:
I noticed that in the spin code that you provided, several methods like SetIdle5, Viewreply, SetIdle2 and others were missing so I omitted them. This is the code I came up with:
CON _clkmode = xtal1 + pll16x _xinfreq = 5_000_000 CON DEBUG = 0 ' Debug port sends results to Parallax Serial Terminal W_PORT = 1 ' Wireless Module W_RESET = 0 ' To RST pin on Wireless Module W_TX = 15 ' To RX pin on Wireless Module W_RX = 16 ' To TX pin on Wireless Module W_CTS = 3 ' To RTS pin on Wireless Module W_RTS = 4 ' To CTS pin on Wireless Module BAUD = 9600 ' Baud Rate 9600 bps mcp3208ratio = 0.0008056640625 'RATIO TO CALCULATE TEMPERATURE VAR long data[4], stack[50] OBJ adc : "MCP3208" fds : "FullDuplexSerial4port" dio : "dataIO4port" f32 : "F32" fstr : "FloatString" CON dpin = 12 cpin = 13 spin = 14 PUB main fds.Init fds.AddPort(W_PORT, W_RX, W_TX, W_CTS, W_RTS, 0, %000000, BAUD) fds.AddPort(DEBUG, 31, 30, -1, -1, 0, %000000, BAUD) ' Debug to the terminal screen fds.Start ' Start the ports f32.start Pause(200) ' UART startup delay adc.start(dpin, cpin, spin, 255) fstr.SetPrecision(4) cognew(wiflytodb, @stack) PUB wiflytodb 'write the standard values to the DB repeat fds.str(DEBUG, string("---")) fds.str(W_PORT, string("$$$")) fds.tx(W_PORT, 13) fds.str(W_PORT, string("open 192.168.2.75 80")) fds.tx(W_PORT, 13) fds.str(W_PORT, string("GET /var/www/html/index.php?")) fds.str(W_PORT, string("TABLE=sensorvalues")) 'DB table name fds.str(W_PORT, string("&ID=1")) 'system ID fds.str(W_PORT, string("&SERVER=192.168.2.75")) '' DB server fds.str(W_PORT, string("&DBU=luxadmin")) ''DB username fds.str(W_PORT, string("&DBP=jm45561031170")) ''DB password ' data[0] := adc.in(0) fds.Str(DEBUG, String("adc channel 0= ")) dio.dec(DEBUG, data[0]) fds.str(W_PORT, string("&analog0=")) dio.dec(W_PORT, data[0]) fds.tx(W_PORT, 13) fds.tx(DEBUG, 13) pause(5000) data[1] := adc.in(1) fds.Str(DEBUG, String("adc channel 1= ")) dio.dec(DEBUG, data[1]) fds.str(W_PORT, string("&analog1=")) dio.dec(W_PORT, data[1]) fds.tx(W_PORT, 13) fds.tx(DEBUG, 13) pause(5000) data[2] := adc.in(2) fds.Str(DEBUG, String("adc channel 2= ")) dio.dec(DEBUG, data[2]) fds.str(W_PORT, string("&analog2=")) dio.dec(W_PORT, data[2]) fds.tx(W_PORT, 13) fds.tx(DEBUG, 13) pause(5000) data[3] := adc.in(3) fds.Str(DEBUG, String("adc channel 0= ")) dio.dec(DEBUG, data[0]) fds.str(W_PORT, string("&analog0=")) dio.dec(W_PORT, data[0]) fds.tx(W_PORT, 13) fds.tx(DEBUG, 13) fds.str(W_PORT, string(" HTTP/1.1")) 'http GET terminator fds.tx(W_PORT, 13) fds.tx(W_PORT, 10) fds.str(W_PORT, string("Host: 192.168.2.75")) fds.tx(W_PORT, 10) ' two LF required fds.tx(W_PORT, 10) ' CR or LF works TESTED fds.str(DEBUG, string("**Write DB DONE**")) fds.tx(DEBUG, 13) pause(5000) PRI Pause(ms) waitcnt(clkfreq / 1000 * ms + cnt) ' Convert to mSI am capable of seeing the values in the PST and using Comm Operator for the Wifly, but the $$$ command or any commands are NOT being executed by the
Wifly unit if that is the intention. I am attaching the full project, I think I am missing methods.
Please T Chap and forum help me.
Thank you.