// Library
#include <SPI.h>
#include <Ethernet.h>
#include <EEPROM.h>
// Watchdog -------------------
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/wdt.h>
// ----------------------------
#define WDTSet 8000 // Solo i seguenti valori in ms: 16 32 64 125 250 500 1000 2000 4000 8000
#define DEBUG true
int chkhttp = 10000; // Valore in ms controllo chkhttp
int vtchk = 5; // Valore in s loop check controller
unsigned long int TWTD; // Timer Watchdog
unsigned long int TCHK; // Timer loop check controller
// Temporizzazioni
unsigned long int VTIME;
// Impostazione scheda ethernet
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x97, 0xFA }; // mac address 16
//byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x97, 0xFD }; // mac address 17
byte ip[] = { 192, 168, 2, 16 }; // ip arduino
byte gateway[] = { 192, 168, 2, 1 }; // Router
byte subnet[] = { 255, 255, 255, 0 }; // Subnet mask
EthernetServer server(8016); // Server port
byte ws_ip[] = { 192, 168, 2, 8 }; // IP Web server
byte cpu_10[] = { 192, 168, 2, 10 }; // IP Controller 10
byte cpu_11[] = { 192, 168, 2, 11 }; // IP Controller 11
byte cpu_12[] = { 192, 168, 2, 12 }; // IP Controller 12
byte cpu_13[] = { 192, 168, 2, 13 }; // IP Controller 13
byte cpu_14[] = { 192, 168, 2, 14 }; // IP Controller 14
byte cpu_15[] = { 192, 168, 2, 15 }; // IP Controller 15
byte cpu_16[] = { 192, 168, 2, 16 }; // IP Controller 16
byte cpu_17[] = { 192, 168, 2, 17 }; // IP Controller 16
String ip_web_server="192.168.2.8"; // Web server
String web_server="http://192.168.2.240"; // Web server
String web_page; // Pagine http
String send_data; // Pagine http
String send_data_cdz; // Pagine http
String send_data_servo; // Pagine http
String send_loop; // Pagine http
String readString;
String str_light; // Stringa led
String chk_page; // Check Page string
void setup()
{
// Inizializzazione Watchdog
WDT_off();
WDT_Init();
Ethernet.begin(mac, ip, gateway, subnet);
server.begin();
if ( DEBUG == true )
{
Serial.begin(9600);
Serial.println("Reset controllore 16");
}
EthernetClient client;
if (client.connect( ws_ip, 80))
{
send_data="";
send_data += "GET /security.php?TYP=SYS&STS=START_Microcontrollore_Test...";
client.println(send_data);
client.stop();
}
TWTD = WDTSet;
}
void loop()
{
// --------------------------------------------------------------
// Read millis() controller
// --------------------------------------------------------------
VTIME=millis();
// --------------------------------------------------------------
// Reset or overflow millis()
// --------------------------------------------------------------
if ( VTIME == 1000 )
{
TWTD = millis() + chkhttp;
}
if ( DEBUG == 1 )
{
Serial.println(millis());
}
http_control();
// ---------------------------------------------------------------------------------
// Controllo ricezione dati http altrimenti restart CPU
// ---------------------------------------------------------------------------------
if ( VTIME >= TWTD )
{
delay(10000);
}
// ---------------------------------------------------------------------------------
// Loop chk Ethernet Shield controllori
// ---------------------------------------------------------------------------------
if ( VTIME >= TCHK )
{
//Chk_controller( cpu_10,8010 );
//Chk_controller( cpu_11,8011 );
//Chk_controller( cpu_12,8012 );
//Chk_controller( cpu_13,8013 );
//Chk_controller( cpu_14,8014 );
//Chk_controller( cpu_15,8015 );
//Chk_controller( cpu_16,8016 );
Chk_controller( cpu_17,8017 );
TCHK = VTIME + ( vtchk * 1000 );
}
// ---------------------------------------------------------
ResetWatchdog(); // Resetto il watchdog file del loop
// ---------------------------------------------------------
}
// --------------------------------------------------------------
// HTTP client
// --------------------------------------------------------------
void http_control()
{
EthernetClient client = server.available();
// EthernetClient client;
if (client)
{
boolean currentLineIsBlank = true;
while (client.connected())
{
if (client.available())
{
char c = client.read();
readString.concat(c);
if (c == '\n' && currentLineIsBlank)
{
if(readString.indexOf("status=1") > 0)
{
web_page = "";
web_page += "Descrizione : \t\tRegolazione CDZ - Power control \n";
web_page += "IP Controller : \t";
web_page += ip[0]; web_page += "."; web_page += ip[1]; web_page += "."; web_page += ip[2]; web_page += "."; web_page += ip[3]; web_page += "\n";
web_page += "Gateway : \t\t";
web_page += gateway[0]; web_page += "."; web_page += gateway[1]; web_page += "."; web_page += gateway[2]; web_page += "."; web_page += gateway[3]; web_page += "\n";
web_page += "Subnet : \t\t";
web_page += subnet[0]; web_page += "."; web_page += subnet[1]; web_page += "."; web_page += subnet[2]; web_page += "."; web_page += subnet[3]; web_page += "\n";
web_page += "Millis : \t\t";
web_page += millis(); web_page += "\n";
client.print(web_page);
}
if(readString.indexOf("wait=1") > 0)
{
web_page = "";
web_page += "Descrizione : \t\tRegolazione CDZ - Power control \n\n\n\n\n";
web_page += "Forzatura attesa 6 sec. per riavvio ..... \t";
client.print(web_page);
delay(10000);
}
if(readString.indexOf("chk=1") > 0)
{
TWTD = VTIME + chkhttp;
web_page = "";
web_page += "IP Controller : \t";
web_page += ip[0]; web_page += "."; web_page += ip[1]; web_page += "."; web_page += ip[2]; web_page += "."; web_page += ip[3]; web_page += "\n";
client.print(web_page);
}
readString="";
client.stop();
}
}
}
}
}
void WDT_Init(void)
{
cli(); // Disable interrupts
wdt_reset(); // Reset watchdog
WDTCSR = (1<<WDCE)|(1<<WDE); // Set up WDT interrupt
// Set timing
if ( WDTSet == 16 ) { WDTCSR = (1<<WDIE)|(1<<WDE)|(0<<WDP3)|(0<<WDP2)|(0<<WDP1)|(0<<WDP0); } // Start watchdog timer with 16 ms prescaller
if ( WDTSet == 32 ) { WDTCSR = (1<<WDIE)|(1<<WDE)|(0<<WDP3)|(0<<WDP2)|(0<<WDP1)|(1<<WDP0); } // Start watchdog timer with 32 ms prescaller
if ( WDTSet == 64 ) { WDTCSR = (1<<WDIE)|(1<<WDE)|(0<<WDP3)|(0<<WDP2)|(1<<WDP1)|(0<<WDP0); } // Start watchdog timer with 64 ms prescaller
if ( WDTSet == 125 ) { WDTCSR = (1<<WDIE)|(1<<WDE)|(0<<WDP3)|(0<<WDP2)|(1<<WDP1)|(1<<WDP0); } // Start watchdog timer with 125 ms prescaller
if ( WDTSet == 250 ) { WDTCSR = (1<<WDIE)|(1<<WDE)|(0<<WDP3)|(1<<WDP2)|(0<<WDP1)|(0<<WDP0); } // Start watchdog timer with 250 ms prescaller
if ( WDTSet == 500 ) { WDTCSR = (1<<WDIE)|(1<<WDE)|(0<<WDP3)|(1<<WDP2)|(0<<WDP1)|(1<<WDP0); } // Start watchdog timer with 500 ms prescaller
if ( WDTSet == 1000 ) { WDTCSR = (1<<WDIE)|(1<<WDE)|(0<<WDP3)|(1<<WDP2)|(1<<WDP1)|(0<<WDP0); } // Start watchdog timer with 1 s prescaller
if ( WDTSet == 2000 ) { WDTCSR = (1<<WDIE)|(1<<WDE)|(0<<WDP3)|(1<<WDP2)|(1<<WDP1)|(1<<WDP0); } // Start watchdog timer with 2 s prescaller
if ( WDTSet == 4000 ) { WDTCSR = (1<<WDIE)|(1<<WDE)|(1<<WDP3)|(0<<WDP2)|(0<<WDP1)|(0<<WDP0); } // Start watchdog timer with 4 s prescaller
if ( WDTSet == 8000 ) { WDTCSR = (1<<WDIE)|(1<<WDE)|(1<<WDP3)|(0<<WDP2)|(0<<WDP1)|(1<<WDP0); } // Start watchdog timer with 8 s prescaller
sei(); // Enable global interrupts
}
void WDT_off(void)
{
MCUSR &= ~(1<<WDRF); // Clear WDRF in MCUSR - Disable interrupt - Watchdog Reset
WDTCSR |= (1<<WDCE) | (1<<WDE); // Write logical one to WDCE and WDE - Keep old prescaler setting to prevent unintentional time-out
WDTCSR = 0x00; // Off WDT
}
void ResetWatchdog()
{
wdt_reset(); //ResetEthernetShield();
}
void Chk_controller(byte cp_ip[], int prt)
{
EthernetClient client;
if ( client.connect( cp_ip, prt))
{
send_data="";
send_data += "GET /?chk=1";
client.println(send_data);
client.stop();
}
}