Shop OBEX P1 Docs P2 Docs Learn Events
Analog Sensor Reading, RN-XV Wifly Connection and mySQL Database Setup — Parallax Forums

Analog Sensor Reading, RN-XV Wifly Connection and mySQL Database Setup

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!!
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

  • T ChapT Chap Posts: 4,198
    edited 2017-04-11 02:40
    The mysql is pretty easy with a little poking around. On your webserver create a mysql database, establish the login and password. Set up the database with the fields and types of data that you want. For example name a row and then you can access that row by name. I cannot remember right off the top what I used for types of data in the fields, maybe something like ascii. Here is a snip of the SPIN that reads the database indirectly using a web page parked on a server. I did this in case the Prop is at a location and not accessible or may be sometimes unavailable but I want it to pick up messages when it gets back on. The Prop talks to a php web page parked on a server. The mysql is parked on a web server. The Prop asks the webpage php to get data or write data to/from the mysql. Using this same method, the Prop can be read a field from the db at some frequency looking for a command or opcode that you sent to the db via some method ie a web page that can send data to the db. You can create a simple page with buttons that write data to the db opcode field, ie Web page Button 1 = set field AA to 1. Prop reads that field and is told to do X. Button 2 writes AA to 2 then Prop reads it and does Y. The prop can write data to designated fields and the prop reads it as needed, and you can read the db from your web page as well if the prop wants to send you data. This is more work having the indirect web page doing the actual work on the DB but at the time I wanted the prop to be able to pick up instructions if I was not able to connect to the Prop at a specific time for any reason ie the Prop is offline due to out of range etc and then when it comes back online it gets the commands at once. The php received the GET with arguments from the prop>wifly the used the arguments to access the mysql, the mysql would reply with the data and then the php would send the data back to the prop>wifly as a reply/echo. I found that accessing the db with php to be very simple stuff and I had a hard time getting the prop>wifly>db to work but you may find it easier to go direct. I had specif reasons for this method. For one multiple people could pick up a message or send a command to the prop from any browser. Hope this helps figure out what you are trying to do.
    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)
        SetIdle2
    
    





  • PHP for readdb and writedb
    read 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>
    
  • Did you checkout WiFly server on the Obex.
  • T Chap wrote: »
    The mysql is pretty easy with a little poking around. On your webserver create a mysql database, establish the login and password. Set up the database with the fields and types of data that you want. For example name a row and then you can access that row by name. I cannot remember right off the top what I used for types of data in the fields, maybe something like ascii. Here is a snip of the SPIN that reads the database indirectly using a web page parked on a server. I did this in case the Prop is at a location and not accessible or may be sometimes unavailable but I want it to pick up messages when it gets back on. The Prop talks to a php web page parked on a server. The mysql is parked on a web server. The Prop asks the webpage php to get data or write data to/from the mysql. Using this same method, the Prop can be read a field from the db at some frequency looking for a command or opcode that you sent to the db via some method ie a web page that can send data to the db. You can create a simple page with buttons that write data to the db opcode field, ie Web page Button 1 = set field AA to 1. Prop reads that field and is told to do X. Button 2 writes AA to 2 then Prop reads it and does Y. The prop can write data to designated fields and the prop reads it as needed, and you can read the db from your web page as well if the prop wants to send you data. This is more work having the indirect web page doing the actual work on the DB but at the time I wanted the prop to be able to pick up instructions if I was not able to connect to the Prop at a specific time for any reason ie the Prop is offline due to out of range etc and then when it comes back online it gets the commands at once. The php received the GET with arguments from the prop>wifly the used the arguments to access the mysql, the mysql would reply with the data and then the php would send the data back to the prop>wifly as a reply/echo. I found that accessing the db with php to be very simple stuff and I had a hard time getting the prop>wifly>db to work but you may find it easier to go direct. I had specif reasons for this method. For one multiple people could pick up a message or send a command to the prop from any browser. Hope this helps figure out what you are trying to do.
    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)
        SetIdle2
    
    





    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 mS
    


    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.
  • Forgot to mention that the ip address of the Apache server is 192.168.2.75
  • I would not be able to review your code to see what is going on but here are some suggestions. You need to start with the basics and learn how to communicate with the device. I easily put 50 hours into figuring this stuff out that I am trying to show you but it was not a trivial thing. You have to understand the working operation of the wifly. I am not even sure what device you are using or if it was the same commands as mine, I think I used the Wifly RN134. $$$ is the method to enter CMD mode. When you send a command to the device you should get a response, and you must create a method to display the response. I used ViewReply to show what came back. So first thing you must be able to send it a something and get a reply. ie $$$ = response CMD to tell you what mode you are in. You should begin with learning to configure the settings in the wifly and show responses, then only after you have mastered that can you move to trying to communicate with the web server and db. I put my entire code and removed some personal info, but this all worked when I last was developing this 5 years ago. I use the 4 port serial so you can replace that with any full duplex serial if you want. You must configure the wifly, see ConfigWifly for how I was doing that. I suggest trying to echo and see the responses direct on your web page from the db, in other words learn how to write and read from the web page to the db, php is very simple to learn and you already have working code examples. There are plenty of php forums for help. I am not sure your goals, so what I am showing you could be more complex that what you are needing. You may want to try to restate your goals a simply as possible and see if others have better ideas what what I am showing you.
  • Here is another file that was working. The one I posted above seems to be not the final version but you should be able to see the basic concepts at work in both. I was using the Prop connected to the Wifly and a Mifi Hotspot for remote standalone operation. I advise you to start with tiny little bite size tests.
  • T Chap nice work on the Wifly. I've been toying with the RN-171 myself and getting to serve up html is a challenge to say the least. A command routine is a must where you enter command mode and read back the get commands, show lease, ip, reboot, exit etc. . Jmmb if you want something more easy to get up and running try the xbee wifi cloud kit. I have one and it's almost too easy it has a dashboard to read I/o analog etc. I jumped into the RN-171 for something a little more challenging. Almost the same price I think the single xbee wifi is about $29 and the RN $27.
  • The Roving networks WiFly app 2.0 is also an easy web server tool to get up and running . Free in the apple app store
  • I just ran the Wifly Web server V01 that is on the Obex and it works out of the box. It can be tricky you have to make sure your connected by way of RN-171 status led lights. Look at your returned ip address on the serial terminal and enter it in the explorer web browser. In my case it was Http://192.168.1.1:80/index.html. Don't the :80 or it doesn't work. This might be a good template for you because it already works. You can just tweek the html index string to get the page to look the way you want it.
Sign In or Register to comment.