Sending data from web page to Arduino trough Raspberry pi

Hi guys ! I would like to send data from a web page that his host on a server at home to an Arduino via a raspberry Pi. Currently I'm running a Python script on the Pi that his receive the serial from the Arduino and that is doing some actions depending on what is received. I'm using this function:

ser = serial.Serial('/dev/ttyACM0', 9600)

while 1 :
    dataReceive = ser.readline()
    if value[:4] == "code":
        mysqlQuery(value[-5:])
...

So what would be the best way to send data from a web page ? Would new Python script close this communication ?

I hope I've been clear !

Thanks :slight_smile:

Hey tchinou1,

What kind of data do you want to send from a web page.
The method I used to send data from my web server to my Arduino Uno is displaying the data on a separate web page using php or html. Then I used a sketch I found online to connect to the web server and collect the data posted on the webpage. I would then perform different tasks based on that data.

// Edited by Zeta
#include <SPI.h>//initializing libraries
#include <Ethernet.h>//initializing libraries
#include <Servo.h>//initializing libraries
//The following code was based on http://bildr.org/2011/06/arduino-ethernet-client/
//https://opensource.org/licenses/mit-license.php

///////////////////// Config //////////////////////////
Servo myservo1;  // create servo object to control a servo
Servo myservo2;  // create servo object to control a servo
int pos = 0;    // variable to store the servo position

//configure MAC address if needed (Rare)
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

//ip Address of the server you will connect to
byte server[] = { 192,168,0,101 }; 

//IP address of the Arduino
IPAddress ip(192,168,0,111);

//The location to go to on the server
//make sure to keep HTTP/1.0 at the end, this is telling it what type of file it is
String location = "/test.php HTTP/1.0";



// Initialize the Ethernet server library
EthernetClient client;

char inString[32]; // string for incoming serial data
int stringPos = 0; // string index counter
boolean startRead = false; // is reading?

////////////////////////Connecting////////////////////////////////


void setup(){
  Ethernet.begin(mac, ip);// start the Ethernet connection
  Serial.begin(9600);// Serial.begin starts the serial connection between computer and Arduino
  myservo1.attach(5);  // attaches the servo on pin 5 to the servo object
  myservo2.attach(3);  // attaches the servo on pin 3 to the servo object
}


////////////////////////////////////////////////////////////////


void loop(){//MAIN LOOP
  
  String pageValue = connectAndRead(); //connect to the server and read the output


 if (pageValue == "1"){//CLOSE LOCK for locking
  for (pos = 0; pos <= 120; pos += 1) { // goes from 0 degrees to 120 degrees
    // in steps of 1 degree
    myservo1.write(pos);              // tell servo to go to position in variable 'pos'
    myservo2.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
 }else if(pageValue == "2"){//OPEN LOCK for unlocking
  for (pos = 120; pos >= 0; pos -= 1) { // goes from 120 degrees to 0 degrees
    myservo1.write(pos);              // tell servo to go to position in variable 'pos'
    myservo2.write(pos);              // tell servo to go to position in variable 'pos'
    delay(15);                       // waits 15ms for the servo to reach the position
  }
 }else if (pageValue == "0"){
    Serial.println("ERROR");//print text
 }
  Serial.println(pageValue); //print out the findings.

  delay(10000); //wait 10 seconds before connecting again
}//end main loop



String connectAndRead(){
  //connect to the server

  Serial.println("connecting...");//print text

  //port 80 is typical of a www page
  if (client.connect(server, 80)) {//if connected
    Serial.println("connected");//print text
////////////
    client.print("GET /test.php?"); //GET /get.php?
    client.print("value="); // This
    client.print("10186593"); // And this is what we did in the testing section above. We are making a GET request just like we would from our browser but now with live data from the sensor
    client.print("&rck=1"); // And this is what we did in the testing section above. We are making a GET request just like we would from our browser but now with live data from the sensor COULD FUCK UP
    client.println(" HTTP/1.1"); // Part of the GET request
    client.println("Host: 192.168.0.101"); // IMPORTANT: If you are using XAMPP you will have to find out the IP address of your computer and put it here (it is explained in previous article). If you have a web page, enter its address (ie.Host: "www.yourwebpage.com")
    client.println("Connection: close"); // Part of the GET request telling the server that we are over transmitting the message
    client.println(); // Empty line
    client.println(); // Empty line


    //Connected - Read the page
    return readPage(); //go and read the output

  }else{
    return "connection failed";
  }

}//end function connectAndRead



//Searching Method
String readPage(){
  //read the page, and capture & return everything between '<' and '>'

  stringPos = 0;
  memset( &inString, 0, 32 ); //clear inString memory

  while(true){

    if (client.available()) {
      char c = client.read();

      if (c == '@' ) { //'<<' is our begining character
        startRead = true; //Ready to start reading the part 
      }else if(startRead){

        if(c != '*'){ //'>>' is our ending character
          inString[stringPos] = c;
          stringPos ++;
        }else{
          //got what we need here! We can disconnect now
          startRead = false;
          client.stop();
          client.flush();
          Serial.println("disconnecting. WE GOT THE STRING");
          return inString;

        }

      }
    }

  }

}

Your title suggests to me that you intend to use a Python program on the RPI to access a web page and extract some values from it and store the values into some variables.

Your Original Post suggests that you already have that part working.

Am I correct so far?

If so then you just need to have another Python function which sends the data in those variables to the Arduino.

Have a look at this Python - Arduino demo and at Serial Input Basics which was written more recently.

...R

Zetakillz, I thought about changing for a wireless arduino, but for the moment the arduino is not connected to the internet.

Robin2, yes I'm already able to do what you said, but my problem is that this happen when a function is called on the Python program, but I need that this happen when I click a button on a web page. Otherwise, if I'm not wrong, I would need to continuously call the web page to check if there was a change.

tchinou1:
yes I'm already able to do what you said, but my problem is that this happen when a function is called on the Python program, but I need that this happen when I click a button on a web page. Otherwise, if I'm not wrong, I would need to continuously call the web page to check if there was a change.

There is not enough information in that.

It seems as if when the Python function is called it accesses the web page and gets the information. But you have not told us where the web page is hosted or whether you created it.

A web page cannot send data to anything but the server that is hosting the website.

If your Python program is acting as the web server then it will be easy to make the browser send a message to the server which causes the server to send data to an Arduino. I have done that myself for a few projects.

Provide full details of the existing set up.

...R

The web server is running on the same lan as the RPI.
Now when the Pi receive a specific string from the arduino, it request a function that call a web page and get some information. What I want is that when I click on a button on a web page that is hosted on my web server, a function (with some parameters) get called on the Pi. This function would send a string to the arduino.

If you want I can join my actual python script.

tchinou1:
The web server is running on the same lan as the RPI.

That suggests to me that it is not actually running on the RPi.

On what computer is the web page being displayed ?

If the server is not running on the RPi I think it will be difficult (or impossible) to do what you want.

It is relatively easy to do it when the server is running on the computer that is connected to the Arduino and when you can write the code for the server.

Maybe you can post a diagram showing how all the parts are related and connected to each other. At the moment the impression I have is that it is like this

server ---- Lan ---- RPi ---- Arduino
             |
             |
        browser

...R

server ---- Lan ---- RPi ---- Arduino
|
|
browser

I think it would look more like this, the computer displaying the web page can be from anywhere. I don't know if something like this could work: the web page request a python script on the web server, this script communicate with the Pi.

If you can program the web server then one solution may be to get it to produce a special RPiData page which the RPi can access and collect the data to send to the Arduino.

So, when a person clicks on the browser page the server recognizes that and changes a value on the special RPiData web page. The RPI is continuously checking the RPiData web page.

Would that sort of thing meet your requirement.

It should also be possible to write code for your server to communicate more directly with the RPI over the Lan - but I have no experience of that.

...R

Yes I think this would be the best solution for the moment.

Thanks for your help !

You can check it out this solution.

http://abetoo.com/wiki/knowledge-base/javascript_to-infinity-and-beyond

You can send a message with whatever content from a website within RPI without a web server and receive this messages with an arduino connected device, another website or even a desktop app.