Vergil Cola
/
MQTTGatewayK64
Fork of my MQTTGateway
XbeeMonitor/XbeeMonitor.cpp@0:f1d3878b8dd9, 2017-04-08 (annotated)
- Committer:
- vpcola
- Date:
- Sat Apr 08 14:45:51 2017 +0000
- Revision:
- 0:f1d3878b8dd9
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vpcola | 0:f1d3878b8dd9 | 1 | #include "XbeeMonitor.h" |
vpcola | 0:f1d3878b8dd9 | 2 | #include "XBeeLib.h" |
vpcola | 0:f1d3878b8dd9 | 3 | #include "Utils.h" |
vpcola | 0:f1d3878b8dd9 | 4 | #include "MQTTSManager.h" |
vpcola | 0:f1d3878b8dd9 | 5 | #include "config.h" |
vpcola | 0:f1d3878b8dd9 | 6 | #include "mbed.h" |
vpcola | 0:f1d3878b8dd9 | 7 | |
vpcola | 0:f1d3878b8dd9 | 8 | using namespace XBeeLib; |
vpcola | 0:f1d3878b8dd9 | 9 | |
vpcola | 0:f1d3878b8dd9 | 10 | #if defined(TARGET_NUCLEO_L476RG) |
vpcola | 0:f1d3878b8dd9 | 11 | XBee802 xbee(D8, D2, NC, NC, NC, 115200); // local xbee |
vpcola | 0:f1d3878b8dd9 | 12 | #else |
vpcola | 0:f1d3878b8dd9 | 13 | XBee802 xbee(PTC15, PTC14, NC, NC, NC, 115200); // local xbee |
vpcola | 0:f1d3878b8dd9 | 14 | #endif |
vpcola | 0:f1d3878b8dd9 | 15 | |
vpcola | 0:f1d3878b8dd9 | 16 | extern float lastTemperature; |
vpcola | 0:f1d3878b8dd9 | 17 | |
vpcola | 0:f1d3878b8dd9 | 18 | static MemoryPool<RadioControlData, 4> mpool; |
vpcola | 0:f1d3878b8dd9 | 19 | static Queue<RadioControlData, 4> mqueue; |
vpcola | 0:f1d3878b8dd9 | 20 | |
vpcola | 0:f1d3878b8dd9 | 21 | void postRadioControl(RadioControlData &data) |
vpcola | 0:f1d3878b8dd9 | 22 | { |
vpcola | 0:f1d3878b8dd9 | 23 | // push data to pool |
vpcola | 0:f1d3878b8dd9 | 24 | RadioControlData *message = mpool.alloc(); |
vpcola | 0:f1d3878b8dd9 | 25 | // Simple copy |
vpcola | 0:f1d3878b8dd9 | 26 | *message = data; |
vpcola | 0:f1d3878b8dd9 | 27 | mqueue.put(message); |
vpcola | 0:f1d3878b8dd9 | 28 | } |
vpcola | 0:f1d3878b8dd9 | 29 | |
vpcola | 0:f1d3878b8dd9 | 30 | // Callback functions |
vpcola | 0:f1d3878b8dd9 | 31 | static void io_data_cb(const RemoteXBee802 & remote, const IOSample802 & data) |
vpcola | 0:f1d3878b8dd9 | 32 | { |
vpcola | 0:f1d3878b8dd9 | 33 | RadioStatus radioStatus; |
vpcola | 0:f1d3878b8dd9 | 34 | const uint64_t remote_addr64 = remote.get_addr64(); |
vpcola | 0:f1d3878b8dd9 | 35 | |
vpcola | 0:f1d3878b8dd9 | 36 | printf("\r\n============== Radio Callback ===================\r\n"); |
vpcola | 0:f1d3878b8dd9 | 37 | printf("\r\nRemote Radio Address :[%08x:%08x] %llu\r\n", |
vpcola | 0:f1d3878b8dd9 | 38 | UINT64_HI32(remote_addr64), |
vpcola | 0:f1d3878b8dd9 | 39 | UINT64_LO32(remote_addr64), |
vpcola | 0:f1d3878b8dd9 | 40 | remote_addr64); |
vpcola | 0:f1d3878b8dd9 | 41 | |
vpcola | 0:f1d3878b8dd9 | 42 | SensorData sdata; |
vpcola | 0:f1d3878b8dd9 | 43 | |
vpcola | 0:f1d3878b8dd9 | 44 | // Get data from the digital input pin (DIO2) Sprinkler |
vpcola | 0:f1d3878b8dd9 | 45 | DioVal dio2; |
vpcola | 0:f1d3878b8dd9 | 46 | radioStatus = data.get_dio(XBee802::DIO2_AD2, &dio2); |
vpcola | 0:f1d3878b8dd9 | 47 | if (radioStatus != Success) { |
vpcola | 0:f1d3878b8dd9 | 48 | printf("Getting data from digital pin DIO2 FAILED\r\n"); |
vpcola | 0:f1d3878b8dd9 | 49 | } |
vpcola | 0:f1d3878b8dd9 | 50 | |
vpcola | 0:f1d3878b8dd9 | 51 | |
vpcola | 0:f1d3878b8dd9 | 52 | // Get data from the humidity pin |
vpcola | 0:f1d3878b8dd9 | 53 | uint16_t humidity; |
vpcola | 0:f1d3878b8dd9 | 54 | radioStatus = data.get_adc( XBee802::DIO3_AD3, &humidity); |
vpcola | 0:f1d3878b8dd9 | 55 | if (radioStatus != Success) { |
vpcola | 0:f1d3878b8dd9 | 56 | printf("Getting data from analog pin AD3 FAILED\r\n" ); |
vpcola | 0:f1d3878b8dd9 | 57 | } |
vpcola | 0:f1d3878b8dd9 | 58 | |
vpcola | 0:f1d3878b8dd9 | 59 | // Get data from the temperature pin |
vpcola | 0:f1d3878b8dd9 | 60 | uint16_t temperature; |
vpcola | 0:f1d3878b8dd9 | 61 | radioStatus = data.get_adc( XBee802::DIO0_AD0, &temperature); |
vpcola | 0:f1d3878b8dd9 | 62 | if (radioStatus != Success) { |
vpcola | 0:f1d3878b8dd9 | 63 | printf("Temperature : (AD0 Not connected) FAILED\r\n"); |
vpcola | 0:f1d3878b8dd9 | 64 | } |
vpcola | 0:f1d3878b8dd9 | 65 | |
vpcola | 0:f1d3878b8dd9 | 66 | // Get data from the luminace pin |
vpcola | 0:f1d3878b8dd9 | 67 | uint16_t luminance; |
vpcola | 0:f1d3878b8dd9 | 68 | radioStatus = data.get_adc( XBee802::DIO1_AD1, &luminance); |
vpcola | 0:f1d3878b8dd9 | 69 | if (radioStatus != Success) { |
vpcola | 0:f1d3878b8dd9 | 70 | printf("Getting data from analog pin AD1 FAILED\r\n"); |
vpcola | 0:f1d3878b8dd9 | 71 | } |
vpcola | 0:f1d3878b8dd9 | 72 | |
vpcola | 0:f1d3878b8dd9 | 73 | sdata.deviceaddr = remote_addr64; |
vpcola | 0:f1d3878b8dd9 | 74 | sdata.temperature = lastTemperature;//temperature; |
vpcola | 0:f1d3878b8dd9 | 75 | sdata.humidity = 1023 - humidity; |
vpcola | 0:f1d3878b8dd9 | 76 | sdata.luminance = luminance; |
vpcola | 0:f1d3878b8dd9 | 77 | // Assign the value to the sensor data |
vpcola | 0:f1d3878b8dd9 | 78 | sdata.sprinkler = dio2; |
vpcola | 0:f1d3878b8dd9 | 79 | |
vpcola | 0:f1d3878b8dd9 | 80 | sdata.debug(); |
vpcola | 0:f1d3878b8dd9 | 81 | postMQTTUpdate(sdata); |
vpcola | 0:f1d3878b8dd9 | 82 | printf("\r\n=================================================\r\n"); |
vpcola | 0:f1d3878b8dd9 | 83 | } |
vpcola | 0:f1d3878b8dd9 | 84 | |
vpcola | 0:f1d3878b8dd9 | 85 | uint64_t getXbeeId() |
vpcola | 0:f1d3878b8dd9 | 86 | { |
vpcola | 0:f1d3878b8dd9 | 87 | return xbee.get_addr64(); |
vpcola | 0:f1d3878b8dd9 | 88 | } |
vpcola | 0:f1d3878b8dd9 | 89 | |
vpcola | 0:f1d3878b8dd9 | 90 | int initXbeeMonitor() |
vpcola | 0:f1d3878b8dd9 | 91 | { |
vpcola | 0:f1d3878b8dd9 | 92 | |
vpcola | 0:f1d3878b8dd9 | 93 | /* Register the callback function */ |
vpcola | 0:f1d3878b8dd9 | 94 | xbee.register_io_sample_cb(&io_data_cb); |
vpcola | 0:f1d3878b8dd9 | 95 | |
vpcola | 0:f1d3878b8dd9 | 96 | /* Initialize the xbee device */ |
vpcola | 0:f1d3878b8dd9 | 97 | RadioStatus radioStatus = xbee.init(); |
vpcola | 0:f1d3878b8dd9 | 98 | if (radioStatus != Success) |
vpcola | 0:f1d3878b8dd9 | 99 | { |
vpcola | 0:f1d3878b8dd9 | 100 | printf("Radio initialization failed!\r\n"); |
vpcola | 0:f1d3878b8dd9 | 101 | return -1; |
vpcola | 0:f1d3878b8dd9 | 102 | } |
vpcola | 0:f1d3878b8dd9 | 103 | printf("XBee radios initialized ...\r\n"); |
vpcola | 0:f1d3878b8dd9 | 104 | |
vpcola | 0:f1d3878b8dd9 | 105 | return 0; |
vpcola | 0:f1d3878b8dd9 | 106 | } |
vpcola | 0:f1d3878b8dd9 | 107 | |
vpcola | 0:f1d3878b8dd9 | 108 | int sendControlData(RadioControlData * data) |
vpcola | 0:f1d3878b8dd9 | 109 | { |
vpcola | 0:f1d3878b8dd9 | 110 | RadioStatus radioStatus; |
vpcola | 0:f1d3878b8dd9 | 111 | const RemoteXBee802 remoteDevice = RemoteXBee802(data->radioID); |
vpcola | 0:f1d3878b8dd9 | 112 | if (data->sprinkler_pin == 0) // Turn on |
vpcola | 0:f1d3878b8dd9 | 113 | { |
vpcola | 0:f1d3878b8dd9 | 114 | radioStatus = xbee.set_dio(remoteDevice, XBee802::DIO2_AD2, Low); |
vpcola | 0:f1d3878b8dd9 | 115 | }else |
vpcola | 0:f1d3878b8dd9 | 116 | { |
vpcola | 0:f1d3878b8dd9 | 117 | radioStatus = xbee.set_dio(remoteDevice, XBee802::DIO2_AD2, High); |
vpcola | 0:f1d3878b8dd9 | 118 | } |
vpcola | 0:f1d3878b8dd9 | 119 | |
vpcola | 0:f1d3878b8dd9 | 120 | return (radioStatus == Success); |
vpcola | 0:f1d3878b8dd9 | 121 | } |
vpcola | 0:f1d3878b8dd9 | 122 | |
vpcola | 0:f1d3878b8dd9 | 123 | void runXbeeMonitor() |
vpcola | 0:f1d3878b8dd9 | 124 | { |
vpcola | 0:f1d3878b8dd9 | 125 | // Continually listen for arriving frames |
vpcola | 0:f1d3878b8dd9 | 126 | while(true) |
vpcola | 0:f1d3878b8dd9 | 127 | { |
vpcola | 0:f1d3878b8dd9 | 128 | osEvent evt = mqueue.get(100); |
vpcola | 0:f1d3878b8dd9 | 129 | if (evt.status == osEventMessage) |
vpcola | 0:f1d3878b8dd9 | 130 | { |
vpcola | 0:f1d3878b8dd9 | 131 | // Unpack the message |
vpcola | 0:f1d3878b8dd9 | 132 | RadioControlData * message = (RadioControlData *)evt.value.p; |
vpcola | 0:f1d3878b8dd9 | 133 | |
vpcola | 0:f1d3878b8dd9 | 134 | // Send the packet, do not queue the call |
vpcola | 0:f1d3878b8dd9 | 135 | sendControlData(message); |
vpcola | 0:f1d3878b8dd9 | 136 | |
vpcola | 0:f1d3878b8dd9 | 137 | // Free the message from mempool after using |
vpcola | 0:f1d3878b8dd9 | 138 | mpool.free(message); |
vpcola | 0:f1d3878b8dd9 | 139 | } |
vpcola | 0:f1d3878b8dd9 | 140 | |
vpcola | 0:f1d3878b8dd9 | 141 | xbee.process_rx_frames(); |
vpcola | 0:f1d3878b8dd9 | 142 | // Thread::wait(100); |
vpcola | 0:f1d3878b8dd9 | 143 | } |
vpcola | 0:f1d3878b8dd9 | 144 | } |