Coordinator code

Dependencies:   EthernetInterface WebSocketClient mbed-rtos mbed

Files at this revision

API Documentation at this revision

Comitter:
vinbel93
Date:
Thu Feb 18 13:21:58 2016 +0000
Commit message:
Coordinator

Changed in this revision

Coordinator.cpp Show annotated file Show diff for this revision Revisions of this file
Coordinator.h Show annotated file Show diff for this revision Revisions of this file
EthernetInterface.lib Show annotated file Show diff for this revision Revisions of this file
WebSocketClient.lib Show annotated file Show diff for this revision Revisions of this file
Xbee.cpp Show annotated file Show diff for this revision Revisions of this file
Xbee.h Show annotated file Show diff for this revision Revisions of this file
mbed-rtos.lib 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 4cb87eb1f914 Coordinator.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Coordinator.cpp	Thu Feb 18 13:21:58 2016 +0000
@@ -0,0 +1,104 @@
+#include "mbed.h"
+#include "Websocket.h"
+#include "Xbee.h"
+#include "EthernetInterface.h"
+#include <string>
+
+// Entrées-sorties
+Serial xbee(p13, p14);
+Serial pc(USBTX, USBRX);
+DigitalOut reset(p8);
+
+// FileSystem (pour lire fichier de config)
+LocalFileSystem local("local");
+
+// Fonction de lecture du fichier de config
+void readConfig(unsigned long long* panID, string* url)
+{    
+    FILE *file = fopen("/local/coord.cfg", "r");
+    char* token;
+    char* delimiter = "=";
+
+    if (file != NULL)
+    {
+        char line[128];
+        
+        // Lire ligne par ligne
+        while (fgets(line, sizeof(line), file) != NULL)
+        {
+            // token = nom du paramètre
+            token = strtok(line, delimiter);
+            
+            if (strcmp(token, "PAN_ID") == 0)
+            {
+                // token = valeur du paramètre
+                token = strtok(NULL, delimiter);
+                
+                // conversion en entier 64 bit
+                *panID = strtoull(token, NULL, 16);
+            }
+            else if(strcmp(token, "URL") == 0)
+            {
+                // token = valeur du paramètre
+                token = strtok(NULL, delimiter);
+                *url = token;
+            }
+        }
+        
+        fclose(file);
+    }
+}
+
+// Fonction principale
+int main()
+{
+    unsigned long long panID;
+    string url;
+
+    // Paramètres réseau statiques
+    EthernetInterface eth;
+    char* ip = "10.0.0.10";
+    char* mask = "255.255.255.0";
+    char* gateway = "10.0.0.1";
+    eth.init(ip, mask, gateway);
+    eth.connect();
+    printf("IP Address is %s\n", eth.getIPAddress());
+
+    // Reset du Xbee
+    reset = 0;
+    wait(0.4);
+    reset = 1;
+    wait(1);
+
+    // Vidage du buffer de lecture du Xbee
+    while (xbee.readable())
+    {
+        char dump = xbee.getc();
+    }
+
+    // Lire la config et assigner le PAN ID
+    readConfig(&panID, &url);    
+    setPanId(&xbee, panID);
+
+    // Convertir l'URL du serveur Websockets de std::string à un char[]
+    char* urlString = new char[url.size() + 1];
+    std::copy(url.begin(), url.end(), urlString);
+    urlString[url.size()] = '\0';
+
+    // Connexion au serveur Websockets
+    Websocket socket(urlString);
+    socket.connect();
+
+    while (true)
+    {
+        char bufferToSend[128];
+
+        // Lecture des données reçues par le Xbee, si true -> un message complet a été reçu
+        if (readPacket(&pc, &xbee, bufferToSend))
+        {
+            // Envoi au serveur
+            socket.send(bufferToSend);
+            pc.printf("Envoi de %s\r\n", bufferToSend);
+        }
+    }
+}
diff -r 000000000000 -r 4cb87eb1f914 Coordinator.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Coordinator.h	Thu Feb 18 13:21:58 2016 +0000
@@ -0,0 +1,3 @@
+#pragma once
+
+void readConfig(unsigned long long* panID, string* url);
diff -r 000000000000 -r 4cb87eb1f914 EthernetInterface.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/EthernetInterface.lib	Thu Feb 18 13:21:58 2016 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/EthernetInterface/#2fc406e2553f
diff -r 000000000000 -r 4cb87eb1f914 WebSocketClient.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/WebSocketClient.lib	Thu Feb 18 13:21:58 2016 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/samux/code/WebSocketClient/#4567996414a5
diff -r 000000000000 -r 4cb87eb1f914 Xbee.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Xbee.cpp	Thu Feb 18 13:21:58 2016 +0000
@@ -0,0 +1,105 @@
+#include "Xbee.h"
+
+static bool frameStarted = false;
+static char readBuffer[128];
+static int  buffer = 0;
+
+// Fonction d'assignation du PAN ID
+void setPanId(Serial* xbee, unsigned long long panId)
+{
+    // Construction de la trame
+    const int frameLength = 16;
+    char frame[frameLength];
+    frame[0] = 0x7E; // Start delimiter
+    frame[1] = 0x00; // Length (MSB)
+    frame[2] = 0x0C; // Length (LSB)
+    frame[3] = 0x08; // AT Command
+    frame[4] = 0x00; // Frame ID
+    frame[5] = 'I';
+    frame[6] = 'D';
+    frame[7] = (panId >> 56) & 0xFF;
+    frame[8] = (panId >> 48) & 0xFF;
+    frame[9] = (panId >> 40) & 0xFF;
+    frame[10] = (panId >> 32) & 0xFF;
+    frame[11] = (panId >> 24) & 0xFF;
+    frame[12] = (panId >> 16) & 0xFF;
+    frame[13] = (panId >> 8) & 0xFF;
+    frame[14] = (panId >> 0) & 0xFF;
+    frame[15] = checksum(frame, 3, 15);
+    
+    // Envoi sur le UART
+    if (xbee->writeable())
+    {
+        for (int i = 0; i < frameLength; i++)
+        {
+            xbee->putc(frame[i]);
+        }
+    }
+}
+
+// Fonction de calcul du checksum
+char checksum(char* frame, int begin, int end)
+{
+    char sum = 0;
+
+    // Addition des bits
+    for (int i = begin; i < end; i++)
+    {
+        sum += frame[i];
+    }
+    
+    return 0xFF - sum;
+}
+
+// Fonction de lecture des données du Xbee
+bool readPacket(Serial* pc, Serial* xbee, char* output)
+{
+    if(xbee->readable())
+    {
+        int c = xbee->getc();
+        
+        // Début d'une trame
+        if (c == START_BYTE)
+        {
+            frameStarted = true;
+        }
+
+        // Ajout du caractère dans le buffer
+        if (frameStarted)
+        {
+            readBuffer[buffer++] = c;
+        }
+        
+        // Si la longueur de la trame a été lue
+        if (buffer > 3)
+        {
+            int length = (readBuffer[1] << 8 ) | (readBuffer[2] & 0xFF);
+
+            // Vérifier que la trame au complet a été reçue et que le checksum est valide
+            if (buffer == length + 4 &&
+                checksum(readBuffer, 3, buffer) == readBuffer[buffer])
+            {
+                // Fin de la trame
+                frameStarted = false;
+                parse(readBuffer, buffer, output);
+                buffer = 0;
+                return true;
+            }
+        }
+    }
+    
+    return false;
+}
+
+void parse(char* buffer, int size, char* output)
+{
+    if (size >= 17)
+    {
+        // Données utiles
+        int button = buffer[15];
+        short accX = buffer[16] | buffer[17] << 8;
+
+        // Formattage pour le serveur Websockets
+        sprintf(output, "Button = %i, Acceleration X = %i\r\n", button, accX);
+    }
+}
diff -r 000000000000 -r 4cb87eb1f914 Xbee.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Xbee.h	Thu Feb 18 13:21:58 2016 +0000
@@ -0,0 +1,11 @@
+#include "mbed.h"
+#include <string>
+
+#pragma once
+
+#define START_BYTE 0x7E
+   
+bool readPacket(Serial* pc, Serial* xbee, char* output);
+void setPanId(Serial* xbee, unsigned long long panId);
+char checksum(char* frame, int begin, int end);
+void parse(char* buffer, int size, char* output);
diff -r 000000000000 -r 4cb87eb1f914 mbed-rtos.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-rtos.lib	Thu Feb 18 13:21:58 2016 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed-rtos/#3d9d2b8b8f17
diff -r 000000000000 -r 4cb87eb1f914 mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Thu Feb 18 13:21:58 2016 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/f141b2784e32
\ No newline at end of file