Hi,
I am verrrrrry new to C, in fact this is my 1st arduino based project, the hardware works ok, but I am having a problem trying to work out why my controller code hangs after running for anything between a few hours and 24+ hours.
The project consists of:
- Remote Device - Atmega 328 / Xbee S2 / 10 x DS18B20 / 2 x NC reed switches
- Controller Device - Atmega 328 / Cbee s2 / WIZ820io module -> posted to remote website
The code as you will see is mostly documented example code that I have hacked together, so each component, xbee, NTP, ethernet should work on its own, however either the way I have hacked them together or, the efficiency of the code it causing it to hang, I suspect that it is possibly running out of memory, however I have no idea on how to work out why, when or at what point it hangs.
Any help, pointers, ideas as to how to solves this, where the inefficiencies are, how to solve them would be greatly appreciated.
I have eliminated power problem by connecting a 1amp supply, no differance.
Thanks in advance
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <XBee.h>
#include <SoftwareSerial.h>
#include <Time.h>
XBee xbee = XBee();
ZBRxResponse rx = ZBRxResponse();
// Enter a MAC address for your Wiz820io below.
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
// server to connect to
char serverName[] = "www.domain.com";
// NTP server address (za.pool.ntp.org)
IPAddress timeServer(196, 25, 1, 9);
// Initialize the Ethernet client library
EthernetClient client;
// UDP instance to send and receive packets over UDP
EthernetUDP Udp;
// Set to offset of your local time (+2hrs in sec's)
const long timeZoneOffset = +7200L; //60sec*60mins*2hrs
// Syncs to NTP server every 1hr 3600 secs
unsigned int ntpSyncTime = 3600;
// Keeps track of how long ago we updated the NTP server
unsigned long ntpLastUpdate = 0;
// local port to listen for UDP packets
unsigned int localPort = 8888;
// NTP time stamp is in the first 48 bytes of the message
const int NTP_PACKET_SIZE= 48;
//buffer to hold incoming and outgoing packets
byte packetBuffer[NTP_PACKET_SIZE];
// Define SoftSerial RX/TX pins
// Ardunio DIO8 (pin14) to RX of usb-serial device
// Arduino DIO9 (pin15) to TX of usb-serial device
SoftwareSerial portOne(9, 8);
// received data,
// 1 byte tx status, 1 byte each door, 4 bytes each sensor
uint8_t data[43] = { //10 sensors * 4 + 1 tx status + 2 doors
0, // tx event
0, // door1
0, // door2
0,0,0,0, // sensor01
0,0,0,0, // sensor02
0,0,0,0, // sensor03
0,0,0,0, // sensor04
0,0,0,0, // sensor05
0,0,0,0, // sensor06
0,0,0,0, // sensor07
0,0,0,0, // sensor08
0,0,0,0, // sensor09
0,0,0,0 // sensor10
};
// union to convery float to byte string
union u_tag {
uint8_t b[4];
float f;
}
u;
// number sensors defined
const int numSensorDefined = 10;
// query string
String query;
// this xbee coordinators 64 bit address
String add64 = "xxxxxxxx";
void setup()
{
// start xbee on serial port
xbee.begin(9600);
// Start software serial
portOne.begin(9600);
delay(1000);
portOne.println();
portOne.println("Start Up Controller ... ");
// start the Ethernet & UDP connection:
int i = 0;
int DHCP = 0;
DHCP = Ethernet.begin(mac);
// Try to get dhcp settings 30 times before giving up
while(DHCP == 0 && i < 30){
delay(1000);
DHCP = Ethernet.begin(mac);
i++;
}
if(!DHCP){
portOne.println("DHCP Failed");
for(;;); //Infinite loop because DHCP Failed
}
portOne.println("DHCP Success");
// print your local IP address:
portOne.print("My IP address: ");
print_ip();
// Update time via NTP server
updateTime();
// send startup notification
startUp();
}
void loop()
{
// Update time via NTP server every X amount of secs
// as determined by ntpSyncTime
if(now() - ntpLastUpdate > ntpSyncTime) {
updateTime();
}
// check on DHCP
checkDHCP();
// read incoming xbee packets
readData();
}