Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: AppNearMe_MuNFC_PN532 RHT03 mbed xbee_lib
main.cpp
00001 /* Copyright (c) 2012 Tristan Hughes, MIT License 00002 * 00003 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software 00004 * and associated documentation files (the "Software"), to deal in the Software without restriction, 00005 * including without limitation the rights to use, copy, modify, merge, publish, distribute, 00006 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 00007 * furnished to do so, subject to the following conditions: 00008 * 00009 * The above copyright notice and this permission notice shall be included in all copies or 00010 * substantial portions of the Software. 00011 * 00012 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 00013 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00014 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 00015 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00016 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00017 */ 00018 00019 /*------INCLUDES-----*/ 00020 #include "mbed.h" 00021 #include "RHT03.h" 00022 #include "MuNFC.h" 00023 #include "xbee.h" 00024 00025 /*------Definitions----*/ 00026 #define SLEEP_TIME 30 //Time to wait between reading and sending data 00027 #define SENSOR_PACKET 2 //Tells the NFC app device is a sensor 00028 #define LOCATION_PRECISION 100000 //The accuracy of the devices location 00029 00030 /*------Pins------*/ 00031 DigitalOut red(p30); 00032 DigitalOut green(p29); 00033 DigitalOut pswitch(p21); 00034 AnalogIn light(p15); 00035 DigitalIn pir(p16); 00036 DigitalIn setup(p14); 00037 DigitalOut led_alive(LED1); 00038 DigitalOut led_progress(LED2); 00039 DigitalOut led_ok(LED3); 00040 DigitalOut led_failed(LED4); 00041 Serial pc1(USBTX, USBRX); 00042 00043 /*-----Globals-----*/ 00044 char send_data[202]; 00045 int cur_temp,cur_hum,device_serial[8]; 00046 float cur_light; 00047 int cur_pir; 00048 int nfc_setup = 0; 00049 Ticker val_check; 00050 00051 /*-----Class Init-----*/ 00052 MuNFC nfc("000000053Tfi0RwY", 1, p11, p12, p13, p19, p18); 00053 xbee xbee_1(p9,p10,p25); 00054 00055 00056 /*-----A class of useful functions for use with NFC----*/ 00057 class sr_configuration 00058 { 00059 public: 00060 sr_configuration( const uint8_t* ieee_address ) { 00061 memcpy( this->ieee_address, ieee_address, 8 ); 00062 } 00063 00064 void hexPrint(const char* name, uint8_t* buf, int len) { 00065 printf("%s: ", name); 00066 for(int i = 0; i < len; i++) { 00067 printf("%02x ", buf[i]); 00068 } 00069 printf("\r\n"); 00070 } 00071 00072 void prettyPrint() { 00073 printf("Name: %s\r\n", name); 00074 hexPrint("IEEE Address", ieee_address, 8); 00075 hexPrint("Gateway IEEE Address", gw_ieee_address, 8); 00076 hexPrint("Gateway Security Key", gw_security_key, 16); 00077 printf("Location: Lat.: %d.%05d; Long.: %d.%05d\r\n", 00078 location[0]/LOCATION_PRECISION, (location[0] - (location[0]/LOCATION_PRECISION)*LOCATION_PRECISION ), 00079 location[1]/LOCATION_PRECISION, (location[1] - (location[1]/LOCATION_PRECISION)*LOCATION_PRECISION ) ); 00080 } 00081 00082 char name[24]; 00083 uint8_t ieee_address[8]; 00084 uint8_t gw_ieee_address[8]; 00085 uint8_t gw_security_key[16]; 00086 uint32_t location[2]; //lat*100000, long*100000 00087 }; 00088 00089 const uint8_t address[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; //setting the IEEE address to 0 00090 sr_configuration config(address); 00091 00092 //Encode call back for NFC 00093 void encode_cb(TLVList* tlv, void *) 00094 { 00095 tlv->putUInt8( SENSOR_PACKET ); //First uint8_t is packet type 00096 tlv->putString(config.name); 00097 tlv->putArray(config.ieee_address, 8); 00098 tlv->putArray(config.gw_ieee_address, 8); 00099 tlv->putArray(config.gw_security_key, 16); 00100 tlv->putUInt32(config.location[0]); 00101 tlv->putUInt32(config.location[1]); 00102 } 00103 00104 //Decode callback for NFC 00105 void decode_cb(TLVList* tlv, void *) 00106 { 00107 TLVList::TLVType t; 00108 00109 if(tlv->getNext() != UINT8) { 00110 return; 00111 } 00112 if(tlv->getUInt8() == SENSOR_PACKET) { //First uint8_t is packet type 00113 if(tlv->getNext() != STRING) { 00114 return; 00115 } 00116 tlv->getString(config.name, 23); 00117 00118 if(tlv->getNext() != UINT8_ARRAY) { 00119 return; 00120 } 00121 //tlv->getArray(config.ieee_address, 8); //IEEE address is ignored 00122 00123 if(tlv->getNext() != UINT8_ARRAY) { 00124 return; 00125 } 00126 tlv->getArray(config.gw_ieee_address, 8); 00127 00128 if(tlv->getNext() != UINT8_ARRAY) { 00129 return; 00130 } 00131 tlv->getArray(config.gw_security_key, 16); 00132 00133 t = tlv->getNext(); 00134 if( (t != UINT32) && (t != UINT8) ) { 00135 return; 00136 } 00137 config.location[0] = tlv->getUInt32(); 00138 00139 t = tlv->getNext(); 00140 if( (t != UINT32) && (t != UINT8) ) { 00141 return; 00142 } 00143 config.location[1] = tlv->getUInt32(); 00144 } 00145 nfc_setup = 1; 00146 } 00147 00148 //NFC event call back (sets LED's on mbed) 00149 void event_cb(NFCEvent event, void*) 00150 { 00151 switch(event) { 00152 case NFC_TRANSACTION_STARTED: 00153 led_progress=1; 00154 led_ok=0; 00155 led_failed=0; 00156 break; 00157 case NFC_TRANSACTION_SUCCESSFUL: 00158 led_progress=0; 00159 led_ok=1; 00160 led_failed=0; 00161 break; 00162 case NFC_TRANSACTION_FAILED: 00163 led_progress=0; 00164 led_ok=0; 00165 led_failed=1; 00166 break; 00167 } 00168 } 00169 00170 //Reads values from Sensors 00171 void read_values(void) 00172 { 00173 int attempts=0; 00174 red = 1; 00175 pswitch = 0; //Power up the sensor network 00176 RHT03 humtemp(p22); //Intalise the RHT03 Class 00177 wait(1); //Wait to let the powersupply stablise 00178 cur_light = light*100; //Read the value of the light sensor *100 to make into percentage 00179 while(attempts <= 10 ) { 00180 wait(2); 00181 if(humtemp.readData() == RHT_ERROR_NONE) attempts = 20; //Read the RHT03 will have 20 attempts to read correclty 00182 attempts++; 00183 } 00184 if(humtemp.getHumidity()>0) { //Used to detect if a valid reading has occured 00185 cur_temp = (int)humtemp.getTemperatureC(); //Read current temp 00186 cur_hum = (int)humtemp.getHumidity(); //Read current humidity 00187 } 00188 00189 cur_pir = pir; //Read the current PIR value 00190 green=1; 00191 wait_ms(1); 00192 sprintf(send_data,"%x,%2d,%2d,%03.0f,%i; \0",device_serial,cur_temp,cur_hum,cur_light,cur_pir); //Formated data string 00193 xbee_1.SendData(send_data); //Send the data to the Xbee 00194 wait(5); //Give the Xbee time to send the data 00195 green = 0; 00196 red = 0; 00197 pswitch = 1; //Power the sensors/Xbee down 00198 } 00199 00200 00201 //Request NFC setup data (called if setup pin=0) 00202 void get_setup(void) 00203 { 00204 pswitch = 0; //Power sensors and NFC up 00205 wait(2); //Let powersupply stabalise 00206 memcpy(config.ieee_address,device_serial,8); //Coppy device serial into ieee_address for encode_cb 00207 00208 nfc.encode(encode_cb, NULL); //Register the encode callback function 00209 nfc.decode(decode_cb, NULL); //Register the decode callback function 00210 nfc.event(event_cb, NULL); //Register the event callback function 00211 00212 bool ret = nfc.init(); //Initalise the NFC 00213 if(ret) { 00214 //printf("\nAppNearMe/MuNFC stack initialized\n"); 00215 00216 } else { 00217 //printf("Could not initialize stack\n"); 00218 while(1) { 00219 green = !green; 00220 red = !red; 00221 wait_ms(500); //Flash both LED's if NFC isn't initalised 00222 } 00223 } 00224 00225 red = 1; 00226 00227 while(nfc_setup==0) { //While the decode callback hasn't been called poll the NFC 00228 nfc.poll(100); 00229 } 00230 red = 0; 00231 green = 1; 00232 led_ok = 0; 00233 xbee_1.ConfigMode(); //Enable config mode on Xbee 00234 xbee_1.SetKey((int*)config.gw_security_key); //Set the security key of the Xbee 00235 xbee_1.WriteSettings(); //Save the settings to the Xbee 00236 xbee_1.ExitConfigMode(); //Exit config 00237 00238 pswitch = 1; //power down the sensors/NFC 00239 wait(1); 00240 green = 0; 00241 } 00242 00243 int main() 00244 { 00245 setup.mode(PullUp); 00246 xbee_1.ConfigMode(); //Enable config mode on Xbee 00247 xbee_1.GetSerial(device_serial); //Read the Xbee's serial number 00248 xbee_1.ExitConfigMode(); //Exit config mode 00249 if(setup == 0) get_setup(); //Call setup if setup=0 00250 pswitch=1; //make sure sensors are powered down 00251 red=1; 00252 wait(1); 00253 green=1; 00254 red=0; 00255 wait(1); 00256 green=0; 00257 read_values(); //Read sensor values 00258 val_check.attach(&read_values,SLEEP_TIME); //Read sesnor data every SLEEP_TIME seconds 00259 while(1) { 00260 00261 } 00262 return 1; 00263 }
Generated on Sun Jul 17 2022 01:05:00 by
1.7.2