Xport to SQL

This program connects a microcontroller to a mySQL database via a Lantronix Xport and a PHP script.
The PHP script it uses is the SQL_datalog.php script in the Network Data Logging Suite.

Technorati Tags: ,



The relevant Xport settings for this application are as follows:

*** Channel 1
Baudrate 9600, I/F Mode 4C, Flow 00
Port 10001
Remote IP Adr: --- none ---, Port 00000
Connect Mode : D4
Disconn Mode : 00
Flush   Mode : 00

*** Expert
TCP Keepalive    : 45s
ARP cache timeout: 600s
High CPU performance: disabled
Monitor Mode @ bootup : enabled
HTTP Port Number : 80
SMTP Port Number : 25

The code:

' Xport_to_SQL
'    Microcontroller is connected to a Lantronix Xport
'    serial-to-ethernet device. This program connects
'    to a HTTP server through the Xport, makes a HTTP GET
'    request for a PHP script, and sends the value
'    of an analog sensor through as a part of the
'    GET request.
    
'    Xport communicates at 9600-8-n-1 non-inverted (true) serial.
 
'    By Tom Igoe, 31 oct 2005
'    updated 25 Oct. 2005

 ' Define ADCIN parameters
DEFINE  ADC_BITS        10     ' Set number of bits in result
DEFINE  ADC_CLOCK       3         ' Set clock source (3=rc)
DEFINE  ADC_SAMPLEUS    50        ' Set sampling time in uS

'   pins to define serial RZX and TX
tx var portc.6
rx var portc.7
'   pins with LEDs to indicate whether we're connecting
'   or sending an HTTP request:
httpPin var portb.7
TCPPin var portb.6
output TCPPin
output httpPin

' constant to set the baud rate:
inv9600 con 16468
'   for non-inverted connections (PIC to Xport direct):
true9600 con 84

' define variables:
adcVar var word
inByte var byte
connected var bit

TRISA = %11111111       ' Set PORTA to all input
ADCON1 = %10000010      ' Set PORTA analog and right justify result

connected = 0

' pause to let Xport boot up:
pause 2000
 main:
  ' if you're connected to the server, then 
  ' make a HTTP call.  If not, then connect
  ' to the server:
  if connected = 1 then
  ' read sensors, convert to bytes:
      adcin 0, adcVar
      ' send HTTP GET request for php script:
    gosub http_request
  else
    ' attempt to connect to the server:
    gosub xport_connect
  endif
  tcpPin = connected
  ' pause so we're not overwhelming the server:
  pause 3000  
goto main


xport_connect:
    ' turn off LED to indicate HTTP GET is in progress:
    'low tcpPin
    '   wait for a "C" byte to come back:
    while inByte <> 67
        serout2 tx, true9600, ["C128.122.253.189/80", 10]
        serin2 rx, true9600, [inByte]
    wend
    ' turn on TCP pin to indicate that we connected:
    'high TCPPin
    connected = 1
return

http_request:
    ' light LED to indicate HTTP GET request in progress:
    high httpPin
    SEROUT2 TX, true9600, ["GET /~tqi6023/sql_datalog.php?action=insert&"]
    serout2 tx, true9600, ["sensorValue=", DEC adcVar]
    serout2 tx, true9600, [" HTTP/1.1", 10]
    serout2 tx, true9600, ["HOST: itp.nyu.edu", 10, 10]
    ' wait for bytes from server:
    ' php script sends a 0 to end transmission:
    while inByte <> 0
        serin2 rx, true9600, [inByte]
    wend
    ' now we're disconnected:
    connected = 0
    ' turn off LED, since GET request is complete:
    low httpPin
return 
This entry was posted in Lantronix, PicBasic Pro. Bookmark the permalink.

Comments are closed.