Back to Thermistor and Solar Cell by John Schimmel


'XPORT TO PHP (TO SQL)
'the pic will read in a thermistor value then
' using the xport will talk to a webserver.
' Inside the message to the webserver will be
' the value of the thermistor.
' for example: www.site.com/form.php?value=123

'Xport settings baudrate 9600, I/F mode 4C, Flow 00
' PORT 10001
' Remote IP address <not set>
' connect mode D4 (announce when connection made), PORT 00000
' disconnect mode 00
' flush mode 00   -this is important

'code by Tom Igoe
'modified by John Schimmel


include "modedefs.bas"

'DEFINE ADCIN VARIABLES
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)

'serial communication variables
'PINS
tx      var portc.6 'send data to the serial port
rx      var portc.7 'receive data from the serial port
xPortTX var portc.3 'send data to the xport, goes to the RX pin of Xport
xPortRX var portc.2 'receive data from the Xport, goes to the TX pin of XPort

'data rates for serial
'two serial modes are used.
inv9600 con 16468 'for normal pic <--> computer terminal communication
non9600 con 84      'for pic <--> xport communication

httpPin var portb.7 'leds for http status
TCPPin  var portb.6
output TCPPin
output httpPin

'variables
thermistor  var word
solar       var word
inByte      var byte
connected   var bit

TRISA = %11111111 'set porta to all input
ADCON1 = %10000010 'set porta analog and right justify result

pause 2000 ' let xport get ip address

main:
    'attempt to connect to the server
    gosub xport_connect

    pause 5000 'dont overload the server

goto main

xport_connect:
    'wait for "C" byte to come back from xport
    while <> 67
        'send debug message before xport message
        serout2 tx, inv9600, ["attempting to connect to the server.",10,13]
        serout2 tx, inv9600, ["C128.122.253.189/80",10,13]

        'now xport serial
        serout2 xPortTX, non9600, ["C128.122.253.189/80",10]

        serin2 xPortRX, non9600,[inByte]
        serout2 tx, inv9600, ["Received from connection request:",inByte,10,13]
    wend

    if inByte = 67 then
        high TCPPin
        gosub http_request
    else
        goto main
    endif

return


http_request:
    'get thermistor and solar values
    adcin 0, thermistor
    adcin 1, solar


    'turn on httppin
    high httpPin

    'write debug messages before xport serial messages
    'next 4 lines for debugging only
    SEROUT2 tx, inv9600,["GET /~js3646/datalogger/nyulogger.php?"]
    SEROUT2 tx, inv9600,["temp=", DEC thermistor,"solar=",DEC solar]
    SEROUT2 tx, inv9600,[" HTTP/1.1",10,13]
    SEROUT2 tx, inv9600,["HOST: itp.nyu.edu,10,13,10,13]

    'xport request
    SEROUT2 xPortTX, non9600,["GET /~js3646/datalogger/nyulogger.php?"]
    SEROUT2 xPortTX, non9600,["temp=", DEC thermistor,"solar=",DEC solar]
    SEROUT2 xPortTX, non9600,[" HTTP/1.1",10]
    SEROUT2 xPortTX, non9600,["HOST: itp.nyu.edu,10,10]

    WHILE inByte <> 0
        SERIN2 xPortRX, non9600, [inByte]
    WEND

    'NOW we're disconnected
    LOW tcpPin

return