Today i used a different power line, and did some additional testing without the 220v connected on the relay and had the same issue.
So i`m thinking the issue might not be the power supply. It could be the code, and the fact that the wifi connection (using a repeater )might be a bit slow. I have been "playing" with the sketch for the past day, with no result. I have tried differnt arduino devices, relays, sensors, power supplies, repeaters and so on. I have increased free memory on the sketch (a different version of the code was using a character buffer, millis, etc).
So... i will upload the sketch and wait for your help:
#include <avr/wdt.h>
#include "DHT.h"
#include <Ethernet.h>
#include <SPI.h>
byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x01 }; // RESERVED MAC ADDRESS
int aisthKinPin = 10; //Aisthitiras Kinisis
int valKin = 0;
byte ip[] = { 192, 168, 42, 59 }; // ip in lan (that's what you need to use in your browser. ("192.168.1.178")
byte gw[] = {192,168,42,5};
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
byte server[] = { 192, 168, 42, 21 }; // Server IP
const char comp[] PROGMEM = {"!01"}; //Tha xrisimopoihthei apo ton parser
boolean pars = false; //Otan ginei true tote arxizei na katagrafei tous xaraktires sto readString
boolean enableAlarm = false;
#define REAP 2 //Repeater
#define RELAY2 3 //Seirina
#define DHTPIN 5 // SENSOR PIN
#define RELAY3 6 //Sintribani
#define RELAY4 7 //Fwta 1,2
#define Led 8
#define RELAY5 9 //Fwta 3
EthernetClient client;
#define DHTTYPE DHT22 // SENSOR TYPE - THE ADAFRUIT LIBRARY OFFERS SUPPORT FOR MORE MODELS
DHT dht(DHTPIN, DHTTYPE);
unsigned long count = 100000000;
float t = 0; // TEMPERATURE VAR
float h = 0; // HUMIDITY VAR
int alarmBol=0;
void setup() {
pinMode (REAP, OUTPUT);
pinMode (RELAY2, OUTPUT);
pinMode (RELAY3, OUTPUT);
pinMode (RELAY4, OUTPUT);
pinMode (RELAY5, OUTPUT);
pinMode (Led, OUTPUT);
pinMode(4, OUTPUT); // SD select pin
Serial.begin(9600);
wdt_enable(WDTO_8S);
wdt_reset();
Serial.println(MCUSR);
Serial.println(WDTCSR);
Serial.flush();
//wdt_reset();
Ethernet.begin(mac, ip, gw, gw, subnet);
dht.begin();
delay(500); // GIVE THE SENSOR SOME TIME TO START
// wdt_reset();
h = dht.readHumidity();
t = dht.readTemperature();
}
void loop(){
boolean notPostTemp=true;
alarmBol=0;
wdt_reset();
Serial.println(t);
int serialByte = Serial.read();
if(serialByte=='A')
{
Serial.println("Reset WDT");
wdt_reset();
}
else if(serialByte=='Z')
{
wdt_enable(WDTO_15MS);
}
valKin = digitalRead(aisthKinPin);
//char charBuf[7];
digitalWrite (Led, HIGH);
wdt_reset();
wdt_reset();
delay(100);
if(count> 7200) { // READ ONLY ONCE PER INTERVAL
h = dht.readHumidity();
t = dht.readTemperature();
//Serial.println("Arxizei to connect:");
wdt_reset();
if (client.connect(server,80)) { // REPLACE WITH YOUR SERVER ADDRESS
//Serial.println("Connected");
client.print("GET /writeSql2.php?");
delay(20);
//Serial.print("GET /writeSql2.php?");
client.print("temp2=");
delay(20);
//Serial.print("temp2=");
client.print(t);
delay(20);
//Serial.print(t);
client.print("&hum2=");
delay(20);
//Serial.print("&hum2=");
client.print(h);
delay(20);
//Serial.print(h);
client.println(" HTTP/1.1");
delay(20);
//Serial.println(" HTTP/1.1");
client.println("Host: 192.168.42.21" );
delay(20);
wdt_reset();
//Serial.println("Host: 192.168.42.21" );
client.println("Content-Type: application/x-www-form-urlencoded" );
delay(20);
//Serial.println("Content-Type: application/x-www-form-urlencoded" );
client.println( "Connection: close" );
delay(20);
//Serial.println( "Connection: close" );
client.println();
delay(20);
//Serial.println();
delay(20);
client.println();
//Serial.println();
}
delay(20);
client.stop(); // DISCONNECT FROM THE SERVER
//Serial.println("Client Stop");
// resetFunc();
count=0;
notPostTemp=false;
}
if (notPostTemp){
if (client.connect(server,80)) { // REPLACE WITH YOUR SERVER ADDRESS
if(valKin==1) {
alarmBol=1;
}
//alarmBol=1;
//Serial.println("Connected");
client.print("GET /alarmSqlAuli.php?");
delay(20);
//Serial.print("GET /alarmSqlAuli.php?");
delay(20);
client.print("alarm1=");
delay(20);
//Serial.print("alarm1=");
client.print(alarmBol);
delay(20);
wdt_reset();
//Serial.print(alarmBol);
client.println(" HTTP/1.1");
delay(20);
//Serial.println(" HTTP/1.1");
delay(20);
client.println("Host: 192.168.42.21" );
delay(20);
//Serial.println("Host: 192.168.42.21" );
client.println("Content-Type: application/x-www-form-urlencoded" );
delay(20);
//Serial.println("Content-Type: application/x-www-form-urlencoded" );
client.println( "Connection: close" );
delay(20);
//Serial.println( "Connection: close" );
client.println();
delay(20);
//Serial.println();
client.println();
delay(20);
//Serial.println();
}
wdt_reset();
delay(300);
// wdt_reset();
int countClient=0;
while(client.connected()) {
if (client.available()) {
char c = client.read();
if (c==comp[0]) {
pars=true;
// Serial.println(pars);
}
if(pars) {
if((c==comp[1])&&(countClient==1)){
digitalWrite(RELAY4, HIGH);
digitalWrite(RELAY5, HIGH);
}
if((c==comp[2])&&(countClient==1)){
// Serial.print("Anoixto Fws");
digitalWrite(RELAY4, LOW);
digitalWrite(RELAY5, LOW);
}
if((c==comp[2])&&(countClient==1)&&(enableAlarm)){
// Serial.print("Xtipise sunagermosfwta");
digitalWrite(RELAY4, LOW);
digitalWrite(RELAY5, LOW);
digitalWrite(RELAY2, LOW);
}
if((c==comp[2])&&(countClient==3)) {
enableAlarm=true;
// Serial.println("Sunagermos Energos");
}
if((c==comp[1])&&(countClient==3)) {
enableAlarm=false;
// Serial.println("Sunagermos Anenergos");
}
if((c==comp[2])&&(countClient==4)){
// Serial.print("Anapse Sirina");
digitalWrite(RELAY2, LOW);
}
if((c==comp[1])&&(countClient==4)){
// Serial.print("Esbise Sirina");
digitalWrite(RELAY2, HIGH);
}
if((c==comp[2])&&(countClient==5)){
// Serial.print("Anoixto Syntribani");
digitalWrite(RELAY3, LOW);
// wdt_reset();
}
if((c==comp[1])&&(countClient==5)){
// Serial.print("Anoixto Syntribani");
digitalWrite(RELAY3, HIGH);
wdt_reset();
}
countClient++;
}
}
}
pars=false;
client.stop();
//Serial.println(readString);
Serial.println("EDW");
// wdt_reset();
digitalWrite (Led, LOW);
// wdt_reset();
delay(300);
// wdt_reset();
//Serial.print(charBuf[i]);
pars=false;
client.stop(); // DISCONNECT FROM THE SERVER
//Serial.println("Client Stop");
// wdt_reset();
}
digitalWrite (Led, LOW);
delay(300);
// wdt_reset();
//client.flush();
//client.stop();
//readString.remove(7);
client.flush();
count++;
}
// delay(300000); // WAIT FIVE MINUTES BEFORE SENDING AGAIN
I have made a lot of changes and lost track. As you can see i use an Ethernet Shield, a DHT sensor, PIR sensor and a relay. The code crashes after a while (if watchdog is not activated) or keeps reseting (wathdog) which in a way allows the code to work...but keeps reseting the relay too...creating issues to the device connected to the relay (power off and on every minute).
So the arduino connects to a php page and posts temperature data, pir sensor data and reads the values of that page regarding the status of the relays (reads the web page and waits for the "!" character. Once found it parses the rest of the characters and depending on the values it handles the relays. So the webpage looks like this !10000 which means that RELAY4 and 5 should be switched on (Relay4 and 5 = lights, Relay2=siren, Relay3=water pump.
A similar version of the code is used on different arduinos across my house and work ok, however they are connected the network using a lan cable and not a repeater.
Do you have any suggestions with the code? Thank you!!