Commit ab869558586ae541667c7946fe308b9423b9a4be

Authored by aknockae
1 parent 6da8f34e

La com série entre le programme et le capteur BME280, le code de com se trouve d…

…ans le fichier BME280test.ino. Il reste encore à faire communiquer indirectement l'arduino avec le site web. Ce travail est en cours
appWeb/index.html
... ... @@ -3,13 +3,13 @@
3 3 <head>
4 4 <meta charset="UTF-8">
5 5 <title>Page Title</title>
6   - <script src="js/script.js"></script>
7 6 <script src="js/jquery-3.2.1.min.js"></script>
  7 + <script src="js/script.js"></script>
8 8 </head>
9 9 <body>
10 10 <h1>Station météo en ligne</h1>
11   - <h2>Gracias-Havard-Kiang-Knockaert</h2>
12   -
  11 + <h2>Gracias-Havard-Qiang-Knockaert</h2>
  12 + <h3>Connection <span class="connecOn">On</span>/<span class="connecOff">Off</span></h3>
13 13 <h3>Météo</h3>
14 14 <div id="apercuGeneral">
15 15 <div class="soleil"></div>
... ... @@ -18,6 +18,7 @@
18 18 </div>
19 19  
20 20 <div id="relevesMeteo">
  21 + <button onclick="requeteEnvoi();">Récupérer donnée</button>
21 22 <table>
22 23 <tr>
23 24 <th>Température</th>
... ... @@ -34,7 +35,6 @@
34 35 </table>
35 36 </div>
36 37  
37   -
  38 +</body>
38 39 </html>
39   -</body>
40 40  
... ...
appWeb/js/script.js 0 → 100644
... ... @@ -0,0 +1,41 @@
  1 +window.WebSocket=(window.WebSocket||window.MozWebSocket);
  2 +var websocket=new WebSocket('ws://192.168.1.111:9000','myprotocol');
  3 +
  4 +websocket.onopen=function(){ $('h3 .connecOn').css('color','green'); };
  5 +
  6 +websocket.onerror=function(){ $('h3 .connecOff').css('color','red'); };
  7 + //on crée des tableaux de Buffer, ces tableaux permettent de "découper" la chaine de bits, en différents octets
  8 +var bufferReponse = new ArrayBuffer(3);
  9 +var bufferRequete = new ArrayBuffer(3);
  10 +
  11 +//DataView permet de traiter un ArrayBuffer
  12 +var requeteView = new DataView(bufferRequete);
  13 +var reponseView = new DataView(bufferReponse);
  14 +
  15 +//on remplie le buffer de requete via requeteView
  16 +requeteView.setInt8(0,'G'.charCodeAt(0));
  17 +requeteView.setInt8(1,'E'.charCodeAt(0));
  18 +requeteView.setInt8(2,'T'.charCodeAt(0));
  19 +
  20 +//affiche le contenu du tableau dans une alert
  21 +alert(String.fromCharCode(requeteView.getInt8(0))+String.fromCharCode(requeteView.getInt8(1))+String.fromCharCode(requeteView.getInt8(2)));
  22 +
  23 +
  24 +websocket.onmessage=function(message){ //Evenement surveillant la reception de message
  25 +
  26 + console.log(message.data);
  27 + /*$('#relevesMeteo .temperature').text(message.data);
  28 + $('#relevesMeteo .humidite').text(message.data);
  29 + $('#relevesMeteo .pression').text(message.data);
  30 + $('#relevesMeteo .luminosite').text(message.data);*/
  31 +
  32 +};
  33 +
  34 +function requeteEnvoi(){
  35 + websocket.send(bufferReponse);
  36 + console.log(bufferReponse);
  37 +}
  38 +
  39 +
  40 +
  41 +//setInterval(requeteEnvoi,1000);
... ...
appWeb/testWebsockets.html 0 → 100644
... ... @@ -0,0 +1,32 @@
  1 +<!DOCTYPE html>
  2 +<html>
  3 + <head>
  4 + <meta charset="utf-8">
  5 + <script src="js/jquery-3.2.1.min.js"></script>
  6 + <script type="text/javascript">
  7 +window.WebSocket=(window.WebSocket||window.MozWebSocket);
  8 +
  9 +var websocket=new WebSocket('ws://192.168.1.111:9000','myprotocol');
  10 +
  11 +websocket.onopen=function(){ $('h1').css('color','green'); };
  12 +
  13 +websocket.onerror=function(){ $('h1').css('color','red'); };
  14 +
  15 +websocket.onmessage=function(message){
  16 +console.log(message.data);
  17 +$('#messages').append($('<p>',{ text: message.data }));
  18 +};
  19 +
  20 +function sendMessage(){
  21 +websocket.send($('#message').val());
  22 +$('#message').val('');
  23 +}
  24 + </script>
  25 + </head>
  26 + <body>
  27 + <h1>WebSockets test</h1>
  28 + <input type="text" id="message"/>
  29 + <button onclick="sendMessage();">Send</button>
  30 + <div id="messages"></div>
  31 + </body>
  32 +</html>
... ...
code arduino/bme280test.ino deleted
... ... @@ -1,83 +0,0 @@
1   -/***************************************************************************
2   - This is a library for the BME280 humidity, temperature & pressure sensor
3   -
4   - Designed specifically to work with the Adafruit BME280 Breakout
5   - ----> http://www.adafruit.com/products/2650
6   -
7   - These sensors use I2C or SPI to communicate, 2 or 4 pins are required
8   - to interface. The device's I2C address is either 0x76 or 0x77.
9   -
10   - Adafruit invests time and resources providing this open source code,
11   - please support Adafruit andopen-source hardware by purchasing products
12   - from Adafruit!
13   -
14   - Written by Limor Fried & Kevin Townsend for Adafruit Industries.
15   - BSD license, all text above must be included in any redistribution
16   - ***************************************************************************/
17   -
18   -#include <Wire.h>
19   -#include <SPI.h>
20   -#include <Adafruit_Sensor.h>
21   -#include <Adafruit_BME280.h>
22   -
23   -#define BME_SCK 13
24   -#define BME_MISO 12
25   -#define BME_MOSI 11
26   -#define BME_CS 10
27   -
28   -#define SEALEVELPRESSURE_HPA (1013.25)
29   -
30   -//Adafruit_BME280 bme; // I2C
31   -//Adafruit_BME280 bme(BME_CS); // hardware SPI
32   -Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK); // software SPI
33   -
34   -unsigned long delayTime;
35   -
36   -void setup() {
37   - Serial.begin(9600);
38   - Serial.println(F("BME280 test"));
39   -
40   - bool status;
41   -
42   - // default settings
43   - status = bme.begin();
44   - if (!status) {
45   - Serial.println("Could not find a valid BME280 sensor, check wiring!");
46   - while (1);
47   - }
48   -
49   - Serial.println("-- Default Test --");
50   - delayTime = 1000;
51   -
52   - Serial.println();
53   -
54   - delay(100); // let sensor boot up
55   -}
56   -
57   -
58   -void loop() {
59   - printValues();
60   - delay(delayTime);
61   -}
62   -
63   -
64   -void printValues() {
65   - Serial.print("Temperature = ");
66   - Serial.print(bme.readTemperature());
67   - Serial.println(" *C");
68   -
69   - Serial.print("Pressure = ");
70   -
71   - Serial.print(bme.readPressure() / 100.0F);
72   - Serial.println(" hPa");
73   -
74   - Serial.print("Approx. Altitude = ");
75   - Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA));
76   - Serial.println(" m");
77   -
78   - Serial.print("Humidity = ");
79   - Serial.print(bme.readHumidity());
80   - Serial.println(" %");
81   -
82   - Serial.println();
83   -}
prog/main.c 0 → 100644
... ... @@ -0,0 +1,192 @@
  1 +/*
  2 + * Test on serial device
  3 + */
  4 +
  5 +////
  6 +// Include files
  7 +////
  8 +#include <stdio.h>
  9 +#include <stdlib.h>
  10 +#include <unistd.h>
  11 +#include <termios.h>
  12 +#include <string.h>
  13 +#include <libwebsockets.h>
  14 +#include "serial.h"
  15 +
  16 +
  17 +#define MAX_FRAME_SIZE 1024
  18 +#define WAIT_DELAY 50
  19 +
  20 +
  21 +#define SERIAL_DEVICE "/dev/ttyACM0"
  22 +
  23 +/*****************************************************************************/
  24 +/**********************PARTIE WEBSOCKETS + COM SERIE**************************/
  25 +/*****************************************************************************/
  26 +
  27 +//Fonction qui retourne un tableau de char, chaque char correspondant à une donnée d'un capteur
  28 +
  29 +char * getDataFromSensors(char *request,int sizeOfRequest)
  30 +{
  31 + int sd=serialOpen(SERIAL_DEVICE,SERIAL_BOTH);
  32 + serialConfig(sd,B9600);
  33 +
  34 + int i;
  35 + char *reponse = NULL;
  36 +
  37 + //On envoie le GET au FPGA pour récupérer les données
  38 + for(i = 0; i < sizeOfRequest;i++)
  39 + {
  40 + if(write(sd,(void*)&request[i],sizeof(char))!=1)
  41 + {
  42 + perror("main.write"); exit(-1);
  43 + }
  44 + }
  45 +
  46 + printf("\nReponse : ");
  47 +
  48 + reponse = malloc(3*sizeof(char));
  49 +
  50 + for(i=0;i < 3;i++)
  51 + {
  52 + if(read(sd,(void*)&reponse[i],sizeof(char))!=1)
  53 + {
  54 + perror("main.read"); exit(-1);
  55 + }
  56 + }
  57 + printf("\nTemperature : %d",reponse[1]);
  58 + printf("\nPressure : %d",reponse[0]);
  59 + printf("\nHumidity : %d\n",reponse[2]);
  60 +
  61 + serialClose(sd);
  62 +
  63 + return reponse;
  64 +}
  65 +
  66 +
  67 +
  68 +static int callback_http(struct libwebsocket_context *this, struct libwebsocket *wsi, enum libwebsocket_callback_reasons reason, void *user,void *in,size_t len)
  69 +{
  70 + return 0;
  71 +}
  72 +
  73 +static int callback_my(struct libwebsocket_context * this, struct libwebsocket *wsi, enum libwebsocket_callback_reasons reason, void *user,void *in,size_t len)
  74 +{
  75 + static char *requete = NULL;
  76 + static char *reponse = NULL;
  77 + static int msize=0;
  78 +
  79 + switch(reason)
  80 + {
  81 + case LWS_CALLBACK_ESTABLISHED:
  82 + printf("connection established\n");
  83 + requete=NULL;
  84 + // Declenchement d'un prochain envoi au navigateur
  85 + libwebsocket_callback_on_writable(this,wsi);
  86 + break;
  87 +
  88 + case LWS_CALLBACK_RECEIVE:
  89 + // Ici sont traites les messages envoyes par le navigateur
  90 + printf("received data: %s\n",(char *)in);
  91 + requete=malloc(len+LWS_SEND_BUFFER_PRE_PADDING+LWS_SEND_BUFFER_POST_PADDING);
  92 +
  93 + if(requete==NULL)
  94 + {
  95 + perror("callback_my.malloc"); exit(EXIT_FAILURE);
  96 + }
  97 +
  98 + memcpy(requete+LWS_SEND_BUFFER_PRE_PADDING,in,len);
  99 + // Declenchement d'un prochain envoi au navigateur
  100 + msize=len;
  101 + libwebsocket_callback_on_writable(this,wsi);
  102 + break;
  103 +
  104 + case LWS_CALLBACK_SERVER_WRITEABLE:
  105 + // Ici sont envoyes les messages au navigateur
  106 + //Si la requete a pour valeur GET, on envoie les donnée au site
  107 + if(requete!= NULL)
  108 + {
  109 + if(requete[0] == 'G' && requete[1] == 'E' && requete[2] == 'T')
  110 + {
  111 + char *reponse = NULL;
  112 + reponse = getDataFromSensors(requete,3);
  113 + int length = 3;
  114 + unsigned char *buf = malloc(LWS_SEND_BUFFER_PRE_PADDING + length + LWS_SEND_BUFFER_POST_PADDING);
  115 + memcpy (buf + LWS_SEND_BUFFER_PRE_PADDING, reponse, length );
  116 + libwebsocket_write(wsi, buf + LWS_SEND_BUFFER_PRE_PADDING, length, LWS_WRITE_TEXT);
  117 + free(reponse);
  118 +
  119 + free(requete);
  120 + requete = NULL;
  121 + reponse = NULL;
  122 + }
  123 + }
  124 +
  125 + break;
  126 + default:
  127 + break;
  128 + }
  129 + return 0;
  130 +}
  131 +
  132 +static struct libwebsocket_protocols protocols[] = {
  133 + {
  134 + "http-only", // name
  135 + callback_http, // callback
  136 + 0, // data size
  137 + 0 // maximum frame size
  138 + },
  139 + {"myprotocol",callback_my,0,MAX_FRAME_SIZE},
  140 + {NULL,NULL,0,0}
  141 +};
  142 +
  143 +
  144 +
  145 +
  146 +/*****************************************************************/
  147 +/**********************MAIN***************************************/
  148 +/*****************************************************************/
  149 +
  150 +int main(void){
  151 +
  152 +
  153 + char j;
  154 + int i;
  155 + unsigned long count = 0;
  156 +
  157 + //Initialisation WebSockets
  158 + int port=9000;
  159 + struct lws_context_creation_info info;
  160 + memset(&info,0,sizeof info);
  161 + info.port=port;
  162 + info.protocols=protocols;
  163 + info.gid=-1;
  164 + info.uid=-1;
  165 + struct libwebsocket_context *context=libwebsocket_create_context(&info);
  166 +
  167 + //Initialisation com Serie
  168 +
  169 +
  170 + if(context==NULL)
  171 + {
  172 + fprintf(stderr, "libwebsocket init failed\n");
  173 + return -1;
  174 + }
  175 + printf("starting server...\n");
  176 +
  177 +
  178 + //boucle infinie du main
  179 + while(1)
  180 + {
  181 + libwebsocket_service(context,WAIT_DELAY);
  182 + }
  183 + libwebsocket_context_destroy(context);
  184 + return 0;
  185 +
  186 +}
  187 +
  188 +
  189 +//********************************************************************************//
  190 +
  191 +
  192 +
... ...
prog/serial.c 0 → 100644
... ... @@ -0,0 +1,57 @@
  1 +/*
  2 + * Serial library
  3 + */
  4 +
  5 +////
  6 +// Include files
  7 +////
  8 +#include <stdio.h>
  9 +#include <stdlib.h>
  10 +#include <unistd.h>
  11 +#include <fcntl.h>
  12 +#include <termios.h>
  13 +#include <strings.h>
  14 +#include <sys/types.h>
  15 +#include <sys/ioctl.h>
  16 +#include <sys/file.h>
  17 +#include <linux/serial.h>
  18 +
  19 +#include "serial.h"
  20 +
  21 +////
  22 +// Functions
  23 +////
  24 +
  25 +//
  26 +// Open serial port device
  27 +//
  28 +int serialOpen(char *device,int mode){
  29 +int flags=(mode==SERIAL_READ?O_RDONLY:(mode==SERIAL_WRITE?O_WRONLY:O_RDWR));
  30 +int fd=open(device,flags|O_NOCTTY);
  31 +if(fd<0){ perror(device); exit(-1); }
  32 +return fd;
  33 +}
  34 +
  35 +//
  36 +// Serial port configuration
  37 +//
  38 +void serialConfig(int fd,int speed){
  39 +struct termios new;
  40 +bzero(&new,sizeof(new));
  41 +new.c_cflag=CLOCAL|CREAD|speed|CS8;
  42 +new.c_iflag=0;
  43 +new.c_oflag=0;
  44 +new.c_lflag=0; /* set input mode (non-canonical, no echo,...) */
  45 +new.c_cc[VTIME]=0; /* inter-character timer unused */
  46 +new.c_cc[VMIN]=1; /* blocking read until 1 char received */
  47 +if(tcsetattr(fd,TCSANOW,&new)<0){ perror("serialInit.tcsetattr"); exit(-1); }
  48 +}
  49 +
  50 +//
  51 +// Serial port termination
  52 +//
  53 +void serialClose(int fd){
  54 +close(fd);
  55 +}
  56 +
  57 +
... ...
prog/serial.exe 0 → 100644
No preview for this file type
prog/serial.h 0 → 100644
... ... @@ -0,0 +1,18 @@
  1 +/*
  2 + * Public definitions for serial library
  3 + */
  4 +
  5 +////
  6 +// Constants
  7 +////
  8 +
  9 +#define SERIAL_READ 0
  10 +#define SERIAL_WRITE 1
  11 +#define SERIAL_BOTH 2
  12 +
  13 +////
  14 +// Public prototypes
  15 +////
  16 +int serialOpen(char *device,int mode);
  17 +void serialConfig(int fd,int speed);
  18 +void serialClose(int fd);
... ...
prog/websockets.c 0 → 100644
... ... @@ -0,0 +1,102 @@
  1 +#include <stdio.h>
  2 +#include <stdlib.h>
  3 +#include <string.h>
  4 +#include <libwebsockets.h>
  5 +
  6 +#define MAX_FRAME_SIZE 1024
  7 +#define WAIT_DELAY 50
  8 +
  9 +
  10 +static int callback_http(struct libwebsocket_context *this, struct libwebsocket *wsi, enum libwebsocket_callback_reasons reason, void *user,void *in,size_t len)
  11 +{
  12 + return 0;
  13 +}
  14 +
  15 +static int callback_my(struct libwebsocket_context * this, struct libwebsocket *wsi, enum libwebsocket_callback_reasons reason, void *user,void *in,size_t len)
  16 +{
  17 + static char *message = NULL;
  18 + static char *reponse = NULL;
  19 + static int msize=0;
  20 + switch(reason)
  21 + {
  22 + case LWS_CALLBACK_ESTABLISHED:
  23 + printf("connection established\n");
  24 + message=NULL;
  25 + // Declenchement d'un prochain envoi au navigateur
  26 + libwebsocket_callback_on_writable(this,wsi);
  27 + break;
  28 +
  29 + case LWS_CALLBACK_RECEIVE:
  30 + // Ici sont traites les messages envoyes par le navigateur
  31 + printf("received data: %s\n",(char *)in);
  32 + message=malloc(len+LWS_SEND_BUFFER_PRE_PADDING+LWS_SEND_BUFFER_POST_PADDING);
  33 +
  34 + if(message==NULL)
  35 + {
  36 + perror("callback_my.malloc"); exit(EXIT_FAILURE);
  37 + }
  38 +
  39 + memcpy(message+LWS_SEND_BUFFER_PRE_PADDING,in,len);
  40 + // Declenchement d'un prochain envoi au navigateur
  41 + msize=len;
  42 + libwebsocket_callback_on_writable(this,wsi);
  43 + break;
  44 +
  45 + case LWS_CALLBACK_SERVER_WRITEABLE:
  46 + // Ici sont envoyes les messages au navigateur
  47 + if(message!= NULL)
  48 + {
  49 + char *text = "number : ";
  50 + int length = strlen (text);
  51 + unsigned char *buf = malloc(LWS_SEND_BUFFER_PRE_PADDING + length + LWS_SEND_BUFFER_POST_PADDING);
  52 + memcpy (buf + LWS_SEND_BUFFER_PRE_PADDING, text, length );
  53 + libwebsocket_write(wsi, buf + LWS_SEND_BUFFER_PRE_PADDING, length, LWS_WRITE_TEXT);
  54 + free(buf);
  55 +
  56 + free(message);
  57 + message = NULL;
  58 + }
  59 +
  60 + break;
  61 + default:
  62 + break;
  63 + }
  64 + return 0;
  65 +}
  66 +
  67 +static struct libwebsocket_protocols protocols[] = {
  68 + {
  69 + "http-only", // name
  70 + callback_http, // callback
  71 + 0, // data size
  72 + 0 // maximum frame size
  73 + },
  74 + {"myprotocol",callback_my,0,MAX_FRAME_SIZE},
  75 + {NULL,NULL,0,0}
  76 +};
  77 +
  78 +int main(void)
  79 +{
  80 + int port=9000;
  81 + struct lws_context_creation_info info;
  82 + memset(&info,0,sizeof info);
  83 + info.port=port;
  84 + info.protocols=protocols;
  85 + info.gid=-1;
  86 + info.uid=-1;
  87 + struct libwebsocket_context *context=libwebsocket_create_context(&info);
  88 +
  89 + if(context==NULL)
  90 + {
  91 + fprintf(stderr, "libwebsocket init failed\n");
  92 + return -1;
  93 + }
  94 + printf("starting server...\n");
  95 +
  96 + while(1)
  97 + {
  98 + libwebsocket_service(context,WAIT_DELAY);
  99 + }
  100 + libwebsocket_context_destroy(context);
  101 + return 0;
  102 +}
... ...
prog/websocketsEx.exe 0 → 100644
No preview for this file type