From 5ce46bb4aff2a0386b0cfb78a244146306033013 Mon Sep 17 00:00:00 2001 From: pribeiro Date: Sat, 12 May 2018 14:27:16 +0200 Subject: [PATCH] fichier ESP32 --- README.md | 0 unwheezeSketch2.ino | 220 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 220 insertions(+), 0 deletions(-) create mode 100644 README.md create mode 100644 unwheezeSketch2.ino diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/README.md diff --git a/unwheezeSketch2.ino b/unwheezeSketch2.ino new file mode 100644 index 0000000..0673288 --- /dev/null +++ b/unwheezeSketch2.ino @@ -0,0 +1,220 @@ +#include +#include +#include +#include + +#include + +//info capteur +#define LENG 31 //0x42 + 31 bytes equal to 32 bytes +unsigned char buf[LENG]; + +//pin ESP32 : +const int SetPin = 5; +const int redLED = 14; +const int yellowLED = 16; + +//info bluetooth +BLECharacteristic *pCharacteristic; +bool deviceConnected = false; + +//variables globales : +std::string rxValue; +float PM10 = 0; +float PM5 = 0; +float CO2 = 0; +int PM01Value=0; +int PM2_5Value=0; +int PM10Value=0; + +// UUIDs: +// https://www.uuidgenerator.net/ +#define SERVICE_UUID "6E400001-B5A3-F393-E0A9-E50E24DCCA9E" +#define CHARACTERISTIC_UUID_RX "6E400002-B5A3-F393-E0A9-E50E24DCCA9E" +#define CHARACTERISTIC_UUID_TX "6E400003-B5A3-F393-E0A9-E50E24DCCA9E" + +class MyServerCallbacks: public BLEServerCallbacks { + void onConnect(BLEServer* pServer) { + deviceConnected = true; + digitalWrite(redLED,HIGH); + }; + + void onDisconnect(BLEServer* pServer) { + deviceConnected = false; + digitalWrite(redLED,LOW); + } +}; + +class MyCallbacks: public BLECharacteristicCallbacks { + void onWrite(BLECharacteristic *pCharacteristic) { + //std::string rxValue = pCharacteristic->getValue(); + rxValue = pCharacteristic->getValue(); + if (rxValue.length() > 0) { + Serial.println("*********"); + Serial.print("Received Value: "); + + for (int i = 0; i < rxValue.length(); i++) { + Serial.print(rxValue[i]); + } + + Serial.println(); + + // Do stuff based on the command received from the app + if (rxValue.find("A") != -1) { + Serial.println("stop measure!"); + } + else if (rxValue.find("B") != -1) { + Serial.println("measure!"); + } + Serial.println(); + Serial.println("*********"); + } + } +}; + + +void setup() { + //bluetooth + Serial.begin(9600); + pinMode(yellowLED,OUTPUT); + pinMode(SetPin, OUTPUT); + pinMode(redLED,OUTPUT); + + //Création du device BLE + BLEDevice::init("MyESP32"); + + //Création du serveur BLE + BLEServer *pServer = BLEDevice::createServer(); + pServer->setCallbacks(new MyServerCallbacks()); + + //Création du service BLE + BLEService *pService = pServer->createService(SERVICE_UUID); + + //Création du BLE characteristic + pCharacteristic = pService->createCharacteristic( + CHARACTERISTIC_UUID_TX, + BLECharacteristic::PROPERTY_NOTIFY + ); + + pCharacteristic->addDescriptor(new BLE2902()); + + BLECharacteristic *pCharacteristic = pService->createCharacteristic(CHARACTERISTIC_UUID_RX,BLECharacteristic::PROPERTY_WRITE); + + pCharacteristic->setCallbacks(new MyCallbacks()); + + //Lancement du service + pService->start(); + + //Start advertising + pServer->getAdvertising()->start(); + Serial.println("Waiting a client connection to notify..."); + digitalWrite(SetPin, LOW); +} + +void loop() +{ + if (deviceConnected) + { + while(rxValue!="B") + { + delay(100); + } + digitalWrite(SetPin, HIGH); + digitalWrite(yellowLED,HIGH); + rxValue="A"; + delay(5000); + + + //commence à lire quand il detecte 0x42 + if(Serial.find(0x42)) + { + //récupération de la trame envoyée par le capteur + Serial.readBytes(buf,LENG); + if(buf[0] == 0x4d) + { + if(checkValue(buf,LENG)) + { + PM01Value=transmitPM01(buf); //récupère le taux de PM1 dans la trame + PM2_5Value=transmitPM2_5(buf);//récupère le taux de PM2.5 dans la trame + PM10Value=transmitPM10(buf); //récupère le taux de PM10 dans la trame + Serial.println(PM10Value); + } + } + } + //on converti ces valeurs en string : + char txString[20]; + //de taille 4 car le capteur mesure entre 0 et 500 normalement. + char PM10String[4]; + dtostrf(PM10Value, 3, 0, PM10String); //float_val, min_width:1, digits_after_decimal, char_buffer + char PM5String[4]; + dtostrf(PM2_5Value, 3, 0, PM5String); + char CO2String[4]; + dtostrf(PM01Value, 3, 0, CO2String); + + txString[0]=PM10String[0]; + txString[1]=PM10String[1]; + txString[2]=PM10String[2]; + txString[3]='-'; + txString[4]=PM5String[0]; + txString[5]=PM5String[1]; + txString[6]=PM5String[2]; + txString[7]='-'; + txString[8]=CO2String[0]; + txString[9]=CO2String[1]; + txString[10]=CO2String[2]; + txString[11]='-'; + + pCharacteristic->setValue(txString); + //envoie de la string + pCharacteristic->notify(); + Serial.print("message envoyé : "); + Serial.print(txString); + } + delay(100); + digitalWrite(SetPin, LOW); + digitalWrite(yellowLED,LOW); +} + + + + +//fonctions de gestion de la trame reçue par le capteur : +char checkValue(unsigned char *thebuf, char leng) +{ + char receiveflag=0; + int receiveSum=0; + + for(int i=0; i<(leng-2); i++){ + receiveSum=receiveSum+thebuf[i]; + } + receiveSum=receiveSum + 0x42; + + if(receiveSum == ((thebuf[leng-2]<<8)+thebuf[leng-1])) //check the serial data + { + receiveSum = 0; + receiveflag = 1; + } + return receiveflag; +} + +int transmitPM01(unsigned char *thebuf) +{ + int PM01Val; + PM01Val=((thebuf[3]<<8) + thebuf[4]); + return PM01Val; +} + +int transmitPM2_5(unsigned char *thebuf) +{ + int PM2_5Val; + PM2_5Val=((thebuf[5]<<8) + thebuf[6]); + return PM2_5Val; +} + +int transmitPM10(unsigned char *thebuf) +{ + int PM10Val; + PM10Val=((thebuf[7]<<8) + thebuf[8]); + return PM10Val; +} + -- libgit2 0.21.2