/ / A Simple Interactive Forms
- Once you are connected to the internet, open the Fetch application
- Connect to the remote server
- Login to the remote server


- Login to the remote server
- Navigate to your web folder by double clicking on it.
- Open BBEdit
. - Create a new blank BBEdit document. Save the document as lightingState.txt
- Type a On in the document. Save.
- Upload the text file to your web folder.
- Create a new XHTML document by pressing Command+ CTRL+N. Save the document as lighting_state_form.php
- Between the body tags insert a pair of <div></div> tags
- Within the <div></div> tags
insert the following link. Look at the action of the form. This form is being sent to itself.
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>"> <input type="radio" name="light" value="On" /> On <br /> <input type="radio" name="light" value="Off" /> Off <br /> <input type="submit" value="Submit" name="send"/> </form> - You want to create a variable to hold the name of your text file.
Then you want to open and read the file. You will store the contents of the file in another variable and then close the file.
Before the form add:<?php // define a variable for the data file $myFile = "_____________.txt"; // open the data file $fh = fopen($myFile, 'r') or die("can't open file"); // read the current light setting from the data file $________ = fread($fh, filesize($myFile)); // close the data file fclose($fh); ?>- Now you want to check if the form was not sent yet. To do that use this conditional
if(!isset($_POST['send'])){ //statements } - Inside the above conditional you want to create code that informs the user about the state of the light:
echo "Light was last: ".$_________."
"; - If this condition was NOT met, then you want to
- Inform the user about the state of the light.
- You want to store the value sent in the form in a variable.
- You want to create a variable that opens your text file for writing:
$fh = fopen($myFile, 'w') or die("can't open file"); - You want to write to your file:
fwrite($fh, $value);
- You need to close the file-clean up after yourself
- Inform the user about the state of the light.
- Save and test the page.
- Select lighting_state_form.php in the fetch window and click on the Edit icon
- Inside the php, change navigate to the end of the conditional and before the close of the php script. Add code to build the form inside the php script:
echo"<form action=".$_SERVER['PHP_SELF']." method='post'>"; echo "<input type='radio' name='light' value='On' /> On<br />"; echo "<input type='radio' name='light' value='Off' /> Off<br />"; echo"</label><br />"; echo"<input type='submit' value='Submit' name='send'/>"; echo"</form>";
- Delete the code that builds the form outside of the php script
- Save and test
/ / Arduino
- Open lighting_state_form.php for editting
- Add two conditionals that change the $lightingFlag and $value to be 1 or 0 rather than "On" or "Off"
- Save and test
- Program an xbee so that
- factory defaults are restored
- DH is set to 0
- DL is set to 0
- PAN ID to AAAA
- MY is set to a unique number
- BD is set to 3 (9600)
- Type in ATCN to exit command mode
- Type in help
- Type in http://www.faludi.com/test.html
- Type in http://pcomp.lizarum.com/your_web_folder/lightSetting.txt
- Program the Arduino:
#include <NewSoftSerial.h> /* * *********ZigBee Internet Gateway Light Example******** * by Rob Faludi http://faludi.com */ #define NAME "ZIG Light Example" #define VERSION "1.00" #define LED_PIN 13 NewSoftSerial mySerial(2, 3); int outputLight = 12; int lightState; unsigned long pt; void setup() { pinMode(LED_PIN,OUTPUT); pinMode(outputLight,OUTPUT); blinkLED(LED_PIN,2,100); blinkLED(outputLight,2,100); Serial.begin(9600); // faster is better for ZIG delay(2000); mySerial.begin(9600); } void loop() { if (millis() -pt > 1000) { // wait a second before sending the next request // request the current light state, set with zig_light_form_example.php pt=millis(); mySerial.println("http://pcomp.lizarum.com/old/old/lightingState.txt"); } if (mySerial.available() > 0) { // if there's a byte waiting lightState = mySerial.read(); // read the ASCII numeral byte Serial.println(lightState);// turn on the light if the integer is 1, or off if the integer is zero if (lightState == '0' || lightState=='1') { lightState=lightState-48; digitalWrite(outputLight, lightState); } } } ////////////////// UTILITIES ////////////////// // this function blinks the an LED light as many times as requested, at the requested blinking rate void blinkLED(byte targetPin, int numBlinks, int blinkRate) { for (int i=0; i<numBlinks; i++) { digitalWrite(targetPin, HIGH); // sets the LED on delay(blinkRate); // waits for blinkRate milliseconds digitalWrite(targetPin, LOW); // sets the LED off delay(blinkRate); } } - Wire up the Arduino with the xbee. Provide power and ground to the xbee. Arduino TX goes to xbee RX and Arduino RX goes to xbee TX
#include <Tone.h>
#include <Button.h>
#include <NewSoftSerial.h>
/*
* *********ZigBee Internet Gateway Light Example********
* by Rob Faludi http://faludi.com
*/
#define NAME "ZIG Light Example"
#define VERSION "1.00"
#define LED_PIN 13
Button btn=Button(5,PULLDOWN);
NewSoftSerial mySerial(2, 3);
int outputLight = 12;
int lightState;
int light;
unsigned long pt;
boolean lightOn=false;
boolean written;
int btnVal;
int btnState;
Tone speaker;
void setup() {
pinMode(LED_PIN,OUTPUT);
pinMode(outputLight,OUTPUT);
blinkLED(LED_PIN,2,100);
blinkLED(outputLight,2,100);
Serial.begin(9600); // faster is better for ZIG
delay(2000);
mySerial.begin(9600);
speaker.begin(7);
}
void loop() {
if(btn.uniquePress()){
if(lightOn) {
mySerial.println("http://pcomp.lizarum.com/liz_web/lighting_state_form2.php?light=0");
speaker.play(NOTE_C3,200);
}
else if(!lightOn){
mySerial.println("http://pcomp.lizarum.com/liz_web/lighting_state_form2.php?light=1");
speaker.play(NOTE_C5,200);
}
}
if(millis()-pt>1000){
pt=millis();
mySerial.println("http://pcomp.lizarum.com/liz_web/lightingState.txt");
}
if (mySerial.available() > 0) { // if there's a byte waiting
lightState = mySerial.read(); // read the ASCII numeral byte
Serial.println(lightState);// turn on the light if the integer is 1, or off if the integer is zero
if (lightState == '0' || lightState=='1') {
lightState=lightState-48;
if(lightState==1){
lightOn=true;
light=1;
}
else if(lightState==0){
lightOn=false;
light=0;
}
}
}
digitalWrite(outputLight, light);
}
////////////////// UTILITIES //////////////////
// this function blinks the an LED light as many times as requested, at the requested blinking rate
void blinkLED(byte targetPin, int numBlinks, int blinkRate) {
for (int i=0; i<numBlinks; i++) {
digitalWrite(targetPin, HIGH); // sets the LED on
delay(blinkRate); // waits for blinkRate milliseconds
digitalWrite(targetPin, LOW); // sets the LED off
delay(blinkRate);
}
}