I asked a related question a few days ago and now tried to achieve my goal using a different method, but it didnt work either. I cant get my head around why, so if you have any idea what I could have missed, please leave a comment :)
In my current setup, the Arduino (with an Ethernet Shield 2) tries to start a php GET script on the server and send it a simple value. Here is my code, only trying to send the value 42:
#include <Ethernet2.h> //library for ethernet functions
#include <SPI.h>
#include <Client.h> //library for client functions
// Ethernet settings
byte mac[] = {0x90,0xA2,0xDA,0x0D,0x8B,0xB3}; //Replace with your Ethernet shield MAC
byte ip[] = { 192,168,1,105}; //The Arduino device IP address
byte subnet[] = { 255,255,255,0};
byte gateway[] = { 192,168,0,1};
IPAddress server(134,119,45,10); // IP-adress of server arduino sends data to
EthernetClient client;
bool connected = false;
void setup(void)
{
Serial.begin(9600);
Serial.println("Initializing Ethernet.");
delay(1000);
Ethernet.begin(mac);
Serial.println("Status ");
analogReference(INTERNAL);
}
void loop(void){
if(!connected)
{
Serial.println("Not connected");
if (client.connect(server,80))
{
connected = true;
Serial.print("Status is ");
Serial.println("42");
Serial.println();
Serial.println("Sending to Server: ");
client.print("GET /php_get_write.php?state=");
Serial.print("GET /php_get_write.php?state=");
client.print("42");
Serial.print("42");
Serial.println();
client.println();
client.println("HTTP/1.1\r\n");
Serial.println();
Serial.println("HTTP/1.1\r\n");
client.println("Host: localhost\r\n");
Serial.println("Host: localhost\r\n");
client.println();
client.println("User-Agent: Arduino\r\n");
Serial.println("User-Agent: Arduino\r\n");
client.println("Accept: text/html\r\n");
Serial.println("Accept: text/html\r\n");
client.println();
Serial.println();
delay(1000);
}
else
{
Serial.println("Cannot connect to Server");
}
}
else
{
delay(1000);
while (client.connected() && client.available())
{
char c = client.read();
Serial.print(c);
}
Serial.println();
client.stop();
connected = false;
}
}
On the server, I have the script that gets the value and writes it to a file:
<?php
$file = "status.txt";
$status = $_GET["state"];
echo "Getting data from Arduino and writing to file...";
echo $_GET["state"];
file_put_contents ($file,$status);
?>
And a script that reads the value from the file and shows it:
<?php
echo ("Status of toilets:");
$data = file_get_contents("status.txt");
echo ($data);
?>
I am pretty sure that the php code is correct and that the problem lies in the configuration of the arduino or the server. Since I only have limited know-how on these subjects I hope that you have an idea of what is going wrong and guide me on the right path! May it be the php.ini configuration? Or is something with the arduino code?
Thanks for help in advance!