'***************************************************************** '* Name : xport_hello.pbp * '* Author : Patrick Dwyer * '* Notice : Copyright (c) 2006 * '* : Licensed under GPL * '* : http://www.opensource.org/licenses/gpl-license.php * '* Date : 2/14/2006 * '* Version : 1.0 * '* Notes : * '* : * '***************************************************************** ' We are using a 20Mhz oscillator DEFINE OSC 20 INCLUDE "modedefs.bas" ' 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 ' We communicate with the XPort using 9600 8N1 serial true9600 con 84 inv9600 con 16468 ' Our serial communication pins tx var PORTC.6 rx var PORTC.7 ' Our blinking pin for status messages for LED statusPin var PORTC.4 OUTPUT statusPin ' Used to read response from the XPort, need a single byte value inByte var byte potH VAR WORD ' Create variable to store result potV VAR WORD ' Create variable to store result TRISA = %11111111 ' Set PORTA to all input ADCON1 = %10000010 ' Set PORTA analog and right justify result Pause 500 ' Wait .5 second ' Track whether or not we are connected to the remote server, BIT value, separate routines 'connect the pin to Xport, website to fresh anew, building a psuedo webbrowser inside the PIC connected var bit connected = 0 ' Turn on our LED so we know that the startup sequence is going high statusPin ' Wait for the XPort to boot up pause 5000 ' Turn off the status LED while operating LOW statusPin ' Just so we know that we're ready, we'll quickly blink the status LED, to check program, ready light counter var byte counter = 0 while counter < 4 high statusPin pause 100 low statusPin pause 100 counter = counter + 1 wend ' In our main loop we need to accomplish the following: ' 1. Connect to the remote server ' 2. Send data to the remote server ' 3. Pause so we don't continually send data ' We use the xport_connect method to establish our connection, ' and the http_request method to send our data to a remote script main: 'connected, sending bits or not if connected = 1 then ' If we're already connected then we can send our message 'read in data from pots ADCIN 0, potH ADCIN 1, potV 'serout2 tx, inv9600, ["potH= ",DEC potH,13, 10] ' print it to serial out, gosub http_request ' back off the server for a few seconds pause 400 else ' try and connect gosub xport_connect endif goto main 'like subroutines in PIC xport_connect: ' Send our connection string to the XPort. This string contains ' the remote IP and the remote port. In this case we're connecting ' to digilutionary.com on port 80 (HTTP) '/80 is worldwideweb 'C : telling Xport to connect serout2 tx, true9600, ["C128.122.253.189/80", 10] ' now we're connected connected = 1 return http_request: ' light LED to indicate HTTP GET request in progress: high statusPin ' To send data to the server we need to create a valid HTTP request ' header. All of the data we transmit here will go to the remote ' server. ' The first line tells the server that we want to get a certain ' page, and includes the url parameters that we want to pass to ' our script. ' The second line tells the remote server what format the header ' we're sending is in. ' The third line tells the server that the domain we want to be ' talking too is digilutionary.com ' The next line identifies what type of device is requesting data. ' Finally we send two newline characters, which tells the server that ' our request header is finished, at which point it runs our remote script 'instead of the message, can use variables, to input pots, buttons etc SEROUT2 TX, true9600, ["GET /~gl637/cgi-bin/write1.py?user=player1&msg=",Dec potH,":",dec potV, 10, 13] serout2 tx, true9600, [" HTTP/1.1", 10] SEROUT2 tx, true9600, ["HOST: itp.nyu.edu", 10] SEROUT2 tx, true9600, ["User-Agent: PIC/XPort"] serout2 tx, true9600, [10, 10] ' wait for bytes from server: ' Our server sends a 0 to finish while inByte <> 0 serin2 rx, true9600, [inByte] wend ' now we're disconnected: connected = 0 ' turn off LED, since GET request is complete: low statusPin return