First of all, Arduino is amazing! I have been struggling with PIC programming in the past but being a C++/C# programmer, this makes more sense to me than assembler 8)
Goal
My aim is to automate some appliances at home and be able to see what's going on with the house from the internet/smartphone.
Problem
Something seems to stall/freeze. The serial output falls in to an infinite loop, as if it were infinite web requests. After a minute or so, everything just freezes. I can see that the led on the shield is still responding to manual web requests or pings but no responds from either the board or the serial output. I've been testing for a few days and can't recreate the problem on-demand, it's quite random.
I've tried attaching WDT to the loop() but it doesn't kick in.
I've tried monitoring the memory but nothing seems to change there (if I'm doing it right).
I've tried moving all serial communication past the e.respond() without any effects.
I've tried scaling the code down, a lot, removing sensors etc without any effects.
I've tried with different browsers and computers (clients).
I'm wondering if it's a malformed web request that the code below can't handle or something wrong with the shield/hardware.
Hardware used
Arduino Uno R3
Ethershield v1.1 - ENC28J60 based, without the SD card extension.
Relay connecting for testing
DHT11 for temp/humidity reading.
Here's the code
/*
Internal adress is 192.168.1.25, port 88. External adress is 213.xxx.xxx.84, port 88.
Router is configured to forward this port to the internal ip of the Arduino.
Used pins are
D13 - SCK
D12 - MISO
D11 - MOSI
D10 - SS for ethernet control
Available pins are
D01-09
A0-5
*/
#include "etherShield.h" //For networking, by Xing Yu
#include "ETHER_28J60.h" //For networking, by Simon Monk
#include "dht.h" //For DHT11 temp/hum sensor
//Mac address, ip address and port
static uint8_t mac[6] = {
0x54, 0x55, 0x58, 0x10, 0x00, 0x24};
static uint8_t ip[4] = {
192, 168, 1, 25};
static uint16_t port = 88;
ETHER_28J60 e;
int Pin_Relay1 = 5;
dht DHT;
#define DHT11_PIN 8
void setup()
{
Serial.begin(9600);
e.setup(mac, ip, port);
pinMode(Pin_Relay1, OUTPUT);
}
void loop()
{
char* params;
DHT.read11(DHT11_PIN); //Initiate the sensor
int valSensor1 = DHT.temperature; //Read the temp.
int valSensor2 = DHT.humidity; //Read the hum.
if (params = e.serviceRequest()) <---- Could this be the problem? Malformed service requests from the client?
{
e.print("<H3>Web Remote</H3>");
e.print("Available modules are
");
e.print("<FORM>");
//Look for Relay1
if (strcmp(params, "?relay1_cmd=on") == 0)
{
e.print("<INPUT TYPE=\"BUTTON\" VALUE=\"Turn Off\" ONCLICK=\"window.location.href='http://213.xxx.xxx.84:88/?relay1_cmd=off'\">");
digitalWrite(Pin_Relay1, HIGH);
}
else if (strcmp(params, "?relay1_cmd=off") == 0)
{
e.print("<INPUT TYPE=\"BUTTON\" VALUE=\"Turn On\" ONCLICK=\"window.location.href='http://213.xxx.xxx.84:88/?relay1_cmd=on'\">");
digitalWrite(Pin_Relay1, LOW);
}
else
{
e.print("<INPUT TYPE=\"BUTTON\" VALUE=\"Turn On\" ONCLICK=\"window.location.href='http://213.xxx.xxx.84:88/?relay1_cmd=on'\">");
}
//Read sensors and write out their values
e.print("
Status of sensors and relays");
e.print("
Sensor 1 = "); e.print(valSensor1);
e.print("
Sensor 2 = "); e.print(valSensor2);
e.print("
Relay 1 = ");
if(digitalRead(Pin_Relay1) == 0) e.print("off");
else e.print("on");
e.print("
[<a href=\"javascript:location.reload(true);\">Refresh</a>] [<a href='http://213.xxx.xxx.84:88/'>Home</a>]");
e.print("</FORM>");
e.respond();
//Add serial communication after e.respond()
Serial.print("Parameters: ");
Serial.println(params);
Serial.print("Relay 1 is ");
if(digitalRead(Pin_Relay1) == 0) Serial.println("off");
else Serial.println("on");
Serial.print("Temperature (C): ");
Serial.println(valSensor1);
Serial.print("Humidity (%): ");
Serial.println(valSensor2);
Serial.print("Free mem: ");
Serial.println(FreeRam());
Serial.println("--------------------------------------");
}
}
int FreeRam () {
extern int __heap_start, *__brkval;
int v;
return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
}
The serial output looks good but suddenly it just starts repeating/looping and there are no responses to either web requests or pings.
(I do get 2 responds to each request, but I think it's the browser requesting "favicon.ico" after each respond)
Parameters: <------ First web request to /
Relay 1 is off
Temperature (C): 11
Humidity (%): 157
Free mem: 619
--------------------------------------
Parameters: favicon.ico <--- "Ghost" request
Relay 1 is off
Temperature (C): 22
Humidity (%): 48
Free mem: 619
--------------------------------------
Parameters: ?relay1_cmd=on <--- Web request from pressing the button
Relay 1 is on
Temperature (C): 22
Humidity (%): 36
Free mem: 619
--------------------------------------
Parameters: favicon.ico <--- "Ghost" request
Relay 1 is on
Temperature (C): 22
Humidity (%): 39
Free mem: 619
--------------------------------------
Parameters: ÿÿ}þ <---- This appears randomly, sometimes it's another set of characters like CR/LF
Relay 1 is off
Temperature (C): 23
Humidity (%): 38
Free mem: 619
--------------------------------------
Parameters: ÿÿ}þ <---- This repeats for about a minute
Relay 1 is off
Temperature (C): 23
Humidity (%): 38
Free mem: 619
--------------------------------------
....
--------------------------------------
....
--------------------------------------
....
--------------------------------------
<--- The entire thing just locks up
Let me know what you think!