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.
XbeeMonitor.cpp
00001 #include "XbeeMonitor.h" 00002 #include "XBeeLib.h" 00003 #include "Utils.h" 00004 #include "MQTTSManager.h" 00005 #include "config.h" 00006 #include "mbed.h" 00007 00008 using namespace XBeeLib; 00009 00010 #if defined(TARGET_NUCLEO_L476RG) 00011 XBee802 xbee(D8, D2, NC, NC, NC, 115200); // local xbee 00012 #else 00013 XBee802 xbee(PTC15, PTC14, NC, NC, NC, 115200); // local xbee 00014 #endif 00015 00016 extern float lastTemperature; 00017 00018 static MemoryPool<RadioControlData, 4> mpool; 00019 static Queue<RadioControlData, 4> mqueue; 00020 00021 void postRadioControl(RadioControlData &data) 00022 { 00023 // push data to pool 00024 RadioControlData *message = mpool.alloc(); 00025 // Simple copy 00026 *message = data; 00027 mqueue.put(message); 00028 } 00029 00030 // Callback functions 00031 static void io_data_cb(const RemoteXBee802 & remote, const IOSample802 & data) 00032 { 00033 RadioStatus radioStatus; 00034 const uint64_t remote_addr64 = remote.get_addr64(); 00035 00036 printf("\r\n============== Radio Callback ===================\r\n"); 00037 printf("\r\nRemote Radio Address :[%08x:%08x] %llu\r\n", 00038 UINT64_HI32(remote_addr64), 00039 UINT64_LO32(remote_addr64), 00040 remote_addr64); 00041 00042 SensorData sdata; 00043 00044 // Get data from the digital input pin (DIO2) Sprinkler 00045 DioVal dio2; 00046 radioStatus = data.get_dio(XBee802::DIO2_AD2, &dio2); 00047 if (radioStatus != Success) { 00048 printf("Getting data from digital pin DIO2 FAILED\r\n"); 00049 } 00050 00051 00052 // Get data from the humidity pin 00053 uint16_t humidity; 00054 radioStatus = data.get_adc( XBee802::DIO3_AD3, &humidity); 00055 if (radioStatus != Success) { 00056 printf("Getting data from analog pin AD3 FAILED\r\n" ); 00057 } 00058 00059 // Get data from the temperature pin 00060 uint16_t temperature; 00061 radioStatus = data.get_adc( XBee802::DIO0_AD0, &temperature); 00062 if (radioStatus != Success) { 00063 printf("Temperature : (AD0 Not connected) FAILED\r\n"); 00064 } 00065 00066 // Get data from the luminace pin 00067 uint16_t luminance; 00068 radioStatus = data.get_adc( XBee802::DIO1_AD1, &luminance); 00069 if (radioStatus != Success) { 00070 printf("Getting data from analog pin AD1 FAILED\r\n"); 00071 } 00072 00073 sdata.deviceaddr = remote_addr64; 00074 sdata.temperature = lastTemperature;//temperature; 00075 sdata.humidity = 1023 - humidity; 00076 sdata.luminance = luminance; 00077 // Assign the value to the sensor data 00078 sdata.sprinkler = dio2; 00079 00080 sdata.debug(); 00081 postMQTTUpdate(sdata); 00082 printf("\r\n=================================================\r\n"); 00083 } 00084 00085 uint64_t getXbeeId() 00086 { 00087 return xbee.get_addr64(); 00088 } 00089 00090 int initXbeeMonitor() 00091 { 00092 00093 /* Register the callback function */ 00094 xbee.register_io_sample_cb(&io_data_cb); 00095 00096 /* Initialize the xbee device */ 00097 RadioStatus radioStatus = xbee.init(); 00098 if (radioStatus != Success) 00099 { 00100 printf("Radio initialization failed!\r\n"); 00101 return -1; 00102 } 00103 printf("XBee radios initialized ...\r\n"); 00104 00105 return 0; 00106 } 00107 00108 int sendControlData(RadioControlData * data) 00109 { 00110 RadioStatus radioStatus; 00111 const RemoteXBee802 remoteDevice = RemoteXBee802(data->radioID); 00112 if (data->sprinkler_pin == 0) // Turn on 00113 { 00114 radioStatus = xbee.set_dio(remoteDevice, XBee802::DIO2_AD2, Low); 00115 }else 00116 { 00117 radioStatus = xbee.set_dio(remoteDevice, XBee802::DIO2_AD2, High); 00118 } 00119 00120 return (radioStatus == Success); 00121 } 00122 00123 void runXbeeMonitor() 00124 { 00125 // Continually listen for arriving frames 00126 while(true) 00127 { 00128 osEvent evt = mqueue.get(100); 00129 if (evt.status == osEventMessage) 00130 { 00131 // Unpack the message 00132 RadioControlData * message = (RadioControlData *)evt.value.p; 00133 00134 // Send the packet, do not queue the call 00135 sendControlData(message); 00136 00137 // Free the message from mempool after using 00138 mpool.free(message); 00139 } 00140 00141 xbee.process_rx_frames(); 00142 // Thread::wait(100); 00143 } 00144 }
Generated on Tue Jul 12 2022 18:06:47 by
1.7.2