xbee gateway
Dependencies: NTPClient SDFileSystem WIZnetInterface XBeeLib mbed
main.cpp@0:4e6019c8c85f, 2015-09-23 (annotated)
- Committer:
- jehoon
- Date:
- Wed Sep 23 04:09:27 2015 +0000
- Revision:
- 0:4e6019c8c85f
my home gateway via xbee
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jehoon | 0:4e6019c8c85f | 1 | #include "mbed.h" |
jehoon | 0:4e6019c8c85f | 2 | #include "XBeeLib.h" |
jehoon | 0:4e6019c8c85f | 3 | #include "NTPClient.h" |
jehoon | 0:4e6019c8c85f | 4 | #include "EthernetInterface.h" |
jehoon | 0:4e6019c8c85f | 5 | #include "SDFileSystem.h" |
jehoon | 0:4e6019c8c85f | 6 | |
jehoon | 0:4e6019c8c85f | 7 | #define _SDENABLE |
jehoon | 0:4e6019c8c85f | 8 | #define _DBG |
jehoon | 0:4e6019c8c85f | 9 | |
jehoon | 0:4e6019c8c85f | 10 | |
jehoon | 0:4e6019c8c85f | 11 | #define REMOTE_NODE_ADDR64_MSB ((uint32_t)0x0013A200) |
jehoon | 0:4e6019c8c85f | 12 | #define REMOTE_NODE_ADDR64_LSB ((uint32_t)0x40e82830) |
jehoon | 0:4e6019c8c85f | 13 | //#define REMOTE_NODE_ADDR64_LSB ((uint32_t)0x40B8EBCE) |
jehoon | 0:4e6019c8c85f | 14 | |
jehoon | 0:4e6019c8c85f | 15 | #define REMOTE_NODE_ADDR16 ((uint16_t)0xFFFF) |
jehoon | 0:4e6019c8c85f | 16 | #define REMOTE_NODE_ADDR64 UINT64(REMOTE_NODE_ADDR64_MSB, REMOTE_NODE_ADDR64_LSB) |
jehoon | 0:4e6019c8c85f | 17 | |
jehoon | 0:4e6019c8c85f | 18 | |
jehoon | 0:4e6019c8c85f | 19 | using namespace XBeeLib; |
jehoon | 0:4e6019c8c85f | 20 | |
jehoon | 0:4e6019c8c85f | 21 | Serial pc(DEBUG_TX, DEBUG_RX); |
jehoon | 0:4e6019c8c85f | 22 | |
jehoon | 0:4e6019c8c85f | 23 | DigitalOut LED(D6); |
jehoon | 0:4e6019c8c85f | 24 | time_t cTime; |
jehoon | 0:4e6019c8c85f | 25 | Ticker now; |
jehoon | 0:4e6019c8c85f | 26 | time_t rtime; |
jehoon | 0:4e6019c8c85f | 27 | unsigned long waterlog = 0; |
jehoon | 0:4e6019c8c85f | 28 | |
jehoon | 0:4e6019c8c85f | 29 | enum dataType { |
jehoon | 0:4e6019c8c85f | 30 | REQTOTAL = 0, |
jehoon | 0:4e6019c8c85f | 31 | SENDDATA |
jehoon | 0:4e6019c8c85f | 32 | }; |
jehoon | 0:4e6019c8c85f | 33 | |
jehoon | 0:4e6019c8c85f | 34 | |
jehoon | 0:4e6019c8c85f | 35 | void ethInit(); |
jehoon | 0:4e6019c8c85f | 36 | int storeSDCard(uint8_t *data, uint16_t len); |
jehoon | 0:4e6019c8c85f | 37 | static void receiveXbee(const RemoteXBee802& remote, bool broadcast, const uint8_t *const data, uint16_t len); |
jehoon | 0:4e6019c8c85f | 38 | static void sendRemoteXbee(XBee802& xbee, const RemoteXBee802& RemoteDevice); |
jehoon | 0:4e6019c8c85f | 39 | time_t getCurTime(); |
jehoon | 0:4e6019c8c85f | 40 | |
jehoon | 0:4e6019c8c85f | 41 | |
jehoon | 0:4e6019c8c85f | 42 | |
jehoon | 0:4e6019c8c85f | 43 | |
jehoon | 0:4e6019c8c85f | 44 | |
jehoon | 0:4e6019c8c85f | 45 | |
jehoon | 0:4e6019c8c85f | 46 | |
jehoon | 0:4e6019c8c85f | 47 | void ledBlink(int cnt, int delay_ms = 50){ |
jehoon | 0:4e6019c8c85f | 48 | |
jehoon | 0:4e6019c8c85f | 49 | LED = 0; |
jehoon | 0:4e6019c8c85f | 50 | |
jehoon | 0:4e6019c8c85f | 51 | for(int i = 0; i < cnt; i++){ |
jehoon | 0:4e6019c8c85f | 52 | LED = 1; |
jehoon | 0:4e6019c8c85f | 53 | wait_ms(delay_ms); |
jehoon | 0:4e6019c8c85f | 54 | LED = 0; |
jehoon | 0:4e6019c8c85f | 55 | wait_ms(delay_ms); |
jehoon | 0:4e6019c8c85f | 56 | } |
jehoon | 0:4e6019c8c85f | 57 | } |
jehoon | 0:4e6019c8c85f | 58 | |
jehoon | 0:4e6019c8c85f | 59 | static void sendRemoteXbee(XBee802& xbee, const RemoteXBee802& RemoteDevice) |
jehoon | 0:4e6019c8c85f | 60 | { |
jehoon | 0:4e6019c8c85f | 61 | char data[20] = {'\0'}; |
jehoon | 0:4e6019c8c85f | 62 | sprintf(data, "init"); |
jehoon | 0:4e6019c8c85f | 63 | |
jehoon | 0:4e6019c8c85f | 64 | const uint16_t data_len = strlen(data); |
jehoon | 0:4e6019c8c85f | 65 | printf("%s\n",data); |
jehoon | 0:4e6019c8c85f | 66 | const TxStatus txStatus = xbee.send_data(RemoteDevice, (const uint8_t *)data, data_len); |
jehoon | 0:4e6019c8c85f | 67 | |
jehoon | 0:4e6019c8c85f | 68 | if (txStatus == TxStatusSuccess) |
jehoon | 0:4e6019c8c85f | 69 | printf(">> Send_data_to_remote_node OK... %s\r\n",data); |
jehoon | 0:4e6019c8c85f | 70 | else |
jehoon | 0:4e6019c8c85f | 71 | printf(">> send_data_to_remote_node failed with %d\r\n", (int)txStatus); |
jehoon | 0:4e6019c8c85f | 72 | } |
jehoon | 0:4e6019c8c85f | 73 | |
jehoon | 0:4e6019c8c85f | 74 | |
jehoon | 0:4e6019c8c85f | 75 | static void receiveXbee(const RemoteXBee802& remote, bool broadcast, const uint8_t *const data, uint16_t len) |
jehoon | 0:4e6019c8c85f | 76 | { |
jehoon | 0:4e6019c8c85f | 77 | char tmp[50] = {'\0'}; |
jehoon | 0:4e6019c8c85f | 78 | |
jehoon | 0:4e6019c8c85f | 79 | #ifdef _DBG |
jehoon | 0:4e6019c8c85f | 80 | |
jehoon | 0:4e6019c8c85f | 81 | if (remote.is_valid_addr16b()) { |
jehoon | 0:4e6019c8c85f | 82 | printf("\r\n>> Got a %s 16-bit RX packet [%04x], len %d\r\nData: ", broadcast ? "BROADCAST" : "UNICAST", remote.get_addr16(), len); |
jehoon | 0:4e6019c8c85f | 83 | } else { |
jehoon | 0:4e6019c8c85f | 84 | printf("\r\n>> Got a %s 64-bit RX packet [%08x:%08x], len %d\r\nData: ", broadcast ? "BROADCAST" : "UNICAST", remote.get_addr64(), len); |
jehoon | 0:4e6019c8c85f | 85 | } |
jehoon | 0:4e6019c8c85f | 86 | #endif |
jehoon | 0:4e6019c8c85f | 87 | |
jehoon | 0:4e6019c8c85f | 88 | for (int i = 1; i < len; i++){ |
jehoon | 0:4e6019c8c85f | 89 | printf("%c", data[i]); |
jehoon | 0:4e6019c8c85f | 90 | tmp[i - 1] = data[i]; |
jehoon | 0:4e6019c8c85f | 91 | } |
jehoon | 0:4e6019c8c85f | 92 | printf("\r\n"); |
jehoon | 0:4e6019c8c85f | 93 | |
jehoon | 0:4e6019c8c85f | 94 | // int val = atoi(tmp); |
jehoon | 0:4e6019c8c85f | 95 | //printf("%d",val); |
jehoon | 0:4e6019c8c85f | 96 | if(data[0] == SENDDATA + 48){ |
jehoon | 0:4e6019c8c85f | 97 | storeSDCard((uint8_t*)(data + 1), len - 1); |
jehoon | 0:4e6019c8c85f | 98 | } |
jehoon | 0:4e6019c8c85f | 99 | else if(data[0] == REQTOTAL + 48){ //plust ascii |
jehoon | 0:4e6019c8c85f | 100 | // readSDCard(); |
jehoon | 0:4e6019c8c85f | 101 | // sendRemoteXbee(xbee, remoteDevice64b, val); |
jehoon | 0:4e6019c8c85f | 102 | } |
jehoon | 0:4e6019c8c85f | 103 | |
jehoon | 0:4e6019c8c85f | 104 | } |
jehoon | 0:4e6019c8c85f | 105 | |
jehoon | 0:4e6019c8c85f | 106 | void ethInit(){ |
jehoon | 0:4e6019c8c85f | 107 | |
jehoon | 0:4e6019c8c85f | 108 | int phy_link; |
jehoon | 0:4e6019c8c85f | 109 | printf("Wait a second...\r\n"); |
jehoon | 0:4e6019c8c85f | 110 | |
jehoon | 0:4e6019c8c85f | 111 | uint8_t mac_addr[6] = {0x00,0x08,0xDC,0x1C,0xA8,0x95}; |
jehoon | 0:4e6019c8c85f | 112 | |
jehoon | 0:4e6019c8c85f | 113 | EthernetInterface eth; |
jehoon | 0:4e6019c8c85f | 114 | // eth.init(mac_addr,"222.98.173.194","255.255.255.0","222.98.173.254"); //Use DHCP |
jehoon | 0:4e6019c8c85f | 115 | eth.init(mac_addr); |
jehoon | 0:4e6019c8c85f | 116 | eth.connect(); |
jehoon | 0:4e6019c8c85f | 117 | |
jehoon | 0:4e6019c8c85f | 118 | do{ |
jehoon | 0:4e6019c8c85f | 119 | phy_link = eth.ethernet_link(); |
jehoon | 0:4e6019c8c85f | 120 | printf("..."); |
jehoon | 0:4e6019c8c85f | 121 | ledBlink(1); |
jehoon | 0:4e6019c8c85f | 122 | wait(2); |
jehoon | 0:4e6019c8c85f | 123 | }while(!phy_link); |
jehoon | 0:4e6019c8c85f | 124 | printf("\r\n"); |
jehoon | 0:4e6019c8c85f | 125 | |
jehoon | 0:4e6019c8c85f | 126 | printf("IP Address is %s\r\n", eth.getIPAddress()); |
jehoon | 0:4e6019c8c85f | 127 | |
jehoon | 0:4e6019c8c85f | 128 | } |
jehoon | 0:4e6019c8c85f | 129 | // copied from Real time bus arrival alarm by eric |
jehoon | 0:4e6019c8c85f | 130 | time_t getCurTime(){ |
jehoon | 0:4e6019c8c85f | 131 | |
jehoon | 0:4e6019c8c85f | 132 | // NTP initialize |
jehoon | 0:4e6019c8c85f | 133 | NTPClient ntp; |
jehoon | 0:4e6019c8c85f | 134 | |
jehoon | 0:4e6019c8c85f | 135 | printf("\r\nTrying to update time...\r\n"); |
jehoon | 0:4e6019c8c85f | 136 | |
jehoon | 0:4e6019c8c85f | 137 | |
jehoon | 0:4e6019c8c85f | 138 | if (ntp.setTime("211.233.40.78") == 0) |
jehoon | 0:4e6019c8c85f | 139 | { |
jehoon | 0:4e6019c8c85f | 140 | printf("Set time successfully\r\n"); |
jehoon | 0:4e6019c8c85f | 141 | time_t ctTime; |
jehoon | 0:4e6019c8c85f | 142 | ctTime = time(NULL); |
jehoon | 0:4e6019c8c85f | 143 | ctTime += 32400; // GMT+9/Seoul |
jehoon | 0:4e6019c8c85f | 144 | printf("Time is set to (GMT+9): %s\r\n", ctime(&ctTime)); |
jehoon | 0:4e6019c8c85f | 145 | return ctTime; |
jehoon | 0:4e6019c8c85f | 146 | } |
jehoon | 0:4e6019c8c85f | 147 | else |
jehoon | 0:4e6019c8c85f | 148 | { |
jehoon | 0:4e6019c8c85f | 149 | printf("Error\r\n"); |
jehoon | 0:4e6019c8c85f | 150 | return 0; |
jehoon | 0:4e6019c8c85f | 151 | } |
jehoon | 0:4e6019c8c85f | 152 | |
jehoon | 0:4e6019c8c85f | 153 | |
jehoon | 0:4e6019c8c85f | 154 | } |
jehoon | 0:4e6019c8c85f | 155 | int readSDCard(){ |
jehoon | 0:4e6019c8c85f | 156 | |
jehoon | 0:4e6019c8c85f | 157 | |
jehoon | 0:4e6019c8c85f | 158 | } |
jehoon | 0:4e6019c8c85f | 159 | |
jehoon | 0:4e6019c8c85f | 160 | int storeSDCard(uint8_t *data, uint16_t len){ |
jehoon | 0:4e6019c8c85f | 161 | char buffer[35]; |
jehoon | 0:4e6019c8c85f | 162 | char storeString[50] = {'\0'}; |
jehoon | 0:4e6019c8c85f | 163 | time_t ctTime = getCurTime(); |
jehoon | 0:4e6019c8c85f | 164 | data[len] = '\0'; |
jehoon | 0:4e6019c8c85f | 165 | strftime(buffer, 35, "%Y/%m/%d %I:%M %p", localtime(&ctTime)); |
jehoon | 0:4e6019c8c85f | 166 | |
jehoon | 0:4e6019c8c85f | 167 | //printf("%s\n%s\n", buffer ,data); |
jehoon | 0:4e6019c8c85f | 168 | |
jehoon | 0:4e6019c8c85f | 169 | mkdir("/sd/WaterLogger", 0777); |
jehoon | 0:4e6019c8c85f | 170 | |
jehoon | 0:4e6019c8c85f | 171 | FILE *fp = fopen("/sd/WaterLogger/WarerLevel.txt", "a"); |
jehoon | 0:4e6019c8c85f | 172 | if(fp == NULL) { |
jehoon | 0:4e6019c8c85f | 173 | error("Could not open file for write\n"); |
jehoon | 0:4e6019c8c85f | 174 | return 0; |
jehoon | 0:4e6019c8c85f | 175 | } |
jehoon | 0:4e6019c8c85f | 176 | ledBlink(5); |
jehoon | 0:4e6019c8c85f | 177 | sprintf(storeString,"%s %sml\r\n", buffer, data); |
jehoon | 0:4e6019c8c85f | 178 | fprintf(fp, storeString); |
jehoon | 0:4e6019c8c85f | 179 | fclose(fp); |
jehoon | 0:4e6019c8c85f | 180 | waterlog += atoi((char*)data); |
jehoon | 0:4e6019c8c85f | 181 | |
jehoon | 0:4e6019c8c85f | 182 | #ifdef _DBG |
jehoon | 0:4e6019c8c85f | 183 | printf("%s",storeString); |
jehoon | 0:4e6019c8c85f | 184 | printf("Stored Water Level\n"); |
jehoon | 0:4e6019c8c85f | 185 | #endif |
jehoon | 0:4e6019c8c85f | 186 | |
jehoon | 0:4e6019c8c85f | 187 | return 1; |
jehoon | 0:4e6019c8c85f | 188 | } |
jehoon | 0:4e6019c8c85f | 189 | |
jehoon | 0:4e6019c8c85f | 190 | |
jehoon | 0:4e6019c8c85f | 191 | ////////////////////////////////////// |
jehoon | 0:4e6019c8c85f | 192 | ////////////////////////////////////// |
jehoon | 0:4e6019c8c85f | 193 | ////////////////////////////////////// |
jehoon | 0:4e6019c8c85f | 194 | ////////////////////////////////////// |
jehoon | 0:4e6019c8c85f | 195 | ////////////////////////////////////// |
jehoon | 0:4e6019c8c85f | 196 | ////////////////////////////////////// |
jehoon | 0:4e6019c8c85f | 197 | ////////////////////////////////////// |
jehoon | 0:4e6019c8c85f | 198 | void addTime(){ |
jehoon | 0:4e6019c8c85f | 199 | |
jehoon | 0:4e6019c8c85f | 200 | rtime++; |
jehoon | 0:4e6019c8c85f | 201 | } |
jehoon | 0:4e6019c8c85f | 202 | |
jehoon | 0:4e6019c8c85f | 203 | |
jehoon | 0:4e6019c8c85f | 204 | int main() |
jehoon | 0:4e6019c8c85f | 205 | { |
jehoon | 0:4e6019c8c85f | 206 | |
jehoon | 0:4e6019c8c85f | 207 | // printf("%d",val); |
jehoon | 0:4e6019c8c85f | 208 | ethInit(); // ethernet initialiaze |
jehoon | 0:4e6019c8c85f | 209 | |
jehoon | 0:4e6019c8c85f | 210 | pc.baud(9600); |
jehoon | 0:4e6019c8c85f | 211 | |
jehoon | 0:4e6019c8c85f | 212 | XBee802 xbee = XBee802(RADIO_TX, RADIO_RX, RADIO_RESET, NC, NC, 9600); |
jehoon | 0:4e6019c8c85f | 213 | SDFileSystem sd(PB_3, PB_2, PB_1, PB_0, "sd"); // the pinout on the mbed Cool Components workshop board |
jehoon | 0:4e6019c8c85f | 214 | |
jehoon | 0:4e6019c8c85f | 215 | /* Register callback */ |
jehoon | 0:4e6019c8c85f | 216 | xbee.register_receive_cb(&receiveXbee); |
jehoon | 0:4e6019c8c85f | 217 | RadioStatus const radioStatus = xbee.init(); |
jehoon | 0:4e6019c8c85f | 218 | MBED_ASSERT(radioStatus == Success); |
jehoon | 0:4e6019c8c85f | 219 | const RemoteXBee802 remoteDevice64b = RemoteXBee802(REMOTE_NODE_ADDR64); |
jehoon | 0:4e6019c8c85f | 220 | |
jehoon | 0:4e6019c8c85f | 221 | rtime = getCurTime(); |
jehoon | 0:4e6019c8c85f | 222 | struct tm *realtime; |
jehoon | 0:4e6019c8c85f | 223 | realtime = localtime(&rtime); |
jehoon | 0:4e6019c8c85f | 224 | set_time(rtime); |
jehoon | 0:4e6019c8c85f | 225 | now.attach(&addTime, 1); //tick for realtime |
jehoon | 0:4e6019c8c85f | 226 | |
jehoon | 0:4e6019c8c85f | 227 | //strftime(buffer, 35, "%Y/%m/%d %I:%M %p", localtime(&rtime)); |
jehoon | 0:4e6019c8c85f | 228 | while (true) { |
jehoon | 0:4e6019c8c85f | 229 | // strftime(buffer, 35, "%Y/%m/%d %I:%M %p", localtime(&rtime)); |
jehoon | 0:4e6019c8c85f | 230 | // strftime(buffer, 35, "%I:%M %p %S\n",localtime(&rtime)); |
jehoon | 0:4e6019c8c85f | 231 | // printf("%s",buffer); |
jehoon | 0:4e6019c8c85f | 232 | |
jehoon | 0:4e6019c8c85f | 233 | //if( realtime->tm_hour == 0 && realtime->tm_min == 0 && realtime->tm_sec == 0) |
jehoon | 0:4e6019c8c85f | 234 | // sendRemoteXbee(xbee, remoteDevice64b); |
jehoon | 0:4e6019c8c85f | 235 | |
jehoon | 0:4e6019c8c85f | 236 | xbee.process_rx_frames(); |
jehoon | 0:4e6019c8c85f | 237 | wait_ms(100); |
jehoon | 0:4e6019c8c85f | 238 | } |
jehoon | 0:4e6019c8c85f | 239 | |
jehoon | 0:4e6019c8c85f | 240 | |
jehoon | 0:4e6019c8c85f | 241 | } |
jehoon | 0:4e6019c8c85f | 242 | |
jehoon | 0:4e6019c8c85f | 243 | |
jehoon | 0:4e6019c8c85f | 244 | |
jehoon | 0:4e6019c8c85f | 245 | |
jehoon | 0:4e6019c8c85f | 246 |