April 5, 2008
WikiConnecting XPort to a Remote Site
Connecting XPort to a Remote Site
I have set up a php file on my server which scrapes weather data from national weather site. It spits out the data like this: “>somenumber”. Here is the php code:
(Also the command for copying file from my local hdd thinking I am in the folder that my file resides to a remote server(dreamhost) is just like this:
scp file.ext username@domain.com:/home/username/somedir/file.ext
<?php
/*
getting current weather data
from central park
and displaying it
on the page for later use!
*/
// getting xml data from national weather service.
$url = 'http://www.weather.gov/data/current_obs/KNYC.xml';
$host = 'www.weather.gov';
$page = '/data/current_obs/KNYC.xml';
// calling read_file_contents functions which returns site as a string.
$st = READ_FILE_CONTENTS_NEW($host,$page);
// small regex to match the line we are looking for <temp_f></temp_f>
if (preg_match_all("#<temp_f(.*?)</temp_f>#s",$st,$result)) {
// small trimming off the '<' bracket.
$t = trim($result[1][0],">");
echo("< result: $t >");
//echo "dede";
}
function READ_FILE_CONTENTS_NEW($host,$page) {
$fp = fsockopen($host,80,$errno,$errdesc);
if(!$fp) {
die("Couldn't connect to $host");
}
$request = "GET $page HTTP/1.0\r\n";
$request .= "Host: $host\r\n";
$request .= "Referer: http://www.google.com/";
$request .= "User-Agent: PHP test client\r\n\r\n";
$output = "";
$page = array();
fputs($fp,$request);
while(!feof($fp)) {
$output = $output.fgets($fp,1024);
}
return $output;
}
?>
So what I am doing for a test is I am connecting to my server and calling this php file through the code and printing out the results. Here is the processing code:
/*
Lantronix serial-to-ethernet HTTP request tester
Language: Processing
This program sends serial messages to a Lantronix serial-to-ethernet device
to get it to connect to a remote webserver and make an HTTP request. To use this
program,
connect your PC to the Lantronix module’s serial port.
*/
// include the serial library
import processing.serial.*;
Serial myPort; // Serial object
int step = 0; // keeps track of which step in the process you're on
char linefeed = 10; // ASCII linefeed character
void setup() {
// get the list of serial ports:
println(Serial.list());
// open the serial port apprropriate to your computer:
myPort = new Serial(this, Serial.list()[2], 9600);
// configure the serial object to buffer text until it
// receives a linefeed character:
myPort.bufferUntil(linefeed);
}
void draw()
{
//no action in the draw loop
}
void serialEvent(Serial myPort) {
// print any string that comes in serially to the monitor pane
print(myPort.readString());
}
void keyReleased() {
// if any key is pressed, take the next step:
switch (step) {
case 0:
// open a connection to the server in question:
myPort.write("C64.111.102.167/80\r");
// add one to step so that the next keystroke causes the next step:
step++;
break;
case 1:
// send a HTTP GET request
myPort.write("GET /scraper2.php HTTP/1.1\n");
myPort.write("HOST:klaweht.com\n\n");
step++;
break;
}
}
After I make sure this is working, I go ahead and test the Arduino code in Tom’s book. I have spent like A LOT again for this to work. I could make it work at some point, but I had a hard time with reset button. Still do. It looks like I cannot reset the xport through its reset pin. So Nick helped me to set up a transistor between the ground pin of xport and normal ground. Also getting the power from the arduino pin that has been set for xport’s reset pin. That worked ok. Only after I was trying to reset the xport through serial and everything is screwed up again and I couldn’t even make the connection to the remote site and I am once again behind where I started!
Couple of notes: Don’t try to program the arduino when it is serially connected to Xport and your chip is already programmed to transfer serial data. IT screws up arduino and you cannot program it.
Continue Reading
Back to Archive