Blame view

unwheezeSketch2.ino 5.33 KB
5ce46bb4   pribeiro   fichier ESP32
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
  #include <BLEDevice.h>
  #include <BLEServer.h>
  #include <BLEUtils.h>
  #include <BLE2902.h>
  
  #include <Arduino.h>
  
  //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;
  }