I've tried what you recommend me, but it doesn't work fine.
I've set the thresolds, and the light and temp sensor does not produce interrupts. However, the aperture sensor, plugged in socket 2 is producing an interrupt, but in the SensorEvents intFlag it seems to be the PIR sensor.
I post here all my code:
Code:
#include "WProgram.h"
#define SENS_PRESION SENS_SOCKET1 //entrada comparador positiva
#define SENS_LUZ SENS_SOCKET3 //entrada comparador positiva
#define SENS_TEMP SENS_SOCKET5 //enttrada comparador positiva
#define SENS_PRESENCIA SENS_SOCKET7 //entrada digital
#define SENS_APERTURA SENS_SOCKET2 //entrada comparador negativa
float leerPresion();
float leerLuz();
float leerTemp();
char leerPresencia();
char leerApertura();
void SensIntConf();
void float2stringNchar (float fl, char str[], int N); //modificación para obtener solo 4 caracteres (incluido el punto de separación de parte entera y decimal)
uint8_t extPanId[8]={1,2,3,4,5,6,7,8};
uint8_t destination[8];
packetXBee* paq_sent;
int8_t state = 0;
long previous = 0;
uint8_t i=0;
char myNwkAddr[4];
char data[20];
int main(void)
{
init();
setup();
for (;;)
loop();
return 0;
}
void setup()
{
USB.begin();
//Start Xbee in Zigbee, 2.4GHz, 2mW
xbeeZB.init(ZIGBEE, FREQ2_4G, NORMAL);
//Xbee power on
xbeeZB.ON();
delay(2000);
xbeeZB.getAssociationIndication();
delay(2000);// Check if creating process success
while (xbeeZB.associationIndication)
{
XBee.println("Imposible unirse a la red");
xbeeZB.getAssociationIndication();
delay(200);
}
XBee.println("Unido a la red!");
RTC.ON();
SensIntConf();
delay(1000);
}
void SensIntConf()
{
SensorEvent.setBoardMode(SENS_ON);
SensorEvent.attachInt();
SensorEvent.setThreshold(SENS_PRESION, 3.3);
SensorEvent.setThreshold(SENS_LUZ,3.3);
SensorEvent.setThreshold(SENS_TEMP,3.3);
SensorEvent.setThreshold(SENS_APERTURA,3.3);
}
void loop()
{
#define st "Estamos en el loop!!!"
XBee.println(st);
//A Dormir...
XBee.println("A dormir...");
PWR.deepSleep("00:00:00:10",RTC_OFFSET,RTC_ALM1_MODE2,UART1_OFF | BAT_OFF | RTC_OFF);
//Evaluacion de interrupcion
USB.begin();
RTC.ON();
char presencia='0';
if( intFlag & RTC_INT )
{
intFlag &= ~(RTC_INT); // Clear flag
XBee.println("Interrupción de RTC");
}
else if(intFlag & SENS_INT)
{
SensorEvent.loadInt();
if(SensorEvent.intFlag & SENS_PRESENCIA)
{
XBee.println("Interrupción de PIR");
presencia='1';
}
else
XBee.println("Interrupción de Otro sensor");
intFlag &= ~(SENS_INT);
}
else
{
XBee.println("Interrupción de otro sitio...");
}
//Lectura de datos
float presion= leerPresion();
char presionC[4];
float2stringNchar(presion,presionC,4);
strcpy(data,presionC);
XBee.print("Presion=");
XBee.println(presionC);
float luz= leerLuz();
char luzC[4];
float2stringNchar(luz,luzC,4);
strcat(data,luzC);
XBee.print("luz=");
XBee.println(luzC);
float temp=leerTemp();
char tempC[4];
float2stringNchar(temp,tempC,4);
strcat(data,tempC);
XBee.print("Temp=");
XBee.println(tempC);
char apertura=leerApertura();
XBee.print("Apertura=");
XBee.println(apertura);
data[12]=apertura;
XBee.print("Presencia=");
XBee.println(presencia);
data[13]=presencia;
data[14]='\0';
XBee.print("Datos a enviar = ");
XBee.println(data);
//enviamos datos...
paq_sent = (packetXBee*) calloc(1, sizeof(packetXBee));
paq_sent->mode = UNICAST;
paq_sent->MY_known = 0;
paq_sent->packetID = 0x52;
paq_sent->opt = 0;
xbeeZB.hops = 0;
xbeeZB.getOwnNetAddress();
Utils.hex2str(xbeeZB.sourceNA, myNwkAddr);
xbeeZB.setOriginParams(paq_sent, myNwkAddr, MY_TYPE);
//Opción de enviarla directamente al coord con nwkAddr=0x0000
char data2[20];
strcpy(data,data2);
xbeeZB.setDestinationParams(paq_sent, "0000",data2, MY_TYPE,DATA_ABSOLUTE);
xbeeZB.sendXBee(paq_sent);
if (!xbeeZB.error_TX) {
XBee.println("enviado OK");
} else {
XBee.print("error al enviar: ");
XBee.println(xbeeZB.error_TX);
}
free(paq_sent);
paq_sent = NULL;
delay(500);
}
float leerPresion()
{
//Socket1 R=560K - 1/R=valorLeido(560K*(3.3-valorLeido))
//1 lbs = 0.45359237 kg --> 25 lbs= 11.34kg
float valorLeido=SensorEvent.readValue(SENS_PRESION);
float cond=valorLeido/(560*(3.3-valorLeido));
XBee.print("Conductancia=");
XBee.print(cond);
return cond;
}
float leerLuz()
{
//valor de luz en %
float valorLeido = SensorEvent.readValue(SENS_LUZ);
float luz = valorLeido/3.3*100;
return luz;
}
float leerTemp()
{
float valorLeido=SensorEvent.readValue(SENS_TEMP);
float temp=valorLeido*100-50; //0.5V = 0ªC , 1V = 50ºC
return temp;
}
char leerApertura()
{
float valorLeido=SensorEvent.readValue(SENS_APERTURA);
valorLeido=(int)valorLeido;
if(valorLeido>1)
return '1';
else
return '0';
}
void float2stringNchar (float fl, char str[], int N)
{
boolean neg = false;
if( fl<0 ){
neg = true;
fl*=-1;
}
float numeroFloat=fl;
int parteEntera[10];
int cifra;
long numero=(long)numeroFloat;
int size=0;
while(1){
size=size+1;
cifra=numero%10;
numero=numero/10;
parteEntera[size-1]=cifra;
if ((numero==0) || (size==N)){
break;
}
}
int indice=0;
if( neg ){
indice++;
str[0]='-';
}
for (int i=size-1; i>=0; i--)
{
str[indice]=parteEntera[i]+'0';
indice++;
}
if(size<N){
str[indice]='.';
size++;
indice++;
numeroFloat=(numeroFloat-(int)numeroFloat);
for (int i=1; i<=N-size ; i++)
{
numeroFloat=numeroFloat*10;
cifra= (long)numeroFloat;
numeroFloat=numeroFloat-cifra;
str[indice]=char(cifra)+48;
indice++;
}
}
str[indice]='\0';
}
If i switch on the waspmote, and dont touch anything, the RTC is waking up the board very 10 seconds.
If I pass my finger 3cm over the PIR sensor, it doesn't produce the interrupt always (neither in H or L position).
When the aperture sensor changes its value (from open to close, or viceversa) an interrupt is produced, but my code resolves it as a PIR interrup.
I've also plugged the Flexiforce sensor in socket 1. When I touch it anywhere (noy only in ), an interrupt is produced as another sensor interrup, or a PIR interrupt, or other interrupt which is not from RTC or SENS INT. The header is not very well, could it be the solder joints?
Is there something wrong in my code that I'm not seeing? I'm quite desperate.
Thanks for all.
Best regards,
Iker.