![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Coordinator code
Dependencies: EthernetInterface WebSocketClient mbed-rtos mbed
Coordinator.cpp@0:4cb87eb1f914, 2016-02-18 (annotated)
- Committer:
- vinbel93
- Date:
- Thu Feb 18 13:21:58 2016 +0000
- Revision:
- 0:4cb87eb1f914
Coordinator
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
vinbel93 | 0:4cb87eb1f914 | 1 | #include "mbed.h" |
vinbel93 | 0:4cb87eb1f914 | 2 | #include "Websocket.h" |
vinbel93 | 0:4cb87eb1f914 | 3 | #include "Xbee.h" |
vinbel93 | 0:4cb87eb1f914 | 4 | #include "EthernetInterface.h" |
vinbel93 | 0:4cb87eb1f914 | 5 | #include <string> |
vinbel93 | 0:4cb87eb1f914 | 6 | |
vinbel93 | 0:4cb87eb1f914 | 7 | // Entrées-sorties |
vinbel93 | 0:4cb87eb1f914 | 8 | Serial xbee(p13, p14); |
vinbel93 | 0:4cb87eb1f914 | 9 | Serial pc(USBTX, USBRX); |
vinbel93 | 0:4cb87eb1f914 | 10 | DigitalOut reset(p8); |
vinbel93 | 0:4cb87eb1f914 | 11 | |
vinbel93 | 0:4cb87eb1f914 | 12 | // FileSystem (pour lire fichier de config) |
vinbel93 | 0:4cb87eb1f914 | 13 | LocalFileSystem local("local"); |
vinbel93 | 0:4cb87eb1f914 | 14 | |
vinbel93 | 0:4cb87eb1f914 | 15 | // Fonction de lecture du fichier de config |
vinbel93 | 0:4cb87eb1f914 | 16 | void readConfig(unsigned long long* panID, string* url) |
vinbel93 | 0:4cb87eb1f914 | 17 | { |
vinbel93 | 0:4cb87eb1f914 | 18 | FILE *file = fopen("/local/coord.cfg", "r"); |
vinbel93 | 0:4cb87eb1f914 | 19 | char* token; |
vinbel93 | 0:4cb87eb1f914 | 20 | char* delimiter = "="; |
vinbel93 | 0:4cb87eb1f914 | 21 | |
vinbel93 | 0:4cb87eb1f914 | 22 | if (file != NULL) |
vinbel93 | 0:4cb87eb1f914 | 23 | { |
vinbel93 | 0:4cb87eb1f914 | 24 | char line[128]; |
vinbel93 | 0:4cb87eb1f914 | 25 | |
vinbel93 | 0:4cb87eb1f914 | 26 | // Lire ligne par ligne |
vinbel93 | 0:4cb87eb1f914 | 27 | while (fgets(line, sizeof(line), file) != NULL) |
vinbel93 | 0:4cb87eb1f914 | 28 | { |
vinbel93 | 0:4cb87eb1f914 | 29 | // token = nom du paramètre |
vinbel93 | 0:4cb87eb1f914 | 30 | token = strtok(line, delimiter); |
vinbel93 | 0:4cb87eb1f914 | 31 | |
vinbel93 | 0:4cb87eb1f914 | 32 | if (strcmp(token, "PAN_ID") == 0) |
vinbel93 | 0:4cb87eb1f914 | 33 | { |
vinbel93 | 0:4cb87eb1f914 | 34 | // token = valeur du paramètre |
vinbel93 | 0:4cb87eb1f914 | 35 | token = strtok(NULL, delimiter); |
vinbel93 | 0:4cb87eb1f914 | 36 | |
vinbel93 | 0:4cb87eb1f914 | 37 | // conversion en entier 64 bit |
vinbel93 | 0:4cb87eb1f914 | 38 | *panID = strtoull(token, NULL, 16); |
vinbel93 | 0:4cb87eb1f914 | 39 | } |
vinbel93 | 0:4cb87eb1f914 | 40 | else if(strcmp(token, "URL") == 0) |
vinbel93 | 0:4cb87eb1f914 | 41 | { |
vinbel93 | 0:4cb87eb1f914 | 42 | // token = valeur du paramètre |
vinbel93 | 0:4cb87eb1f914 | 43 | token = strtok(NULL, delimiter); |
vinbel93 | 0:4cb87eb1f914 | 44 | *url = token; |
vinbel93 | 0:4cb87eb1f914 | 45 | } |
vinbel93 | 0:4cb87eb1f914 | 46 | } |
vinbel93 | 0:4cb87eb1f914 | 47 | |
vinbel93 | 0:4cb87eb1f914 | 48 | fclose(file); |
vinbel93 | 0:4cb87eb1f914 | 49 | } |
vinbel93 | 0:4cb87eb1f914 | 50 | } |
vinbel93 | 0:4cb87eb1f914 | 51 | |
vinbel93 | 0:4cb87eb1f914 | 52 | // Fonction principale |
vinbel93 | 0:4cb87eb1f914 | 53 | int main() |
vinbel93 | 0:4cb87eb1f914 | 54 | { |
vinbel93 | 0:4cb87eb1f914 | 55 | unsigned long long panID; |
vinbel93 | 0:4cb87eb1f914 | 56 | string url; |
vinbel93 | 0:4cb87eb1f914 | 57 | |
vinbel93 | 0:4cb87eb1f914 | 58 | // Paramètres réseau statiques |
vinbel93 | 0:4cb87eb1f914 | 59 | EthernetInterface eth; |
vinbel93 | 0:4cb87eb1f914 | 60 | char* ip = "10.0.0.10"; |
vinbel93 | 0:4cb87eb1f914 | 61 | char* mask = "255.255.255.0"; |
vinbel93 | 0:4cb87eb1f914 | 62 | char* gateway = "10.0.0.1"; |
vinbel93 | 0:4cb87eb1f914 | 63 | eth.init(ip, mask, gateway); |
vinbel93 | 0:4cb87eb1f914 | 64 | eth.connect(); |
vinbel93 | 0:4cb87eb1f914 | 65 | printf("IP Address is %s\n", eth.getIPAddress()); |
vinbel93 | 0:4cb87eb1f914 | 66 | |
vinbel93 | 0:4cb87eb1f914 | 67 | // Reset du Xbee |
vinbel93 | 0:4cb87eb1f914 | 68 | reset = 0; |
vinbel93 | 0:4cb87eb1f914 | 69 | wait(0.4); |
vinbel93 | 0:4cb87eb1f914 | 70 | reset = 1; |
vinbel93 | 0:4cb87eb1f914 | 71 | wait(1); |
vinbel93 | 0:4cb87eb1f914 | 72 | |
vinbel93 | 0:4cb87eb1f914 | 73 | // Vidage du buffer de lecture du Xbee |
vinbel93 | 0:4cb87eb1f914 | 74 | while (xbee.readable()) |
vinbel93 | 0:4cb87eb1f914 | 75 | { |
vinbel93 | 0:4cb87eb1f914 | 76 | char dump = xbee.getc(); |
vinbel93 | 0:4cb87eb1f914 | 77 | } |
vinbel93 | 0:4cb87eb1f914 | 78 | |
vinbel93 | 0:4cb87eb1f914 | 79 | // Lire la config et assigner le PAN ID |
vinbel93 | 0:4cb87eb1f914 | 80 | readConfig(&panID, &url); |
vinbel93 | 0:4cb87eb1f914 | 81 | setPanId(&xbee, panID); |
vinbel93 | 0:4cb87eb1f914 | 82 | |
vinbel93 | 0:4cb87eb1f914 | 83 | // Convertir l'URL du serveur Websockets de std::string à un char[] |
vinbel93 | 0:4cb87eb1f914 | 84 | char* urlString = new char[url.size() + 1]; |
vinbel93 | 0:4cb87eb1f914 | 85 | std::copy(url.begin(), url.end(), urlString); |
vinbel93 | 0:4cb87eb1f914 | 86 | urlString[url.size()] = '\0'; |
vinbel93 | 0:4cb87eb1f914 | 87 | |
vinbel93 | 0:4cb87eb1f914 | 88 | // Connexion au serveur Websockets |
vinbel93 | 0:4cb87eb1f914 | 89 | Websocket socket(urlString); |
vinbel93 | 0:4cb87eb1f914 | 90 | socket.connect(); |
vinbel93 | 0:4cb87eb1f914 | 91 | |
vinbel93 | 0:4cb87eb1f914 | 92 | while (true) |
vinbel93 | 0:4cb87eb1f914 | 93 | { |
vinbel93 | 0:4cb87eb1f914 | 94 | char bufferToSend[128]; |
vinbel93 | 0:4cb87eb1f914 | 95 | |
vinbel93 | 0:4cb87eb1f914 | 96 | // Lecture des données reçues par le Xbee, si true -> un message complet a été reçu |
vinbel93 | 0:4cb87eb1f914 | 97 | if (readPacket(&pc, &xbee, bufferToSend)) |
vinbel93 | 0:4cb87eb1f914 | 98 | { |
vinbel93 | 0:4cb87eb1f914 | 99 | // Envoi au serveur |
vinbel93 | 0:4cb87eb1f914 | 100 | socket.send(bufferToSend); |
vinbel93 | 0:4cb87eb1f914 | 101 | pc.printf("Envoi de %s\r\n", bufferToSend); |
vinbel93 | 0:4cb87eb1f914 | 102 | } |
vinbel93 | 0:4cb87eb1f914 | 103 | } |
vinbel93 | 0:4cb87eb1f914 | 104 | } |