Hi! I am building up a web interface for Arduino. but I am not really experienced in Arduino programming. I have two php files in a server and i would like to send data to one (receive.php) of them and receive data from the other (send.php).
This is my Arduino code, it sucessfully sends data, but fails to receive a string from the server (which could be either "led1=0" or "led1=255").
I suspect that at least part of the problem is requiring that the server respond within a few microseconds.
client.println();
while (client.connected()){
while (client.available()) {
char thisChar = client.read();
inData += thisChar;
}
client.stop();
return 1;
}
You should note the time and wait until you receive the result or a reasonable timeout expires. I would also send the received lines to Serial Monitor to see what exactly is being returned.
@johmwasser: That code waits for a server response. Granted it is "Perfect World" code with no timeout, but it should read the response from the server. The server will close the connection from its end when finished sending.
The challenge I see is the String data type. It has never failed to crash my sketches.
But besides that, inData has no limit on it. A server can respond with a rather large amount of text.
Because I want the website to tell me whether the led is on or off, because if I don't, I wouldn't know if I'm turning the led on or off when clicking the button. The server scripts and html are finished and they work.
Because I want the website to tell me whether the led is on or off, because if I don't, I wouldn't know if I'm turning the led on or off when clicking the button. The server scripts and html are finished and they work.
You have two scripts where the Arduino is a client. You should have ONE script with the Arduino as server. There is no reason to be storing light status in a relational database. Every time the client wants to know the status of the lights, it should ask the Arduino. If the status might change outside of the client's control, the client needs to be told to ask for the status on a regular basis (using the appropriate meta tags for refresh).
The client should make a GET request when a form's submit button is pressed.
After some thinking, I concluded that it was better to have only one php script, so my arduino would only need to make one connection. I came up with these:
PHP:
<?php
require_once("connect.php");
$dev = $_POST['device'];
$value = $_POST['value'];
if ($value == "255") {
$value = 255;
}
if ($value == "0") {
$value = 0;
}
$db = "dbname";
mysqli_select_db($connection, $db);
$write = "UPDATE control SET status = $value WHERE device='$dev'";
$reswrite = mysqli_query($connection, $write);
$query = "SELECT * FROM control";
$result = mysqli_query($connection, $query);
while ($row = mysqli_fetch_assoc($result)) {
if($row['action'] == 0){
echo 'a';
}
if($row['action'] == 255){
echo 'b';
}
}
?>
ARDUINO
#include <Ethernet.h>
int led1 = 8;
// assign a MAC address for the ethernet controller.
// fill in your address here:
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
// fill in an available IP address on your network here,
// for manual configuration:
//IPAddress ip(192, 168, 1, 177);
// fill in your Domain Name Server address here:
//IPAddress myDns(1, 1, 1, 1);
// initialize the library instance:
EthernetClient client;
char server[] = "SERVERNAME";
//IPAddress server(64,131,82,241);
unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
const unsigned long postingInterval = 1000L; // delay between updates, in milliseconds
// the "L" is needed to use long type numbers
void setup() {
// give the ethernet module time to boot up:
delay(1000);
// start the Ethernet connection using a fixed IP address and DNS server:
Ethernet.begin(mac);
pinMode(led1, OUTPUT);
digitalWrite(led1, HIGH);
delay(1000);
digitalWrite(led1, LOW);
delay(1000);
}
void loop() {
// if there's incoming data from the net connection.
// send it out the serial port. This is for debugging
// purposes only:
if (client.available()) {
char c = client.read();
if(c == 'a'){
digitalWrite(led1, LOW);
}
if(c == 'b'){
digitalWrite(led1, HIGH);
}
}
// if ten seconds have passed since your last connection,
// then connect again and send data:
if (millis() - lastConnectionTime > postingInterval) {
httpRequest();
}
}
// this method makes a HTTP connection to the server:
void httpRequest() {
// close any connection before send a new request.
// This will free the socket on the WiFi shield
client.stop();
String stat;
if(digitalRead(led1) == HIGH){
stat+="";
stat+="255";
}
if(digitalRead(led1) == LOW){
stat+="";
stat+="0";
}
String data;
data+="";
data+="device=led1&value=" + stat;
// if there's a successful connection:
if (client.connect(server, 80)) {
// send the HTTP PUT request:
client.println("POST /php/hardware.php HTTP/1.1");
client.println("Host: HERE-WAS-THE-SERVERNAME");
client.println("Content-Type: application/x-www-form-urlencoded");
client.println("Connection: close");
client.print("Content-Length: ");
client.println(data.length());
client.println();
client.print(data);
client.println();
// note the time that the connection was made:
lastConnectionTime = millis();
} else {
// if you couldn't make a connection:
}
}
The arduino code above is a modified example. Both are only for testing purposes. Shouldn't it work? Where am I making the mistake?
Thank you for your help!
But, suppose that the server said "The requested script does not have the proper permissions". What is your code going to do? It is going to turn an LED off. How will THAT communicate anything to you?
The comment was referring to some code you deleted. PUT THAT CODE BACK!
Where am I making the mistake?
Well, the code did something. You didn't tell us what it actually did. So, we don't even know what the mistake was, let alone how to fix it.
There was information missing. Arduino is going to post some data to the php script. The script will modify a database table accordingly, then read another field of the same table and echo it to arduino. The table, which is named control, is structured like this:
device
status
action
led1
0
0
The field to be modified according to the posted data is status, and the field read and returned to arduino is action. The web interface will read the status and a button will modify the action field.