dear programmers,
for my school project i need to request and receive data from my PHP server to my arduino attached with an ethernet shield
With help from the internet i kinda got the idea on how it works, allthough it doesn't really work yet so this is why i'm asking for help: i can't find the problem!
my arduino code:
#include <Ethernet.h>
#include <SPI.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
// if need to change the MAC address (Very Rare)
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 1, 177 };
byte dns[] = { 8, 8, 8, 8 };
EthernetClient client;
char server[] = "lifestyle.daandamhuis.nl";
LiquidCrystal_I2C lcd(0x27, 16, 2);
char inString[32]; // string for incoming serial data
int stringPos = 0; // string index counter
boolean startRead = false; // is reading?
void setup(){
Ethernet.begin(mac, ip, dns);
Serial.begin(9600);
lcd.init();
lcd.backlight();
lcd.clear();
lcd.setCursor(0,0);
}
void loop(){
String pageValue = connectAndRead(); //connect to the server and read the output
Serial.println(pageValue); //print out the findings.
delay(500); //wait 5 seconds before connecting again
}
String connectAndRead(){
//connect to the server
lcd.print("Connecting...");
Serial.println("Connecting...");
//port 80 is typical of a www page
if (client.connect(server, 80)) {
lcd.setCursor(0,1);
lcd.print("Connected");
Serial.println("Connected");
client.print("GET /backend/fetchAantalOpen.php HTTP/1.0");
client.println();
//Connected - Read the page
return readPage(); //go and read the output
}
else{
return "connection failed";
}
}
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...");
return inString;
}
}
}
}
}
the line: client.print("GET /backend/fetchAantalOpen.php HTTP/1.0");
/backend/ is the folder and fetchAantalOpen.php is the file in that folder i want to use.
my PHP server code:
<?php
//session_start();
//include('../scripts/php/connect.php');
//Vraag ID
//$id = $_GET['id'];
//Query samen stellen om te kijken of er een antwoord bestaat
//$query = "SELECT * FROM unread_awnsers WHERE gid = '" . $id . "'";
//$result = mysql_query($query);
//$count = mysql_num_rows($result);
$what_the_arduino_reads = '1'.base_convert(rand(10000,9999999), 10, 36);
echo '<'.$what_the_arduino_reads.'>';
?>
the serial.monitor on Arduino tells me it can connect, but after that it should be printing random words and numbers (coded in PHP) on my serial monitor, but that aint happening. anyone know what i am doing wrong?
thanks for the help!