Você está na página 1de 25

/* * ------Waspmote XBee DigiMesh Creating a Network Example-----* * Explanation: This example shows how to create a Network using Waspmote

* XBee DigiMesh API * * Note: XBee modules must be configured at 38400bps and with API enabled. * * Copyright (C) 2009 Libelium Comunicaciones Distribuidas S.L. * http://www.libelium.com * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * * Version: 0.2 * Design: David Gascn * Implementation: Alberto Bielsa */ uint8_t PANID[2]={0x12,0x34}; char* KEY="WaspmoteKey"; void setup() { // Inits the XBee DigiMesh library xbeeDM.init(DIGIMESH,FREQ2_4G,NORMAL); // Powers XBee xbeeDM.ON();

void loop() { // Chosing a channel : channel 0x0D xbeeDM.setChannel(0x0D); if( !xbeeDM.error_AT ) XBee.println("Channel set OK"); else XBee.println("Error while changing channel"); // Chosing a PANID : PANID=0x1234 xbeeDM.setPAN(PANID); if( !xbeeDM.error_AT ) XBee.println("PANID set OK"); else XBee.println("Error while changing PANID"); // Enabling security : KEY="WaspmoteKey" xbeeDM.encryptionMode(1); if( !xbeeDM.error_AT ) XBee.println("Security enabled"); else XBee.println("Error while enabling security"); xbeeDM.setLinkKey(KEY);

if( !xbeeDM.error_AT ) XBee.println("Key set OK"); else XBee.println("Error while setting Key"); // Keep values xbeeDM.writeValues(); if( !xbeeDM.error_AT ) XBee.println("Changes stored OK"); else XBee.println("Error while storing values"); delay(3000); }

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * */

------Waspmote XBee DigiMesh Sending & Receiving Example-----Explanation: This example shows how to send and receive packets using Waspmote XBee DigiMesh API This code sends a packet to another node and waits for an answer from it. When the answer is received it is shown. Copyright (C) 2009 Libelium Comunicaciones Distribuidas S.L. http://www.libelium.com This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . Version: Design: Implementation: 0.2 David Gascn Alberto Bielsa

packetXBee* paq_sent; int8_t state=0; long previous=0; char* data="Test message!"; void setup() { // Inits the XBee DigiMesh library xbeeDM.init(DIGIMESH,FREQ2_4G,NORMAL); // Powers XBee xbeeDM.ON();

void loop() { // Set params to send

paq_sent=(packetXBee*) calloc(1,sizeof(packetXBee)); paq_sent->mode=BROADCAST; paq_sent->MY_known=0; paq_sent->packetID=0x52; paq_sent->opt=0; xbeeDM.hops=0; xbeeDM.setOriginParams(paq_sent, "5678", MY_TYPE); xbeeDM.setDestinationParams(paq_sent, "0013A20040304F9C", data, MAC_TYPE, DATA_ABSOLUTE); xbeeDM.sendXBee(paq_sent); if( !xbeeDM.error_TX ) { XBee.println("ok"); } free(paq_sent); paq_sent=NULL; // Waiting the answer previous=millis(); while( (millis()-previous) < 20000 ) { if( XBee.available() ) { xbeeDM.treatData(); if( !xbeeDM.error_RX ) { // Writing the parameters of the packet received while(xbeeDM.pos>0) { XBee.print("Network Address Source: "); XBee.print(xbeeDM.packet_finished[xbeeDM.pos-1]->naS[0],HEX); XBee.print(xbeeDM.packet_finished[xbeeDM.pos-1]->naS[1],HEX); XBee.println(""); XBee.print("MAC Address Source: "); for(int b=0;b<4;b++) { XBee.print(xbeeDM.packet_finished[xbeeDM.pos-1]>macSH[b],HEX); } for(int c=0;c<4;c++) { XBee.print(xbeeDM.packet_finished[xbeeDM.pos-1]>macSL[c],HEX); } XBee.println(""); XBee.print("Network Address Origin: "); XBee.print(xbeeDM.packet_finished[xbeeDM.pos-1]->naO[0],HEX); XBee.print(xbeeDM.packet_finished[xbeeDM.pos-1]->naO[1],HEX); XBee.println(""); XBee.print("MAC Address Origin: "); for(int d=0;d<4;d++) { XBee.print(xbeeDM.packet_finished[xbeeDM.pos-1]>macOH[d],HEX); } for(int e=0;e<4;e++) { XBee.print(xbeeDM.packet_finished[xbeeDM.pos-1]>macOL[e],HEX); }

XBee.println(""); XBee.print("RSSI: "); XBee.print(xbeeDM.packet_finished[xbeeDM.pos-1]->RSSI,HEX); XBee.println(""); XBee.print("16B(0) or 64B(1): "); XBee.print(xbeeDM.packet_finished[xbeeDM.pos-1]->mode,HEX); XBee.println(""); XBee.print("Data: "); for(int f=0;fdata_length;f++) { XBee.print(xbeeDM.packet_finished[xbeeDM.pos-1]>data[f],BYTE); } XBee.println(""); XBee.print("PacketID: "); XBee.print(xbeeDM.packet_finished[xbeeDM.pos-1]->packetID,HEX); XBee.println(""); XBee.print("Type Source ID: "); XBee.print(xbeeDM.packet_finished[xbeeDM.pos-1]>typeSourceID,HEX); XBee.println(""); XBee.print("Network Identifier Origin: "); for(int g=0;g<4;g++) { XBee.print(xbeeDM.packet_finished[xbeeDM.pos-1]->niO[g],BYTE); } XBee.println(""); free(xbeeDM.packet_finished[xbeeDM.pos-1]); xbeeDM.packet_finished[xbeeDM.pos-1]=NULL; xbeeDM.pos--; } previous=millis(); } } } delay(5000); }

/* * * * * * * * * * * * * * * * * *

------Waspmote XBee DigiMesh Sending & Receiving Example-----Explanation: This example shows how to send and receive packets using Waspmote XBee DigiMesh API This code sends a packet to another node and waits for an answer from it. When the answer is received it is shown. This is the code for the receiver. Copyright (C) 2009 Libelium Comunicaciones Distribuidas S.L. http://www.libelium.com This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any later version.

* * * * * * * * * * * * */

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . Version: Design: Implementation: 0.2 David Gascn Alberto Bielsa

packetXBee* paq_sent; int8_t state=0; long previous=0; char* data="Test message!"; uint8_t destination[8]; uint8_t i=0; void setup() { // Inits the XBee DigiMesh library xbeeDM.init(DIGIMESH,FREQ2_4G,NORMAL); // Powers XBee xbeeDM.ON();

void loop() { // Waiting message previous=millis(); while( (millis()-previous) < 20000 ) { if( XBee.available() ) { xbeeDM.treatData(); if( !xbeeDM.error_RX ) { // Sending answer back while(xbeeDM.pos>0) { if( (xbeeDM.packet_finished[xbeeDM.pos-1]->naO[0]==0x56) && (xbeeDM.packet_finished[xbeeDM.pos-1]->naO[1]==0x78) ) { paq_sent=(packetXBee*) calloc(1,sizeof(packetXBee)); paq_sent->mode=UNICAST; paq_sent->MY_known=0; paq_sent->packetID=0x52; paq_sent->opt=0; xbeeDM.hops=0; xbeeDM.setOriginParams(paq_sent, "ACK", NI_TYPE); while(i<4) { destination[i]=xbeeDM.packet_finished[xbeeDM.pos-1]>macSH[i]; i++; }

while(i<8) { destination[i]=xbeeDM.packet_finished[xbeeDM.pos-1]>macSL[i-4]; i++; } xbeeDM.setDestinationParams(paq_sent, destination, data, MAC_TYPE, DATA_ABSOLUTE); state=xbeeDM.sendXBee(paq_sent); if(state==0) { XBee.println("ok"); } free(paq_sent); paq_sent=NULL; } free(xbeeDM.packet_finished[xbeeDM.pos-1]); xbeeDM.packet_finished[xbeeDM.pos-1]=NULL; xbeeDM.pos--; } } } } delay(5000); }

/* * ------Waspmote XBee DigiMesh Getting RSSI Example-----* * Explanation: This example shows how to get RSSI after receiving a packet * * Note: XBee modules must be configured at 38400bps and with API enabled. * * Copyright (C) 2009 Libelium Comunicaciones Distribuidas S.L. * http://www.libelium.com * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * * Version: 0.2 * Design: David Gascn * Implementation: Alberto Bielsa */ long previous=0;

uint8_t state=0; int ByteRead[10]; int RSSI_rec=0; int i=0; char resul; void setup() { // Inits the XBee DigiMesh library xbeeDM.init(DIGIMESH,FREQ2_4G,NORMAL); // Powers XBee xbeeDM.ON();

void loop() { // Waiting message previous=millis(); while( (millis()-previous) < 20000 ) { if( XBee.available() ) { xbeeDM.treatData(); if( !xbeeDM.error_RX ) { // Getting RSSI using the API function xbeeDM.getRSSI(); if( !xbeeDM.error_AT ){ XBee.print("RSSI: "); XBee.println(xbeeDM.valueRSSI[0],HEX); } free(xbeeDM.packet_finished[xbeeDM.pos-1]); xbeeDM.packet_finished[xbeeDM.pos-1]=NULL; xbeeDM.pos--; } } } }

/* * ------Waspmote XBee DigiMesh Setting Low Power Mode Example-----* * Explanation: This example shows how to set low power mode * * Note: XBee modules must be configured at 38400bps and with API enabled. * * Copyright (C) 2009 Libelium Comunicaciones Distribuidas S.L. * http://www.libelium.com * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful,

* * * * * * * * * * */

but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . Version: Design: Implementation: 0.2 David Gascn Alberto Bielsa

long previous=0; uint8_t estado=0; uint8_t awake[3]={0x36,0xEE,0x80}; uint8_t sleep[3]={0x02,0x32,0x80}; void setup() { // Inits the XBee DigiMesh library xbeeDM.init(DIGIMESH,FREQ2_4G,NORMAL); // Powers XBee xbeeDM.ON();

void loop() { // Getting MAC xbeeDM.getOwnMac(); if( !xbeeDM.error_AT ) XBee.println("MAC received OK"); else XBee.println("Error while getting MAC"); // Setting Sleep Mode xbeeDM.setSleepMode(4); if( !xbeeDM.error_AT ) XBee.println("Sleep Mode set OK"); else XBee.println("Error while setting Sleep Mode"); // Sleeping xbeeDM.sleep(); if( !xbeeDM.error_AT ) XBee.println("Sleep OK"); else XBee.println("Error while entering to Sleep"); // Getting MAC xbeeDM.getOwnMac(); if( !xbeeDM.error_AT ) XBee.println("MAC received OK"); else XBee.println("Error while getting MAC"); // Waking up xbeeDM.wake(); if( !xbeeDM.error_AT ) XBee.println("Waked up OK"); else XBee.println("Error while waiking up");

/* * * *

------Waspmote RTC Setting and Reading Time Example-------Explanation: This example shows how to set and read the Time using

* * * * * * * * * * * * * * * * * * * * * */

the Waspmote RTC Copyright (C) 2009 Libelium Comunicaciones Distribuidas S.L. http://www.libelium.com This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . Version: Design: Implementation: 0.1 David Gascn Alberto Bielsa

void setup(){ // Setup for Serial port over USB USB.begin(); USB.println("USB port started..."); // Powers RTC up, init I2C bus and read initial values USB.println("Init RTC"); RTC.ON();

void loop(){ // Setting and getting time RTC.setTime("09:10:20:03:17:35:00"); USB.print("Time: "); USB.println(RTC.getTime()); RTC.setTime(9,10,20,3,17,36,0); USB.print("Time: "); USB.println(RTC.getTime()); delay(5000); }

/* * * * * * * *

------Waspmote RTC Reading Temperature Example-------Explanation: This example shows how to read the temperature using the sensor integrated in Waspmote Copyright (C) 2009 Libelium Comunicaciones Distribuidas S.L. http://www.libelium.com

* * * * * * * * * * * * * * * * * */

This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . Version: Design: Implementation: 0.1 David Gascn Alberto Bielsa

void setup(){ // Setup for Serial port over USB USB.begin(); USB.println("USB port started..."); // Powers RTC up, init I2C bus and read initial values USB.println("Init RTC"); RTC.ON(); // Setting time RTC.setTime("09:10:20:03:17:35:00"); USB.print("Time: "); USB.println(RTC.getTime()); } void loop(){ // Getting Temperature USB.print("Temperature: "); USB.println(RTC.getTemperature(),DEC); delay(5000); }

/* * * * * * * * * * * *

------Waspmote RTC Complete Example-------Explanation: This example shows how to read the temperature using the sensor integrated in Waspmote Copyright (C) 2009 Libelium Comunicaciones Distribuidas S.L. http://www.libelium.com This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 2 of the License, or

* * * * * * * * * * * * * */

(at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . Version: Design: Implementation: 0.1 David Gascn Alberto Bielsa

void setup(){ // Setup for Serial port over USB USB.begin(); USB.println("USB port started..."); // Powers RTC up, init I2C bus and read initial values USB.println("Init RTC"); RTC.ON(); } void loop(){ // Setting time RTC.setTime("09:10:20:03:17:35:00"); USB.print("Time: "); USB.println(RTC.getTime()); // Setting and getting Alarms RTC.setAlarm1("20:17:35:30",RTC_ABSOLUTE,RTC_ALM1_MODE2); USB.print("Alarm1: "); USB.println(RTC.getAlarm1()); // Setting Waspmote to Low-Power Consumption Mode PWR.sleep(ALL_OFF); // After setting Waspmote to power-down, UART is closed, so it // is necessary to open it again USB.begin(); // Waspmote wakes up at '17:35:30' if( intFlag & RTC_INT ) { intFlag &= ~(RTC_INT); // Clear flag Utils.blinkLEDs(1000); // Blinking LEDs Utils.blinkLEDs(1000); // Blinking LEDs } RTC.ON(); RTC.setAlarm2("20:17:36",RTC_ABSOLUTE,RTC_ALM2_MODE2); USB.print("Alarm2: "); USB.println(RTC.getAlarm2()); // Setting Waspmote to Low-Power Consumption Mode

PWR.sleep(ALL_OFF); // After setting Waspmote to power-down, UART is closed, so it // is necessary to open it again USB.begin(); // Waspmote wakes up at '17:36' if( intFlag & RTC_INT ) { intFlag &= ~(RTC_INT); // Clear flag Utils.blinkLEDs(1000); // Blinking LEDs Utils.blinkLEDs(1000); // Blinking LEDs } // Getting Temperature RTC.ON(); USB.print("Temperature: "); USB.println(RTC.getTemperature(),DEC); delay(5000); }

/* * * * * * * * * * * * * * * * * * * * * * * * * */

------Waspmote Accelerator Complete Example-------Explanation: This example shows how to manage all the functions developed in Waspmote Accelerometer libraries. Copyright (C) 2009 Libelium Comunicaciones Distribuidas S.L. http://www.libelium.com This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . Version: Design: Implementation: 0.1 David Gascn David Cuartielles, Alberto Bielsa

void setup() { ACC.ON(); USB.begin(); // starts using the serial port }

long timer = 0; uint8_t ctrl1=0; uint8_t ctrl2=0; uint8_t ctrl3=0; uint8_t status=0; uint8_t check=0; uint8_t adcMode=0; int accX=0; int accY=0; int accZ=0; uint8_t flag=0; int16_t auxReg=0; void loop() { //----------Check Register----------------------// should always answer 0x3A, it is used to check // the proper functionality of the accelerometer byte check = ACC.check(); //----------X Values----------------------int x_acc = ACC.getX(); //----------Y Values----------------------int y_acc = ACC.getY(); //----------Z Values----------------------int z_acc = ACC.getZ(); //------------------------------USB.print("\n------------------------------\nCheck: 0x"); USB.println(check, HEX); USB.println("\n \t0X\t0Y\t0Z"); USB.print(" ACC\t"); USB.print(x_acc, DEC); USB.print("\t"); USB.print(y_acc, DEC); USB.print("\t"); USB.println(z_acc, DEC); // Get accelerometer status status=ACC.getStatus(); USB.print("status: "); USB.println(status,BIN); // Get CTRL1 register ctrl1=ACC.getCTRL1(); USB.print("ctrl1: "); USB.println(ctrl1,BIN); // Set CTRL1 register to other value if(!ACC.setCTRL1(B01010111)) { // Check the register was changed ctrl1=ACC.getCTRL1(); USB.print("ctrl1: ");

USB.println(ctrl1,BIN); } // Get CTRL2 register ctrl2=ACC.getCTRL2(); USB.print("ctrl2: "); USB.println(ctrl2,BIN); // Set CTRL2 register to other value if(!ACC.setCTRL2(B00000000)) { // Check the register was changed ctrl2=ACC.getCTRL2(); USB.print("ctrl2: "); USB.println(ctrl2,BIN); } // Get CTRL3 register ctrl3=ACC.getCTRL3(); USB.print("ctrl3: "); USB.println(ctrl3,BIN); // Set CTRL3 register to other value if(!ACC.setCTRL3(B00001000)) { // Check the register was changed ctrl3=ACC.getCTRL3(); USB.print("ctrl3: "); USB.println(ctrl3,BIN); } // Another way to write into a register if(!ACC.writeRegister(ctrlReg3,B00001000)) { // Another way to read from a register ctrl3=ACC.readRegister(ctrlReg3); USB.print("ctrl3: "); USB.println(ctrl3,BIN); } // Get ADC mode: 16 bit or 12 bit mode adcMode=ACC.getADCmode(); USB.print("adcMode: "); USB.println(adcMode,DEC); if(!ACC.setADCmode(0)) { adcMode=ACC.getADCmode(); USB.print("adcMode: "); USB.println(adcMode,DEC); } delay(2000); }

/* * * * * * *

------Waspmote Accelerator Direction Change Example-------Explanation: This example shows how to manage the direction change interruption. Copyright (C) 2009 Libelium Comunicaciones Distribuidas S.L.

* * * * * * * * * * * * * * * * * * */

http://www.libelium.com This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . Version: Design: Implementation: 0.1 David Gascn David Cuartielles, Alberto Bielsa

long previous = 0; void setup() { ACC.ON(); ACC.setDD(); USB.begin(); // starts using the serial port } void loop() { // check the interruptions for 2 seconds previous = millis(); while(millis() - previous <= 2000) { if( intFlag & ACC_INT ) finishDDtest(); } printAcc(); } //// Functions /////////////////////////////////////////////////////////// /* * printAcc(void) - dump to the USB accX, accY, accZ if they are good data */ void printAcc(void) { //----------Check Register----------------------// should always answer 0x3A, it is used to check // the proper functionality of the accelerometer byte check = ACC.check(); //----------X Values----------------------int x_valH, x_valL, x_acc; x_valH = ACC.readRegister(outXhigh); x_valL = ACC.readRegister(outXlow); x_acc = ACC.getX();

//----------Y Values----------------------int y_valH, y_valL, y_acc; y_valH = ACC.readRegister(outYhigh); y_valL = ACC.readRegister(outYlow); y_acc = ACC.getY(); //----------Z Values----------------------int z_valH, z_valL, z_acc; z_valH = ACC.readRegister(outZhigh); z_valL = ACC.readRegister(outZlow); z_acc = ACC.getZ(); //------------------------------// print out data only if the sensor reports its identity properly if (check == 0x3A && x_valH != -1 && y_valH != -1 && z_valH != -1) { USB.println("------------------------------------------"); USB.println(" \t0X\t0Y\t0Z"); // show the thresholds (for orientation) USB.print("ThsI:\t"); USB.print(DD_THSI_H_val<<4 | DD_THSI_L_val>>4, DEC); USB.print("\t"); USB.print(DD_THSI_H_val<<4 | DD_THSI_L_val>>4, DEC); USB.print("\t"); USB.println(DD_THSI_H_val<<4 | DD_THSI_L_val>>4, DEC); // show the acceleration USB.print(" ACC\t"); USB.print(x_acc, DEC); USB.print("\t"); USB.print(y_acc, DEC); USB.print("\t"); USB.println(z_acc, DEC); // show the thresholds (for orientation) USB.print("ThsE:\t"); USB.print(DD_THSE_H_val<<4 | DD_THSE_L_val>>4, DEC); USB.print("\t"); USB.print(DD_THSE_H_val<<4 | DD_THSE_L_val>>4, DEC); USB.print("\t"); USB.println(DD_THSE_H_val<<4 | DD_THSE_L_val>>4, DEC); } else USB.print("no data"); } delay(500);

/* * finishFFinterrupt(void) - detach the free fall interrupt */ void finishDDtest () { // clear the accelerometer interrupt flag on the general interrupt vector intFlag &= ~(ACC_INT); // read the acceleration source register int auxReg = ACC.readRegister(DD_SRC); // print the results if the interrupt bit is // active. It could happen that there was an

// error in the communication, IA is the Active // Interrupt pin inside the DD_SRC register if (auxReg & IA && auxReg & ~(IA)) { USB.println("------------------------------------------"); USB.print("DD_SRC: "); USB.println(auxReg, BIN); USB.print("\nAcceleration detected on the axis: "); if (auxReg & XHIE) USB.print("OXH "); if (auxReg & YHIE) USB.print("OYH "); if (auxReg & ZHIE) USB.print("OZH "); if (auxReg & XLIE) USB.print("OXL "); if (auxReg & YLIE) USB.print("OYL "); if (auxReg & ZLIE) USB.print("OZL "); USB.println(); // make the LEDs blink Utils.blinkLEDs(1000);

// unset the Free Fall interrupt ACC.setDD();

/* * ------Waspmote OTA Digimesh Set up Example-----* * Explanation: This example shows how to set up Waspmote for operating * with OTA (Over The Air Programming). This code is valid for XBee Digimesh * * This code writes OTA's Authentication Key and Mote ID in EEPROM memory * This code can be used as a template to develop any other code that * provides OTA functionality. * * * Copyright (C) 2011 Libelium Comunicaciones Distribuidas S.L. * http://www.libelium.com * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * * Version: 0.1 * Design: David Gascn * Implementation: Alberto Bielsa, Yuri Carmona */

#define key_access "LIBELIUM" #define id_mote "WASPMOTE00000001" void setup() { USB.begin(); USB.println("Waspmote OTA Setup"); // Write Authentication Key in EEPROM memory for(int i=0;i<8;i++) { Utils.writeEEPROM(107+i,key_access[i]); } // Write Mote ID in EEPROM memory for(int i=0;i<16;i++) { Utils.writeEEPROM(147+i,id_mote[i]); } // Initialize Xbee module xbeeDM.init(DIGIMESH,FREQ2_4G,NORMAL); xbeeDM.ON(); // CheckNewProgram is mandatory in every OTA program xbeeDM.checkNewProgram(); } void loop() { // Check if new data is available if( XBee.available() ) { xbeeDM.treatData(); // Keep inside this loop while a new program is being received while( xbeeDM.programming_ON && !xbeeDM.checkOtapTimeout() ) { if( XBee.available() ) { xbeeDM.treatData(); } } } // Blink LED1 while messages are not received Utils.setLED(LED1,LED_ON); delay(100); Utils.setLED(LED1,LED_OFF); delay(100); }

Monitoring Sensor Data and Sending Alarm Emails


This code example has been tested and improved to be executed in Meshlium with the data coming from a Wireless Sensor Network created with Waspmote.

Alerts
This example will define a simple trigger based on an arbitrary value. Values over the trigger will be considered an alert and emailed. Note that on real applications each sensor usage has it's own alert parameters, and several alerts can be launched over different sensors.

For this example the frame will have the same structure used in previous tutorials

ID_<mote>#Sensor_value

A valid frame can be:

ID_12#239

Creating Database
The database to store data will store the following values: ID of the mote, sensor value recived, and the time of the received frame will be stored.

First we need to create the database for the example.

create database sensordemo; use sensordemo;

Add a user that can access the database.

GRANT ALL PRIVILEGES ON sensordemo.* TO 'sdemo'@'localhost' IDENTIFIED BY 'sdemopass' WITH GRANT OPTION; flush privileges;

Finally create the database.

CREATE TABLE sensor_data ( id INT UNSIGNED NOT NULL AUTO_INCREMENT, date timestamp not null, mote_id INT UNSIGNED NOT NULL, sensor_value INT UNSIGNED NOT NULL, PRIMARY KEY ); (id)

Sniffer with email alert code


This program reads frames received through a Bluetooth Serial Port or a XBee module and store it on a MySQL database. If a detected value if greater that alert_value, an alarm is processed and email sent.

File: "sniffer_ddbb_mail_alarm.c" Compilation on Meshlium: gcc -o sniffer_ddbb_mail_alarm -I/usr/include/mysql sniffer_ddbb_mail_alarm.c -L/usr/lib/mysql -lmysqlclient -Wall Usage: sniffer_ddbb_mail_alarm S0 localhost Note: Serial port speed should be set on sniffer execution.

Code

/* * * * * * * * * * * * * * * * * * * */

Copyright (C) 2008 Libelium Comunicaciones Distribuidas S.L. http://www.libelium.com This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. Version 0.1 Author: Octavio Benedi Sanchez

#include #include #include #include #include #include #include #include #include

<stdio.h> <string.h> <unistd.h> <fcntl.h> <errno.h> <stdlib.h> <termios.h> /* Terminal control library (POSIX) */ <ctype.h> <mysql.h>

#define MAX 500 #define MAX_SQL 700

int main(int argc, char *argv[]) { int sd = 3; char *serialPort = ""; char *serialPort0 = "/dev/ttyS0"; char *serialPort1 = "/dev/ttyS1"; char *USBserialPort0 = "/dev/ttyUSB0"; char *USBserialPort1 = "/dev/ttyUSB1"; char *USBserialPort2 = "/dev/ttyUSB2"; char *USBserialPort3 = "/dev/ttyUSB3"; char *USBserialPort4 = "/dev/ttyUSB4"; char *USBserialPort5 = "/dev/ttyUSB5"; char *BTserialPort0 = "/dev/rfcomm0"; char *BTserialPort1 = "/dev/rfcomm1"; char *BTserialPort2 = "/dev/rfcomm2"; char valor[MAX] = ""; char c; struct termios opciones; char *s0 = "S0"; char *s1 = "S1"; char *u0 = "USB0"; char *u1 = "USB1"; char *u2 = "USB2"; char *u3 = "USB3"; char *u4 = "USB4"; char *u5 = "USB5"; char *bt0 = "rfcomm0"; char *bt1 = "rfcomm1"; char *bt2 = "rfcomm2"; int speed = B19200; typedef struct { char *name; int flag; } speed_spec; speed_spec speeds[] = { {"1200", B1200}, {"2400", B2400}, {"4800", B4800}, {"9600", B9600}, {"19200", B19200}, {"38400", B38400}, {"57600", B57600}, {"115200", B115200}, {NULL, 0} }; /************************************** * Defines for MYSQL *************************************/ char sql[MAX_SQL] = ""; MYSQL mysql; /************************************** * Alarm values *************************************/ int alarm_value = 612;

/* STARTING MAIN */ if (argc != 4) { fprintf(stderr, "Usage: %s port speed bbdd_ip\n", argv[0]); exit(0); } if (argc == 4) { speed_spec *s; for (s = speeds; s->name; s++) { if (strcmp(s->name, argv[2]) == 0) { speed = s->flag; fprintf(stderr, "setting speed %s\n", s->name); break; } } } if (!strcmp(argv[1], s0)) { //fprintf(stderr,"ttyS0 chosen\n..."); serialPort = serialPort0; } if (!strcmp(argv[1], s1)) { //fprintf(stderr,"ttyS1 chosen\n..."); serialPort = serialPort1; } if (!strcmp(argv[1], u0)) { //fprintf(stderr,"ttyUSB0 chosen\n..."); serialPort = USBserialPort0; } if (!strcmp(argv[1], u1)) { //fprintf(stderr,"ttyUSB1 chosen\n..."); serialPort = USBserialPort1; } if (!strcmp(argv[1], u2)) { //fprintf(stderr,"ttyUSB2 chosen\n..."); serialPort = USBserialPort2; } if (!strcmp(argv[1], u3)) { //fprintf(stderr,"ttyUSB3 chosen\n..."); serialPort = USBserialPort3; } if (!strcmp(argv[1], u4)) { //fprintf(stderr,"ttyUSB4 chosen\n..."); serialPort = USBserialPort4; } if (!strcmp(argv[1], u5)) { //fprintf(stderr,"ttyUSB5 chosen\n..."); serialPort = USBserialPort5; } if (!strcmp(argv[1], bt0)) { //fprintf(stderr,"rfcomm0 chosen\n..."); serialPort = BTserialPort0; } if (!strcmp(argv[1], bt1)) { //fprintf(stderr,"rfcomm1 chosen\n..."); serialPort = BTserialPort1; } if (!strcmp(argv[1], bt2)) { //fprintf(stderr,"rfcomm2 chosen\n..."); serialPort = BTserialPort2; }

if (!strcmp(serialPort, "")) { fprintf(stderr, "Choose a valid port (S0, S1, USB0, USB1, USB2, USB3, USB4, USB5, rfcomm0, rfcomm1, rfcomm2)\n"); exit(0); } if ((sd = open(serialPort, O_RDWR | O_NOCTTY | O_NONBLOCK)) == -1) { fprintf(stderr, "Unable to open the serial port %s - \n", serialPort); exit(-1); } else { if (!sd) { /*Sometimes the first time you call open it does not return the * right value (3) of the free file descriptor to use, for this * reason you can set manually the sd value to 3 or call again * the open function (normally returning 4 to sd), advised!*/ sd = open(serialPort, O_RDWR | O_NOCTTY | O_NONBLOCK); } //fprintf(stderr,"Serial Port open at: %i\n", sd); fcntl(sd, F_SETFL, 0); } tcgetattr(sd, &opciones); cfsetispeed(&opciones, speed); cfsetospeed(&opciones, speed); opciones.c_cflag |= (CLOCAL | CREAD); /*No parity*/ opciones.c_cflag &= ~PARENB; opciones.c_cflag &= ~CSTOPB; opciones.c_cflag &= ~CSIZE; opciones.c_cflag |= CS8; /*raw input: * making the applycation ready to receive*/ opciones.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); /*Ignore parity errors*/ opciones.c_iflag |= ~(INPCK | ISTRIP | PARMRK); opciones.c_iflag |= IGNPAR; opciones.c_iflag &= ~(IXON | IXOFF | IXANY | IGNCR | IGNBRK); opciones.c_iflag |= BRKINT; /*raw output * making the applycation ready to transmit*/ opciones.c_oflag &= ~OPOST; /*aply*/ tcsetattr(sd, TCSANOW, &opciones); int j = 0; int ret; int mote_id; int sensor_value; mysql_init(&mysql); mysql_options(&mysql, MYSQL_READ_DEFAULT_GROUP, "your_prog_name"); if (!mysql_real_connect(&mysql, argv[3], "sdemo", "sdemopass", "sensordemo", 0, NULL, 0)) { fprintf(stderr, "Failed to connect to database: Error: %s\n", mysql_error(&mysql)); exit(-1); } else { fprintf(stderr, "Connected to database: return: %s\n", mysql_error(&mysql));

} while (1) { read(sd,&c,1); valor[j]=c; j++; if((c=='\n') || (j==(MAX-1))){ valor[j]='\0'; ret=sscanf(valor,"ID_%d#%d",&mote_id,&sensor_value); if (ret==2){ sprintf(sql,"insert into sensor_data values(NULL,NULL,'%d','%d');",mote_id,sensor_value); if (mysql_query(&mysql, sql) != 0){ fprintf(stderr,"BBDDFailed:%s\n",sql); } else { fprintf(stdout,"BBDDSucceeded:%s\n",sql); } if (sensor_value>alarm_value) { // send alarm mail int pid; pid = fork(); if(pid == 0){ // This is the child. Should exec and die. FILE *file; file = fopen("/tmp/message.txt", "w"); if(file==NULL) { fprintf(stderr,"Error: can't create file.\n"); return 1; } else { fprintf(file,"To: alert_destination@libelium.com\nFrom: meshlium_alert@libelium.com\nSubject: Sensor alert\n\nSensor value %d detected!\n",sensor_value); printf("Messafe file created. \nSending mail alert.\n"); fclose(file); system("ssmtp alert_destination@libelium.com.com &lt; /tmp/message.txt"); } return(0); } } } else{ fprintf(stderr,"Bad frame received\n"); } j=0; valor[j]='\0'; } } mysql_close(&mysql); close(sd);

exit(0); }

Você também pode gostar