Commit 5ce46bb4aff2a0386b0cfb78a244146306033013

Authored by pribeiro
0 parents

fichier ESP32

Showing 2 changed files with 220 additions and 0 deletions   Show diff stats
README.md 0 → 100644
  1 +++ a/README.md
unwheezeSketch2.ino 0 → 100644
  1 +++ a/unwheezeSketch2.ino
@@ -0,0 +1,220 @@ @@ -0,0 +1,220 @@
  1 +#include <BLEDevice.h>
  2 +#include <BLEServer.h>
  3 +#include <BLEUtils.h>
  4 +#include <BLE2902.h>
  5 +
  6 +#include <Arduino.h>
  7 +
  8 +//info capteur
  9 +#define LENG 31 //0x42 + 31 bytes equal to 32 bytes
  10 +unsigned char buf[LENG];
  11 +
  12 +//pin ESP32 :
  13 +const int SetPin = 5;
  14 +const int redLED = 14;
  15 +const int yellowLED = 16;
  16 +
  17 +//info bluetooth
  18 +BLECharacteristic *pCharacteristic;
  19 +bool deviceConnected = false;
  20 +
  21 +//variables globales :
  22 +std::string rxValue;
  23 +float PM10 = 0;
  24 +float PM5 = 0;
  25 +float CO2 = 0;
  26 +int PM01Value=0;
  27 +int PM2_5Value=0;
  28 +int PM10Value=0;
  29 +
  30 +// UUIDs:
  31 +// https://www.uuidgenerator.net/
  32 +#define SERVICE_UUID "6E400001-B5A3-F393-E0A9-E50E24DCCA9E"
  33 +#define CHARACTERISTIC_UUID_RX "6E400002-B5A3-F393-E0A9-E50E24DCCA9E"
  34 +#define CHARACTERISTIC_UUID_TX "6E400003-B5A3-F393-E0A9-E50E24DCCA9E"
  35 +
  36 +class MyServerCallbacks: public BLEServerCallbacks {
  37 + void onConnect(BLEServer* pServer) {
  38 + deviceConnected = true;
  39 + digitalWrite(redLED,HIGH);
  40 + };
  41 +
  42 + void onDisconnect(BLEServer* pServer) {
  43 + deviceConnected = false;
  44 + digitalWrite(redLED,LOW);
  45 + }
  46 +};
  47 +
  48 +class MyCallbacks: public BLECharacteristicCallbacks {
  49 + void onWrite(BLECharacteristic *pCharacteristic) {
  50 + //std::string rxValue = pCharacteristic->getValue();
  51 + rxValue = pCharacteristic->getValue();
  52 + if (rxValue.length() > 0) {
  53 + Serial.println("*********");
  54 + Serial.print("Received Value: ");
  55 +
  56 + for (int i = 0; i < rxValue.length(); i++) {
  57 + Serial.print(rxValue[i]);
  58 + }
  59 +
  60 + Serial.println();
  61 +
  62 + // Do stuff based on the command received from the app
  63 + if (rxValue.find("A") != -1) {
  64 + Serial.println("stop measure!");
  65 + }
  66 + else if (rxValue.find("B") != -1) {
  67 + Serial.println("measure!");
  68 + }
  69 + Serial.println();
  70 + Serial.println("*********");
  71 + }
  72 + }
  73 +};
  74 +
  75 +
  76 +void setup() {
  77 + //bluetooth
  78 + Serial.begin(9600);
  79 + pinMode(yellowLED,OUTPUT);
  80 + pinMode(SetPin, OUTPUT);
  81 + pinMode(redLED,OUTPUT);
  82 +
  83 + //Création du device BLE
  84 + BLEDevice::init("MyESP32");
  85 +
  86 + //Création du serveur BLE
  87 + BLEServer *pServer = BLEDevice::createServer();
  88 + pServer->setCallbacks(new MyServerCallbacks());
  89 +
  90 + //Création du service BLE
  91 + BLEService *pService = pServer->createService(SERVICE_UUID);
  92 +
  93 + //Création du BLE characteristic
  94 + pCharacteristic = pService->createCharacteristic(
  95 + CHARACTERISTIC_UUID_TX,
  96 + BLECharacteristic::PROPERTY_NOTIFY
  97 + );
  98 +
  99 + pCharacteristic->addDescriptor(new BLE2902());
  100 +
  101 + BLECharacteristic *pCharacteristic = pService->createCharacteristic(CHARACTERISTIC_UUID_RX,BLECharacteristic::PROPERTY_WRITE);
  102 +
  103 + pCharacteristic->setCallbacks(new MyCallbacks());
  104 +
  105 + //Lancement du service
  106 + pService->start();
  107 +
  108 + //Start advertising
  109 + pServer->getAdvertising()->start();
  110 + Serial.println("Waiting a client connection to notify...");
  111 + digitalWrite(SetPin, LOW);
  112 +}
  113 +
  114 +void loop()
  115 +{
  116 + if (deviceConnected)
  117 + {
  118 + while(rxValue!="B")
  119 + {
  120 + delay(100);
  121 + }
  122 + digitalWrite(SetPin, HIGH);
  123 + digitalWrite(yellowLED,HIGH);
  124 + rxValue="A";
  125 + delay(5000);
  126 +
  127 +
  128 + //commence à lire quand il detecte 0x42
  129 + if(Serial.find(0x42))
  130 + {
  131 + //récupération de la trame envoyée par le capteur
  132 + Serial.readBytes(buf,LENG);
  133 + if(buf[0] == 0x4d)
  134 + {
  135 + if(checkValue(buf,LENG))
  136 + {
  137 + PM01Value=transmitPM01(buf); //récupère le taux de PM1 dans la trame
  138 + PM2_5Value=transmitPM2_5(buf);//récupère le taux de PM2.5 dans la trame
  139 + PM10Value=transmitPM10(buf); //récupère le taux de PM10 dans la trame
  140 + Serial.println(PM10Value);
  141 + }
  142 + }
  143 + }
  144 + //on converti ces valeurs en string :
  145 + char txString[20];
  146 + //de taille 4 car le capteur mesure entre 0 et 500 normalement.
  147 + char PM10String[4];
  148 + dtostrf(PM10Value, 3, 0, PM10String); //float_val, min_width:1, digits_after_decimal, char_buffer
  149 + char PM5String[4];
  150 + dtostrf(PM2_5Value, 3, 0, PM5String);
  151 + char CO2String[4];
  152 + dtostrf(PM01Value, 3, 0, CO2String);
  153 +
  154 + txString[0]=PM10String[0];
  155 + txString[1]=PM10String[1];
  156 + txString[2]=PM10String[2];
  157 + txString[3]='-';
  158 + txString[4]=PM5String[0];
  159 + txString[5]=PM5String[1];
  160 + txString[6]=PM5String[2];
  161 + txString[7]='-';
  162 + txString[8]=CO2String[0];
  163 + txString[9]=CO2String[1];
  164 + txString[10]=CO2String[2];
  165 + txString[11]='-';
  166 +
  167 + pCharacteristic->setValue(txString);
  168 + //envoie de la string
  169 + pCharacteristic->notify();
  170 + Serial.print("message envoyé : ");
  171 + Serial.print(txString);
  172 + }
  173 + delay(100);
  174 + digitalWrite(SetPin, LOW);
  175 + digitalWrite(yellowLED,LOW);
  176 +}
  177 +
  178 +
  179 +
  180 +
  181 +//fonctions de gestion de la trame reçue par le capteur :
  182 +char checkValue(unsigned char *thebuf, char leng)
  183 +{
  184 + char receiveflag=0;
  185 + int receiveSum=0;
  186 +
  187 + for(int i=0; i<(leng-2); i++){
  188 + receiveSum=receiveSum+thebuf[i];
  189 + }
  190 + receiveSum=receiveSum + 0x42;
  191 +
  192 + if(receiveSum == ((thebuf[leng-2]<<8)+thebuf[leng-1])) //check the serial data
  193 + {
  194 + receiveSum = 0;
  195 + receiveflag = 1;
  196 + }
  197 + return receiveflag;
  198 +}
  199 +
  200 +int transmitPM01(unsigned char *thebuf)
  201 +{
  202 + int PM01Val;
  203 + PM01Val=((thebuf[3]<<8) + thebuf[4]);
  204 + return PM01Val;
  205 +}
  206 +
  207 +int transmitPM2_5(unsigned char *thebuf)
  208 +{
  209 + int PM2_5Val;
  210 + PM2_5Val=((thebuf[5]<<8) + thebuf[6]);
  211 + return PM2_5Val;
  212 +}
  213 +
  214 +int transmitPM10(unsigned char *thebuf)
  215 +{
  216 + int PM10Val;
  217 + PM10Val=((thebuf[7]<<8) + thebuf[8]);
  218 + return PM10Val;
  219 +}
  220 +