dzdzd

Dependencies:   XBeeLib mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /**
00002  * Copyright (c) 2015 Digi International Inc.,
00003  * All rights not expressly granted are reserved.
00004  *
00005  * This Source Code Form is subject to the terms of the Mozilla Public
00006  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
00007  * You can obtain one at http://mozilla.org/MPL/2.0/.
00008  *
00009  * Digi International Inc. 11001 Bren Road East, Minnetonka, MN 55343
00010  * =======================================================================
00011  */
00012 
00013 #include "mbed.h"
00014 #include "XBeeLib.h"
00015 #if defined(ENABLE_LOGGING)
00016 #include "DigiLoggerMbedSerial.h"
00017 using namespace DigiLog;
00018 #endif
00019 
00020 
00021 #define REMOTE_NODE_ADDR64_MSB  ((uint32_t)0x0013A200)
00022 
00023 //#error "Replace next define with the LSB of the remote module's 64-bit address (SL parameter)"
00024 #define REMOTE_NODE_ADDR64_LSB  ((uint32_t)0x40E779AF)
00025 
00026 #define REMOTE_NODE_ADDR64      UINT64(REMOTE_NODE_ADDR64_MSB, REMOTE_NODE_ADDR64_LSB)
00027 
00028 using namespace XBeeLib;
00029 
00030 //feux gauche
00031     //rouge
00032 DigitalOut feux_gauche_rouge(p30);
00033     //orange
00034 DigitalOut feux_gauche_orange(p28);
00035     //vert
00036 DigitalOut feux_gauche_vert(p26);
00037 
00038     //liste des message a envoyer rouge et vert seulement pour le coordinateur
00039     ////////////////////////// 
00040     /////
00041     char rouge[] = "r";
00042     char vert[] = "v";
00043     /////
00044     uint8_t ok = 0x02;
00045     //////////////////////////
00046 
00047 
00048 Serial *log_serial;
00049 
00050 /** Callback function, invoked at packet reception */
00051 static void receive_cb(const RemoteXBeeZB& remote, bool broadcast, const uint8_t *const data, uint16_t len)
00052 {
00053     const uint64_t remote_addr64 = remote.get_addr64();
00054 
00055     log_serial->printf("\r\nGot a %s RX packet [%08x:%08x|%04x], len %d\r\nData: ", broadcast ? "BROADCAST" : "UNICAST", UINT64_HI32(remote_addr64), UINT64_LO32(remote_addr64), remote.get_addr16(), len);
00056 
00057     for (int i = 0; i < len; i++)
00058         log_serial->printf("%02x ", data[i]);
00059         
00060     log_serial->printf("\r\n");
00061 }
00062 
00063 
00064 static void send_explicit_data_to_remote_node(XBeeZB& xbee, const RemoteXBeeZB& RemoteDevice,int message){
00065     
00066     //const uint8_t data_len = strlen(data);
00067     const uint8_t dstEP = 0xE8;
00068     const uint8_t srcEP = 0xE8;
00069     const uint16_t clusterID = 0x0011;
00070     const uint16_t profileID = 0xC105;
00071 
00072     char data1[]="r";
00073     char data2[]="v";
00074     char data3[]="e";
00075     if (message ==0){
00076         const TxStatus txStatus = xbee.send_data(RemoteDevice, dstEP, srcEP, clusterID, profileID, (const uint8_t *)data1, strlen(data1)); 
00077         if (txStatus == TxStatusSuccess)
00078             log_serial->printf("send_explicit_data_to_remote_node OK\r\n");
00079         else
00080             log_serial->printf("send_explicit_data_to_remote_node failed with %d\r\n", (int) txStatus);
00081     }
00082     else if (message ==1){
00083         const TxStatus txStatus = xbee.send_data(RemoteDevice, dstEP, srcEP, clusterID, profileID, (const uint8_t *)data2, strlen(data2)); 
00084         if (txStatus == TxStatusSuccess)
00085             log_serial->printf("send_explicit_data_to_remote_node OK\r\n");
00086         else
00087             log_serial->printf("send_explicit_data_to_remote_node failed with %d\r\n", (int) txStatus);
00088     }
00089     else {
00090         const TxStatus txStatus = xbee.send_data(RemoteDevice, dstEP, srcEP, clusterID, profileID, (const uint8_t *)data3, strlen(data3));
00091         if (txStatus == TxStatusSuccess)
00092             log_serial->printf("send_explicit_data_to_remote_node OK\r\n");
00093         else
00094             log_serial->printf("send_explicit_data_to_remote_node failed with %d\r\n", (int) txStatus);
00095     }
00096    
00097     
00098 }
00099 
00100 
00101 int main()
00102 {
00103     log_serial = new Serial(DEBUG_TX, DEBUG_RX);
00104     log_serial->baud(9600);
00105     log_serial->printf("Sample application to demo how to receive unicast and broadcast data with the XBeeZB\r\n\r\n");
00106     log_serial->printf(XB_LIB_BANNER);
00107     
00108 
00109 
00110 #if defined(ENABLE_LOGGING)
00111     new DigiLoggerMbedSerial(log_serial, LogLevelInfo);
00112 #endif
00113 
00114     XBeeZB xbee = XBeeZB(RADIO_TX, RADIO_RX, RADIO_RESET, NC, NC, 9600);
00115 
00116     /* Register callbacks */
00117     xbee.register_receive_cb(&receive_cb);
00118 
00119     RadioStatus const radioStatus = xbee.init();
00120     MBED_ASSERT(radioStatus == Success);
00121 
00122     /* Wait until the device has joined the network */
00123     log_serial->printf("Waiting for device to join the network: ");
00124     while (!xbee.is_joined()) {
00125         wait_ms(1000);
00126         log_serial->printf(".");
00127     }
00128     log_serial->printf("OK\r\n");
00129 
00130     int i =0;
00131     const RemoteXBeeZB remoteDevice = RemoteXBeeZB(REMOTE_NODE_ADDR64);
00132     while (true) {
00133         i++;
00134         uint32_t receive_value = xbee.process_rx_frames();
00135         log_serial->printf("%d");
00136         wait_ms(100);
00137         log_serial->printf(".");
00138         
00139         
00140         //partie gestion des feux a mettre sur le coordinateur 
00141         /////////////////////////////////////////////
00142         if(i==10){
00143             send_explicit_data_to_remote_node(xbee, remoteDevice,1);
00144             feux_gauche_rouge = 0;
00145             feux_gauche_vert = 1;
00146             }
00147         if(i==20){
00148             send_explicit_data_to_remote_node(xbee, remoteDevice,0);
00149             feux_gauche_rouge = 1;
00150             feux_gauche_vert = 0;
00151             i=0;
00152             }
00153         ////////////////////////////////////////////
00154     }
00155 
00156    // delete(log_serial);
00157 }