Show Posts
|
|
Pages: [1] 2
|
|
1
|
Topics / Home Automation and Networked Objects / Re: controlling servo motor over internet using erduino ethernet shield
|
on: June 30, 2012, 10:44:56 am
|
Hi, Are you hosting the web server on the Arduino itself? In such case you need to parse the data from the client (web browser). The web page buttons shall post some variables to the server, and the server shall parse the incoming data from the client and move the servos. Check this http://www.instructables.com/id/ServDuino-Arduino-Webserver/Also, the following code will probably work for you: #include <SPI.h> #include <Ethernet.h> #include <Servo.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address byte ip[] = { 192, 168, 1, 102 }; byte gateway[] = { 192, 168, 1, 1 }; byte subnet[] = { 255, 255, 255, 0 }; //subnet mask EthernetServer server(84); //server port
String readString, servo1, servo2;
Servo myservo1;
void setup(){
//start Ethernet Ethernet.begin(mac, ip, gateway, subnet); server.begin();
//enable serial data print Serial.begin(9600); myservo1.attach(7);
}
void loop(){ // Create a client connection EthernetClient client = server.available(); if (client) { while (client.connected()) { if (client.available()) { char c = client.read();
//read char by char HTTP request if (readString.length() < 100) {
//store characters to string readString += c; }
//if HTTP request has ended if (c == '\n') { Serial.println(readString);
//readString looks like "GET /?-1500 HTTP/1.1"
if (readString.length() >0) { Serial.println(readString); servo1 = readString.substring(7, 11); Serial.println(servo1); int n1; char carray1[6]; servo1.toCharArray(carray1, sizeof(carray1)); n1 = atoi(carray1); myservo1.writeMicroseconds(n1); readString=""; } client.stop();
readString="";
} } } } }s
|
|
|
|
|
4
|
Topics / Home Automation and Networked Objects / Re: Arduino + Wifly RN-XV + Processing
|
on: June 01, 2012, 07:14:35 am
|
Sorry, previous post contains server code for the WiFly, but you need the Client code to connect to the Processing sketch (which is creating a server). #include "WiFly.h" #include "Credentials.h"
byte server[] = { 192,168,1,1 }; // Your PC IP
Client client(server, 1234);
void setup() { Serial.begin(9600);
WiFly.begin(); if (!WiFly.join(ssid, passphrase)) { Serial.println("Association failed."); while (1) { // Hang on failure. } }
Serial.println("connecting...");
if (client.connect()) { Serial.println("connected"); client.println("Hello World!\n"); client.println(); } else { Serial.println("connection failed"); } }
void loop() { if (client.available()) { char c = client.read(); Serial.print(c); } while (Serial.available() > 0) { char inChar = Serial.read(); if (client.connected()) { client.print(inChar); } } if (!client.connected()) { Serial.println(); Serial.println("disconnecting."); client.stop(); for(;;) ; } }
|
|
|
|
|
6
|
Topics / Home Automation and Networked Objects / Re: Arduino and PHP
|
on: May 28, 2012, 04:12:25 am
|
Hi, are you trying to just store values from the Arduino on MySQL or send values to the Arduino from the html page as well? If it is only the first care, all you need is the php script that will receive values directly from the Arduino. It is more easy to use GET instead of POST, so here is your php code slightly modified: <?php $con = mysql_connect("my_webhost","username","password"); if (!$con) { die('could not connect: ' .mysql_error()); } mysql_select_db("my_database", $con);
$sql="INSERT INTO Automation (arduino,pin2,pin3,pin4,pin5,pin6) VALUES ('$_GET[arduino]','$_GET[pin2]','$_GET[pin3]','$_GET[pin4]','$_GET[pin5]','$_GET[pin6]')";
if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "1 record added";
mysql_close($con); ?>
Here is an Arduino sketch that connects to the php script and sends pin2, pin3, ...variables through POST: #include <SPI.h> #include <Ethernet.h>
// Enter a MAC address for your controller below. // Newer Ethernet shields have a MAC address printed on a sticker on the shield byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; IPAddress server(192,168,1,1); // YOUR SERVER'S IP ADDRESS
// Initialize the Ethernet client library // with the IP address and port of the server // that you want to connect to (port 80 is default for HTTP): EthernetClient client;
int pin2, pin3, pin4, pin5, pin6;
//String representations of the pin values String spin2, spin3, spin4, spin5, spin6;
void setup() { // start the serial library: Serial.begin(9600); // start the Ethernet connection: if (Ethernet.begin(mac) == 0) { Serial.println("Failed to configure Ethernet using DHCP"); // no point in carrying on, so do nothing forevermore: for(;;) ; } // give the Ethernet shield a second to initialize: delay(1000); }
void loop() { //read pin values: pin2 = analogRead(0); //... spin2 = String(pin2, DEC); if (client.connect(server, 80)) { Serial.println("connected"); // Make a HTTP request: client.println("GET /arduinoInsert.php?arduino=arduino&pin2="+spin2+"&pin3="+spin3+"&pin4="+spin4+"&pin5="+spin5+"&pin6="+spin6+" HTTP/1.0"); client.println(); } else { // kf you didn't get a connection to the server: Serial.println("connection failed"); }
// if the server's disconnected, stop the client: if (!client.connected()) { Serial.println(); Serial.println("disconnecting."); client.stop();
// do nothing forevermore: for(;;) ; } }
|
|
|
|
|
10
|
Topics / Home Automation and Networked Objects / Re: Arduino + Wifly RN-XV + Processing
|
on: May 22, 2012, 05:48:27 am
|
Hi, You have misplaced some of the code in your sketch, check the following code. Have not tested if it runs properly but I least it compiles  #include "WiFly.h" #include "Credentials.h"
Server server(80);
void setup() { // start the WiFly connection: WiFly.begin(); if (!WiFly.join(ssid, passphrase)) { while (1) { // Hang on failure. } }
Serial.begin(9600); Serial.print("IP: "); Serial.println(WiFly.ip()); server.begin();
// give the WiFly shield a second to initialize: delay(1000); Serial.println("connecting..."); //------rest shall be implemented inside loop() // if you get a connection, report back via serial: //if (client.connect(server, 1234)) { // // Serial.println("connected"); //} //else { // // if you didn't get a connection to the server: // Serial.println("connection failed"); //} }
void loop() { Client client = server.available(); //If client has been instantiated if (client) { //check for client connection while (client.connected()) { //if client has connected if (client.available()) { //The following code will simply send characters for Terminal input to the server - use it as a test while (Serial.available() > 0) { char inChar = Serial.read(); if (client.connected()) { client.print(inChar); } } } } } // if the server's disconnected, stop the client: if (!client.connected()) { Serial.println(); Serial.println("disconnecting."); client.stop(); // do nothing: while(true); } }
|
|
|
|
|
13
|
Topics / Home Automation and Networked Objects / Re: Arduino + Wifly RN-XV + Processing
|
on: May 07, 2012, 10:11:40 am
|
I suggest you implement your own server in Processing and talk to it through the Arduino check the following code: import processing.net.*; Server myServer;
void setup() { size(200, 200); // Starts a myServer on port 1234, change this to whatever suits you! myServer = new Server(this, 1234); }
void draw() { // Get the next available client Client thisClient = myServer.available(); // write the output of the client if (thisClient !=null) { String whatClientSaid = thisClient.readString(); if (whatClientSaid != null) { println(thisClient.ip() + "t" + whatClientSaid); } } }
and the Arduino code (for Ethernet example, easy to adopt): #include <SPI.h> #include <Ethernet.h>
// Enter a MAC address and IP address for your controller below. // The IP address will be dependent on your local network: byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; IPAddress ip(192,168,1,177);
// Enter the IP address of the server you're connecting to: IPAddress server(192,168,1,1);
// Initialize the Ethernet client library // with the IP address and port of the server EthernetClient client;
void setup() { // start the Ethernet connection: Ethernet.begin(mac, ip); // start the serial library: Serial.begin(9600); // give the Ethernet shield a second to initialize: delay(1000); Serial.println("connecting...");
// if you get a connection, report back via serial: if (client.connect(server, 1234)) { Serial.println("connected"); } else { // if you didn't get a connection to the server: Serial.println("connection failed"); } }
void loop() { //The following code will simply send characters for Terminal input to the server - use it as a test while (Serial.available() > 0) { char inChar = Serial.read(); if (client.connected()) { client.print(inChar); } }
// if the server's disconnected, stop the client: if (!client.connected()) { Serial.println(); Serial.println("disconnecting."); client.stop(); // do nothing: while(true); } }
|
|
|
|
|