Fork of my original MQTTGateway

Dependencies:   mbed-http

Committer:
vpcola
Date:
Sat Apr 08 14:43:14 2017 +0000
Revision:
0:a1734fe1ec4b
Initial commit

Who changed what in which revision?

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