hello,
i got an ethernetshield attached to the arduino with also a I2C LCD connected.
at my current project i do the following:
On a microSD card is a text file and within this text file is written a number (in this case number 6).
this SD card is plugged in the SDcardslot on the ethernetshield.
Also the ethernetshield will connect to a PHP (connected with a database) and i call a GET function that will return me information.
This information is printed on the LCD.
It will reconnect every 5 seconds to check if new data is send.
now my problem:
It works fine, but after like 4 reconnects it gets bugged.
i made it so, that i prints the data on the serial monitor and i get the following:
gebruiker:
6
is ingelogd
connecting...
connected
GET http://lifestyle.daandamhuis.nl/backend/fetchAantalOpen.php?id=6 HTTP/1.0
disconnecting.
Geen nieuw Antwoord
connecting...
connected
GET http://lifestyle.daandamhuis.nl/backend/fetchAantalOpen.php?id=6 HTTP/1.0
disconnecting.
Geen nieuw Antwoord
connecting...
connected
GET http://lifestyle.daandamhuis.nl/backend/fetchAantalOpen.php?id=6 HTTP/1.0
disconnecting.
Geen nieuw Antwoord
connecting...
connected
HTTP/1.0
disconnecting.
Nieuwe Antwoorden
As you can see at the 4th reconnect and reconnects after that will just print HTTP/1.0 and im not receiving what i want to receive.
Now i tested it with the SDcard code removed, and then the problem is fixed.
allthough, thats not the solution.
anyone has any idea how to fix this?
thanks!
//ARDUINO 1.0+ ONLY
//ARDUINO 1.0+ ONLY
#include <LiquidCrystal_I2C.h>
#include <Ethernet.h>
#include <SPI.h>
#include <Wire.h>
#include <SD.h>
////////////////////////////////////////////////////////////////////////
//CONFIGURE
////////////////////////////////////////////////////////////////////////
LiquidCrystal_I2C lcd(0x27,16,2);
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = {
192, 168, 1, 177 };
byte dnss[] = {
8, 8, 8, 8 };
//Connectie naar de Webserver
EthernetClient client;
char server[] = "lifestyle.daandamhuis.nl";
char inString[32]; // string for incoming serial data
int stringPos = 0; // string index counter
boolean startRead = false; // is reading?
//Controle variabelen voor de Notificaties
int oudAantal;
int nieuwAantal = 1;
int state;
int pageValue;
File usersd;
int userid;
int led1 = 7;
int led2 = 6;
int user;
void setup(){
Ethernet.begin(mac, ip, dnss);
Serial.begin(9600);
lcd.init();
lcd.backlight();
lcd.setCursor(0,0);
pinMode(10, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
if (!SD.begin(4)) {
Serial.println("initialization failed!");
return;
}
// re-open the file for reading{
usersd = SD.open("test.txt");
if (usersd) {
// read from the file until there's nothing else in it:
char data;
while ((data = usersd.read()) > 0)
{
user = data - '0'; // yes, accumulate the value
Serial.println("gebruiker:");
lcd.print("gebruiker:");
Serial.println(user);
lcd.print(user);
lcd.setCursor(0,1);
Serial.println("is ingelogd");
lcd.print("is ingelogd");
}
// close the file:
usersd.close();
}
else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
}
void loop(){
pageValue = connectAndRead(); //connect to the server and read the output
//pageValue = stringToInt(pageValue);
nieuwAantal = pageValue;
if (nieuwAantal == oudAantal) {
state = 1;
}
if (nieuwAantal != oudAantal) {
state = 2;
}
switch(state)
{
case 1:
Serial.println("Geen nieuw Antwoord");
oudAantal = nieuwAantal;
functie(nieuwAantal);
break;
case 2:
Serial.println("Nieuwe Antwoorden");
lcd.backlight();
oudAantal = nieuwAantal;
functie(nieuwAantal);
break;
}
//Serial.println(pageValue); //print out the findings.
delay(5000); //wait 5 seconds before connecting again
}
int connectAndRead(){
//connect to the server
Serial.println("connecting...");
//port 80 is typical of a www page
if (client.connect(server, 80)) {
Serial.println("connected");
String ConnectionString = "GET http://lifestyle.daandamhuis.nl/backend/fetchAantalOpen.php?id=";
ConnectionString = ConnectionString + user;
ConnectionString = ConnectionString + " HTTP/1.0";
Serial.println(ConnectionString);
client.println(ConnectionString);
client.println("Host: lifestyle.daandamhuis.nl");
client.println();
//Connected - Read the page
return readPage(); //go and read the output
}
else{
return 0; //Geen connectie met de server
}
}
int 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 stringToInt(inString);
}
}
}
}
}
int stringToInt(String number)
{
int length = number.length() + 1;
char arrayOfCharacters[length];
number.toCharArray(arrayOfCharacters, length);
arrayOfCharacters[length - 1] = '\0';
return atoi(arrayOfCharacters);
}
void functie(int getal)
{
if(getal == 1)
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Er is ");
lcd.print(pageValue);
lcd.print(" vraag");
lcd.setCursor(0,1);
lcd.print("beantwoord");
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
delay(3000);
lcd.noBacklight();
digitalWrite(7, LOW);
digitalWrite(8, LOW);
}
else if(getal == 0)
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Er zijn ");
lcd.print("geen");
lcd.print(" vra");
lcd.setCursor(0,1);
lcd.print("gen beantwoord");
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
delay(3000);
lcd.noBacklight();
digitalWrite(7, LOW);
digitalWrite(8, LOW);
}
else
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Er zijn ");
lcd.print(pageValue);
lcd.print(" vragen");
lcd.setCursor(0,1);
lcd.print("beantwoord");
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
delay(3000);
lcd.noBacklight();
digitalWrite(7, LOW);
digitalWrite(8, LOW);
}
}