Analog Sensor Reading, RN-XV Wifly Connection and mySQL Database Setup
jmmb392
Posts: 22
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
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:
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:
I 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.