My setup is a Mega 2560 with Ethernet Shield. My Sketch has 2 buttons that toggle Booleans and also listens for UDP packets, decrypts them, parses them, then based on the text received it can also toggle Booleans. When i run this sketch it works great then it eventually and randomly stops operating, no reactions from the buttons, UDP packets, nothing.
Please give me some insight to what might be happening to my sketch...
#include <Metro.h>
#include <aes.h>
#include <aes128_dec.h>
#include <aes128_enc.h>
#include <aes192_dec.h>
#include <aes192_enc.h>
#include <aes256_dec.h>
#include <aes256_enc.h>
#include <AESLib.h>
#include <aes_dec.h>
#include <aes_enc.h>
#include <aes_invsbox.h>
#include <aes_keyschedule.h>
#include <aes_sbox.h>
#include <aes_types.h>
#include <bcal-basic.h>
#include <bcal-cbc.h>
#include <bcal-cmac.h>
#include <bcal-ofb.h>
#include <bcal_aes128.h>
#include <bcal_aes192.h>
#include <bcal_aes256.h>
#include <blockcipher_descriptor.h>
#include <gf256mul.h>
#include <keysize_descriptor.h>
#include <memxor.h>
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#include "Wire.h"
#include "BlinkM_funcs.h"/
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF };
IPAddress ip(192, 168, 0, 1);
unsigned int localPort = 8888; // local port to listen on
// buffers for receiving and sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
char ReplyBuffer[] = "ack"; // a string to send back
// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;
byte key[] = {
0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; //must be 16 bytes long...
int CabinetDoor = 22;
int DOOR1_position_Pin = 23;
int DOOR1_lock_Pin = 24;
boolean deadboltSensor, doorSensor, systemCabinetSensor;
boolean ALARM_ARMED = false;
boolean ALARM_FAULT = false;
boolean DOOR1_FAULT = false;
boolean DOOR1_shut = false;
boolean DOOR1_lock = false;
//boolean b3 = false;
//boolean b4 = false;
//boolean b5 = false;
Metro Metro1 = Metro(250);
void setup() {
pinMode(CabinetDoor, INPUT_PULLUP);
pinMode(DOOR1_position_Pin, INPUT_PULLUP);
pinMode(DOOR1_lock_Pin, INPUT_PULLUP);
//pinMode(25, INPUT_PULLUP);
// start the Ethernet and UDP:
Ethernet.begin(mac,ip);
Udp.begin(localPort);
Serial.begin(9600);
BlinkM_begin();
delay(100); // wait a bit for things to stabilize
BlinkM_off(0);
Serial.print ("System Online @ : ");
Serial.println(Ethernet.localIP());
}
void loop() {
Serial.println("Looping");
CheckSensors();
// if there's data available, read a packet
int packetSize = Udp.parsePacket();
if(packetSize)
{
Serial.print("Received packet of size ");
Serial.println(packetSize);
Serial.print("From ");
IPAddress remote = Udp.remoteIP();
for (int i =0; i < 4; i++)
{
Serial.print(remote[i], DEC);
if (i < 3)
{
Serial.print(".");
}
}
Serial.print(", port ");
Serial.println(Udp.remotePort());
// read the packet into packetBufffer
Udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE);
Serial.print("Contents: ");
Serial.println(packetBuffer);
aes128_dec_single(key, packetBuffer); // DECRYPTION
Serial.print("Decrypted: ");
Serial.println(packetBuffer);
CheckCommands(packetBuffer);
}
CheckSystemStatus();
delay(10);
}
void CheckCommands(String s){
int i = s.length();
if(s.startsWith("cmd:") || s.startsWith("CMD:")){
Serial.println("FOUND CMFOUND CMD: ");
int x = s.indexOf(":");
int x2 = s.indexOf(":",x+1);
int x3 = s.indexOf(":",x2+1);
Serial.println(x);
Serial.println(x2);
Serial.println(x3);
String sub = s.substring(x+1,x2);
String sub2 = s.substring(x2+1,x3);
String sub3 = s.substring(x3+1);
sub.trim();
sub2.trim();
sub3.trim();
Serial.print(sub);
Serial.print("-");
Serial.print(sub2);
Serial.print("-");
Serial.print(sub3);
Serial.println("|");
if(sub.indexOf("HOME") >= 0){
if(sub2.indexOf("ARM") >= 0){
Serial.println("ARM");
if(sub3.indexOf("0") >= 0){
ClearAlarm();
}
else if(sub3.indexOf("1") >= 0){
ALARM_ARMED = true;
Serial.println("1");
}
else {
Serial.println("ARM LEVEL ELSE: "+ sub3);
}
//SendReturnMessage(Boolean2String("A:",ALARM_ARMED,""));
}
else if(sub2.indexOf("GET") >= 0){
Serial.println("GET");
//SendReturnMessage(Boolean2String("A:",ALARM_ARMED,""));
}
else {
//BAD COMMAND IN SYSTEM LEVEL
Serial.println("SYSTEM LEVEL ELSE: "+sub2);
}
}
else if(sub.indexOf("DOOR1") >= 0){
Serial.println("DOOR1-LEVEL");
if(sub2.indexOf("GET") >= 0){
Serial.println("GET");
//SendReturnMessage(MultiBoolean2String("D:",doorSensor, "L:", deadboltSensor,""));
}
else if(sub2.indexOf("SET") >= 0){
Serial.println("SET");
if(sub3.indexOf("0") >= 0){
Serial.println("LOW");
//analogWrite(12, 0);
//SendReturnMessage("pin 12 - LOW");
}
else if(sub3.indexOf("1") >= 0){
Serial.println("HIGH");
//analogWrite(12, 255);
//SendReturnMessage("pin 12 - HIGH");
}
else {
Serial.println("SET LEVEL ELSE: "+ sub3);
}
}
else {
//BAD COMMAND IN DOOR1 LEVEL
Serial.println("DOOR1 LEVEL ELSE: "+ sub2);
}
}
else {
//BAD COMMAND IN TOP LEVEL
Serial.println("TOP LEVEL ELSE: "+ sub);
}
// send a reply, to the IP address and port that sent us the packet we received
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
Udp.write(ReplyBuffer);
Udp.endPacket();
}
}
void CheckSensors(){
doorSensor = digitalRead(DOOR1_position_Pin);
deadboltSensor = digitalRead(DOOR1_lock_Pin);
//Serial.println(doorSensor);
//Serial.println(deadboltSensor);
if(ALARM_ARMED){
if(doorSensor || deadboltSensor){
DOOR1_FAULT = true;
Serial.println("Door 1 Faulted");
}
else {
//DOOR1_FAULT = false;
}
if(DOOR1_FAULT){
ALARM_FAULT = true;
Serial.println("Alarm Faulted");
}
else {
//ALARM_FAULT = false;
}
}
else {
// State when not Armed...
}
}
void CheckSystemStatus(){
if(ALARM_ARMED){ // ALARM_ARMED = true
if(ALARM_FAULT){
if(DOOR1_FAULT){
if((!doorSensor && deadboltSensor) || (doorSensor && !deadboltSensor)){
scriptLEDS(2000, 0, 8, 1);
}
else if(doorSensor && deadboltSensor){
scriptLEDS(2000, 0, 3, 1);
}
else if(!doorSensor && !deadboltSensor){
fadeLEDS(0,255,0,0);
}
}
else {
fadeLEDS(0,255,0,0);
}
}
else {
//scriptLEDS(1000, 0, 8, 1);
fadeLEDS(0,0,1,0);
}
}
else { // ALARM_ARMED = false
if((!doorSensor && deadboltSensor) || (doorSensor && !deadboltSensor)){
scriptLEDS(2000, 0, 8, 1);
}
else if(doorSensor && deadboltSensor){
scriptLEDS(2000, 0, 3, 1);
}
else{
setLEDS(0,1,0,0);
}
}
}
void scriptLEDS(int s, int address, int sNumber, int reps){
Metro1.interval(s);
if (Metro1.check() == 1) {
//BlinkM_setFadeSpeed(0x00,0x30);
BlinkM_playScript(address,sNumber,reps,0x00);
}
}
void fadeLEDS(int address, int r, int g, int b){
BlinkM_fadeToRGB(address,r,g,b);
}
void setLEDS(int address, int r, int g, int b){
BlinkM_setRGB(address,r,g,b);
}
void ClearAlarm(){
ALARM_ARMED = false;
Serial.println("Alarm Disarmed");
ALARM_FAULT = false;
Serial.println("Alarm Fault Cleared");
DOOR1_FAULT = false;
Serial.println("Door 1 Fault Cleared");
}