Created Wed, 30 Oct 2013 17:55:46 +0000 by AhMedina
Wed, 30 Oct 2013 17:55:46 +0000
Hi everybody, sorry for my english.....
I'm writing a code to communicate a chipkit uno32 with a pc using the XBee protoshield and the XBIB-DEV REV. 4 connected to the pc. On my code I'm using the Xbee.h library, and my XBees are using the API mode sending TX 16 and receiving RX 16 packets. The packet that i send through the XBIB-DEV from the PC to the chipkit uno32 is the following:
0x 7E 00 11 01 01 00 04 01 00 00 00 00 00 00 00 00 00 00 00 00 F8
and the payload data is:
0x 01 00 00 00 00 00 00 00 00 00 00 00 00
I'm trying to do different stuff with different payloads using a switch case structure, but when I try to compile I get these errors:
error: previously used here
error: duplicate case value
I know that the data (defined as int16_t data = 0;) value is too big and the chipkit can't handle it so, what can I do to handle these long packets? How can I read the data and then split the packet in 2byte parts and only take the needed one (in my example just reading the first byte 0x01, 0x02, etc) ?
Please help, any advice would be useful!! :!:
Here's my code:
#include "System_Defs.h"
#include <XBee.h>
#include <Sensirion.h>
int16_t statusLed = 9;
int16_t errorLed = 10;
int16_t salValue0 = LOW;
int16_t salValue1 = LOW;
int16_t salValue2 = LOW;
int16_t salValue3 = LOW;
//TASKs
int medicion_id;
unsigned long medicion_var;
// XBee payload
uint8_t payload[32] = {0};
// with Series 1 you can use either 16-bit or 64-bit addressing
// 16-bit addressing: Enter address of remote XBee, typically the coordinator
Tx16Request tx = Tx16Request(0x0001, payload, sizeof(payload));
XBee xbee = XBee();
XBeeResponse response = XBeeResponse();
// create reusable response objects for responses we expect to handle
Rx16Response rx16 = Rx16Response();
Rx64Response rx64 = Rx64Response();
//variables
boolean Manual = true;
boolean Auto = true;
int16_t Comando = 0;
int16_t PAtm = 0;
int16_t Ilum = 0;
int16_t TemperaturaPila1 = 0;
int16_t HumedadRelativaPila1 = 0;
int16_t TemperaturaPila2 = 0;
int16_t HumedadRelativaPila2 = 0;
int16_t TemperaturaPila3 = 0;
int16_t HumedadRelativaPila3 = 0;
int16_t TemperaturaPila4 = 0;
int16_t HumedadRelativaPila4 = 0;
float precipitacion = 0;
//SHT
const int16_t dataPin = 2;
const int16_t clockPin = 3;
int16_t option = 0;
int16_t data = 0;
int16_t DataComando = data >> 0xff;
float temperature;
float humidity;
float dewpoint;
Sensirion tempSensor = Sensirion(dataPin, clockPin);
void flashLed(int16_t pin, int16_t times, int16_t wait) {
for (int16_t i = 0; i < times; i++) {
digitalWrite(pin, HIGH);
delay(wait);
digitalWrite(pin, LOW);
if (i + 1 < times) {
delay(wait);
}
}
}
//Task: collect variable values and then send a packet to the pc’s XBee
void envio_medicion(int id, void * tptr) {
Serial.print(" Task ");
//SHT75 measures
tempSensor.measure(&temperature, &humidity, &dewpoint);
delay (10);
//envio de datos por el Xbee
payload[0] = Manual;
payload[1] = Comando;
payload[2] = salValue0;
payload[3] = (int16_t) temperature >> 8 & 0xff; //cast a temperature para
payload[4] = (int16_t) temperature & 0xff; //que pueda ser manipulada
payload[5] = (int16_t) humidity >> 8 & 0xff; //cast a humedad para
payload[6] = (int16_t) humidity & 0xff;//que pueda ser manipulada
payload[7] = (int16_t) PAtm >> 8 & 0xff;
payload[8] = (int16_t) PAtm & 0xff;
payload[9] = (int16_t) precipitacion >> 8 & 0xff;
payload[10] = (int16_t) precipitacion & 0xff;
payload[11] = (int16_t) Ilum >> 8 & 0xff;
payload[12] = (int16_t) Ilum & 0xff;
payload[13] = (int16_t) TemperaturaPila1 >> 8 & 0xff;
payload[14] = (int16_t) TemperaturaPila1 & 0xff;
payload[15] = (int16_t) HumedadRelativaPila1 >> 8 & 0xff;
payload[16] = (int16_t) HumedadRelativaPila1 & 0xff;
payload[17] = (int16_t) TemperaturaPila2 >> 8 & 0xff;
payload[18] = (int16_t) TemperaturaPila2 & 0xff;
payload[19] = (int16_t) HumedadRelativaPila2 >> 8 & 0xff;
payload[20] = (int16_t) HumedadRelativaPila2 & 0xff;
payload[22] = (int16_t) TemperaturaPila3 >> 8 & 0xff;
payload[22] = (int16_t) TemperaturaPila3 & 0xff;
payload[23] = (int16_t) HumedadRelativaPila3 >> 8 & 0xff;
payload[24] = (int16_t) HumedadRelativaPila3 & 0xff;
payload[25] = (int16_t) TemperaturaPila4 >> 8 & 0xff;
payload[26] = (int16_t) TemperaturaPila4 & 0xff;
payload[27] = (int16_t) HumedadRelativaPila4 >> 8 & 0xff;
payload[28] = (int16_t) HumedadRelativaPila4 & 0xff;
delay (20);
xbee.send(tx);
// flash TX indicator
flashLed(statusLed, 1, 100);
}
void setup() {
pinMode(statusLed, OUTPUT);
pinMode(errorLed, OUTPUT);
// start serial
Serial.begin(9600);
xbee.begin(9600);
flashLed(statusLed, 3, 50);
//Task
medicion_id = createTask(envio_medicion, 10000, TASK_ENABLE, &medicion_var);
}
// continuously reads packets, looking for RX16 or RX64
void loop() {
xbee.readPacket();
if (xbee.getResponse().isAvailable()) {
// got something
if (xbee.getResponse().getApiId() == RX_16_RESPONSE || xbee.getResponse().getApiId() == RX_64_RESPONSE) {
// got a rx packet
if (xbee.getResponse().getApiId() == RX_16_RESPONSE) {
xbee.getResponse().getRx16Response(rx16);
option = rx16.getOption();
data = rx16.getData(0);
}
else {
xbee.getResponse().getRx64Response(rx64);
option = rx64.getOption();
data = rx64.getData(0);
}
// check option, rssi bytes
flashLed(statusLed, 1, 10);
/////Operation modes
switch (data){
///Read commands
case 0x01000000000000000000000000:
Serial.print(" send 1 ");
xbee.send(tx);
flashLed(statusLed, 1, 100);
break;
case 0x02000000000000000000000000:
xbee.send(tx);
flashLed(statusLed, 1, 100);
Serial.print(" send 2 ");
break;
case 0x03000000000000000000000000:
xbee.send(tx);
flashLed(statusLed, 1, 100);
Serial.print(" send 3 ");
break;
}
Serial.print(data);
}
else {
// not something we were expecting
flashLed(errorLed, 1, 25);
}
}
else if (xbee.getResponse().isError()) {
//nss.print("Error reading packet. Error code: ");
//nss.println(xbee.getResponse().getErrorCode());
// or flash error led
}
}
Fri, 20 Dec 2013 00:18:15 +0000
Hi AhMedina,
I can see one possible reason your code might not work. I had a quick read through it and noticed that you you declared your variable "data" as a 16-bit int:
int16_t data = 0;
then you use it in a switch-case statement:
switch (data){
///Read commands
case 0x01000000000000000000000000:
...
However, the largest values an int16_t variable can hold is: 0xFFFF. So the values 0x01000000000000000000000000 would need a 128 bit variable rather than a 16 bit variable. Not sure the PIC32 can hold a variable of this size.
You might want to store your values in an array and look at the specific bytes you are interested in.
I know this does not solve your problem, but might solve at least one issue.
Fri, 20 Dec 2013 10:39:53 +0000
I'm trying to do different stuff with different payloads using a switch case structure, but when I try to compile I get these errors:
error: previously used here
error: duplicate case value
What line number? What is it saying is already defined? Please post the ENTIRE error message for it to be of any use to us.