If I put this link "http: //localhost/write_data.php? Value = 200" on the website address,
then it functions without problems.
The values are entered in SQL database.
With arduino the connection does not work.
#include <SPI.h>
#include <UIPEthernet.h>
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,2,177); // IP address for Arduino
int anaolgPin = 0; // Analog input pin
int analogReading;
char server[] = "192.168.2.11"; // XAMPP Server
EthernetClient client;
unsigned long lastConnectionTime = 0;
const unsigned long postingInterval = 10L * 1000L;
char inString[32];
int stringPos = 0;
boolean startRead = false;
void setup()
{
Serial.begin(9600);
Ethernet.begin(mac, ip);
}
void loop()
{
if (client.available())
{
char c = client.read();
Serial.write(c);
}
if (millis() - lastConnectionTime > postingInterval)
{
sendSQL();
}
}
void sendSQL()
{
analogReading = analogRead(analogPin);
Serial.print("My IP address: ");
Serial.print(Ethernet.localIP());
Serial.print(" value=");
Serial.println(photocellReading);
if (client.connect(server, 80)) // Connect to the sql server
{
Serial.println("connected");
client.print("GET /write_data.php?");
client.print("value=");
client.print(analogReading);
client.println(" HTTP/1.1");
client.println("Host: 192.168.2.177");
client.println("Connection: close");
client.println();
client.println();
client.stop();
return reading();
}
else
{
// If Arduino can't connect to the server (your computer or web page)
Serial.println("--> connection failed\n");
}
}
String reading()
{
stringPos = 0;
memset( &inString, 0, 32 );
while(true)
{
if (client.available())
{
char c = client.read();
if (c == '<' )
{
startRead = true;
}
else if(startRead)
{
if(c != '>')
{
inString[stringPos] = c;
stringPos ++;
}
else
{
startRead = false;
client.stop();
client.flush();
Serial.println("disconnecting.");
return inString;
}
}
}
}
delay(1000);
}
In Arduino 1.8.5 serial output, a message does not connect?
I can not find the mistake?
My IP address: 192.168.2.177 value=159
--> connection failed
My IP address: 192.168.2.177 value=159
--> connection failed
My IP address: 192.168.2.177 value=160
--> connection failed
My IP address: 192.168.2.177 value=160
--> connection failed
My IP address: 192.168.2.177 value=160
--> connection failed
My IP address: 192.168.2.177 value=161
--> connection failed
My IP address: 192.168.2.177 value=160
--> connection failed
My IP address: 192.168.2.177 value=161
--> connection failed
My IP address: 192.168.2.177 value=161
--> connection failed
My IP address: 192.168.2.177 value=161
--> connection failed