Hi,
I've removed some of the code, and now I'm running into another problem (among others).
I.e. when receiving data over Zigbee treatData() always fails.
freeMemory() returns low values like 170.
Does that mean there's not enough memory for it to work?
There's not much code I can remove now left anymore.
Code of my sketch:
Quote:
// Coordinator that collects data from the different sensors
// using a predefined 64-bit PAN ID that all nodes use
// Coordinator only parameters
char* PRM_SMS_RECEIVER="+31618408210"; // MdH's mobile phone number
char* PRM_GPRS_PIN=NULL; // NOT using a GPRS PIN
// as soon as we manage to get the TCP client working we can send the data over to 'Thomas' platform!!!!
char* PRM_TCP_SERVER_ADDRESS="131.180.117.130";
char* PRM_TCP_SERVER_PORT="23456";
char* PRM_USER_ID="A.1"; // to be prefixed in front of any message to send... (see sendToTcpServer)
// A gateway node might also be active to send log data to
uint8_t PRM_GATEWAY[8]={0x00,0x13,0xA2,0x00,0x40,0x55,0x95,0xB1}; // the MAC address of the gateway
char* PRM_NETKEY="WaspmoteKey";
char* PRM_LINKKEY="WaspmoteKey";
// end Coordinator only parameters
char* PRM_ID="COORDINATOR_MHV_1"; // ID of this node
uint8_t PRM_COORDINATOR_PAN_ID[8]={0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08};
void setLEDs(boolean led0, boolean led1) {
Utils.setLED(LED0,led0?LED_ON:LED_OFF);
Utils.setLED(LED1,led1?LED_ON:LED_OFF);
}
void setup(){
// setup for Serial port over USB
USB.begin();
USB.println("USB port started...");
delay(10000);
GPRS.begin();
delay(1000);
GPRS.setMode(GPRS_ON);
//////////GPRS.ON();
reportGPRSStatus("GPRS module");
delay(1000);
////GPRS.configureGPRS();
////reportGPRSStatus("GPRS configure");
////GPRS.setPIN("6523");
////reportGPRSStatus("GPRS PIN");
delay(1000);
GPRS.flag=0;
// waiting while GPRS connects to the network
int i=0;
while(++i<=5) {
USB.print("Checking GPRS connection #");
USB.print(i);
USB.println("...");
if (GPRS.check())
break;
}
reportGPRSStatus("Check");
USB.println("GPRS connected to the network...");
// configure SMS and Incoming Calls
if (GPRS.setInfoIncomingCall())
USB.println("Info Incoming Call OK");
else
USB.println("Info Incoming Call Error");
reportGPRSStatus("Info Incoming Call");
if (GPRS.setInfoIncomingSMS())
USB.println("Info Incoming SMS OK");
else
USB.println("Info Incoming SMS Error");
reportGPRSStatus("Info Incoming SMS");
if (GPRS.setTextModeSMS())
USB.println("Text Mode SMS OK");
else
USB.println("Text Mode SMS Error");
reportGPRSStatus("Text Mode SMS");
// why does the following take so long???????
if (GPRS.sendSMS("Een nieuwe lente, een nieuw geluid!!","+31618408210"))
USB.println("SMS Sent OK."); // * should be replaced by the desired tlfn number
else
USB.println("Error sending sms.");
delay(1000);
// start XBee in order to be able to receive data from the routers
xbeeInitializeCoordinator();
USB.println("Coordinator initialized..");
delay(1000);
xbeeStartCoordinator();
USB.println("Coordinator started...");
}
/*
// methods for initializing a router and a coordinator
void setLinkKey() {
xbeeZB.setLinkKey(LINKKEY);
if (!xbeeZB.error_AT)
XBee.println("Link Key set OK");
else
XBee.println("Error while setting Key");
}
*/
void xbeeInitializeRouter() {
// Inits the XBee ZigBee library
xbeeZB.init(ZIGBEE,FREQ2_4G,NORMAL);
// turn it on, so we can get something done
xbeeZB.ON();
// Enabling security
xbeeZB.encryptionMode(0);
if (!xbeeZB.error_AT)
XBee.println("Security disabled");
else
XBee.println("Error while disabling security");
// setLinkKey();
xbeeZB.setAPSencryption(XBEE_ON);
if (!xbeeZB.error_AT)
XBee.println("APS Encryption turned on");
else
XBee.println("Error while setting APS Encryption");
}
void xbeeInitializeCoordinator() {
// a coordinator is also a router
xbeeInitializeRouter();
// Setting Network Key (only in Coordinator)
xbeeZB.setNetworkKey(PRM_NETKEY);
if(!xbeeZB.error_AT)
XBee.println("Network Key set OK");
else
XBee.println("Error while setting Key");
// Configuring Trust Center (only in the coordinator)
!xbeeZB.setEncryptionOptions(0x02);
if (!xbeeZB.error_AT)
XBee.println("Security options configured");
else
XBee.println("Error while configuring security");
// store the coordinator values
xbeeZB.writeValues();
if(!xbeeZB.error_AT)
XBee.println("Changes stored OK");
else
XBee.println("Error while storing values");
}
void xbeeStartCoordinator() {
// Coordinator Startup Process. Selected Extended PAN ID (see § 8.4.2 Waspmote-zigbee-networking-guide.pdf)
// MDH 03MAY2012: I suppose we can set the pan here as well
xbeeZB.setPAN(PRM_COORDINATOR_PAN_ID);
xbeeZB.setScanningChannels(0xFF,0xFF); // scan all channels
xbeeZB.setDurationEnergyChannels(3);
// ? do we need to tell it to actually start scanning?
// blink the red LED while we're starting
setLEDs(true,false); // turn red LED on and green LED off
}
///*
boolean isCoordinatorStarted() {
// determine based on the association indication whether we succeeded in creating the network (i.e. start the coordinator)
xbeeZB.getAssociationIndication();
switch (xbeeZB.associationIndication) { // success
case 0: // Successful. Coordinator started or Router/End Device joined with a parent.
// if the channel is also correct, we report it and return true
xbeeZB.getChannel();
if(!xbeeZB.error_AT) {
XBee.print("Channel is: ");
XBee.println(xbeeZB.channel,HEX);
return true;
}
XBee.println("ERROR: Failed to get XBee channel.");
break;
// all other values indicate some kind of failure
case 0xAB: //Attempted to join a device did not respond.
break;
case 0xAC: // Secure join error. Network security key received unsecured.
XBee.println("ERROR: Network security key received unsecured, joining.");
break;
case 0xAD: // Secure join error. Network security key not received.
XBee.println("ERROR: Network security key not received, joining.");
break;
case 0xAF: // Secure join error. Joining device does not have the right preconfigured link key.
XBee.println("ERROR: Joining device does have the right preconfigured link key.");
break;
case 0x21: // Scan found no PANs.
XBee.println("ERROR: No PANs found.");
break;
case 0x22: // Scan found no valid PANs based on current ‘Scan Channels’ and ‘Extended PAN ID’.
XBee.println("ERRROR: No PANs found on any of the scan channels and given PAN ID.");
break;
case 0x23: // Valid Coordinator or Routers found, but they are not allowing joining.
XBee.println("ERROR: Coordinator/Router found does not allow joining.");
break;
case 0x27: // Node joining attempt failed.
XBee.println("ERROR: Joining attempt failed.");
break;
case 0x2A: // Coordinator Start attempt failed.
XBee.println("ERROR: Failed to start as coordinator.");
// NOTE we might need to attempt to start again here
xbeeStartCoordinator();
break;
case 0xFF: // Scanning for a parent.
XBee.println("NOTE: Scanning for a parent.");
break;
case 0x2B: // Checking for an existing coordinator.
XBee.println("NOTE: Checking for an existing coordinator.");
break;
}
return false;
}
//*/
// GPRS stuff (copied over from TUD_MHV_Router_Test)
void reportGPRSStatus(char *statusPrefix) {
int gprsFlag=GPRS.flag;
USB.print("GPRS status after '");
USB.print(statusPrefix);
USB.print("': ");
USB.print(gprsFlag);
USB.println(".");
if (gprsFlag&1) USB.println(" Error powering up the GRPS module.");
if (gprsFlag&2) USB.println(" Error making a call.");
if (gprsFlag&4) USB.println(" Error hanging up a call.");
if (gprsFlag&8) USB.println(" Error while working with SMS.");
if (gprsFlag&16) USB.println(" Error setting the PIN.");
if (gprsFlag&32) USB.println(" Error receiving a call.");
if (gprsFlag&64) USB.println(" Error receiving a SMS.");
if (gprsFlag&128) USB.println(" Error configuring the GPRS connection.");
if (gprsFlag&256) USB.println(" Error setting the pattern of the TCP connection.");
if (gprsFlag&512) USB.println(" Error setting the timer for the TCP connection.");
if (gprsFlag&1024) USB.println(" Error setting the profile for the TCP connection.");
if (gprsFlag&2048) USB.println(" Error setting the socket.");
if (gprsFlag&4096) USB.println(" Error checking the connection.");
if (gprsFlag&8192) USB.println(" Error closing the socket.");
if (gprsFlag&16384) USB.println(" Error while setting SMTP parameters.");
}
/*
boolean sendSMSavailable=false; // if succeeding in sending the initial SMS, we're ready to send additional ones
void gprsInitialize() {
// setup for Serial port over USB
USB.begin();
USB.println("USB port started...");
////////GPRS.OFF();
delay(10000);
GPRS.begin();
delay(1000);
GPRS.setMode(GPRS_ON);
//////////GPRS.ON();
reportGPRSStatus("GPRS module");
delay(1000);
////GPRS.configureGPRS();
////reportGPRSStatus("GPRS configure");
////GPRS.setPIN("6523");
////reportGPRSStatus("GPRS PIN");
delay(1000);
GPRS.flag=0;
// waiting while GPRS connects to the network
int i=0;
while(++i<=5&&GPRS.check()==0)
reportGPRSStatus("Check");
// mostly the same as above
// turn the thing on
GPRS.begin();
reportGPRSStatus("begin");
delay(1000);
GPRS.setMode(GPRS_ON);
///// GPRS.ON(); // Powers GPRS on
reportGPRSStatus("set mode on");
USB.print("GPRS power mode: ");
switch (GPRS.getMode()) {
case GPRS_ON:
USB.print("ON");
break;
case GPRS_SLEEP:
USB.print("SLEEP");
break;
case GPRS_HIBERNATE:
USB.print("HIBERNATE");
break;
default:
USB.print("?");
}
/////////USB.print(GPRS.getMode(),HEX); // Get GPRS power mode
USB.println(".");
// setting the PIN is essential
if (PRM_GPRS_PIN) {
delay(1000);
// we'll be testing the flag
int pinCount=0;
do {
USB.print("PIN set #");
USB.print(++pinCount);
USB.println(".");
// NOTE interesting to see that I can change the flag!!!!
GPRS.flag=GPRS.flag&0xFFFD; // getting rid of the 2 in there
GPRS.setPIN(PRM_GPRS_PIN);
reportGPRSStatus("PIN set");
break;
} while (GPRS.flag&2);
// force the flag to be OK
GPRS.flag=GPRS.flag&0xFFFD; // getting rid of the 2 in there
reportGPRSStatus("GPRS PIN set");
} else
USB.println("No PIN to set!");
// let's try a longer delay (seemed to work in WaspGPRS_2_sendingSMS2)
delay(5000);
GPRS.flag=0; // or perhaps this does the trick?
setLEDs(true,false);
while (!GPRS.check()) {
reportGPRSStatus("checking");
Utils.blinkLEDs(500);
delay(1000);
setLEDs(true,false);
}
// we did it!
setLEDs(false,true);
USB.println("GPRS connected to the network.");
// configure SMS and Incoming Calls
GPRS.setInfoIncomingCall();
reportGPRSStatus("Info Incoming Call");
GPRS.setInfoIncomingSMS();
reportGPRSStatus("Info Incoming SMS");
GPRS.setTextModeSMS();
reportGPRSStatus("Text Mode SMS");
char smsText[100];
sprintf(smsText,"Battery level of %s: %u",PRM_ID,PWR.getBatteryLevel());
sendSMSavailable=(GPRS.sendSMS(smsText,PRM_SMS_RECEIVER)==1);
reportGPRSStatus("SMS test");
}
void sendSMS(char *smsText) {
if (!PRM_SMS_RECEIVER) {
USB.print("Can't SMS '");
USB.print(smsText);
USB.println("': SMS receiver not defined!");
} else
if (sendSMSavailable) {
if (GPRS.sendSMS(smsText,PRM_SMS_RECEIVER)) {
USB.println("Succeeded in sending '");
USB.print(smsText);
USB.println("' as SMS.");
} else {
USB.print("ERROR: Failed to SMS '");
USB.print(smsText);
USB.println("'.");
}
} else {
USB.print("Can't SMS '");
USB.print(smsText);
USB.println("': Failed to send initial SMS.");
}
}
// end GPRS stuff
packetXBee* paq_sent;
void unicastXB(char* message) {
paq_sent=(packetXBee*)calloc(1,sizeof(packetXBee));
paq_sent->mode=UNICAST; // set Broadcast mode
paq_sent->MY_known=0; // set 16-bit NA unknown
paq_sent->packetID=0x52; // set ID application level
paq_sent->opt=0; // set options. No option selected.
xbeeZB.hops=0;
xbeeZB.setOriginParams(paq_sent,MAC_TYPE);
xbeeZB.setDestinationParams(paq_sent,PRM_GATEWAY,message,MAC_TYPE,DATA_ABSOLUTE);
xbeeZB.sendXBee(paq_sent);
// we can check for success here
uint8_t transmitError=xbeeZB.error_TX;
if (transmitError==0) {
// NOTE we will see the RSSI leds flashing, so there's no need to change the LEDs
// we turn both LEDs off indicatative of a successful send NOTE they will be turned on shortly!!!
////////setLEDs(LED_OFF,LED_OFF);
USB.print("'");
USB.print(message);
USB.println("' sent!");
} else {
// turn both LEDs on indicating failing to send something
setLEDs(LED_ON,LED_ON); // failed to send, both LEDs should be on to indicate this
switch (transmitError) {
case 1:
USB.print("ERROR: Error in sending '");
USB.print(message);
USB.println("'.");
break;
case 2:
USB.print("ERROR: Failed to send '");
USB.print(message);
USB.println("'.");
break;
case -1:
USB.print("ERROR: Not allowed to send '");
USB.print(message);
USB.println("'.");
break;
}
}
free(paq_sent);
paq_sent=NULL;
}
void sendToGateway(char* msg) {
if (PRM_GATEWAY)
unicastXB(msg);
}
*/
void sendToTcpServer(char* msg) {
if (GPRS.socket_ID) { // some socket ID available to send data to
char messageToSend[100];
sprintf(messageToSend,"%s\t%s\n\r",PRM_USER_ID,msg);
USB.print("'");
USB.print(msg);
if (GPRS.sendData(messageToSend,GPRS.socket_ID))
USB.println("' sent to TCP server.");
else
USB.println("' NOT sent to TCP server.");
} else {
USB.print("ERROR: Failed to send '");
USB.print(msg);
USB.println("' to the TCP server: no TCP client available.");
}
}
void sendHi() {
sendToTcpServer("hi");
}
/*
void prepostsetup() {
USB.begin();
USB.print("'");
USB.print(PRM_ID);
USB.println("' starting.");
// keep LEDs turned off initially
// NOTE we could NOT do this given that start coordinator will set the LEDs as well BUT this does ascertain that we see something all the time
setLEDs(LED_OFF,LED_OFF);
}
void postsetup() {
//////prepostsetup();
// start GPRS
gprsInitialize(); // will turn the LEDs on when failing!!!!!
// start XBee in order to be able to receive data from the routers
xbeeInitializeCoordinator();
xbeeStartCoordinator();
}
*/
// exactly the same setup as in sendingSMS2
/*
void setup() {
// setup for Serial port over USB
USB.begin();
USB.println("USB port started...");
////////GPRS.OFF();
delay(10000);
GPRS.begin();
delay(1000);
GPRS.setMode(GPRS_ON);
//////////GPRS.ON();
reportGPRSStatus("GPRS module");
delay(1000);
////GPRS.configureGPRS();
////reportGPRSStatus("GPRS configure");
////GPRS.setPIN("6523");
////reportGPRSStatus("GPRS PIN");
delay(1000);
GPRS.flag=0;
// waiting while GPRS connects to the network
int i=0;
while(++i<=5&&GPRS.check()==0)
reportGPRSStatus("Check");
postsetup();
}
*/
void loop() {
USB.println("Looping");
// try to get a tcp client socket up and running
if (!GPRS.socket_ID) { // no tcp client socket ID running
GPRS.configureGPRS();
reportGPRSStatus("GPRS configuration");
// create the socket as soon as we manage to get a configuration
if (!GPRS.flag) {
USB.println("Will attempt to create a TCP client socket.");
// create the socket to communicate over
int gprsSocketCreated=GPRS.createSocket(PRM_TCP_SERVER_ADDRESS,PRM_TCP_SERVER_PORT,GPRS_CLIENT);
reportGPRSStatus("GPRS create socket");
if (!(GPRS.flag&2048))
sendHi();
else
USB.println("NOTE: Hi message not sent: socket not created!");
if (GPRS.socket_ID) {
USB.print("Client socket to ");
USB.print(PRM_TCP_SERVER_ADDRESS);
USB.print(":");
USB.print(PRM_TCP_SERVER_PORT);
USB.println(" created!");
} else {
USB.print("ERROR: Failed to connect to ");
USB.print(PRM_TCP_SERVER_ADDRESS);
USB.print(":");
USB.print(PRM_TCP_SERVER_PORT);
USB.println("!");
}
}
}
// if we managed to start as coordinator, we can go ahead and receive packets from the joined sensor nodes
if (isCoordinatorStarted()) {
// I suppose we can turn off the blinking of the red LED
setLEDs(false,true);
USB.println("Coordinator started!");
/*
// Waiting the answer
long previous=millis();
while ((millis()-previous)<10000) {
if (XBee.available()) {
xbeeZB.treatData();
if (!xbeeZB.error_RX) {
// Writing the parameters of the packet received
while (--xbeeZB.pos>=0) {
///*
XBee.print("Network Address Source: ");
XBee.print(xbeeZB.packet_finished[xbeeZB.pos-1]->naS[0],HEX);
XBee.print(xbeeZB.packet_finished[xbeeZB.pos-1]->naS[1],HEX);
XBee.println("");
XBee.print("MAC Address Source: ");
for (int b=0;b<4;b++) {
XBee.print(xbeeZB.packet_finished[xbeeZB.pos-1]->macSH[b],HEX);
}
for (int c=0;c<4;c++) {
XBee.print(xbeeZB.packet_finished[xbeeZB.pos-1]->macSL[c],HEX);
}
XBee.println();
XBee.print("Network Address Origin: ");
XBee.print(xbeeZB.packet_finished[xbeeZB.pos-1]->naO[0],HEX);
XBee.print(xbeeZB.packet_finished[xbeeZB.pos-1]->naO[1],HEX);
XBee.println();
XBee.print("MAC Address Origin: ");
for (int d=0;d<4;d++) {
XBee.print(xbeeZB.packet_finished[xbeeZB.pos-1]->macOH[d],HEX);
}
for (int e=0;e<4;e++) {
XBee.print(xbeeZB.packet_finished[xbeeZB.pos-1]->macOL[e],HEX);
}
XBee.println();
XBee.print("RSSI: ");
XBee.print(xbeeZB.packet_finished[xbeeZB.pos-1]->RSSI,HEX);
XBee.println("");
XBee.print("16B(0) or 64B(1): ");
XBee.print(xbeeZB.packet_finished[xbeeZB.pos-1]->mode,HEX);
XBee.println();
XBee.print("Data: ");
// to send the stuff along we need to put it in a string
char message[xbeeZB.packet_finished[xbeeZB.pos]->data_length];
for (int f=0;f<xbeeZB.packet_finished[xbeeZB.pos]->data_length;f++) {
/////XBee.print(xbeeZB.packet_finished[xbeeZB.pos-1]->data[f],BYTE);
message[f]=(char)xbeeZB.packet_finished[xbeeZB.pos]->data[f];
}
XBee.print("Message: '");
XBee.print(message);
XBee.println("'.");
////sendSMS(message);
////sendToTcpServer(message);
////sendToGateway(message);
///*
XBee.print("PacketID: ");
XBee.print(xbeeZB.packet_finished[xbeeZB.pos-1]->packetID,HEX);
XBee.println("");
XBee.print("Type Source ID: ");
XBee.print(xbeeZB.packet_finished[xbeeZB.pos-1]->typeSourceID,HEX);
XBee.println();
XBee.print("Network Identifier Origin: ");
for (int g=0;g<4;g++) {
XBee.print(xbeeZB.packet_finished[xbeeZB.pos-1]->niO[g],BYTE);
}
XBee.println();
XBee.print("Source Destination: ");
XBee.println(xbeeZB.packet_finished[xbeeZB.pos-1]->SD,HEX);
XBee.print("Destination Endpoint: ");
XBee.println(xbeeZB.packet_finished[xbeeZB.pos-1]->DE,HEX);
XBee.print("Cluster ID: ");
XBee.print(xbeeZB.packet_finished[xbeeZB.pos-1]->CID[0],HEX);
XBee.println(xbeeZB.packet_finished[xbeeZB.pos-1]->CID[1],HEX);
XBee.print("Profile ID: ");
XBee.print(xbeeZB.packet_finished[xbeeZB.pos-1]->PID[0],HEX);
XBee.println(xbeeZB.packet_finished[xbeeZB.pos-1]->PID[1],HEX);
free(xbeeZB.packet_finished[xbeeZB.pos]);
xbeeZB.packet_finished[xbeeZB.pos]=NULL;
///////////////xbeeZB.pos--;
}
previous=millis();
}
}
}
*/
}
//*/
delay(500);
}