Go Down

Topic: How would I control the Arduino through internet? (Read 2585 times) previous topic - next topic

AKSoapy29

Jan 09, 2011, 01:29 am Last Edit: Jan 09, 2011, 02:04 am by Soapy29 Reason: 1
Hi! Yes, I am a noob and that's why I'm posting this question. I'd really like to try something like this to see if it works and just to play around with. I have no clue where to begin, so if you have suggestions, post them. Thanks!
Andrew K.

wossname

Have the arduino check a known email account for new messages.  Making it download the message headers would probably be enough.

Then to control it, simply send it emails with crafted "Subject" texts.  The arduino would examine the Subject text and decide what action to take accordingly.

The arduino would need to download the headers on a fairly frequent basis of course.

This is just one of a million possible methods.  It may well be easier to have it check an FTP account instead.  Whatever suits you.

AKSoapy29

@Richard,

What would the code be for something like that? And whould it be like a GUI interfase, where there's buttons, etc?
Andrew K.

martin_bg

Code: [Select]
/*
Arduino Data Web Server

Performs actions on Arduino and/or returns data from Arduino to a webbrowser request URL:
 - To turn on LED on pin 8, use web browser to visit your Arduino via: http://x.x.x.x/digitalWrite/7/1
 - To turn it off send: http://x.x.x.x/digitalWrite/7/0
 - To read analog0 value, send: http://x.x.x.x/analogRead/0

IMPORTANT:
Commands are case sensitive, but I wrote this as simple as possible so it can be easily adapted to your needs.  
Have fun!

created Sept 17, 2010
by Hari Wiguna, g33k.blogspot.com
*/

#include <SPI.h>
#include <Ethernet.h>
#include <string.h>

// MAC address can be anything that is unique within your network.
// IP is the address the Arduino Ethernet Card would respond to.  It needs to be an unused address within your network.
byte mac[] = {0x00, 0x1E, 0x2A, 0x77, 0x24, 0x02 };
byte ip[] = {192,168,7,12 }; // This is typically 10.0.0.x

Server server(80); // Port 80 is http.

//-- Commands and parameters (sent by browser) --
char cmd[15];    // Nothing magical about 15, increase these if you need longer commands/parameters
char param1[15];
char param2[15];

//-- Sample Ports ---
void SetupSamplePorts()
{
 // To illustrate how to use this, I have an LED and a Potentiometer.
 // The 10K potentiometer left lead is connected to GND, right lead to +5V, and middle lead to Analog 0.
 // The LED cathode is on digital pin 7 and anode is on pin 8.
 pinMode(7,OUTPUT); digitalWrite(7,LOW);  // I use this pin as GND for the LED.
 pinMode(8,OUTPUT); // Sample output, unable to use built-in LED at pin 13 because Ethernet Shield uses pins 10,11,12,13.
}

void setup()
{
 Ethernet.begin(mac, ip);
 server.begin();

 Serial.begin(57600);
 SetupSamplePorts();
}

#define bufferMax 128
int bufferSize;
char buffer[bufferMax];

void loop()
{
 Client client = server.available();
 if (client)
 {
   WaitForRequest(client);
   ParseReceivedRequest();
   PerformRequestedCommands();
   
   client.stop();
 }
}

void WaitForRequest(Client client) // Sets buffer[] and bufferSize
{
 bufferSize = 0;

 while (client.connected()) {
   if (client.available()) {
     char c = client.read();
     if (c == '\n')
       break;
     else
       if (bufferSize < bufferMax)
         buffer[bufferSize++] = c;
       else
         break;
   }
 }
 
 PrintNumber("bufferSize", bufferSize);
}

void ParseReceivedRequest()
{
 Serial.println("in ParseReceivedRequest");
 Serial.println(buffer);
 
 //Received buffer contains "GET /cmd/param1/param2 HTTP/1.1".  Break it up.
 char* slash1;
 char* slash2;
 char* slash3;
 char* space2;
 
 slash1 = strstr(buffer, "/") + 1; // Look for first slash
 slash2 = strstr(slash1, "/") + 1; // second slash
 slash3 = strstr(slash2, "/") + 1; // third slash
 space2 = strstr(slash2, " ") + 1; // space after second slash (in case there is no third slash)
 if (slash3 > space2) slash3=slash2;

 PrintString("slash1",slash1);
 PrintString("slash2",slash2);
 PrintString("slash3",slash3);
 PrintString("space2",space2);
 
 // strncpy does not automatically add terminating zero, but strncat does! So start with blank string and concatenate.
 cmd[0] = 0;
 param1[0] = 0;
 param2[0] = 0;
 strncat(cmd, slash1, slash2-slash1-1);
 strncat(param1, slash2, slash3-slash2-1);
 strncat(param2, slash3, space2-slash3-1);
 
 PrintString("cmd",cmd);
 PrintString("param1",param1);
 PrintString("param2",param2);
}

void PerformRequestedCommands()
{
 if ( strcmp(cmd,"digitalWrite") == 0 ) RemoteDigitalWrite();
 if ( strcmp(cmd,"analogRead") == 0 ) RemoteAnalogRead();
}

void RemoteDigitalWrite()
{
 int ledPin = param1[0] - '0'; // Param1 should be one digit port
 int ledState = param2[0] - '0'; // Param2 should be either 1 or 0
 digitalWrite(ledPin, ledState);

 //-- Send response back to browser --
 server.print("D");
 server.print(ledPin, DEC);
 server.print(" is ");
 server.print( (ledState==1) ? "ON" : "off" );

 //-- Send debug message to serial port --
 Serial.println("RemoteDigitalWrite");
 PrintNumber("ledPin", ledPin);
 PrintNumber("ledState", ledState);
}

void RemoteAnalogRead()
{
 // If desired, use more server.print() to send http header instead of just sending the analog value.
 int analogPin = param1[0] - '0'; // Param1 should be one digit analog port
 int analogValue = analogRead(analogPin);
 
 //-- Send response back to browser --
 server.print("A");
 server.print(analogPin, DEC);
 server.print(" is ");
 server.print(analogValue,DEC);
 
 //-- Send debug message to serial port --
 Serial.println("RemoteAnalogRead");
 PrintNumber("analogPin", analogPin);
 PrintNumber("analogValue", analogValue);
}

void PrintString(char* label, char* str)
{
 Serial.print(label);
 Serial.print("=");
 Serial.println(str);
}

void PrintNumber(char* label, int number)
{
 Serial.print(label);
 Serial.print("=");
 Serial.println(number, DEC);
}

AKSoapy29

Ok, how would I make it turn on an led when you press a button onthe web page? Is there any examples I can look at?
Andrew K.

tkbyd

There are Arduino examples of this sort of thing at Pachube.com.

Over there, they are also involving, reasonably enough!, Pachube data streams. But the same thing that turns on an LED in the house when the temperature in the farm outbuilding goes below 5 deg C can be adapted to turn on an LED when you press a button.

Arduino TCP/IP experts: Can an Arduino webserver "do" PHP? Even a subset? What is the Arduino alternative, if any, please. (Apologies if the answer is: "The code in the post before yours!!")

Ran Talbott

Quote
Can an Arduino webserver "do" PHP? Even a subset?


You could probably come up with some extremely tiny subset (I've got an ancient 8052 that runs BASIC),  but you wouldn't like it much.

Alternatively,  you could host a very sophisticated PHP app on a PC,  and have it talk to a simple HTTP server on the Arduino.  Or,  depending on what you want to do,  embed the Arduino's webpage within the one served up by the PC.  I did something like that once with a Netburner that controlled a remote camera:  all the graphics for the user interface were served up by the PC,  but pan/tilt/zoom commands were POSTed to the Netburner,  which served up the position and status readout in a frame inside the main page.  The video came from yet another server,  also embedded in a frame.  To the non-geek users,  it all looked like it was coming from a single source.

zoomkat

The links below have some info on simple web arduino control.

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1293087639
http://www.lynxmotion.net/viewtopic.php?f=20&t=6343

Google forum search: Use Google Advanced Search and use Http://forum.arduino.cc/index in the "site or domain:" box.

Go Up