Some code:

Examples of computer programming in Perl, HTML, Javascript, as well as SitePlayer and PICAXE programming code.



Code to enable UDP data transfer from SitePlayer to PC

SitePlayer "index.htm" code

<html><body>
<br>

<form action="udp.spi">
<input size=16 type="text" name="udptext">
<input type="hidden" name="udpsend" value=1>
<input type="submit" value="Send UDP Text!">
</form>

<br>

<a href="udp.spi?udptext=Steve!&udpsend=1">Send UDP!</a>
<br>
</body></html>

SitePlayer "udp.spd" code

$Devicename "Siteplayer1"
$DHCP off
$DownloadPassword "password"
$SitePassword ""
$InitialIP "192.168.0.10"     
$Sitefile "d:\my documents\siteplayer\udp\udp.spb"
$Sitepath "d:\my documents\siteplayer\udp"

org 0020h
udptext db "Steve was here!"

org 02D0h
udpmac1 db FFh  ;mac address of receiver 
udpmac2 db FFh  ;mac (FF = broadcast)
udpmac3 db FFh  ;mac
udpmac4 db FFh  ;mac
udpmac5 db FFh  ;mac
udpmac6 db FFh  ;mac

udpip1  db 192  ;ip address of receiver
udpip2  db 168  ;ip
udpip3  db 0    ;ip
udpip4  db 255  ;ip (255 = broadcast)

udpport dw 26482  ;UDP port
udpdata dw 0020h  ;Address of data to send
udpcount dw 16    ;How many bytes to send (1-768)

org FF1Eh
UDPsend ds 1


SitePlayer "udp.spi" code

HTTP/1.0 302 Found
Location: /index.htm

UDP server (receiver) Perl code to run on your PC

##

use IO::Socket::INET; 
# from: http://search.cpan.org/src/GBARR/IO-1.2301/IO/Socket/INET.pm 
# put it in your Perl \lib\io\socket\ folder.
$MySocket=new IO::Socket::INET->new(LocalPort=>26482,Proto=>'udp');

print "\n\nwaiting...\n\n";
$MySocket->recv($text,128);
open(FILE,">>udp.txt");
print FILE "$text\n";
close(FILE);
print "$text\n";



Client code (in Perl) to send UDP from one PC to another - works with above UDP server code.
##

use IO::Socket::INET;
$MySocket=new IO::Socket::INET->new(PeerPort=>26482,Proto=>'udp',PeerAddr=>'192.168.0.255');

$msg="okok!";
$MySocket->send($msg);
print "\n\n $msg sent.\n\n";


Perl code to send UDP to SitePlayer.
##

use IO::Socket::INET;
$MySocket=new IO::Socket::INET->new(PeerPort=>26482,Proto=>'udp',PeerAddr=>'192.168.0.255');
print "\nworking...\n\n";
Use the above "udp.spd" code example,
but in order to receive UDP on SitePlayer,
make sure it also contains:
org FF20h
UDPrecv db 1
UDP on Siteplayer must be activated externally
via serial port command such as
serout 4,T9600,($90,$20,$ff,1)
or http link such as
http://192.168.0.10/udp.spi?udprecv=1
$text="Steve was here!"; $spaddr1=0; $spaddr2=0; $tl=length($text); $msg=chr($tl).chr(255-$tl); $msg.=chr($spaddr1).chr($spaddr2); $msg.=$text; $msg.=chr(0).chr(0); if($MySocket->send($msg)) { print length($msg)." bytes sent."; }

Perl code to 'get' the contents of SitePlayer (or any) webpage,
send text out SP serial port, and update an internal SP variable (^udptext).
##

use LWP::Simple qw($ua get);
use Net::SMTP; # get SMTP.pm from http://search.cpan.org/~gbarr/libnet-1.19/Net/SMTP.pm 
               # place the 'Source' in your PERL NET folder.
$sercode="12345";
$intcode="abcdef";
$site="http://192.168.0.10/test.spi?com=$sercode&udptext=$intcode";
$content = get($site);
print "\n\n$content\n";

If your SitePlayer *.spd file contains:

org ff00h
p1 ds 1  ;Port 1, all 8 bits

and your SitePlayer index.htm file contains:
^p1

the status of the parallel port (or any other internal SP variable 
which appears on the index.htm page) will be acquired by the above 
Perl code, in the $content variable.

PICAXE-08M code to communicate with MAX7219
(Serially Interfaced, 8-Digit, LED Display Driver)
  symbol DIN  = 0
  symbol LOAD = 2
  symbol CLK  = 4

  low DIN
  low LOAD
  low CLK

  b1=$09  'decode mode
  b0=$ff  '=yes, all digits=BCD 
  gosub shiftout
  b1=$0a  'intensity
  b0=$0f  '=max
  gosub shiftout
  b1=$0b  'scan limit
  b0=$03  '=4 digits
  gosub shiftout
  b1=$0c  'shutdown mode
  b0=$01  '=normal
  gosub shiftout:
  b1=$0f  'display test
  b0=$01  '=all on
  gosub shiftout
  b1=$0f  'display test
  b0=$00  '=normal
  gosub shiftout
  b1=1:b0=0  'digit 1 shows "0"
  gosub shiftout
  b1=2:b0=0  'digit 2 shows "0"
  gosub shiftout

  b4=76 'number to be displayed 

  b1=3:b0=b4/10   'digit 3 shows "7"
  gosub shiftout
  b1=4:b0=b4//10  'digit 4 shows "6"
  gosub shiftout

  stop

shiftout:
  for b5=1 to 16
  w6=w0 & $8000
  low DIN
  if w6=0 then bit_is_low
  high DIN  
bit_is_low:
  pulsout CLK,5
  w0=w0*2  '<< (shift bits left) 
  next b5
  pulsout LOAD,5
  return


Perl code to communicate with the Windows serial port.
This examples communicates with a modem on COM1:
# download "SerialPort.pm" to the Perl /WIN32/ lib
# download "CommPort.pm" to the Perl /WIN32API/ lib
# install "Win32-API" using "ppm" from DOS

use Win32::SerialPort; 
$com="COM1";  # no colon here!
$PortObj=Win32::SerialPort->new($com);
$PortObj->baudrate(9600);
$PortObj->databits(8);
$PortObj->parity("none");
$PortObj->stopbits(1);
$PortObj->handshake("none");
$PortObj->write_settings;

$send="AT\r";
$PortObj->write($send);

sleep 1; #delay for reply to be sent

$result=$PortObj->input;
print "$result";

$PortObj->close;
undef $PortObj;

Perl code to 'get' a webpage from the internet.
This is more reliable than the "LWP::Simple" technique.
##

print "Content-type: text/html\n\n";

use LWP::UserAgent;
use HTTP::Request;
use HTTP::Response;
$browser=LWP::UserAgent->new();
$browser->timeout(5);

$site="http://yahoo.com";
&getit;
print $content;

sub getit {
  $request = HTTP::Request->new(GET => $site);
  $response = $browser->request($request);
  $content = $response->content();
}



PICAXE-18X code to communicate with MAX7301 4-wire interfaced 20-port I/O expander
  symbol DOUT=input6  ;<-MAX7301
  symbol CSEL=output6 ;chip select (load)
  symbol DIN =output5 ;->MAX7301
  symbol CLK =output4 ;clock
  low CSEL:low DIN:low CLK

  wait 1 'system stabilize

  b1=$04 'config
  b0=$01 'NORMAL
  gosub shiftout
  b1=$09  'ports 4-7  = virtual/unused
  b0=%01010101   '01=OUTPUT so won't float
  gosub shiftout
  b1=$0a  'ports 8-11 = virtual/unused
  b0=%01010101   '01=OUTPUT so won't float
  gosub shiftout
  b1=$0b 'ports 12-15
  b0=%01010101   '01=OUTPUT, 10 or 11=INPUT 
  gosub shiftout '    (11=internal pullups)
  b1=$0c 'ports 16-19
  b0=%11111111  'inputs
  gosub shiftout
  b1=$0d 'ports 20-23
  b0=%11111111  'inputs
  gosub shiftout
  b1=$0e 'ports 24-27
  b0=%11111111  'inputs
  gosub shiftout
  b1=$0f 'ports 28-31
  b0=%10101010  'inputs
  gosub shiftout
  setfreq m8  'run at 8MHz instead of 4MHz

loop:
  b1=$2c 'port 12  'Just toggling
  b0=$00 'set LO   'these ports to
  gosub shiftout   'flash some LEDs.
  b1=$2d 'port 13
  b0=$01 'set HI
  gosub shiftout
  b1=$2c 'port 12
  b0=$01 'set HI
  gosub shiftout
  b1=$2d 'port 13
  b0=$00 'set LO
  gosub shiftout

  b1=$d0 'read input ports 16-23 (8 bits)
  gosub shiftout 'must shiftout before shiftin 
  gosub shiftin  'b0 is the returned value

  sertxd ("ok:",#b0," ") '9600 baud if @ 8MHz

  goto loop

shiftout:
  for b10=1 to 16
    w6=w0 & $8000  'mask; (w0=b1:b0)
    low DIN
    if w6=0 then bit_is_low
    high DIN  
bit_is_low:
    pulsout CLK,5
    w0=w0*2  '<< shift bits left
  next b10
  pulsout CSEL,5
  return

shiftin:
  w0=0
  for b10=1 to 16
    w0=w0*2 '<< shift bits left
    if DOUT=0 then skipMSB
    w0=w0+1
skipMSB:
    pulsout CLK,5
  next b10
  return

Perl code to display an image (using MS-Windows)
##

print "Content-type: image/gif\n\n";
$pic="../pics/bart.gif";
open(PIC, $pic);
binmode STDOUT; 
print <PIC>;
close(PIC);
Simple Perl code to read-from and write-to a file
##

print "Content-type: text/plain \n\n";

open(FILE,"<file.txt");
@data=<FILE>;
close(FILE);

print @data;

open(FILE,">>file.txt");
print FILE "appended data!\n";
close(FILE);

Javascript code to extract 'form' or 'search' parameters from page URL

With example: http://webpage.com?name=jessica&sex=female&age=27

This script prints:

jessica
female
27
<script>
  ls=unescape(location.search.substr(1));
  ls=ls.split("&")
  for(x=0;x<ls.length;x++) {
    t=ls[x].split("=")
    eval("_"+t[0]+"='"+t[1]+"'")
  }
  document.write(_name+"<br>");
  document.write(_sex+"<br>");
  document.write(_age+"<br>");
</script>


Sample .htaccess file - redirect domain to a folder or another domain.

ErrorDocument 404 /error404.html

RewriteEngine On

RewriteCond %{HTTP_HOST} ^(www\.)?somedomain\.com$ [NC]
RewriteCond %{REQUEST_URI} !sdomain/
RewriteRule ^(.*)$ sdomain/$1 [L]

RewriteCond %{HTTP_HOST} ^(www\.)?thisdomain\.com$ [NC]
RewriteRule ^ http://anotherdomain.com%{REQUEST_URI} [R,L]



Last updated May 2009
More stuff coming soon!

Google
HOME
Send email to: