Hi,
I have the below code which basically listens for a serial command comming over RF.
When a certain serial command is received, it then calls a PHP script depending on the command received.
All works well, for the first and normally second reception for this command, but on the third reception, the PHP is not called and an error is produced. Sometimes this happens after the second reception and it hangs for a littlewhile on 'Alarm Activated (pin 11)' and then produces the predefined error 'failed to call PHP'.
I have also had to place in a RESET command at the end of the alarm_1 function as without it, the Arduino seems to crash, on the third 'activation' before even calling the PHP script.
The code is fairly modular, but don't know if there is something glaringly wrong......its driving me mad.
If I call the PHP from a web browser, it works fine, no matter how many times it is called.
Any help would be greatly appricated
static String ident = "server"; // Identification for this device
#include <VirtualWire.h> // 433Mhz Module Library
#include <SPI.h>
#include <Ethernet.h> // Ethernet Library
#include <DHT.h> // DHT Temp & Humidity Library
byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x79, 0xED };
byte ip[] = { 192,168,1,142 }; // local Arduino IP
//byte gateway[] = { 192,168,1,1 }; // IP of your gateway
//byte subnet[] = { 255, 255, 255, 0 };
byte server[] = { 192,168,1,101 }; // IP of your web server
//Server server(80);
Client client(server, 80);
const int ledPin = 13; // The number of the LED pin, LED used to indicate we got a message
long onMillis = 0; // Will store last time LED was updated
// Uncomment whatever type of Temp sensor being used!
#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT22 // DHT 22 (AM2302)
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
#define DHTPIN 8 // Sets DHT Temp Sensor Pin
// *** Temperature Variables ***
#define maxTemp 35 // Fan will start at this temperature
#define minTemp 32 // Sets Temp Hysteresis variation (3 degress)
// *** RF Variables ***
#define rxPin 2 // Sets 433Mhz Rx Pin
// *** Other Variables ***
#define startPin 11 // The first pin we connect a relay to
#define stopPin 12 // The last pin we connect a relay to
#define fanPin 5
//Set-up Temperature Sensor
DHT dht(DHTPIN, DHTTYPE);
void(* resetFunc) (void) = 0; //declare reset function @ address 0
void setup()
{
Ethernet.begin(mac, ip);
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
//Set-up the RF receiver
vw_setup(1000); // Bits per sec
vw_set_rx_pin(rxPin); // We will be receiving on pin 2 ie the RX pin from the module connects to this pin.
vw_rx_start(); // Start the receiver
dht.begin(); // Begin reading DHT sensor
for (int i = startPin; i <= stopPin; i++) // We need to set the pins that control the relays to OUTPUT
{
Serial.print("Setting pinMode on pin: ");
Serial.println(i);
pinMode(i, OUTPUT);
digitalWrite(i, HIGH); // We set the pin to HIGH because pulling it LOW will turn the relay on
}
Serial.println("Ready to receive!");
}
void loop()
{
temperature(); // Calls Temperature function
uint8_t buf[VW_MAX_MESSAGE_LEN];
uint8_t buflen = VW_MAX_MESSAGE_LEN;
if (vw_get_message(buf, &buflen)) // Check to see if anything has been received
{
String message;
for (int i = 0; i < buflen; i++) // Run through the stuff we just received
{
message += buf[i]; // Append the stuff to a String
}
if (message.substring(0,ident.length()) == ident) // Check if this message was for us
{
digitalWrite(ledPin, HIGH); // Turn led on to indicate we received something, and is going to look more at it
onMillis = millis(); // Save when led was turned on so we remember to turn it off again later
message = message.substring(ident.length()+1); // Remove the identifier from the message
int startCommand = message.indexOf('-'); // Find where we split pin number from pin status
String getPinNum = message.substring(0, startCommand); // Save a String with the pin number
String getPinSet = message.substring(startCommand+1, message.length()); // Save a String with the value we are going to send to the pin
char tempNum[2]; //Char to hold the message before converting it to an integer
tempNum[getPinNum.length() + 1]; // Temp save pin number
getPinNum.toCharArray(tempNum, getPinNum.length() + 1); // Put the temp number into a char array
int pinNum = atoi(tempNum); // Convert char array to an integer
tempNum[getPinSet.length() + 1]; // Temp save pin value
getPinSet.toCharArray(tempNum, getPinSet.length() + 1); // Put the temp value into a char array
int pinSet = atoi(tempNum); // Convert char array to an integer
Serial.print("Pin num: ");
Serial.print(pinNum);
Serial.print(" - Pin status: ");
Serial.println(pinSet);
// digitalWrite(pinNum, pinSet); // Set the pin defined earlier
if (pinNum == 11)
{
Serial.println("Alarm Activated (pin 11)");
alarm_1();
}
if (pinNum == 12)
{
Serial.println("Alarm Activated (pin 12)");
alarm_2();
}
onMillis = millis();
}
if (millis() - onMillis > 250 && onMillis > 0) // Check if it is time to turn the led off
{
onMillis = 0;
digitalWrite(ledPin, LOW);
}
}
}
void alarm_1(){
unsigned long startTime = millis();
if (client.connect()) { //connect to server
Serial.println("connected to server");
// Make a HTTP request:
client.println("GET /alarm_1.php HTTP/1.0"); //location of ProwlPHP script
client.println();
delay(1000);
}
else {
Serial.println("failed to call PHP");
}
delay(1000);
Serial.print("Response from server: ");
while ((!client.available()) && ((millis() - startTime ) < 5000));
while (client.available()) {
char c = client.read();
Serial.print(c);
}
// if the server's disconnected, stop the client:
if (!client.connected()) {
Serial.println("disconnecting from server");
client.stop();
client.flush();
Serial.println("Disconnected...");
}
resetFunc(); //call reset
}
void alarm_2(){
resetFunc(); //call reset
}
void temperature (){
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float humidity = dht.readHumidity();
float temp = dht.readTemperature();
// check if returns are valid, if they are NaN (not a number) then something went wrong!
if (isnan(temp) || isnan(humidity)) {
Serial.println("Failed to read from DHT");
} else {
if (temp >= maxTemp){
Serial.print("Temperature: ");
Serial.print(temp);
Serial.println(" *C");
fan(true);}
else if (temp <= minTemp)
{fan(false);}
}
if (temp <= maxTemp && temp >= minTemp){
Serial.print("Temperature: ");
Serial.print(temp);
Serial.println(" *C");}
}
// if (temp >= minTemp || temp >= minTemp-hysterine){
// tone(3,1000);
// Serial.print("Humidity: ");
// Serial.print(humidity);
// Serial.print(" %\t");
// Serial.print("Temperature: ");
// Serial.print(temp);
// Serial.println(" *C");
// } else {
// Serial.println("Ready to receive!");
// }
// }
void fan(boolean stat){
digitalWrite(fanPin, stat);
}
This is the output from a serial debug window.
Setting pinMode on pin: 11
Setting pinMode on pin: 12
Ready to receive!
Pin num: 11 - Pin status: 1
Alarm Activated (pin 11)
connected to server
Response from server: HTTP/1.1 200 OK
Connection: close
Date: Tue, 06 Sep 2011 14:33:36 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
X-Powered-By: PHP/5.2.6
Content-type: text/html
Sent. 992 Messages left. (Resets at: 2011-09-06 15:13:16)
disconnecting from server
Disconnected...
Setting pinMode on pin: 11
Setting pinMode on pin: 12
Ready to receive!
Pin num: 11 - Pin status: 1
Alarm Activated (pin 11)
failed to call PHP
Response from server: disconnecting from server
Disconnected...
Setting pinMode on pin: 11
Setting pinMode on pin: 12
Ready to receive!