Hello, i have a setup of cloned arduino nano ethernet module W5500 and relay. I am trying based on request sent via http server into arduino nano to turn on and off a relay which is closing and opening 240V power cable from wall which is powering a radiator. At least thats basically what it should be doing. But there is an issue, sometimes when i turn on the relay the server just turns off and wont come back so i have to manually restart the arduino to make it work for some time. The times the relay can be turned on and off before the server shuts down is on average 5 times it is random. Have written two files of code to see if another way of receiving requests thats working.
In one there is exactly the issue that the server shuts down sometimes when relay turns on tha calble.
#include <Ethernet.h>
#include <SPI.h>
#include <OneWire.h>
#include <DallasTemperature.h>
byte mac[] = { 0x30, 0x46, 0x9A, 0x32, 0x7D, 0x5A };
IPAddress ip(192, 168, 4, 228);
EthernetServer server(80);
#define ONE_WIRE_BUS 4
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
DeviceAddress insideThermometer;
#define ONE_WIRE_BUS2 5
OneWire oneWire2(ONE_WIRE_BUS2);
DallasTemperature sensors2(&oneWire2);
DeviceAddress insideThermometer2;
String command;
float numericCommand;
const int relay = 2;
bool connection_on;
bool get_to_hys = true;
float ref;
float hys;
String request;
void setup() {
sensors.begin();
sensors.getAddress(insideThermometer, 0);
sensors.setResolution(insideThermometer, 9);
sensors2.begin();
sensors2.getAddress(insideThermometer2, 0);
sensors2.setResolution(insideThermometer2, 9);
pinMode(relay, OUTPUT);
Ethernet.begin(mac, ip);
server.begin();
Serial.begin(9600);
}
float measure_temp() {
sensors.requestTemperatures();
float tempC = sensors.getTempC(insideThermometer);
return tempC; // Return temperature value
//delay(10);
}
float measure_temp2() {
sensors2.requestTemperatures();
float temp2C = sensors2.getTempC(insideThermometer2);
return temp2C; // Return temperature value
//delay(10);
}
float customRound(float value) {
float roundedValue = round(value * 2) / 2.0; // Round to nearest half integer
return roundedValue;
}
void loop() {
float temperature = measure_temp();
float temperature2 = measure_temp2();
String strtemp = String(temperature);
String strtemp2 = String(temperature2);
EthernetClient client = server.available();
Serial.print("client = ");
Serial.println(server.available());
if (client) {
Serial.println("Client is true");
request = "";
while (client.connected()) {
Serial.println("in while") ;
if (client.available()) {
Serial.println("in if");
char c = client.read();
request += c;
if (c == '\n'){
client.print("Vnutorna teplota: ");
client.print(strtemp);
client.println("C");
client.print("Vonkajsia teplota: ");
client.print(strtemp2);
client.println("C");
client.println();
client.print("Now: ");
if (connection_on == true){
client.println("On");
}
else if (connection_on == false){
client.println("Off");
}
client.println();
client.print("Vypinacia teplota: ");
client.println(ref);
client.print("Zapinacia teplota: ");
client.println(hys);
client.stop();
break;
}
}
}
request.remove(0,5);
request.remove(request.length() - 11);
Serial.println(request);
if (request != "favicon.ico" && request != "alvik/2.1.0" && request != "r" && request != ""){
command = request;
Serial.println(command);
}
}
Serial.print("Connecton: ");
Serial.println(connection_on);
if (command == "on"){
connection_on = true;
hys = 0;
ref = 0;
}
if (command == "off"){
connection_on = false;
hys = 0;
ref = 0;
}
if (command == "00"){
hys = 0;
}
numericCommand = command.toFloat();
if (numericCommand != 0){
if (numericCommand > 900 && numericCommand < 1100){
ref = numericCommand-1000;
ref = customRound(ref);
}
if (numericCommand < 900){
hys = numericCommand;
hys = customRound(hys);
}
}
if (hys != ref && ref > hys){
if (ref > temperature && get_to_hys == true && temperature > hys){
connection_on = true;
}
if (hys >= temperature){
get_to_hys = true;
connection_on = true;
}
if (ref <= temperature){
connection_on = false;
get_to_hys = false;
}
}
if (connection_on == true){
digitalWrite(relay, HIGH);
Serial.println("HIGH done");
}
else if (connection_on == false || command == "vyp"){
digitalWrite(relay, LOW);
}
}
in the second file the http server is not shutting down but sometimes the relay just turns off on itself but mainly when i power up the arduino with uploaded sketch inside i have to manually press onboard button to restart it few times to make it work as i wanted.
Here is the second one:
#include <OneWire.h>
#include <DallasTemperature.h>
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0x30, 0x46, 0x9A, 0x32, 0x7D, 0x5A }; //server stuff
IPAddress ip(192, 168, 4, 228);
EthernetServer server(80);
#define ONE_WIRE_BUS 4
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
DeviceAddress insideThermometer;
#define ONE_WIRE_BUS2 5
OneWire oneWire2(ONE_WIRE_BUS2);
DallasTemperature sensors2(&oneWire2);
DeviceAddress insideThermometer2;
String command;
float numericCommand;
const int relay = 6;
bool connection_on;
bool get_to_hys = true;
float ref;
float hys;
void setup(void) {
Serial.begin(9600);
sensors.begin();
sensors.getAddress(insideThermometer, 0);
sensors.setResolution(insideThermometer, 9);
sensors2.begin();
sensors2.getAddress(insideThermometer2, 0);
sensors2.setResolution(insideThermometer2, 9);
pinMode(relay, OUTPUT);
//digitalWrite(3,LOW);
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
while (true);
}
//Serial.print("Server IP Address: ");
//Serial.println(Ethernet.localIP());
server.begin();
//pinMode(8,INPUT_PULLUP);
}
float measure_temp() { // Changed return type to float
sensors.requestTemperatures();
float tempC = sensors.getTempC(insideThermometer);
return tempC; // Return temperature value
//delay(10);
}
float measure_temp2() { // Changed return type to float
sensors2.requestTemperatures();
float temp2C = sensors2.getTempC(insideThermometer2);
return temp2C; // Return temperature value
//delay(10);
}
float customRound(float value) {
float roundedValue = round(value * 2) / 2.0; // Round to nearest half integer
return roundedValue;
}
void loop(void) {
float temperature = measure_temp();
float temperature2 = measure_temp2();// Assign the return value of measure_temp() to a variable
String strtemp = String(temperature);
String strtemp2 = String(temperature2);
//Serial.print("Temperature 1: ");
//Serial.println(temperature);
//Serial.print("Temperature 2: ");
//Serial.println(temperature2);
delay(10);
// Listen for incoming clients
EthernetClient client = server.available();
//delay(10);
//Serial.println(server.available());
if (true) {
//String strtemp = String(temperature);
//String strtemp2 = String(temperature2);
String request = client.readStringUntil('\r');
//Serial.println(request);
int pathStart = request.indexOf(' ') + 1; // Find the position of the first space character
int pathEnd = request.indexOf(' ', pathStart); // Find the position of the next space character after the first space
String requestedPath = request.substring(pathStart, pathEnd); // Extract the substring between the two space characters
requestedPath.remove(0, 1); // odstrani sa /
if (requestedPath != "favicon.ico" && requestedPath != "alvik/2.1.0" && requestedPath != "r" && requestedPath != ""){
command = requestedPath;
//Serial.print("Request: ");
//Serial.println(requestedPath);
}
else{
command = command;
}
//client.stop();
client.print("Vnutorna teplota: ");
client.print(strtemp);
client.println("C");
client.print("Vonkajšia teplota: ");
client.print(strtemp2);
client.println("C");
client.println();
client.print("Aktualne: ");
if (connection_on == true){
client.println("ZAPNUTE");
}
else if (connection_on == false){
client.println("vypnute");
}
client.println();
client.print("Vypinacia teplota: ");
client.println(ref);
client.print("Zapinacia teplota: ");
client.println(hys);
}
client.stop();
//Serial.println("Client disconnected");
if (command == "zap" || connection_on == true){
connection_on = true;
hys = 0;
ref = 0;
}
if (command == "vyp"){
connection_on = false;
hys = 0;
ref = 0;
}
if (command == "00"){
hys = 0;
}
numericCommand = command.toFloat();
if (numericCommand != 0){
if (numericCommand > 900 && numericCommand < 1100){
ref = numericCommand-1000;
ref = customRound(ref);
}
if (numericCommand <900){
hys = numericCommand;
hys = customRound(hys);
}
}
if (hys != ref && ref > hys){
if (ref > temperature && get_to_hys == true && temperature > hys){
connection_on = true;
}
if (hys >= temperature){
get_to_hys = true;
connection_on = true;
}
if (ref <= temperature){
connection_on = false;
get_to_hys = false;
}
}
if (connection_on == true){
delay(1000);
digitalWrite(relay, HIGH);
}
else if (connection_on == false || command == "vyp"){
digitalWrite(relay, LOW);
}
}
Overall i think that there is a problem with this line of code:
EthernetClient client = server.available();
The code also based on tempereature readings turns on off the relay and i can edit the turning on and turning off temperature by the requests from http server. And its also notifying about the status of tempreatures and whether the relay is turned on or off.
I have tried to change the arduino for a different one and also putting it away from the power cable so there is not a magnetic interference but none of them worked.
I would like to know if there is a way to fix the issue or i should try to do the same with esp32 or original arduino nano or something else, so if anyone has clue of how to fix it i would be happy to know.