Arduino (C++) send variable to PHP page

Asked

Viewed 201 times

0

I have the following code in C++ on Arduino

int variavelemc;
variavelemc = 10;

And I have the following code in PHP

$variavelemphp = $_POST['variavelemc'];

How do I pass the variavelemc to the $variavelemphp ?

Additional Information

Arduino is connected to Internet.

The PHP code is hosted on the internet.

  • It depends. This PHP is being served by a web server, I suppose, so you have to send an Arduino HTTP request to your web server. You could run PHP via command line and do serial communication.

  • 1

    Given the issue that PHP is hosted and Arduino connected to the internet, there are libraries HTTP client that you can use to make a request.

  • Is it feasible to implement Mysql (save the data in a database) directly in Arduino? Or you necessarily need parameters sent to be treated in a PHP script?

  • @Darleifernandozillmer would need to be in PHP, since direct connection to Mysql is not provided.

1 answer

3


Using the Ethernet library

Initializing

byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x01 }; // RESERVED MAC ADDRESS
EthernetClient client;

In the Begin()

Serial.begin(115200);
if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP"); 
}

in the loop()

    if (client.connect("www.*****.*************.com",80)) {
        client.println("POST /add.php HTTP/1.1"); 
        client.println("Host: *****.*************.com");
        client.println("Content-Type: application/x-www-form-urlencoded"); 
        client.print("Content-Length: "); 
        client.println(data.length()); 
        client.println(); 
        client.print(data); 
    } 

    if (client.connected()) { 
        client.stop();  
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.