From ab869558586ae541667c7946fe308b9423b9a4be Mon Sep 17 00:00:00 2001 From: Amaury Date: Sun, 21 May 2017 23:38:24 +0200 Subject: [PATCH] La com série entre le programme et le capteur BME280, le code de com se trouve dans le fichier BME280test.ino. Il reste encore à faire communiquer indirectement l'arduino avec le site web. Ce travail est en cours --- appWeb/index.html | 10 +++++----- appWeb/js/script.js | 41 +++++++++++++++++++++++++++++++++++++++++ appWeb/testWebsockets.html | 32 ++++++++++++++++++++++++++++++++ code arduino/bme280test.ino | 83 ----------------------------------------------------------------------------------- prog/main.c | 192 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ prog/serial.c | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ prog/serial.exe | Bin 0 -> 9264 bytes prog/serial.h | 18 ++++++++++++++++++ prog/websockets.c | 102 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ prog/websocketsEx.exe | Bin 0 -> 9472 bytes 10 files changed, 447 insertions(+), 88 deletions(-) create mode 100644 appWeb/js/script.js create mode 100644 appWeb/testWebsockets.html delete mode 100644 code arduino/bme280test.ino create mode 100644 prog/main.c create mode 100644 prog/serial.c create mode 100644 prog/serial.exe create mode 100644 prog/serial.h create mode 100644 prog/websockets.c create mode 100644 prog/websocketsEx.exe diff --git a/appWeb/index.html b/appWeb/index.html index bd1b44b..bdf7c0f 100644 --- a/appWeb/index.html +++ b/appWeb/index.html @@ -3,13 +3,13 @@ Page Title - +

Station météo en ligne

-

Gracias-Havard-Kiang-Knockaert

- +

Gracias-Havard-Qiang-Knockaert

+

Connection On/Off

Météo

@@ -18,6 +18,7 @@
+ @@ -34,7 +35,6 @@
Température
- + - diff --git a/appWeb/js/script.js b/appWeb/js/script.js new file mode 100644 index 0000000..9e33d96 --- /dev/null +++ b/appWeb/js/script.js @@ -0,0 +1,41 @@ +window.WebSocket=(window.WebSocket||window.MozWebSocket); +var websocket=new WebSocket('ws://192.168.1.111:9000','myprotocol'); + +websocket.onopen=function(){ $('h3 .connecOn').css('color','green'); }; + +websocket.onerror=function(){ $('h3 .connecOff').css('color','red'); }; + //on crée des tableaux de Buffer, ces tableaux permettent de "découper" la chaine de bits, en différents octets +var bufferReponse = new ArrayBuffer(3); +var bufferRequete = new ArrayBuffer(3); + +//DataView permet de traiter un ArrayBuffer +var requeteView = new DataView(bufferRequete); +var reponseView = new DataView(bufferReponse); + +//on remplie le buffer de requete via requeteView +requeteView.setInt8(0,'G'.charCodeAt(0)); +requeteView.setInt8(1,'E'.charCodeAt(0)); +requeteView.setInt8(2,'T'.charCodeAt(0)); + +//affiche le contenu du tableau dans une alert +alert(String.fromCharCode(requeteView.getInt8(0))+String.fromCharCode(requeteView.getInt8(1))+String.fromCharCode(requeteView.getInt8(2))); + + +websocket.onmessage=function(message){ //Evenement surveillant la reception de message + + console.log(message.data); + /*$('#relevesMeteo .temperature').text(message.data); + $('#relevesMeteo .humidite').text(message.data); + $('#relevesMeteo .pression').text(message.data); + $('#relevesMeteo .luminosite').text(message.data);*/ + +}; + +function requeteEnvoi(){ + websocket.send(bufferReponse); + console.log(bufferReponse); +} + + + +//setInterval(requeteEnvoi,1000); diff --git a/appWeb/testWebsockets.html b/appWeb/testWebsockets.html new file mode 100644 index 0000000..b0bdb2e --- /dev/null +++ b/appWeb/testWebsockets.html @@ -0,0 +1,32 @@ + + + + + + + + +

WebSockets test

+ + +
+ + diff --git a/code arduino/bme280test.ino b/code arduino/bme280test.ino deleted file mode 100644 index 8c13ff7..0000000 --- a/code arduino/bme280test.ino +++ /dev/null @@ -1,83 +0,0 @@ -/*************************************************************************** - This is a library for the BME280 humidity, temperature & pressure sensor - - Designed specifically to work with the Adafruit BME280 Breakout - ----> http://www.adafruit.com/products/2650 - - These sensors use I2C or SPI to communicate, 2 or 4 pins are required - to interface. The device's I2C address is either 0x76 or 0x77. - - Adafruit invests time and resources providing this open source code, - please support Adafruit andopen-source hardware by purchasing products - from Adafruit! - - Written by Limor Fried & Kevin Townsend for Adafruit Industries. - BSD license, all text above must be included in any redistribution - ***************************************************************************/ - -#include -#include -#include -#include - -#define BME_SCK 13 -#define BME_MISO 12 -#define BME_MOSI 11 -#define BME_CS 10 - -#define SEALEVELPRESSURE_HPA (1013.25) - -//Adafruit_BME280 bme; // I2C -//Adafruit_BME280 bme(BME_CS); // hardware SPI -Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK); // software SPI - -unsigned long delayTime; - -void setup() { - Serial.begin(9600); - Serial.println(F("BME280 test")); - - bool status; - - // default settings - status = bme.begin(); - if (!status) { - Serial.println("Could not find a valid BME280 sensor, check wiring!"); - while (1); - } - - Serial.println("-- Default Test --"); - delayTime = 1000; - - Serial.println(); - - delay(100); // let sensor boot up -} - - -void loop() { - printValues(); - delay(delayTime); -} - - -void printValues() { - Serial.print("Temperature = "); - Serial.print(bme.readTemperature()); - Serial.println(" *C"); - - Serial.print("Pressure = "); - - Serial.print(bme.readPressure() / 100.0F); - Serial.println(" hPa"); - - Serial.print("Approx. Altitude = "); - Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA)); - Serial.println(" m"); - - Serial.print("Humidity = "); - Serial.print(bme.readHumidity()); - Serial.println(" %"); - - Serial.println(); -} diff --git a/prog/main.c b/prog/main.c new file mode 100644 index 0000000..535b92d --- /dev/null +++ b/prog/main.c @@ -0,0 +1,192 @@ +/* + * Test on serial device + */ + +//// +// Include files +//// +#include +#include +#include +#include +#include +#include +#include "serial.h" + + +#define MAX_FRAME_SIZE 1024 +#define WAIT_DELAY 50 + + +#define SERIAL_DEVICE "/dev/ttyACM0" + +/*****************************************************************************/ +/**********************PARTIE WEBSOCKETS + COM SERIE**************************/ +/*****************************************************************************/ + +//Fonction qui retourne un tableau de char, chaque char correspondant à une donnée d'un capteur + +char * getDataFromSensors(char *request,int sizeOfRequest) +{ + int sd=serialOpen(SERIAL_DEVICE,SERIAL_BOTH); + serialConfig(sd,B9600); + + int i; + char *reponse = NULL; + + //On envoie le GET au FPGA pour récupérer les données + for(i = 0; i < sizeOfRequest;i++) + { + if(write(sd,(void*)&request[i],sizeof(char))!=1) + { + perror("main.write"); exit(-1); + } + } + + printf("\nReponse : "); + + reponse = malloc(3*sizeof(char)); + + for(i=0;i < 3;i++) + { + if(read(sd,(void*)&reponse[i],sizeof(char))!=1) + { + perror("main.read"); exit(-1); + } + } + printf("\nTemperature : %d",reponse[1]); + printf("\nPressure : %d",reponse[0]); + printf("\nHumidity : %d\n",reponse[2]); + + serialClose(sd); + + return reponse; +} + + + +static int callback_http(struct libwebsocket_context *this, struct libwebsocket *wsi, enum libwebsocket_callback_reasons reason, void *user,void *in,size_t len) +{ + return 0; +} + +static int callback_my(struct libwebsocket_context * this, struct libwebsocket *wsi, enum libwebsocket_callback_reasons reason, void *user,void *in,size_t len) +{ + static char *requete = NULL; + static char *reponse = NULL; + static int msize=0; + + switch(reason) + { + case LWS_CALLBACK_ESTABLISHED: + printf("connection established\n"); + requete=NULL; + // Declenchement d'un prochain envoi au navigateur + libwebsocket_callback_on_writable(this,wsi); + break; + + case LWS_CALLBACK_RECEIVE: + // Ici sont traites les messages envoyes par le navigateur + printf("received data: %s\n",(char *)in); + requete=malloc(len+LWS_SEND_BUFFER_PRE_PADDING+LWS_SEND_BUFFER_POST_PADDING); + + if(requete==NULL) + { + perror("callback_my.malloc"); exit(EXIT_FAILURE); + } + + memcpy(requete+LWS_SEND_BUFFER_PRE_PADDING,in,len); + // Declenchement d'un prochain envoi au navigateur + msize=len; + libwebsocket_callback_on_writable(this,wsi); + break; + + case LWS_CALLBACK_SERVER_WRITEABLE: + // Ici sont envoyes les messages au navigateur + //Si la requete a pour valeur GET, on envoie les donnée au site + if(requete!= NULL) + { + if(requete[0] == 'G' && requete[1] == 'E' && requete[2] == 'T') + { + char *reponse = NULL; + reponse = getDataFromSensors(requete,3); + int length = 3; + unsigned char *buf = malloc(LWS_SEND_BUFFER_PRE_PADDING + length + LWS_SEND_BUFFER_POST_PADDING); + memcpy (buf + LWS_SEND_BUFFER_PRE_PADDING, reponse, length ); + libwebsocket_write(wsi, buf + LWS_SEND_BUFFER_PRE_PADDING, length, LWS_WRITE_TEXT); + free(reponse); + + free(requete); + requete = NULL; + reponse = NULL; + } + } + + break; + default: + break; + } + return 0; +} + +static struct libwebsocket_protocols protocols[] = { + { + "http-only", // name + callback_http, // callback + 0, // data size + 0 // maximum frame size + }, + {"myprotocol",callback_my,0,MAX_FRAME_SIZE}, + {NULL,NULL,0,0} +}; + + + + +/*****************************************************************/ +/**********************MAIN***************************************/ +/*****************************************************************/ + +int main(void){ + + + char j; + int i; + unsigned long count = 0; + + //Initialisation WebSockets + int port=9000; + struct lws_context_creation_info info; + memset(&info,0,sizeof info); + info.port=port; + info.protocols=protocols; + info.gid=-1; + info.uid=-1; + struct libwebsocket_context *context=libwebsocket_create_context(&info); + + //Initialisation com Serie + + + if(context==NULL) + { + fprintf(stderr, "libwebsocket init failed\n"); + return -1; + } + printf("starting server...\n"); + + + //boucle infinie du main + while(1) + { + libwebsocket_service(context,WAIT_DELAY); + } + libwebsocket_context_destroy(context); + return 0; + +} + + +//********************************************************************************// + + + diff --git a/prog/serial.c b/prog/serial.c new file mode 100644 index 0000000..1a40653 --- /dev/null +++ b/prog/serial.c @@ -0,0 +1,57 @@ +/* + * Serial library + */ + +//// +// Include files +//// +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "serial.h" + +//// +// Functions +//// + +// +// Open serial port device +// +int serialOpen(char *device,int mode){ +int flags=(mode==SERIAL_READ?O_RDONLY:(mode==SERIAL_WRITE?O_WRONLY:O_RDWR)); +int fd=open(device,flags|O_NOCTTY); +if(fd<0){ perror(device); exit(-1); } +return fd; +} + +// +// Serial port configuration +// +void serialConfig(int fd,int speed){ +struct termios new; +bzero(&new,sizeof(new)); +new.c_cflag=CLOCAL|CREAD|speed|CS8; +new.c_iflag=0; +new.c_oflag=0; +new.c_lflag=0; /* set input mode (non-canonical, no echo,...) */ +new.c_cc[VTIME]=0; /* inter-character timer unused */ +new.c_cc[VMIN]=1; /* blocking read until 1 char received */ +if(tcsetattr(fd,TCSANOW,&new)<0){ perror("serialInit.tcsetattr"); exit(-1); } +} + +// +// Serial port termination +// +void serialClose(int fd){ +close(fd); +} + + diff --git a/prog/serial.exe b/prog/serial.exe new file mode 100644 index 0000000..57ad052 Binary files /dev/null and b/prog/serial.exe differ diff --git a/prog/serial.h b/prog/serial.h new file mode 100644 index 0000000..e7eac5b --- /dev/null +++ b/prog/serial.h @@ -0,0 +1,18 @@ +/* + * Public definitions for serial library + */ + +//// +// Constants +//// + +#define SERIAL_READ 0 +#define SERIAL_WRITE 1 +#define SERIAL_BOTH 2 + +//// +// Public prototypes +//// +int serialOpen(char *device,int mode); +void serialConfig(int fd,int speed); +void serialClose(int fd); diff --git a/prog/websockets.c b/prog/websockets.c new file mode 100644 index 0000000..bd9d387 --- /dev/null +++ b/prog/websockets.c @@ -0,0 +1,102 @@ +#include +#include +#include +#include + +#define MAX_FRAME_SIZE 1024 +#define WAIT_DELAY 50 + + +static int callback_http(struct libwebsocket_context *this, struct libwebsocket *wsi, enum libwebsocket_callback_reasons reason, void *user,void *in,size_t len) +{ + return 0; +} + +static int callback_my(struct libwebsocket_context * this, struct libwebsocket *wsi, enum libwebsocket_callback_reasons reason, void *user,void *in,size_t len) +{ + static char *message = NULL; + static char *reponse = NULL; + static int msize=0; + switch(reason) + { + case LWS_CALLBACK_ESTABLISHED: + printf("connection established\n"); + message=NULL; + // Declenchement d'un prochain envoi au navigateur + libwebsocket_callback_on_writable(this,wsi); + break; + + case LWS_CALLBACK_RECEIVE: + // Ici sont traites les messages envoyes par le navigateur + printf("received data: %s\n",(char *)in); + message=malloc(len+LWS_SEND_BUFFER_PRE_PADDING+LWS_SEND_BUFFER_POST_PADDING); + + if(message==NULL) + { + perror("callback_my.malloc"); exit(EXIT_FAILURE); + } + + memcpy(message+LWS_SEND_BUFFER_PRE_PADDING,in,len); + // Declenchement d'un prochain envoi au navigateur + msize=len; + libwebsocket_callback_on_writable(this,wsi); + break; + + case LWS_CALLBACK_SERVER_WRITEABLE: + // Ici sont envoyes les messages au navigateur + if(message!= NULL) + { + char *text = "number : "; + int length = strlen (text); + unsigned char *buf = malloc(LWS_SEND_BUFFER_PRE_PADDING + length + LWS_SEND_BUFFER_POST_PADDING); + memcpy (buf + LWS_SEND_BUFFER_PRE_PADDING, text, length ); + libwebsocket_write(wsi, buf + LWS_SEND_BUFFER_PRE_PADDING, length, LWS_WRITE_TEXT); + free(buf); + + free(message); + message = NULL; + } + + break; + default: + break; + } + return 0; +} + +static struct libwebsocket_protocols protocols[] = { + { + "http-only", // name + callback_http, // callback + 0, // data size + 0 // maximum frame size + }, + {"myprotocol",callback_my,0,MAX_FRAME_SIZE}, + {NULL,NULL,0,0} +}; + +int main(void) +{ + int port=9000; + struct lws_context_creation_info info; + memset(&info,0,sizeof info); + info.port=port; + info.protocols=protocols; + info.gid=-1; + info.uid=-1; + struct libwebsocket_context *context=libwebsocket_create_context(&info); + + if(context==NULL) + { + fprintf(stderr, "libwebsocket init failed\n"); + return -1; + } + printf("starting server...\n"); + + while(1) + { + libwebsocket_service(context,WAIT_DELAY); + } + libwebsocket_context_destroy(context); + return 0; +} diff --git a/prog/websocketsEx.exe b/prog/websocketsEx.exe new file mode 100644 index 0000000..9b12db2 Binary files /dev/null and b/prog/websocketsEx.exe differ -- libgit2 0.21.2