first commit

Dependencies:   C12832_lcd DigiLogger LM75B XBeeLib2 mbed

Fork of XBee802_Send_Data by Digi International Inc.

Revision:
11:44571f2b239e
Parent:
9:db657fba2ccf
--- a/main.cpp	Fri Jul 29 12:11:33 2016 +0200
+++ b/main.cpp	Fri Dec 16 02:59:48 2016 +0000
@@ -1,84 +1,83 @@
-/**
- * Copyright (c) 2015 Digi International Inc.,
- * All rights not expressly granted are reserved.
- *
- * This Source Code Form is subject to the terms of the Mozilla Public
- * License, v. 2.0. If a copy of the MPL was not distributed with this file,
- * You can obtain one at http://mozilla.org/MPL/2.0/.
- *
- * Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343
- * =======================================================================
- */
+#include "mbed.h"
+#include "C12832_lcd.h"
+#include "LM75B.h"
+#include "XBeeLib.h"
 
-#include "mbed.h"
-#include "XBeeLib.h"
 #if defined(ENABLE_LOGGING)
 #include "DigiLoggerMbedSerial.h"
 using namespace DigiLog;
 #endif
 
-#define REMOTE_NODE_ADDR64_MSB  ((uint32_t)0x0013A200)
-
-#error "Replace next define with the LSB of the remote module's 64-bit address (SL parameter)"
-#define REMOTE_NODE_ADDR64_LSB  ((uint32_t)0x01234567)
-
-#error "Replace next define with the remote module's 16-bit address (MY parameter)"
-#define REMOTE_NODE_ADDR16      ((uint16_t)0x1111)
-
-#define REMOTE_NODE_ADDR64      UINT64(REMOTE_NODE_ADDR64_MSB, REMOTE_NODE_ADDR64_LSB)
+#define REMOTE_NODE_ADDR16      ((uint16_t)0x0)
 
 using namespace XBeeLib;
 
-Serial *log_serial;
+const int NODE_MODE=0; //0 = send reading, 1 = receive command
+LM75B tempSensor(p28,p27);
+C12832_LCD lcd;
+AnalogIn current(p17);
+DigitalOut leds [] ={(LED1),(LED2)};
+XBee802 xbee = XBee802(RADIO_TX, RADIO_RX, RADIO_RESET, NC, NC, 9600);
+const RemoteXBee802 remoteDevice16b = RemoteXBee802(REMOTE_NODE_ADDR16);
+char send_data [98];
 
-static void send_broadcast_data(XBee802& xbee)
-{
-    const char data[] = "send_broadcast_data";
-    const uint16_t data_len = strlen(data);
+void clear_send_data() {
+    for (int i=0;i<98;i++) send_data[i]=0;
+}
 
-    const TxStatus txStatus = xbee.send_data_broadcast((const uint8_t *)data, data_len);
+int calculate_send_data_size() {
+    for (int i=0;i<98;i++) if (send_data[i]==0) return i;
+    return 98;
+}
 
-    if (txStatus == TxStatusSuccess)
-        log_serial->printf("send_broadcast_data OK\r\n");
-    else
-        log_serial->printf("send_broadcast_data failed with %d\r\n", (int) txStatus);
+void sendTemp(const char* format, XBee802& xbee, const RemoteXBee802& remoteDevice) {
+    leds[0]=1;
+    snprintf(send_data,98,format,(tempSensor.temp()+55)/(175));
+    
+    xbee.send_data(remoteDevice,(const uint8_t *)send_data,calculate_send_data_size());
+    clear_send_data();
+    leds[0]=0;
 }
 
-static void send_data_to_remote_node(XBee802& xbee, const RemoteXBee802& RemoteDevice)
-{
-    const char data[] = "send_data_to_remote_node";
-    const uint16_t data_len = strlen(data);
-
-    const TxStatus txStatus = xbee.send_data(RemoteDevice, (const uint8_t *)data, data_len);
-
-    if (txStatus == TxStatusSuccess)
-        log_serial->printf("send_data_to_remote_node OK\r\n");
-    else
-        log_serial->printf("send_data_to_remote_node failed with %d\r\n", (int) txStatus);
+void sendCurrent(const char* format, XBee802& xbee, const RemoteXBee802& remoteDevice) {
+    leds[1]=1;
+    float max=0.0;
+    time_t start=time(NULL);
+    while (time(NULL)-start<2) {
+        float value=1.0-current;
+        if (value>0 && value>max) max=value;
+    }
+    
+    lcd.cls();
+    lcd.locate(0,0);
+    lcd.printf("Waterflow : %f L/min",max*19.8);
+    snprintf(send_data,98,format,max);
+            
+    xbee.send_data(remoteDevice,(const uint8_t *)send_data,calculate_send_data_size());
+    clear_send_data();
+    leds[1]=0;
 }
 
+const char tempFormat []="0;Toilet_Monitor;ToiletTemperature;%f";
+const char currentFormat []="0;Toilet_Monitor;ToiletWaterflow;%f";
+
+void sendReadings () {
+    while (1) {
+        sendTemp(tempFormat,xbee,remoteDevice16b);
+        wait(0.1);
+        sendCurrent(currentFormat,xbee,remoteDevice16b);
+        wait(58.0);
+    }
+}
 int main()
 {
-    log_serial = new Serial(DEBUG_TX, DEBUG_RX);
-    log_serial->baud(9600);
-    log_serial->printf("Sample application to demo how to send unicast and broadcast data with the XBee802\r\n\r\n");
-    log_serial->printf(XB_LIB_BANNER);
-
-#if defined(ENABLE_LOGGING)
-    new DigiLoggerMbedSerial(log_serial, LogLevelInfo);
-#endif
-
-    XBee802 xbee = XBee802(RADIO_TX, RADIO_RX, RADIO_RESET, NC, NC, 9600);
-
+    set_time(0);
+    mbed_interface_disconnect(); //Disable debugging to improve ADC precision. | Comment this if you need debugging via serial.
+    
     RadioStatus radioStatus = xbee.init();
     MBED_ASSERT(radioStatus == Success);
-
-    const RemoteXBee802 remoteDevice64b = RemoteXBee802(REMOTE_NODE_ADDR64);
-    const RemoteXBee802 remoteDevice16b = RemoteXBee802(REMOTE_NODE_ADDR16);
-
-    send_broadcast_data(xbee);
-    send_data_to_remote_node(xbee, remoteDevice64b);
-    send_data_to_remote_node(xbee, remoteDevice16b);
-
-    delete(log_serial);
-}
+    
+    if (NODE_MODE==0) {
+        sendReadings();
+    } else {}
+}
\ No newline at end of file