Is it working Ben?

Dependencies:   EthernetInterface WebSocketClient mbed-rtos mbed ID12RFID XBeeLib

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "rtos.h"
00003 #include "EthernetInterface.h"
00004 #include "Websocket.h"
00005 #include "ID12RFID.h"
00006 #include "XBeeLib.h"
00007 #include "DigiLoggerMbedSerial.h"
00008 
00009 using namespace XBeeLib;
00010 //static const char* mbedIp       = "192.168.1.2";
00011 //static const char* mbedMask     = "255.255.255.0";
00012 //static const char* mbedGateway  = "192.168.1.1";
00013 DigitalOut myled(LED1);
00014 Serial pc(USBTX, USBRX);
00015 //static char* WEBSOCKET_URL= "ws://braceletus.herokuapp.com:8080/"; // Adresse Serveur M-A
00016 //static char* WEBSOCKET_URL = "ws://24.203.139.78:8080/"; // Adresse Serveur M-A
00017 static char* WEBSOCKET_URL = "ws://192.168.0.20:3000/"; // Adresse Serveur M-A
00018 DigitalOut sw(p20);
00019 void keep_alive();
00020 Thread thread_alive(keep_alive);
00021 
00022 
00023 ID12RFID rfid(p10); // uart rx
00024 //EthernetInterface eth;
00025 Websocket ws(WEBSOCKET_URL);
00026 
00027 static void receive_cb(const RemoteXBeeZB& remote, bool broadcast, const uint8_t *const data, uint16_t len)
00028 {
00029     const uint64_t remote_addr64 = remote.get_addr64();
00030 
00031     for (int i = 0; i < len; i++)
00032         pc.printf("%02x ", data[i]);
00033     sw = (bool)data[0];
00034     uint32_t x ;                //Construire le ID avec les 3 char recu
00035     x = data[1];
00036     x= (x<<8)|data[2];
00037     x= (x<<8)|data[3];
00038 
00039     pc.printf("%d %02x %02x %02x", x, data[1], data[2], data[3]);
00040 
00041     char message[64];
00042     sprintf(message,"1,%d,%d",x,data[0]) ;
00043 
00044     if(ws.send(message)==-1) {
00045         ws.close();
00046         ws.connect();
00047         ws.send(message);
00048     } //envoie de message de chute
00049 
00050 
00051     pc.printf("\r\n");
00052 }
00053 
00054 
00055 void keep_alive()
00056 {
00057     Thread::signal_wait(0x1);       //signal keep alive pouvant être utilisé plus tard pour vérifier que les poste fixe sont fonctionnel
00058     ws.send("2");
00059 }
00060 void sendSignal()
00061 {
00062     thread_alive.signal_set(0x1);
00063 }
00064 
00065 int main()
00066 {
00067     
00068     // Setup du port ethernet
00069     EthernetInterface eth;
00070     eth.init(); //Use DHCP
00071     //eth.init(mbedIp, mbedMask, mbedGateway); //Use config
00072     eth.connect();
00073     //printf("IP Address is %s\n\r", eth.getIPAddress());
00074 
00075     XBeeZB xbee = XBeeZB(RADIO_TX, RADIO_RX, RADIO_RESET, NC, NC, 9600);
00076 
00077     /* Register callbacks */
00078     xbee.register_receive_cb(&receive_cb);
00079     Ticker ticker_num;
00080     RadioStatus const radioStatus = xbee.init();
00081     
00082     if(ws.connect()) {
00083         pc.printf("ok");
00084     } else {
00085         pc.printf("bad");
00086     }
00087     ticker_num.attach(&sendSignal, 15);  //tick a chaque 50ms pour la thread numerique
00088     myled = false;
00089 
00090     while (1) {
00091         xbee.process_rx_frames();
00092 
00093         if(rfid.readable()) {
00094             int i = rfid.read();
00095             printf("RFID Tag number : %d\n", i);
00096             char message[64];
00097             sprintf(message,"0,%d,1",i) ;
00098             
00099             if(ws.send(message)==-1) {
00100                 ws.close();
00101                 ws.connect();
00102                 ws.send(message);
00103             } //envoie de commande rfid
00104 
00105 
00106         }
00107         wait_ms(100);       
00108     }
00109 }