Hi.
I got this working code that works fine for around a few hours but when I check the next day the webserver is unresponsive and I need to restart the device to get it working again.
I was wondering if anyone can help me get it to automatically restart every 30 minutes or so?
The program & Hardware:
Arduino uno connected to Ethernet shield + a radio transmitter and a few LEDs
When the device is powered on it hosts a website with clickable buttons, each clickable button transmits a radio command to some remote power plugs I have in the house.
What I need help with:
A way to restart the device every 30 minutes or so without the solution stopping the webserver. So I would really want 2 loops running at the same time, one for the web server and one for the restarting code (watchdog etc), but from my understanding the arduino uno does not support multiple loops so I am wondering how I can implement a restart without stopping the webserver.
My only solution so far is to use another arduino uno just to restart the other one, but seems a bit silly....
Working code:
#include "SPI.h"
#include "Ethernet.h"
#include <NewRemoteTransmitter.h>
const unsigned long TRANSMITTER_ADDRESS = 27612394; //My personal transmitter address
const int PIN_TRANSMITTER = 8;
NewRemoteTransmitter transmitter(TRANSMITTER_ADDRESS, PIN_TRANSMITTER, 260, 3);
uint8_t mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; // MAC Address
uint8_t ip[] = { 192,168,1,50 }; // IP Address
EthernetServer server(80); // Server Port 80
// RemoteTransmitter configuration
// Connect Transmitter to Pin 11 and receiver to pin 2 (all digital)
// FAN 1 ON/OFF:
// Addr 27612394 unit 0 on, period: 267us.
// Addr 27612394 unit 0 off, period: 267us.
// FAN 2 ON/OFF:
// Addr 27612394 unit 1 on, period: 266us.
// Addr 27612394 unit 2 off, period: 267us.
// FAN 3 ON/OFF:
// Addr 27612394 unit 2 on, period: 267us.
// Addr 27612394 unit 1 off, period: 266us.
// ALL POWER OFF:
// Addr 27612394 group off, period: 267us.
String controlString; // Captures out URI querystring;;
int blueLEDPin = 2; // pin where our blue LED is connected
void setup(){
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
digitalWrite(3, HIGH);
Ethernet.begin(mac, ip);
server.begin();
pinMode(PIN_TRANSMITTER, OUTPUT);
Serial.begin(9600);
Serial.print("Serial Write turned on");
}
void loop(){
// Create a client connection
EthernetClient client = server.available();
Serial.print("Launching Ethernet WebServer");
if (client) {
while (client.connected()) {
if (client.available()) {
char c = client.read();
//read the HTTP request
if (controlString.length() < 100) {
// write characters to string
controlString += c;
}
//if HTTP request has ended– 0x0D is Carriage Return \n ASCII
if (c == 0x0D) {
digitalWrite(4, HIGH);
httpResponseHome(client);
Serial.print("Website is up");
//stopping client
client.stop();
digitalWrite(4, LOW);
// control arduino pin
if(controlString.indexOf("?1-on") > -1) //checks for switchon
{
transmitter.sendGroup(true);
httpResponseRedirect(client);
}
else
if(controlString.indexOf("?1-off") > -1) //checks for switchoff
{
transmitter.sendGroup(false);
httpResponseRedirect(client);
}
else
if(controlString.indexOf("?2-on") > -1) //checks for switchon
{
transmitter.sendUnit(0, true);
httpResponseRedirect(client);
}
else
if(controlString.indexOf("?2-off") > -1) //checks for switchoff
{
transmitter.sendUnit(0, false);
httpResponseRedirect(client);
}
else
if(controlString.indexOf("?3-on") > -1) //checks for switchon
{
transmitter.sendUnit(1, true);
httpResponseRedirect(client);
}
else
if(controlString.indexOf("?3-off") > -1) //checks for switchoff
{
transmitter.sendUnit(1, false);
httpResponseRedirect(client);
}
else
if(controlString.indexOf("?4-on") > -1) //checks for switchon
{
transmitter.sendUnit(2, true);
httpResponseRedirect(client);
}
else
if(controlString.indexOf("?4-off") > -1) //checks for switchoff
{
transmitter.sendUnit(2, false);
httpResponseRedirect(client);
}
//clearing string for next read
controlString="";
}
}
}
}
}
void httpResponseHome(EthernetClient c) {
c.println("HTTP/1.1 200 OK");
c.println("Content-Type: text/html");
c.println();
c.println("<html>");
c.println("<head>");
c.println( "<title>MPC NEXA Webserver</title>");
c.println( "<style>");
c.println( "body { font-family: Arial, sans-serif; font-size:12px; }");
c.println( "</style>");
c.println("</head>");
c.println("<body>");
c.println( "<h1>MPC NEXA Webserver</h1>");
c.println( "<ul>");
c.println( "<li><a href=\"./?1-on\">Turn on power</a></li>");
c.println( "<li><a href=\"./?1-off\">Turn off power</a></li>");
c.println( "</ul>");
c.println( "<ul>");
c.println( "<li><a href=\"./?2-on\">Turn on power 1</a></li>");
c.println( "<li><a href=\"./?2-off\">turn off power 1</a></li>");
c.println( "</ul>");
c.println( "<ul>");
c.println( "<li><a href=\"./?3-on\">Turn on power 2</a></li>");
c.println( "<li><a href=\"./?3-off\">turn off power 2</a></li>");
c.println( "</ul>");
c.println( "<ul>");
c.println( "<li><a href=\"./?4-on\">Turn on power 3</a></li>");
c.println( "<li><a href=\"./?4-off\">turn off power 3</a></li>");
c.println( "</ul>");
c.println( "<hr>");
c.println("</body>");
c.println("</html>");
}
void httpResponseRedirect(EthernetClient c) {
c.println("HTTP/1.1 301 Found");
c.println("Location: /");
c.println();
}