xbee gateway

Dependencies:   NTPClient SDFileSystem WIZnetInterface XBeeLib mbed

Files at this revision

API Documentation at this revision

Comitter:
jehoon
Date:
Wed Sep 23 04:09:27 2015 +0000
Commit message:
my home gateway via xbee

Changed in this revision

NTPClient.lib Show annotated file Show diff for this revision Revisions of this file
SDFileSystem.lib Show annotated file Show diff for this revision Revisions of this file
WIZnetInterface.lib Show annotated file Show diff for this revision Revisions of this file
XBeeLib.lib Show annotated file Show diff for this revision Revisions of this file
config.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 4e6019c8c85f NTPClient.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/NTPClient.lib	Wed Sep 23 04:09:27 2015 +0000
@@ -0,0 +1,1 @@
+https://developer.mbed.org/users/donatien/code/NTPClient/#881559865a93
diff -r 000000000000 -r 4e6019c8c85f SDFileSystem.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SDFileSystem.lib	Wed Sep 23 04:09:27 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/SDFileSystem/#c8f66dc765d4
diff -r 000000000000 -r 4e6019c8c85f WIZnetInterface.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/WIZnetInterface.lib	Wed Sep 23 04:09:27 2015 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/teams/WIZnet/code/WIZnetInterface/#3b64df29662f
diff -r 000000000000 -r 4e6019c8c85f XBeeLib.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/XBeeLib.lib	Wed Sep 23 04:09:27 2015 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/teams/Digi-International-Inc/code/XBeeLib/#629712865107
diff -r 000000000000 -r 4e6019c8c85f config.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/config.h	Wed Sep 23 04:09:27 2015 +0000
@@ -0,0 +1,57 @@
+/**
+ * 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
+ * =======================================================================
+ */
+
+#ifndef __CONFIG_H_
+#define __CONFIG_H_
+
+/** Library configuration options *///
+//#define ENABLE_LOGGING
+#define ENABLE_ASSERTIONS
+#define FRAME_BUFFER_SIZE           4
+#define MAX_FRAME_PAYLOAD_LEN       128
+
+#define SYNC_OPS_TIMEOUT_MS         2000
+
+#define RADIO_TX                D1 /* TODO: specify your setup's Serial TX pin connected to the XBee module DIN pin */
+#define RADIO_RX                D0 /* TODO: specify your setup's Serial RX pin connected to the XBee module DOUT pin */
+//#define RADIO_RTS               NC /* TODO: specify your setup's Serial RTS# pin connected to the XBee module RTS# pin */
+//#define RADIO_CTS               NC /* TODO: specify your setup's Serial CTS# pin connected to the XBee module CTS# pin */
+#define RADIO_RESET             D2 /* TODO: specify your setup's GPIO (output) connected to the XBee module's reset pin */
+//#define RADIO_SLEEP_REQ         NC /* TODO: specify your setup's GPIO (output) connected to the XBee module's SLEEP_RQ pin */
+//#define RADIO_ON_SLEEP          NC /* TODO: specify your setup's GPIO (input) connected to the XBee module's ON_SLEEP pin */
+#define DEBUG_TX                USBTX /* TODO: specify your setup's Serial TX for debugging */
+#define DEBUG_RX                USBRX /* TODO: specify your setup's Serial RX for debugging (optional) */
+
+#if !defined(RADIO_TX)
+    #error "Please define RADIO_TX pin"
+#endif
+
+#if !defined(RADIO_RX)
+    #error "Please define RADIO_RX pin"
+#endif
+
+#if !defined(RADIO_RESET)
+    #define RADIO_RESET             NC
+    #warning "RADIO_RESET not defined, defaulted to 'NC'"
+#endif
+
+#if defined(ENABLE_LOGGING)
+    #if !defined(DEBUG_TX)
+        #error "Please define DEBUG_TX"
+    #endif
+    #if !defined(DEBUG_RX)
+        #define DEBUG_RX                NC
+        #warning "DEBUG_RX not defined, defaulted to 'NC'"
+    #endif
+#endif
+
+#endif /* __CONFIG_H_ */
diff -r 000000000000 -r 4e6019c8c85f main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Wed Sep 23 04:09:27 2015 +0000
@@ -0,0 +1,246 @@
+#include "mbed.h"
+#include "XBeeLib.h"
+#include "NTPClient.h"
+#include "EthernetInterface.h"
+#include "SDFileSystem.h"
+
+#define _SDENABLE 
+#define _DBG
+
+
+#define REMOTE_NODE_ADDR64_MSB  ((uint32_t)0x0013A200)
+#define REMOTE_NODE_ADDR64_LSB  ((uint32_t)0x40e82830)
+//#define REMOTE_NODE_ADDR64_LSB  ((uint32_t)0x40B8EBCE)
+
+#define REMOTE_NODE_ADDR16      ((uint16_t)0xFFFF)
+#define REMOTE_NODE_ADDR64      UINT64(REMOTE_NODE_ADDR64_MSB, REMOTE_NODE_ADDR64_LSB)
+
+
+using namespace XBeeLib;
+
+Serial pc(DEBUG_TX, DEBUG_RX);
+
+DigitalOut LED(D6);
+time_t cTime;
+Ticker now;
+time_t rtime;
+unsigned long waterlog = 0;
+
+enum dataType {
+    REQTOTAL = 0,    
+    SENDDATA
+};
+
+
+void ethInit();
+int storeSDCard(uint8_t *data, uint16_t len);
+static void receiveXbee(const RemoteXBee802& remote, bool broadcast, const uint8_t *const data, uint16_t len);
+static void sendRemoteXbee(XBee802& xbee, const RemoteXBee802& RemoteDevice);
+time_t getCurTime();
+
+
+
+
+
+
+
+void ledBlink(int cnt, int delay_ms = 50){
+    
+    LED = 0;
+    
+    for(int i = 0; i < cnt; i++){
+        LED = 1;
+        wait_ms(delay_ms);
+        LED = 0;
+        wait_ms(delay_ms);
+    }
+}
+
+static void sendRemoteXbee(XBee802& xbee, const RemoteXBee802& RemoteDevice)
+{   
+    char data[20] = {'\0'};
+    sprintf(data, "init");
+    
+    const uint16_t data_len = strlen(data);
+    printf("%s\n",data);
+    const TxStatus txStatus = xbee.send_data(RemoteDevice, (const uint8_t *)data, data_len);
+
+    if (txStatus == TxStatusSuccess)
+        printf(">> Send_data_to_remote_node OK...  %s\r\n",data);
+    else
+        printf(">> send_data_to_remote_node failed with %d\r\n", (int)txStatus);
+}
+
+
+static void receiveXbee(const RemoteXBee802& remote, bool broadcast, const uint8_t *const data, uint16_t len)
+{
+    char tmp[50] = {'\0'};
+        
+#ifdef _DBG
+
+    if (remote.is_valid_addr16b()) {
+        printf("\r\n>> Got a %s 16-bit RX packet [%04x], len %d\r\nData: ", broadcast ? "BROADCAST" : "UNICAST", remote.get_addr16(), len);
+    } else {
+        printf("\r\n>> Got a %s 64-bit RX packet [%08x:%08x], len %d\r\nData: ", broadcast ? "BROADCAST" : "UNICAST", remote.get_addr64(), len);
+    }
+#endif
+    
+    for (int i = 1; i < len; i++){
+        printf("%c", data[i]);
+        tmp[i - 1] = data[i];
+    }
+    printf("\r\n");
+    
+   // int val = atoi(tmp);
+    //printf("%d",val);
+    if(data[0] == SENDDATA + 48){
+         storeSDCard((uint8_t*)(data + 1), len - 1); 
+     }
+    else if(data[0] == REQTOTAL + 48){  //plust ascii
+   //     readSDCard();  
+    //    sendRemoteXbee(xbee, remoteDevice64b, val);  
+    }
+    
+}
+
+void ethInit(){
+    
+    int phy_link;
+    printf("Wait a second...\r\n");
+
+    uint8_t mac_addr[6] = {0x00,0x08,0xDC,0x1C,0xA8,0x95}; 
+    
+    EthernetInterface eth;
+//    eth.init(mac_addr,"222.98.173.194","255.255.255.0","222.98.173.254"); //Use DHCP
+    eth.init(mac_addr);
+    eth.connect();
+    
+     do{
+        phy_link = eth.ethernet_link();
+        printf("...");
+        ledBlink(1);
+        wait(2);
+     }while(!phy_link);
+     printf("\r\n");
+  
+    printf("IP Address is %s\r\n", eth.getIPAddress());
+
+}
+// copied from Real time bus arrival alarm by eric
+time_t getCurTime(){
+
+    // NTP initialize
+    NTPClient ntp;
+
+    printf("\r\nTrying to update time...\r\n");    
+
+
+    if (ntp.setTime("211.233.40.78") == 0) 
+    {
+        printf("Set time successfully\r\n");
+        time_t ctTime;
+        ctTime = time(NULL);
+        ctTime += 32400; // GMT+9/Seoul
+        printf("Time is set to (GMT+9): %s\r\n", ctime(&ctTime));
+        return ctTime;
+    }
+    else
+    {
+        printf("Error\r\n");
+        return 0;
+    }
+    
+    
+}
+int readSDCard(){
+    
+
+}
+
+int storeSDCard(uint8_t *data, uint16_t len){
+    char buffer[35];
+    char storeString[50] = {'\0'};
+    time_t ctTime = getCurTime();
+    data[len] = '\0';
+    strftime(buffer, 35, "%Y/%m/%d %I:%M %p", localtime(&ctTime));
+
+    //printf("%s\n%s\n", buffer ,data);
+    
+    mkdir("/sd/WaterLogger", 0777);
+    
+    FILE *fp = fopen("/sd/WaterLogger/WarerLevel.txt", "a");
+    if(fp == NULL) {
+        error("Could not open file for write\n");
+        return 0;
+    }
+    ledBlink(5);
+    sprintf(storeString,"%s %sml\r\n", buffer, data);
+    fprintf(fp, storeString);
+    fclose(fp); 
+    waterlog += atoi((char*)data);
+    
+#ifdef _DBG
+    printf("%s",storeString);    
+    printf("Stored Water Level\n");
+#endif
+
+    return 1;
+}
+
+
+//////////////////////////////////////
+//////////////////////////////////////
+//////////////////////////////////////
+//////////////////////////////////////
+//////////////////////////////////////
+//////////////////////////////////////
+//////////////////////////////////////
+void addTime(){
+    
+    rtime++;
+}
+
+
+int main()
+{
+    
+   // printf("%d",val);
+    ethInit(); // ethernet initialiaze
+
+    pc.baud(9600);
+    
+    XBee802 xbee = XBee802(RADIO_TX, RADIO_RX, RADIO_RESET, NC, NC, 9600);
+    SDFileSystem sd(PB_3, PB_2, PB_1, PB_0, "sd"); // the pinout on the mbed Cool Components workshop board
+    
+    /* Register callback */
+    xbee.register_receive_cb(&receiveXbee);
+    RadioStatus const radioStatus = xbee.init();
+    MBED_ASSERT(radioStatus == Success);
+    const RemoteXBee802 remoteDevice64b = RemoteXBee802(REMOTE_NODE_ADDR64);  
+    
+    rtime = getCurTime();
+    struct tm *realtime;
+    realtime = localtime(&rtime);
+    set_time(rtime);
+    now.attach(&addTime, 1); //tick for realtime
+    
+    //strftime(buffer, 35, "%Y/%m/%d %I:%M %p", localtime(&rtime));    
+    while (true) {
+       // strftime(buffer, 35, "%Y/%m/%d %I:%M %p", localtime(&rtime));    
+     //  strftime(buffer, 35, "%I:%M %p %S\n",localtime(&rtime));
+       // printf("%s",buffer);
+        
+        //if( realtime->tm_hour == 0 && realtime->tm_min == 0 && realtime->tm_sec == 0)
+           // sendRemoteXbee(xbee, remoteDevice64b);  
+        
+        xbee.process_rx_frames();
+        wait_ms(100);
+    }
+    
+
+}
+
+
+
+
+
diff -r 000000000000 -r 4e6019c8c85f mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Wed Sep 23 04:09:27 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/4f6c30876dfa
\ No newline at end of file