// This is a modified sketch from a sample found at arduino playground and something else // from Tom Igoe's book Making Things talk // It shows how to receive data from the onboard serial port and send it to a // ldc on the software serial port // in this case, the onboard serial port is connected to a motorola i415 running // a mobile.processing sketch that gets data from a web and sends it to this arduino board // include the SoftwareSerial library so you can use its functions: #include #define lcdrxPin 2 // not used - the LCD only receives data #define lcdtxPin 3 // pin that connects to the lcd rx pin #define ledPin 13 // a little blinky to see the data flowing // set up a new serial port SoftwareSerial LCDSerial = SoftwareSerial(lcdrxPin, lcdtxPin); byte pinState = 0; int endbyte = 35; //end of serial data long previousMillis = 0; // will store last time data was sent to serial port long interval = 15000; // interval at which to send data (milliseconds) // theurl is what the phone will use to connect to a website // the format is domain,/path/filename.ext -- Note the comma!! // make sure to change the server and page path to match your server adddress char theurl[] = "webserver.com,/path/page.ext"; #define slen 64 // max lenght for the serial string - serial buffer max is 128 bytes char serInStr[slen]; // array to hold the incoming serial string bytes boolean serverinfo = false; // variable to define initial settings void setup() { // define pin modes for tx, rx, led pins: pinMode(lcdrxPin, INPUT); pinMode(lcdtxPin, OUTPUT); pinMode(ledPin, OUTPUT); // set the data rate for the SoftwareSerial port LCDSerial.begin(9600); // initalize the lcd and clear the screen clearLCD(); backlightOn(); delay(100); // set the data rate for the main serial port // 9600 is just the value I used, depending on your phone (or other device) // you connect to, you may want to bump it up Serial.begin(9600); } void loop() { // start the count down if (millis() - previousMillis > interval) { // if the end has been met previousMillis = millis(); // zero the counter // send sersor data to serial followed by endchar // if you have anything to send to the web, this is where to place it. // you have to compose the whole query string and end it with the endbyte # // in this sample I'm just sending the amount of milsecs passed since the // beginning. . . if you have nothing to pass and just want to fetch // data from a webpage, just pass the endbyte along. // for every request we send theurl, and anything else we got. Serial.print(theurl); // if we want to pass anything else we pass ",varcname=value&varname1=value1 etc Serial.print(",millis="); Serial.print(previousMillis); // we end the message with the endbyte Serial.print(endbyte, BYTE); } else { // if the count down is still running // we just keep watching the serial port int datalenght = readSerialString(); // if we get a # on the serial and server info (theurl) has not been sent // we need to tell the phone where to send the data if (datalenght > 0) { // if we get anything other than the endbyte and server info has been sent it means the phone is // talking back with web data - so we print to the lcd // if you are not using a LCD, you can use pick apart the info // on the serInStr string and make things happen like turn a pin high or low etc // in this case I'm just passing it along to the lcd writetolcd(datalenght); } } } int readSerialString () { int i=0; // if the serial is not available, we return an error // you will notice i'm not really checking for errors in the main loop but the sample I copied this from was ;) if(!Serial.available()) { return -1; } int c = 0; // we wait on the serial port while it is open // and not too long // we append each byte received to serInStr // in the end we return the lenght of the data received while (Serial.available() && i < slen) { c = Serial.read(); serInStr[i++] = c; } return i; } void writetolcd (int i) { // This could have been easier but the lcd is twichy // so i found somewhere some info on how to send // the info one byte at a time for (int f = 0; f < i; f++){ LCDSerial.print(serInStr[f]); delay(500); toggle(13); } } // the remaining functions are straight from the Arduino Playground LCD example // http://www.arduino.cc/playground/Learning/SparkFunSerLCD void toggle(int pinNum) { // set the LED pin using the pinState variable: digitalWrite(pinNum, pinState); // if pinState = 0, set it to 1, and vice versa: pinState = !pinState; } void selectLineOne(){ //puts the cursor at line 0 char 0. LCDSerial.print(0xFE, BYTE); //command flag LCDSerial.print(128, BYTE); //position } void selectLineTwo(){ //puts the cursor at line 0 char 0. LCDSerial.print(0xFE, BYTE); //command flag LCDSerial.print(192, BYTE); //position } void clearLCD(){ LCDSerial.print(0xFE, BYTE); //command flag LCDSerial.print(0x01, BYTE); //clear command. } void backlightOn(){ //turns on the backlight LCDSerial.print(0x7C, BYTE); //command flag for backlight stuff LCDSerial.print(157, BYTE); //light level. } void backlightOff(){ //turns off the backlight LCDSerial.print(0x7C, BYTE); //command flag for backlight stuff LCDSerial.print(128, BYTE); //light level for off. } void serCommand(){ //a general function to call the command flag for issuing all other commands LCDSerial.print(0xFE, BYTE); } void setlenght(){ //turns off the backlight LCDSerial.print(0x7C, BYTE); //command flag for backlight stuff LCDSerial.print(4, BYTE); //light level for off. }