I'm making website controlled lights. I made basic script which turn on and off led light(pin4)
I have arduino and ethernet shield from ebay.I have troubles after conenct it to power(ethernet is always connected)
It works 5-10minutes sometims one minute. aFter this time it no detect change in file on my site.
Code:
//ARDUINO 1.0+ ONLY
//ARDUINO 1.0+ ONLY
#include <Ethernet.h>
#include <SPI.h>
////////////////////////////////////////////////////////////////////////
//CONFIGURE
////////////////////////////////////////////////////////////////////////
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 0, 110 };
byte dnss[] = { 8, 8, 8, 8 };
EthernetClient client;
char server[] = "kaska3er.ugu.pl";
//The location to go to on the server
//make sure to keep HTTP/1.0 at the end, this is telling it what type of file it is
String location = "/siema.txt HTTP/1.0";
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, dnss);
Serial.begin(9600);
pinMode(6, OUTPUT);
}
void loop(){
String pageValue = connectAndRead(); //connect to the server and read the output
Serial.println(pageValue); //print out the findings.
if(pageValue == "wlacz")
{
digitalWrite(6, HIGH);
}
else if(pageValue == "wylacz")
{
digitalWrite(6, LOW);
}
delay(5000); //wait 5 seconds before connecting again
}
String connectAndRead(){
//connect to the server
Serial.println("connecting...");
//port 80 is typical of a www page
if (client.connect(server, 80)) {
Serial.println("connected");
client.println("GET http://kaska3er.ugu.pl/siema.txt HTTP/1.0");
client.println("Host: kaska3er.ugu.pl");
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;
}
}
}
}
}
Sometimes it can work 5-6h.But I need it works 24/7/365.
If it not work only RX led blink sometimes.
But if it work RX and TX blinks fast every 5 sec(delay(5000)
The use of String class confused me.
The 'location' is a String, a function returns a String, and you use an index [] of a String.
Is it possible to remove all String and use simple char buffer[] arrays ?
The return value of a function can be an integer (for example an error value).
//ARDUINO 1.0+ ONLY
//ARDUINO 1.0+ ONLY
#include <Ethernet.h>
#include <SPI.h>
////////////////////////////////////////////////////////////////////////
//CONFIGURE
////////////////////////////////////////////////////////////////////////
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 0, 110 };
byte dnss[] = { 8, 8, 8, 8 };
EthernetClient client;
char outputred[32];
char server[] = "kaska3er.ugu.pl";
//The location to go to on the server
//make sure to keep HTTP/1.0 at the end, this is telling it what type of file it is
char location[] = "/siema.txt HTTP/1.0";
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, dnss);
Serial.begin(9600);
pinMode(6, OUTPUT);
}
void loop(){
char *pageValue = connectAndRead(); //connect to the server and read the output
Serial.println(pageValue); //print out the findings.
if(strcmp(pageValue, "wlacz") == 0)
{
digitalWrite(6, HIGH);
}
else if(strcmp(pageValue, "wylacz") == 0)
{
digitalWrite(6, LOW);
}
delay(5000); //wait 5 seconds before connecting again
}
char *connectAndRead(){
//connect to the server
Serial.println("connecting...");
//port 80 is typical of a www page
if (client.connect(server, 80)) {
Serial.println("connected");
client.println("GET http://kaska3er.ugu.pl/siema.txt HTTP/1.0");
client.println("Host: kaska3er.ugu.pl");
client.println();
//Connected - Read the page
return readPage(); //go and read the output
}else{
return "connectfailed";
}
}
char *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;
}
}
}
}
}
I changed Strings to chars. It compiles good but i didn't test it.
There is still a mix between String and char buffer[] array.
The 'pageValue' is a char buffer, but you put a String into it from connectAndRead().
Also the readPage() returns a String class, but in that function you return a pointer to a global char buffer.
I think you have mixed two examples, one with the String class and one with char buffer[] array.
Ofcourse, it is possible to use the String class, it makes a smaller an better to read code. But in your sketch with the mix of them, I suggest to use good old char buffer[] arrays.
I connected it and it work very well. I write a post on the morning because i leave it powered for a night.
Sorry for my bad code because i'm 13 years old
Can I remove delay(5000) in void loop or do it smaller 1-2sec?
Sorry, I don't know yet.
Do you use the newest Arduino IDE version 1.0.5 or 1.5.7 BETA ?
I could be a missing connection due to ethernet or a router. At least make something so it can get out of the endless while-loop when the client is no longer available.
I'm not sure about your code, but I noticed that you do a flush after a stop. I think you should flush first and stop after that.
//ARDUINO 1.0+ ONLY
//ARDUINO 1.0+ ONLY
#include <Ethernet.h>
#include <SPI.h>
////////////////////////////////////////////////////////////////////////
//CONFIGURE
////////////////////////////////////////////////////////////////////////
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 0, 110 };
byte dnss[] = { 8, 8, 8, 8 };
EthernetClient client;
char outputred[32];
char server[] = "kaska3er.ugu.pl";
//The location to go to on the server
//make sure to keep HTTP/1.0 at the end, this is telling it what type of file it is
char location[] = "/siema.txt HTTP/1.0";
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, dnss);
Serial.begin(9600);
pinMode(6, OUTPUT);
}
void loop(){
char *pageValue = connectAndRead(); //connect to the server and read the output
Serial.println(pageValue); //print out the findings.
if(strcmp(pageValue, "wlacz") == 0)
{
digitalWrite(6, HIGH);
}
else if(strcmp(pageValue, "wylacz") == 0)
{
digitalWrite(6, LOW);
}
delay(5000); //wait 5 seconds before connecting again
}
char *connectAndRead(){
//connect to the server
Serial.println("connecting...");
//port 80 is typical of a www page
if (client.connect(server, 80)) {
Serial.println("connected");
client.println("GET http://kaska3er.ugu.pl/siema.txt HTTP/1.0");
client.println("Host: kaska3er.ugu.pl");
client.println();
//Connected - Read the page
return readPage(); //go and read the output
}else{
return "connectfailed";
}
}
char *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.flush();
client.stop();
Serial.println("disconnecting.");
return inString;
}
}
}
}
}
My internet connection is good i use Facebook,skype at the same time without problem.
I'm testing now option with first flush then stop
There is one more thing....
You do this: return "connectfailed";
I think that is not good programming. That string is locally inside the function, and yet, you bring a pointer to it outside that function. I think it is better to either copy that string or use a global string.
I tested it with changed memset and it work only for 15 minutes.Error is in the same code(client.available).
Now i'm testing with changed return "" to return (msgConenctionFailed)
Code actually looks that:
//ARDUINO 1.0+ ONLY
//ARDUINO 1.0+ ONLY
#include <Ethernet.h>
#include <SPI.h>
////////////////////////////////////////////////////////////////////////
//CONFIGURE
////////////////////////////////////////////////////////////////////////
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 0, 110 };
byte dnss[] = { 8, 8, 8, 8 };
EthernetClient client;
char outputred[32];
char msgConnectionFailed[] = "connectfailed";
char server[] = "kaska3er.ugu.pl";
//The location to go to on the server
//make sure to keep HTTP/1.0 at the end, this is telling it what type of file it is
char location[] = "/siema.txt HTTP/1.0";
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, dnss);
Serial.begin(9600);
pinMode(6, OUTPUT);
}
void loop(){
char *pageValue = connectAndRead(); //connect to the server and read the output
Serial.println(pageValue); //print out the findings.
if(strcmp(pageValue, "wlacz") == 0)
{
digitalWrite(6, HIGH);
}
else if(strcmp(pageValue, "wylacz") == 0)
{
digitalWrite(6, LOW);
}
delay(5000); //wait 5 seconds before connecting again
}
char *connectAndRead(){
//connect to the server
Serial.println("connecting...");
//port 80 is typical of a www page
if (client.connect(server, 80)) {
Serial.println("connected");
client.println("GET http://kaska3er.ugu.pl/siema.txt HTTP/1.0");
client.println("Host: kaska3er.ugu.pl");
client.println();
//Connected - Read the page
return readPage(); //go and read the output
}else{
return (msgConnectionFailed);
}
}
char *readPage(){
//read the page, and capture & return everything between '<' and '>'
stringPos = 0;
memset(inString, 0, 32 ); //clear inString memory
while(true){
Serial.println("poczatek petli");
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.flush();
client.stop();
Serial.println("disconnecting.");
return inString;
}
}
}
}
}
I've added code in readPage function. It return error message if client is not available. Now i'm testing it.
//ARDUINO 1.0+ ONLY
//ARDUINO 1.0+ ONLY
#include <Ethernet.h>
#include <SPI.h>
////////////////////////////////////////////////////////////////////////
//CONFIGURE
////////////////////////////////////////////////////////////////////////
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 0, 110 };
byte dnss[] = { 8, 8, 8, 8 };
EthernetClient client;
char outputred[32];
char clienterr[] = "error";
char msgConnectionFailed[] = "connectfailed";
char server[] = "kaska3er.ugu.pl";
//The location to go to on the server
//make sure to keep HTTP/1.0 at the end, this is telling it what type of file it is
char location[] = "/siema.txt HTTP/1.0";
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, dnss);
Serial.begin(9600);
pinMode(6, OUTPUT);
}
void loop(){
char *pageValue = connectAndRead(); //connect to the server and read the output
Serial.println(pageValue); //print out the findings.
if(strcmp(pageValue, "wlacz") == 0)
{
digitalWrite(6, HIGH);
}
else if(strcmp(pageValue, "wylacz") == 0)
{
digitalWrite(6, LOW);
}
delay(5000); //wait 5 seconds before connecting again
}
char *connectAndRead(){
//connect to the server
Serial.println("connecting...");
//port 80 is typical of a www page
if (client.connect(server, 80)) {
Serial.println("connected");
client.println("GET http://kaska3er.ugu.pl/siema.txt HTTP/1.0");
client.println("Host: kaska3er.ugu.pl");
client.println();
//Connected - Read the page
return readPage(); //go and read the output
}else{
return (msgConnectionFailed);
}
}
char *readPage()
{
//read the page, and capture & return everything between '<' and '>'
stringPos = 0;
memset(inString, 0, 32 ); //clear inString memory
while(true)
{
Serial.println("poczatek petli");
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.flush();
client.stop();
Serial.println("disconnecting.");
return inString;
}
}
}
else
{
return (clienterr);
}
}
}
You print the message "poczatek petli" for every single character that is received.
Perhaps you can print only the character that was received, so you can see what is received.
You print "poczatek petli" for every single character that is received.
Perhaps that causes a timeout for the ethernet.
According to the messages in the serial monitor, I think it works for the first time, but fails after that.