
code pour recevoir des trames et mettre les char sur le port usb
Dependencies: mbed
Fork of APP4 by
Revision 7:a6f58e5d1188, committed 2014-04-09
- Comitter:
- RufflesAllD
- Date:
- Wed Apr 09 19:16:59 2014 +0000
- Parent:
- 6:f1e9dc6b24c7
- Child:
- 8:ecc7c75a37ee
- Commit message:
- code pour recevoir des trames et mettre les char sur le port usb
Changed in this revision
--- a/WebSocketClient.lib Mon Apr 07 15:43:16 2014 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -http://mbed.org/teams/S5info_H14/code/WebSocketClient/#71d34230a98b
--- a/coordinateur.cpp Mon Apr 07 15:43:16 2014 +0000 +++ b/coordinateur.cpp Wed Apr 09 19:16:59 2014 +0000 @@ -1,5 +1,7 @@ #include "coordinateur.hpp" +Serial pc(USBTX, USBRX); + Coordinateur::Coordinateur(PinName _tx, PinName _rx) : trame(_tx, _rx), xbee(_tx, _rx), etat(Start) { @@ -18,24 +20,11 @@ void Coordinateur::setPanID(string _pan) { - pan = _pan; - trame.sendATCommand("ID", pan.c_str(), pan.length()); // Set le PANID selon le fichier de config + trame.sendATCommand("ID", _pan.c_str(), _pan.length()); // Set le PANID selon le fichier de config trame.sendATCommand("WR", 0, 0); // Écrit la valeur du PANID en mémoire trame.sendATCommand("AC", 0, 0); // Applique les changements effectués } -void Coordinateur::setURL(string _url) -{ - url = _url; - - eth = new EthernetInterface(); - eth->init(); - eth->connect(); - - ws = new Websocket(const_cast<char*>(url.c_str())); - ws->connect(); -} - void Coordinateur::getBytes() { if (xbee.readable()) @@ -64,7 +53,7 @@ case Length2: length[1] = c; length_int = (length[0] << 8) | length[1]; - if (length_int == 18 || length_int == 13) + if (length_int == 16) etat = Data; else etat = Start; @@ -84,50 +73,17 @@ else { //trame.checkTrame(data, c, length_int); - sendDataToWeb(data, length_int); + pc.putc(0xFF); + wait(0.1); + pc.putc(data[12]); + wait(0.1); + pc.putc(data[13]); + wait(0.1); + pc.putc(data[14]); + wait(0.1); + pc.putc(data[15]); etat = Start; } break; } } - -void Coordinateur::sendDataToWeb(char *data, int length) -{ - if (length == 13) - { - if (data[length - 1] == 0x0) - { - ws->send("Le circuit du capteur a contact sec est ouvert."); - } - else - { - ws->send("Le circuit du capteur a contact sec est ferme."); - } - } - else - { - int results[3] = {0}; - - // Reconstruit la valeur sur 12 bits - for (int i = 0; i < 3; i++) - { - results[i] = (data[i*2 + 12] << 4) | (data[2*i + 13] >> 4); - } - - string temp = "Valeur de l'accelerometre: X:"; - temp += intToString(results[0]); - temp += " Y:"; - temp += intToString(results[1]); - temp += " Z:"; - temp += intToString(results[2]); - - ws->send(const_cast<char*>(temp.c_str())); - } -} - -string Coordinateur::intToString(int value) -{ - stringstream ss; - ss << value; - return ss.str(); -}
--- a/coordinateur.hpp Mon Apr 07 15:43:16 2014 +0000 +++ b/coordinateur.hpp Wed Apr 09 19:16:59 2014 +0000 @@ -2,10 +2,7 @@ #define COORDINATEUR_HPP #include <string> -#include <sstream> #include "trame.hpp" -#include "EthernetInterface.h" -#include "Websocket.h" using namespace std; @@ -27,30 +24,20 @@ // Set le réseau du coordinateur void setPanID(string _pan); - // Initialise la connection ethernet et le websocket - void setURL(string _url); // Attend de recevoir les trames void getBytes(); private: // Machine à état qui reçoit les trames void etatTrame(char c); - // Envoie le data reçu à la page web - void sendDataToWeb(char *data, int length); - string intToString(int value); Trame trame; - string pan; - string url; Serial xbee; Etat etat; char length[2]; int length_int; int counter; char *data; - - EthernetInterface *eth; - Websocket *ws; }; #endif
--- a/main.cpp Mon Apr 07 15:43:16 2014 +0000 +++ b/main.cpp Wed Apr 09 19:16:59 2014 +0000 @@ -1,25 +1,15 @@ -#include "readfile.hpp" - -LocalFileSystem local("local"); +#include "coordinateur.hpp" int main() { Coordinateur coord(p13, p14); - Routeur rout(p13, p14); - ReadFile readfile; - bool coordBool = false; - coordBool = readfile.setConfigCoord(&coord, "/local/cgfcoord.txt"); // Si retourne vrai, c'est le coordinateur - if (!coordBool) - readfile.setConfigRouteur(&rout, "/local/cgfrout.txt"); // Sinon c'est un routeur + //coord.setPanID("ACE"); wait(2); while(true) { - if (coordBool) - coord.getBytes(); // Attend pour recevoir les trames - else - rout.getValues(); // Prend les valeurs des capteurs + coord.getBytes(); // Attend pour recevoir les trames } }
--- a/readfile.cpp Mon Apr 07 15:43:16 2014 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,80 +0,0 @@ -#include "readfile.hpp" - -ReadFile::ReadFile() -{} - -bool ReadFile::setConfigCoord(Coordinateur *coord, const string filename) -{ - string line; - ifstream myfile(filename.c_str()); - if (myfile.is_open()) - { - while (getline(myfile,line)) - { - if (line[0] != '#') - { - // Trouve le PANID dans le fichier de config - if (line.find("PANID") != string::npos) - { - coord->setPanID(line.substr(6)); - } - // Trouve le URL dans le fichier de config - if (line.find("URL") != string::npos) - { - coord->setURL(line.substr(4)); - } - } - } - - myfile.close(); - - return true; - } - - return false; -} - -bool ReadFile::setConfigRouteur(Routeur *rout, const string filename) -{ - string line; - ifstream myfile(filename.c_str()); - if (myfile.is_open()) - { - while (getline(myfile,line)) - { - if (line[0] != '#') - { - // Trouve le PANID dans le fichier de config - if (line.find("PANID") != string::npos) - { - rout->setPanID(line.substr(6)); - } - // Trouve le Type de capteur dans le fichier de config - if (line.find("Type") != string::npos) - { - rout->setType(string2int(line.substr(5))); - } - // Trouve la période entre chaque lecture de capteur dans le fichier de config - if (line.find("Period") != string::npos) - { - rout->setPeriod(string2int(line.substr(7))); - } - } - } - - myfile.close(); - - return true; - } - - return false; -} - -int ReadFile::string2int(string t) -{ - int value = 0; - istringstream ss(t); - ss >> value; - - return value; -} \ No newline at end of file
--- a/readfile.hpp Mon Apr 07 15:43:16 2014 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,34 +0,0 @@ -#ifndef READFILE_HPP -#define READFILE_HPP - -#include <iostream> -#include <fstream> - -#include "coordinateur.hpp" -#include "routeur.hpp" - -using namespace std; - -/* Description de la classe ReadFile -* -* La classe ReadFile lit les fichiers de config et configure le routeur/coordinateur -* -*/ -class ReadFile -{ -public: - - // Constructeur - ReadFile(); - - // Set information of the coordinateur - bool setConfigCoord(Coordinateur *coord, const string filename); - - // Set information of the routeur - bool setConfigRouteur(Routeur *rout, const string filename); - -private: - int string2int(string t); -}; - -#endif \ No newline at end of file
--- a/routeur.cpp Mon Apr 07 15:43:16 2014 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,122 +0,0 @@ -#include "routeur.hpp" - -#define CTRL_REG1 0x2A -#define DATA_REG_W 0x3A -#define DATA_REG_R 0x3B - -#define OUT_X_MSB 0x01 - -const char COORD_ADRESS[8] = {0x00}; // Adresse 64bits du coordinateur (si tous des 0, le routeur envoie à son coordinateur par défaut) - -Routeur::Routeur(PinName _tx, PinName _rx) : - trame(_tx, _rx), - accel(p9, p10), // Pin I2C (pour l'accéléromètre) - piton(p30) // Pin DigitalIn (pour le contact sec) -{ - // reset le xbee - DigitalOut reset(p8); - - reset = 0; - wait(0.4); - reset = 1; -} - -void Routeur::setPanID(string _pan) -{ - pan = _pan; - trame.sendATCommand("ID", pan.c_str(), pan.length());// Set le PANID selon le fichier de config - trame.sendATCommand("WR", 0, 0); // Écrit la valeur du PANID en mémoire - trame.sendATCommand("AC", 0, 0); // Applique les changements effectués -} - -void Routeur::setType(int t) -{ - type = static_cast<Type>(t); // 0: accéléromètre 1: switch - if (type == Accelerometer) - initAccel(); -} - -void Routeur::setPeriod(int t) -{ - period = t; -} - -void Routeur::getValues() -{ - if (type == Accelerometer) - sendAccelValues(); - else - sendSwitchValue(); - - wait(period); -} - -void Routeur::initAccel() -{ - // Mise de l'accéléromètre en standby - writeRegister(CTRL_REG1, 0x10); - - // Set offset de l'accéléromètre - writeRegister(0x31, 0xEE); - - // activate de l'accéléromètre - char c; - readRegister(CTRL_REG1, &c); - writeRegister(CTRL_REG1, c | 0x01); -} - -void Routeur::sendSwitchValue() -{ - switchValue[0] = piton; - - trame.sendTransmitRequest(COORD_ADRESS, switchValue, 1); -} - -void Routeur::sendAccelValues() -{ - int ack = readRegisters(OUT_X_MSB, 6, accelValues); - - trame.sendTransmitRequest(COORD_ADRESS, accelValues, 6); -} - -int Routeur::readRegister(char reg, char* data) -{ - int ack = 0; - accel.start(); - ack = accel.write(DATA_REG_W); - ack = accel.write(reg); - accel.start(); - ack = accel.write(DATA_REG_R); - data[0] = accel.read(0); - accel.stop(); - - return ack; -} - -int Routeur::readRegisters(char reg, int range, char* data) -{ - int ack = 0; - accel.start(); - ack = accel.write(DATA_REG_W); - ack = accel.write(reg); - accel.start(); - ack = accel.write(DATA_REG_R); - for (int i = 0; i < range - 1; i++) - data[i] = accel.read(1); - data[range - 1] = accel.read(0); - accel.stop(); - - return ack; -} - -int Routeur::writeRegister(char reg, char data) -{ - int ack = 0; - accel.start(); - ack = accel.write(DATA_REG_W); - ack = accel.write(reg); - ack = accel.write(data); - accel.stop(); - - return ack; -}
--- a/routeur.hpp Mon Apr 07 15:43:16 2014 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,59 +0,0 @@ -#ifndef ROUTEUR_HPP -#define ROUTEUR_HPP - -#include <string> -#include "trame.hpp" - -using namespace std; - -/* Description de la classe Routeur -* -* La classe Routeur configure le routeur, reçoit les valeurs des capteurs et les envoie sur le réseau zigBee -* -*/ -class Routeur -{ -public: - // Enum pour le type de capteur utilisé par le routeur (selon le fichier de config) - enum Type { Accelerometer, Switch }; - // Constructeur - Routeur(PinName _tx, PinName _rx); - - // Prend les données du capteur à chaque période définie par le fichier de config - void getValues(); - - // Initialise acceleromètre - void initAccel(); - - // Envoie les valeurs de acceleromètre - void sendAccelValues(); - - // Envoie la valeur d'un capteur sec - void sendSwitchValue(); - - // Set le réseau du routeur - void setPanID(string _pan); - // Définie quel capteur le routeur va utiliser - void setType(int t); - // Définie la période entre chaque prise de données - void setPeriod(int t); - -private: - int readRegister(char reg, char* data); // - int readRegisters(char reg, int range, char* dest); // Fonctions nécessaire à l'initialisation et la lecture des valeurs de l'accéléromètre - int writeRegister(char reg, char data); // - - Trame trame; - - string pan; - int period; - Type type; - - I2C accel; - DigitalIn piton; - - char switchValue[1]; - char accelValues[6]; -}; - -#endif
--- a/trame.cpp Mon Apr 07 15:43:16 2014 +0000 +++ b/trame.cpp Wed Apr 09 19:16:59 2014 +0000 @@ -30,36 +30,6 @@ delete trm; } -void Trame::sendTransmitRequest(const char* destination, const char* data, int length) -{ - int tmp = length + 14; // length (grandeur du data à envoyer) 14 (API(1), FrameID(1), MAC(8), 16bit(2), Radius(1), Options(1)) - char* trm = new char[tmp + 4]; // 4 (Delimiter(1), Length(2), Checksum(1)) - - trm[0] = 0x7E; // Delimiter - trm[1] = static_cast<char>((tmp >> 8) & 0xFF); // MSB de length - trm[2] = static_cast<char>(tmp & 0xFF); // LSB length - trm[3] = 0x10; // Type - trm[4] = 0x01; // API - for (int i = 0; i < 8; i++) - trm[5 + i] = destination[i]; //Destination - trm[13] = 0xFF; // 16 bits address - trm[14] = 0xFE; // 16 bits address - trm[15] = 0x00; // Radius - trm[16] = 0x00; // Options - - for (int i = 0; i < length; i++) - trm[17 + i] = data[i]; // Data - - trm[17 + length] = crc8(trm, tmp + 3); - - Serial xbee(tx, rx); - for (int i = 0; i < tmp + 4; i++) - xbee.putc(trm[i]); - - wait(0.01); - delete trm; -} - unsigned char Trame::crc8(const char* data, int length) { unsigned char crc = 0;
--- a/trame.hpp Mon Apr 07 15:43:16 2014 +0000 +++ b/trame.hpp Wed Apr 09 19:16:59 2014 +0000 @@ -19,9 +19,6 @@ // Envoie une trame pour une commande AT void sendATCommand(const char* command, const char* data, int length); - // Envoie une trame pour une requete transmition - void sendTransmitRequest(const char* destination, const char* data, int length); - // Effectue le checksum d'un AT command unsigned char crc8(const char* data, int length);