Browse Source

Last commit with ISS Project

Joao Conceicao Nunes 3 years ago
parent
commit
16abbedf3e

+ 134
- 0
ISS_Project/ESP32_script/ESP32_script.ino View File

@@ -0,0 +1,134 @@
1
+
2
+#include "BluetoothSerial.h"
3
+#include "dht.h"
4
+#include <Wire.h>
5
+#include "MutichannelGasSensor.h"
6
+#define dht_pin 4 
7
+#define mq2_pin 2 
8
+
9
+#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
10
+#error Bluetooth is not enabled! Please run 'make menuconfig' to and enable it
11
+#endif
12
+
13
+BluetoothSerial SerialBT;
14
+float co;
15
+float no2;
16
+float ethanol ;
17
+float hydrogene;
18
+float methane;
19
+float propane;
20
+float isobutane;
21
+int temp;
22
+int hum;
23
+int delay_secondes = 1000;
24
+dht DHT;
25
+
26
+void setup(){
27
+ 
28
+  Serial.begin(115200);
29
+  SerialBT.begin("Scarty");
30
+  gas.begin(0x04);
31
+  gas.powerOn();
32
+  delay(500);//Delay to let system boot
33
+  delay(1000);//Wait before accessing Sensor
34
+
35
+  
36
+  
37
+ 
38
+}
39
+void loop(){
40
+  
41
+DHT.read11(dht_pin);
42
+temp = DHT.temperature;
43
+temp = temp + 1000;
44
+hum = DHT.humidity;
45
+hum = hum + 1200;
46
+
47
+co = gas.measure_CO();
48
+co = co + 2000;
49
+no2 = gas.measure_NO2();
50
+no2 = no2 + 3500;
51
+//ethanol = gas.measure_NH3;
52
+//ethanol = ethanol + 4000;
53
+hydrogene = gas.measure_H2();
54
+hydrogene = hydrogene +5000;
55
+methane = gas.measure_CH4();
56
+methane = methane + 10000;
57
+propane = gas.measure_C3H8();
58
+propane = propane + 20000;
59
+isobutane = gas.measure_C4H10();
60
+isobutane = isobutane + 30000;
61
+
62
+
63
+
64
+
65
+
66
+  
67
+//Temperature
68
+SerialBT.println(temp);
69
+if (Serial.available()){
70
+    SerialBT.write(Serial.read());
71
+  }
72
+  delay(delay_secondes);
73
+  
74
+//Humidite
75
+ SerialBT.println(hum);
76
+  if (Serial.available()){
77
+    SerialBT.write(Serial.read());
78
+  }
79
+  delay(delay_secondes);
80
+ 
81
+  //CO
82
+  Serial.println(co);
83
+ SerialBT.println(co);
84
+  if (Serial.available()){
85
+    SerialBT.write(Serial.read());
86
+    
87
+  }
88
+  delay(delay_secondes);
89
+
90
+  //NO2
91
+ SerialBT.println(no2);
92
+  if (Serial.available()){
93
+    SerialBT.write(Serial.read());
94
+  }
95
+  delay(delay_secondes);
96
+
97
+  //Ethanol
98
+ SerialBT.println(ethanol);
99
+  if (Serial.available()){
100
+    SerialBT.write(Serial.read());
101
+  }
102
+  delay(delay_secondes);
103
+
104
+  //Hydrogene
105
+ SerialBT.println(hydrogene);
106
+  if (Serial.available()){
107
+    SerialBT.write(Serial.read());
108
+  }
109
+  delay(delay_secondes);
110
+
111
+  //Methane
112
+ SerialBT.println(methane);
113
+  if (Serial.available()){
114
+    SerialBT.write(Serial.read());
115
+  }
116
+  delay(delay_secondes);
117
+
118
+  //Propane
119
+ SerialBT.println(propane);
120
+  if (Serial.available()){
121
+    SerialBT.write(Serial.read());
122
+  }
123
+  delay(delay_secondes);
124
+
125
+  //Iso-Butane
126
+ SerialBT.println(isobutane);
127
+  if (Serial.available()){
128
+    SerialBT.write(Serial.read());
129
+  }
130
+  delay(delay_secondes);
131
+  
132
+ 
133
+
134
+}

+ 683
- 0
ISS_Project/ESP32_script/MutichannelGasSensor.cpp View File

@@ -0,0 +1,683 @@
1
+/*
2
+    MutichannelGasSensor.cpp
3
+    2015 Copyright (c) Seeed Technology Inc.  All right reserved.
4
+
5
+    Author: Jacky Zhang
6
+    2015-3-17
7
+    http://www.seeed.cc/
8
+    modi by Jack, 2015-8
9
+
10
+    The MIT License (MIT)
11
+
12
+    Copyright (c) 2015 Seeed Technology Inc.
13
+
14
+    Permission is hereby granted, free of charge, to any person obtaining a copy
15
+    of this software and associated documentation files (the "Software"), to deal
16
+    in the Software without restriction, including without limitation the rights
17
+    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18
+    copies of the Software, and to permit persons to whom the Software is
19
+    furnished to do so, subject to the following conditions:
20
+
21
+    The above copyright notice and this permission notice shall be included in
22
+    all copies or substantial portions of the Software.
23
+
24
+    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25
+    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26
+    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27
+    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28
+    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29
+    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
30
+    THE SOFTWARE.
31
+*/
32
+
33
+#include <math.h>
34
+#include <Wire.h>
35
+#include <Arduino.h>
36
+#include "MutichannelGasSensor.h"
37
+
38
+/*********************************************************************************************************
39
+** Function name:           begin
40
+** Descriptions:            initialize I2C
41
+*********************************************************************************************************/
42
+void MutichannelGasSensor::begin(int address)
43
+{
44
+    __version = 1;          // version 1/2
45
+    r0_inited = false;
46
+
47
+    Wire.begin();
48
+    i2cAddress = address;
49
+    __version = getVersion();
50
+}
51
+
52
+unsigned char MutichannelGasSensor::getVersion()
53
+{
54
+    if(get_addr_dta(CMD_READ_EEPROM, ADDR_IS_SET) == 1126)        // get version 
55
+    {
56
+        __version = 2;
57
+        Serial.println("version = 2");
58
+        return 2;
59
+    }
60
+    
61
+    __version = 1;
62
+    Serial.println("version = 1");
63
+    return 1;
64
+}
65
+
66
+void MutichannelGasSensor::begin()
67
+{
68
+    begin(DEFAULT_I2C_ADDR);
69
+}
70
+
71
+/*********************************************************************************************************
72
+** Function name:           sendI2C
73
+** Descriptions:            send one byte to I2C Wire
74
+*********************************************************************************************************/
75
+void MutichannelGasSensor::sendI2C(unsigned char dta)
76
+{
77
+    Wire.beginTransmission(i2cAddress);               // transmit to device #4
78
+    Wire.write(dta);                                    // sends one byte
79
+    Wire.endTransmission();                             // stop transmitting
80
+}
81
+
82
+
83
+unsigned int MutichannelGasSensor::get_addr_dta(unsigned char addr_reg)
84
+{
85
+START:
86
+    Wire.beginTransmission(i2cAddress);
87
+    Wire.write(addr_reg);
88
+    Wire.endTransmission();    // stop transmitting
89
+    
90
+    Wire.requestFrom(i2cAddress, 2);
91
+    
92
+    unsigned int dta = 0;
93
+    
94
+    unsigned char raw[10];
95
+    int cnt = 0;
96
+    
97
+    while(Wire.available())
98
+    {
99
+        raw[cnt++] = Wire.read();
100
+    }
101
+    
102
+    if(cnt == 0)goto START;
103
+
104
+    dta = raw[0];
105
+    dta <<= 8;
106
+    dta += raw[1];
107
+    
108
+    switch(addr_reg)
109
+    {
110
+        case CH_VALUE_NH3:
111
+        
112
+        if(dta > 0)
113
+        {
114
+            adcValueR0_NH3_Buf = dta;
115
+        }
116
+        else 
117
+        {
118
+            dta = adcValueR0_NH3_Buf;
119
+        }
120
+        
121
+        break;
122
+        
123
+        case CH_VALUE_CO:
124
+        
125
+        if(dta > 0)
126
+        {
127
+            adcValueR0_CO_Buf = dta;
128
+        }
129
+        else 
130
+        {
131
+            dta = adcValueR0_CO_Buf;
132
+        }
133
+        
134
+        break;
135
+        
136
+        case CH_VALUE_NO2:
137
+        
138
+        if(dta > 0)
139
+        {
140
+            adcValueR0_NO2_Buf = dta;
141
+        }
142
+        else 
143
+        {
144
+            dta = adcValueR0_NO2_Buf;
145
+        }
146
+        
147
+        break;
148
+        
149
+        default:;
150
+    }
151
+    return dta;
152
+}
153
+
154
+unsigned int MutichannelGasSensor::get_addr_dta(unsigned char addr_reg, unsigned char __dta)
155
+{
156
+    
157
+START: 
158
+    Wire.beginTransmission(i2cAddress);
159
+    Wire.write(addr_reg);
160
+    Wire.write(__dta);
161
+    Wire.endTransmission();    // stop transmitting
162
+    
163
+    Wire.requestFrom(i2cAddress, 2);
164
+    
165
+    unsigned int dta = 0;
166
+    
167
+    unsigned char raw[10];
168
+    int cnt = 0;
169
+    
170
+    while(Wire.available())
171
+    {
172
+        raw[cnt++] = Wire.read();
173
+    }
174
+    
175
+    if(cnt == 0)goto START;
176
+
177
+    dta = raw[0];
178
+    dta <<= 8;
179
+    dta += raw[1];
180
+    
181
+
182
+    return dta;
183
+}
184
+
185
+void MutichannelGasSensor::write_i2c(unsigned char addr, unsigned char *dta, unsigned char dta_len)
186
+{
187
+    Wire.beginTransmission(addr);
188
+    for(int i=0; i<dta_len; i++)
189
+    {
190
+        Wire.write(dta[i]);
191
+    }
192
+    Wire.endTransmission();
193
+}
194
+
195
+
196
+/*********************************************************************************************************
197
+** Function name:           readData
198
+** Descriptions:            read 4 bytes from I2C slave
199
+*********************************************************************************************************/
200
+int16_t MutichannelGasSensor::readData(uint8_t cmd)
201
+{
202
+    uint16_t timeout = 0;
203
+    uint8_t buffer[4];
204
+    uint8_t checksum = 0;
205
+    int16_t rtnData = 0;
206
+
207
+    //send command
208
+    sendI2C(cmd);
209
+    //wait for a while
210
+    delay(2);
211
+    //get response
212
+    Wire.requestFrom(i2cAddress, (uint8_t)4);    // request 4 bytes from slave device
213
+    while(Wire.available() == 0)
214
+    {
215
+        if(timeout++ > 100)
216
+            return -2;//time out
217
+        delay(2);
218
+    }
219
+    if(Wire.available() != 4)
220
+        return -3;//rtnData length wrong
221
+    buffer[0] = Wire.read();
222
+    buffer[1] = Wire.read();
223
+    buffer[2] = Wire.read();
224
+    buffer[3] = Wire.read();
225
+    checksum = (uint8_t)(buffer[0] + buffer[1] + buffer[2]);
226
+    if(checksum != buffer[3])
227
+        return -4;//checksum wrong
228
+    rtnData = ((buffer[1] << 8) + buffer[2]);
229
+
230
+    return rtnData;//successful
231
+}
232
+
233
+/*********************************************************************************************************
234
+** Function name:           readR0
235
+** Descriptions:            read R0 stored in slave MCU
236
+*********************************************************************************************************/
237
+int16_t MutichannelGasSensor::readR0(void)
238
+{
239
+    int16_t rtnData = 0;
240
+
241
+    rtnData = readData(0x11);
242
+   
243
+    if(rtnData > 0)
244
+        res0[0] = rtnData;
245
+    else
246
+        return rtnData;         //unsuccessful
247
+
248
+    rtnData = readData(0x12);
249
+    if(rtnData > 0)
250
+        res0[1] = rtnData;
251
+    else
252
+        return rtnData;         //unsuccessful
253
+
254
+    rtnData = readData(0x13);
255
+    if(rtnData > 0)
256
+        res0[2] = rtnData;
257
+    else
258
+        return rtnData;         //unsuccessful
259
+
260
+    return 1;//successful
261
+}
262
+
263
+/*********************************************************************************************************
264
+** Function name:           readR
265
+** Descriptions:            read resistance value of each channel from slave MCU
266
+*********************************************************************************************************/
267
+int16_t MutichannelGasSensor::readR(void)
268
+{
269
+    int16_t rtnData = 0;
270
+
271
+    rtnData = readData(0x01);
272
+    if(rtnData >= 0)
273
+        res[0] = rtnData;
274
+    else
275
+        return rtnData;//unsuccessful
276
+
277
+    rtnData = readData(0x02);
278
+    if(rtnData >= 0)
279
+        res[1] = rtnData;
280
+    else
281
+        return rtnData;//unsuccessful
282
+
283
+    rtnData = readData(0x03);
284
+    if(rtnData >= 0)
285
+        res[2] = rtnData;
286
+    else
287
+        return rtnData;//unsuccessful
288
+
289
+    return 0;//successful
290
+}
291
+
292
+/*********************************************************************************************************
293
+** Function name:           readR
294
+** Descriptions:            calculate gas concentration of each channel from slave MCU
295
+** Parameters:
296
+                            gas - gas type
297
+** Returns:
298
+                            float value - concentration of the gas
299
+*********************************************************************************************************/
300
+float MutichannelGasSensor::calcGas(int gas)
301
+{
302
+
303
+    float ratio0, ratio1, ratio2;
304
+    if(1 == __version)
305
+    {
306
+        if(!r0_inited)
307
+        {
308
+            if(readR0() >= 0) r0_inited = true;
309
+            else return -1.0f;
310
+        }
311
+        
312
+        if(readR() < 0)
313
+            return -2.0f;
314
+
315
+        ratio0 = (float)res[0] / res0[0];
316
+        ratio1 = (float)res[1] / res0[1];
317
+        ratio2 = (float)res[2] / res0[2];
318
+    }
319
+    else if(2 == __version)
320
+    {
321
+        // how to calc ratio/123
322
+        ledOn();
323
+        int A0_0 = get_addr_dta(6, ADDR_USER_ADC_HN3);
324
+        int A0_1 = get_addr_dta(6, ADDR_USER_ADC_CO);
325
+        int A0_2 = get_addr_dta(6, ADDR_USER_ADC_NO2);
326
+        
327
+        int An_0 = get_addr_dta(CH_VALUE_NH3);
328
+        int An_1 = get_addr_dta(CH_VALUE_CO);
329
+        int An_2 = get_addr_dta(CH_VALUE_NO2);
330
+        
331
+        ratio0 = (float)An_0/(float)A0_0*(1023.0-A0_0)/(1023.0-An_0);
332
+        ratio1 = (float)An_1/(float)A0_1*(1023.0-A0_1)/(1023.0-An_1);
333
+        ratio2 = (float)An_2/(float)A0_2*(1023.0-A0_2)/(1023.0-An_2);
334
+        
335
+    }
336
+    
337
+    float c = 0;
338
+
339
+    switch(gas)
340
+    {
341
+        case CO:
342
+        {
343
+            c = pow(ratio1, -1.179)*4.385;  //mod by jack
344
+            break;
345
+        }
346
+        case NO2:
347
+        {
348
+            c = pow(ratio2, 1.007)/6.855;  //mod by jack
349
+            break;
350
+        }
351
+        case NH3:
352
+        {
353
+            c = pow(ratio0, -1.67)/1.47;  //modi by jack
354
+            break;
355
+        }
356
+        case C3H8:  //add by jack
357
+        {
358
+            c = pow(ratio0, -2.518)*570.164;
359
+            break;
360
+        }
361
+        case C4H10:  //add by jack
362
+        {
363
+            c = pow(ratio0, -2.138)*398.107;
364
+            break;
365
+        }
366
+        case CH4:  //add by jack
367
+        {
368
+            c = pow(ratio1, -4.363)*630.957;
369
+            break;
370
+        }
371
+        case H2:  //add by jack
372
+        {
373
+            c = pow(ratio1, -1.8)*0.73;
374
+            break;
375
+        }
376
+        case C2H5OH:  //add by jack
377
+        {
378
+            c = pow(ratio1, -1.552)*1.622;
379
+            break;
380
+        }
381
+        default:
382
+            break;
383
+    }
384
+    
385
+    if(2==__version)ledOff();
386
+    return isnan(c)?-3:c;
387
+}
388
+
389
+/*********************************************************************************************************
390
+** Function name:           changeI2cAddr
391
+** Descriptions:            change I2C address of the slave MCU, and this address will be stored in EEPROM of slave MCU
392
+*********************************************************************************************************/
393
+void MutichannelGasSensor::changeI2cAddr(uint8_t newAddr)
394
+{
395
+    Wire.beginTransmission(i2cAddress); // transmit to device
396
+    Wire.write(0x23);              // sends one byte
397
+    Wire.write(newAddr);              // sends one byte
398
+    Wire.endTransmission();    // stop transmitting
399
+    i2cAddress = newAddr;
400
+}
401
+
402
+/*********************************************************************************************************
403
+** Function name:           doCalibrate
404
+** Descriptions:            tell slave to do a calibration, it will take about 8s
405
+                            after the calibration, must reread the R0 values
406
+*********************************************************************************************************/
407
+void MutichannelGasSensor::doCalibrate(void)
408
+{
409
+
410
+    if(1 == __version)
411
+    {
412
+    START:
413
+
414
+        sendI2C(0x22);
415
+        if(readR0() > 0)
416
+        {
417
+            for(int i=0; i<3; i++)
418
+            {
419
+                Serial.print(res0[i]);
420
+                Serial.print('\t');
421
+            }
422
+        }
423
+        else
424
+        {
425
+            delay(5000);
426
+            Serial.println("continue...");
427
+            for(int i=0; i<3; i++)
428
+            {
429
+                Serial.print(res0[i]);
430
+                Serial.print('\t');
431
+            }
432
+            Serial.println();
433
+            goto START;
434
+        }
435
+    }
436
+    else if(2 == __version)
437
+    {
438
+        unsigned int i, a0, a1, a2;
439
+        while(1)
440
+        {
441
+            a0 = get_addr_dta(CH_VALUE_NH3);
442
+            a1 = get_addr_dta(CH_VALUE_CO);
443
+            a2 = get_addr_dta(CH_VALUE_NO2);
444
+            
445
+            Serial.print(a0);
446
+            Serial.print('\t');
447
+            Serial.print(a1);
448
+            Serial.print('\t');
449
+            Serial.print(a2);
450
+            Serial.println('\t');
451
+            ledOn();
452
+            
453
+            int cnt = 0;
454
+            for(i=0; i<20; i++)
455
+            {
456
+                if((a0 - get_addr_dta(CH_VALUE_NH3)) > 2 || (get_addr_dta(CH_VALUE_NH3) - a0) > 2)cnt++;
457
+                if((a1 - get_addr_dta(CH_VALUE_CO)) > 2 || (get_addr_dta(CH_VALUE_CO) - a1) > 2)cnt++;
458
+                if((a2 - get_addr_dta(CH_VALUE_NO2)) > 2 || (get_addr_dta(CH_VALUE_NO2) - a2) > 2)cnt++;
459
+                
460
+                if(cnt>5)
461
+                {
462
+                    break;
463
+                }
464
+                delay(1000);
465
+            }
466
+                        
467
+            ledOff();
468
+            if(cnt <= 5)break;
469
+            delay(200);
470
+        }
471
+        
472
+        Serial.print("write user adc value: ");
473
+        Serial.print(a0);Serial.print('\t');
474
+        Serial.print(a1);Serial.print('\t');
475
+        Serial.print(a2);Serial.println('\t');
476
+        
477
+        unsigned char tmp[7];
478
+    
479
+        tmp[0] = 7;
480
+
481
+        tmp[1] = a0>>8;
482
+        tmp[2] = a0&0xff;
483
+           
484
+        tmp[3] = a1>>8;
485
+        tmp[4] = a1&0xff;
486
+
487
+        tmp[5] = a2>>8;
488
+        tmp[6] = a2&0xff;
489
+            
490
+        write_i2c(i2cAddress, tmp, 7);
491
+    }
492
+}
493
+
494
+/*********************************************************************************************************
495
+** Function name:           powerOn
496
+** Descriptions:            power on sensor heater
497
+*********************************************************************************************************/
498
+void MutichannelGasSensor::powerOn(void)
499
+{
500
+    if(__version == 1)
501
+    sendI2C(0x21);
502
+    else if(__version == 2)
503
+    {
504
+        dta_test[0] = 11;
505
+        dta_test[1] = 1;
506
+        write_i2c(i2cAddress, dta_test, 2);
507
+    }
508
+}
509
+
510
+/*********************************************************************************************************
511
+** Function name:           powerOff
512
+** Descriptions:            power off sensor heater
513
+*********************************************************************************************************/
514
+void MutichannelGasSensor::powerOff(void)
515
+{
516
+    if(__version == 1)
517
+    sendI2C(0x20);
518
+    else if(__version == 2)
519
+    {
520
+        dta_test[0] = 11;
521
+        dta_test[1] = 0;
522
+        write_i2c(i2cAddress, dta_test, 2);
523
+    }
524
+}
525
+
526
+void MutichannelGasSensor::display_eeprom()
527
+{
528
+    if(__version == 1)
529
+    {
530
+        Serial.println("ERROR: display_eeprom() is NOT support by V1 firmware.");
531
+        return ;
532
+    }
533
+    
534
+    Serial.print("ADDR_IS_SET = "); Serial.println(get_addr_dta(CMD_READ_EEPROM, ADDR_IS_SET));
535
+    Serial.print("ADDR_FACTORY_ADC_NH3 = "); Serial.println(get_addr_dta(CMD_READ_EEPROM, ADDR_FACTORY_ADC_NH3));
536
+    Serial.print("ADDR_FACTORY_ADC_CO = "); Serial.println(get_addr_dta(CMD_READ_EEPROM, ADDR_FACTORY_ADC_CO));
537
+    Serial.print("ADDR_FACTORY_ADC_NO2 = "); Serial.println(get_addr_dta(CMD_READ_EEPROM, ADDR_FACTORY_ADC_NO2));
538
+    Serial.print("ADDR_USER_ADC_HN3 = "); Serial.println(get_addr_dta(CMD_READ_EEPROM, ADDR_USER_ADC_HN3));
539
+    Serial.print("ADDR_USER_ADC_CO = "); Serial.println(get_addr_dta(CMD_READ_EEPROM, ADDR_USER_ADC_CO));
540
+    Serial.print("ADDR_USER_ADC_NO2 = "); Serial.println(get_addr_dta(CMD_READ_EEPROM, ADDR_USER_ADC_NO2));
541
+    Serial.print("ADDR_I2C_ADDRESS = "); Serial.println(get_addr_dta(CMD_READ_EEPROM, ADDR_I2C_ADDRESS));
542
+}
543
+
544
+float MutichannelGasSensor::getR0(unsigned char ch)         // 0:CH3, 1:CO, 2:NO2
545
+{
546
+    if(__version == 1)
547
+    {
548
+        Serial.println("ERROR: getR0() is NOT support by V1 firmware.");
549
+        return -1;
550
+    }
551
+    
552
+    int a = 0;
553
+    switch(ch)
554
+    {
555
+        case 0:         // CH3
556
+        a = get_addr_dta(CMD_READ_EEPROM, ADDR_USER_ADC_HN3);
557
+        Serial.print("a_ch3 = ");
558
+        Serial.println(a);
559
+        break;
560
+        
561
+        case 1:         // CO
562
+        a = get_addr_dta(CMD_READ_EEPROM, ADDR_USER_ADC_CO);
563
+        Serial.print("a_co = ");
564
+        Serial.println(a);
565
+        break;
566
+        
567
+        case 2:         // NO2
568
+        a = get_addr_dta(CMD_READ_EEPROM, ADDR_USER_ADC_NO2);
569
+        Serial.print("a_no2 = ");
570
+        Serial.println(a);
571
+        break;
572
+        
573
+        default:;
574
+    }
575
+
576
+    float r = 56.0*(float)a/(1023.0-(float)a);
577
+    return r;
578
+}
579
+
580
+float MutichannelGasSensor::getRs(unsigned char ch)         // 0:CH3, 1:CO, 2:NO2
581
+{
582
+    
583
+    if(__version == 1)
584
+    {
585
+        Serial.println("ERROR: getRs() is NOT support by V1 firmware.");
586
+        return -1;
587
+    }
588
+    
589
+    int a = 0;
590
+    switch(ch)
591
+    {
592
+        case 0:         // NH3
593
+        a = get_addr_dta(1);
594
+        break;
595
+        
596
+        case 1:         // CO
597
+        a = get_addr_dta(2);
598
+        break;
599
+        
600
+        case 2:         // NO2
601
+        a = get_addr_dta(3);
602
+        break;
603
+        
604
+        default:;
605
+    }
606
+    
607
+    float r = 56.0*(float)a/(1023.0-(float)a);
608
+    return r;
609
+}
610
+
611
+// 1. change i2c address to 0x04
612
+// 2. change adc value of R0 to default
613
+void MutichannelGasSensor::factory_setting()
614
+{
615
+    
616
+    unsigned char tmp[7];
617
+
618
+    unsigned char error;
619
+    unsigned char address = 0;
620
+    
621
+    for(address = 1; address < 127; address++ )
622
+    {
623
+        // The i2c_scanner uses the return value of
624
+        // the Write.endTransmisstion to see if
625
+        // a device did acknowledge to the address.
626
+        Wire.beginTransmission(address);
627
+        error = Wire.endTransmission();
628
+
629
+        if (error == 0)
630
+        {
631
+            // change i2c to 0x04
632
+            
633
+            Serial.print("I2C address is: 0x");
634
+            Serial.println(address, HEX);
635
+            Serial.println("Change I2C address to 0x04");
636
+            
637
+            dta_test[0] = CMD_CHANGE_I2C;
638
+            dta_test[1] = 0x04;
639
+            write_i2c(address, dta_test, 2);
640
+
641
+            i2cAddress = 0x04;
642
+            delay(100);
643
+            getVersion();
644
+            break;
645
+        }
646
+    }
647
+
648
+    unsigned int a0 = get_addr_dta(CMD_READ_EEPROM, ADDR_FACTORY_ADC_NH3);
649
+    unsigned int a1 = get_addr_dta(CMD_READ_EEPROM, ADDR_FACTORY_ADC_CO);
650
+    unsigned int a2 = get_addr_dta(CMD_READ_EEPROM, ADDR_FACTORY_ADC_NO2);
651
+    
652
+    tmp[0] = 7;
653
+    tmp[1] = a0>>8;
654
+    tmp[2] = a0&0xff;     
655
+    tmp[3] = a1>>8;
656
+    tmp[4] = a1&0xff;
657
+
658
+    tmp[5] = a2>>8;
659
+    tmp[6] = a2&0xff;   
660
+    delay(100);
661
+    write_i2c(i2cAddress, tmp, 7);
662
+    delay(100);
663
+}
664
+
665
+void MutichannelGasSensor::change_i2c_address(unsigned char addr)
666
+{
667
+    dta_test[0] = CMD_CHANGE_I2C;
668
+    dta_test[1] = addr;
669
+    write_i2c(i2cAddress, dta_test, 2);
670
+    
671
+    
672
+    Serial.print("FUNCTION: CHANGE I2C ADDRESS: 0X");
673
+    Serial.print(i2cAddress, HEX);
674
+    Serial.print(" > 0x");
675
+    Serial.println(addr, HEX);
676
+    
677
+    i2cAddress = addr;
678
+}
679
+    
680
+MutichannelGasSensor gas;
681
+/*********************************************************************************************************
682
+  END FILE
683
+*********************************************************************************************************/

+ 151
- 0
ISS_Project/ESP32_script/MutichannelGasSensor.h View File

@@ -0,0 +1,151 @@
1
+/*
2
+    MutichannelGasSensor.h
3
+    2015 Copyright (c) Seeed Technology Inc.  All right reserved.
4
+
5
+    Author: Jacky Zhang
6
+    2015-3-17
7
+    http://www.seeed.cc/
8
+    modi by Jack, 2015-8
9
+    
10
+    V2 by Loovee
11
+    2016-11-11
12
+
13
+    The MIT License (MIT)
14
+
15
+    Copyright (c) 2015 Seeed Technology Inc.
16
+
17
+    Permission is hereby granted, free of charge, to any person obtaining a copy
18
+    of this software and associated documentation files (the "Software"), to deal
19
+    in the Software without restriction, including without limitation the rights
20
+    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
21
+    copies of the Software, and to permit persons to whom the Software is
22
+    furnished to do so, subject to the following conditions:
23
+
24
+    The above copyright notice and this permission notice shall be included in
25
+    all copies or substantial portions of the Software.
26
+
27
+    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28
+    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29
+    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
30
+    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
31
+    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
32
+    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
33
+    THE SOFTWARE.
34
+*/
35
+
36
+#ifndef __MUTICHANNELGASSENSOR_H__
37
+#define __MUTICHANNELGASSENSOR_H__
38
+
39
+#define DEFAULT_I2C_ADDR    0x04
40
+
41
+#define ADDR_IS_SET             0           // if this is the first time to run, if 1126, set 
42
+#define ADDR_FACTORY_ADC_NH3    2
43
+#define ADDR_FACTORY_ADC_CO     4
44
+#define ADDR_FACTORY_ADC_NO2    6
45
+
46
+#define ADDR_USER_ADC_HN3       8
47
+#define ADDR_USER_ADC_CO        10
48
+#define ADDR_USER_ADC_NO2       12
49
+#define ADDR_IF_CALI            14          // IF USER HAD CALI
50
+
51
+#define ADDR_I2C_ADDRESS        20
52
+
53
+#define CH_VALUE_NH3            1
54
+#define CH_VALUE_CO             2
55
+#define CH_VALUE_NO2            3
56
+
57
+#define CMD_ADC_RES0            1           // NH3
58
+#define CMD_ADC_RES1            2           // CO
59
+#define CMD_ADC_RES2            3           // NO2
60
+#define CMD_ADC_RESALL          4           // ALL CHANNEL
61
+#define CMD_CHANGE_I2C          5           // CHANGE I2C
62
+#define CMD_READ_EEPROM         6           // READ EEPROM VALUE, RETURN UNSIGNED INT
63
+#define CMD_SET_R0_ADC          7           // SET R0 ADC VALUE
64
+#define CMD_GET_R0_ADC          8           // GET R0 ADC VALUE
65
+#define CMD_GET_R0_ADC_FACTORY  9           // GET FACTORY R0 ADC VALUE
66
+#define CMD_CONTROL_LED         10
67
+#define CMD_CONTROL_PWR         11
68
+
69
+enum{CO, NO2, NH3, C3H8, C4H10, CH4, H2, C2H5OH};
70
+
71
+class MutichannelGasSensor{
72
+
73
+private:
74
+
75
+    int __version;
76
+    unsigned char dta_test[20];
77
+    
78
+    unsigned int readChAdcValue(int ch);
79
+    unsigned int adcValueR0_NH3_Buf;
80
+    unsigned int adcValueR0_CO_Buf;
81
+    unsigned int adcValueR0_NO2_Buf;
82
+    
83
+public:
84
+
85
+    uint8_t i2cAddress;     //I2C address of this MCU
86
+    uint16_t res0[3];       //sensors res0
87
+    uint16_t res[3];        //sensors res
88
+    bool r0_inited;
89
+
90
+
91
+    inline unsigned int get_addr_dta(unsigned char addr_reg);
92
+    inline unsigned int get_addr_dta(unsigned char addr_reg, unsigned char __dta);
93
+    inline void write_i2c(unsigned char addr, unsigned char *dta, unsigned char dta_len);
94
+
95
+    void sendI2C(unsigned char dta);
96
+    int16_t readData(uint8_t cmd);
97
+    int16_t readR0(void);
98
+    int16_t readR(void);
99
+    float calcGas(int gas);
100
+    
101
+public:
102
+
103
+    void begin(int address);
104
+    void begin();
105
+    void changeI2cAddr(uint8_t newAddr);
106
+    void powerOn(void);
107
+    void powerOff(void);
108
+    void doCalibrate(void);
109
+    
110
+    //get gas concentration, unit: ppm
111
+    float measure_CO(){return calcGas(CO);}
112
+    float measure_NO2(){return calcGas(NO2);}
113
+    float measure_NH3(){return calcGas(NH3);}
114
+    float measure_C3H8(){return calcGas(C3H8);}
115
+    float measure_C4H10(){return calcGas(C4H10);}
116
+    float measure_CH4(){return calcGas(CH4);}
117
+    float measure_H2(){return calcGas(H2);}
118
+    float measure_C2H5OH(){return calcGas(C2H5OH);}
119
+    
120
+    float getR0(unsigned char ch);      // 0:CH3, 1:CO, 2:NO2
121
+    float getRs(unsigned char ch);      // 0:CH3, 1:CO, 2:NO2
122
+
123
+public:
124
+
125
+    void ledOn()
126
+    {
127
+        dta_test[0] = CMD_CONTROL_LED;
128
+        dta_test[1] = 1;
129
+        write_i2c(i2cAddress, dta_test, 2);
130
+    }
131
+    
132
+    void ledOff()
133
+    {
134
+        dta_test[0] = CMD_CONTROL_LED;
135
+        dta_test[1] = 0;
136
+        write_i2c(i2cAddress, dta_test, 2);
137
+    }
138
+    
139
+    void display_eeprom();
140
+    void factory_setting();
141
+    void change_i2c_address(unsigned char addr);
142
+    unsigned char getVersion();
143
+};
144
+
145
+extern MutichannelGasSensor gas;
146
+
147
+#endif
148
+
149
+/*********************************************************************************************************
150
+  END FILE
151
+*********************************************************************************************************/

+ 133
- 0
ISS_Project/ESP32_script/dht.cpp View File

@@ -0,0 +1,133 @@
1
+//
2
+//    FILE: dht22.cpp
3
+// VERSION: 0.1.00
4
+// PURPOSE: DHT22 Temperature & Humidity Sensor library for Arduino
5
+//
6
+// DATASHEET: 
7
+//
8
+// HISTORY:
9
+// 0.1.0 by Rob Tillaart (01/04/2011)
10
+// inspired by DHT11 library
11
+//
12
+
13
+#include "dht.h"
14
+
15
+#define TIMEOUT 10000
16
+
17
+/////////////////////////////////////////////////////
18
+//
19
+// PUBLIC
20
+//
21
+
22
+
23
+// return values:
24
+//  0 : OK
25
+// -1 : checksum error
26
+// -2 : timeout
27
+int dht::read11(uint8_t pin)
28
+{
29
+	// READ VALUES
30
+	int rv = read(pin);
31
+	if (rv != 0) return rv;
32
+
33
+	// CONVERT AND STORE
34
+	humidity    = bits[0];  // bit[1] == 0;
35
+	temperature = bits[2];  // bits[3] == 0;
36
+
37
+	// TEST CHECKSUM
38
+	uint8_t sum = bits[0] + bits[2]; // bits[1] && bits[3] both 0
39
+	if (bits[4] != sum) return -1;
40
+
41
+	return 0;
42
+}
43
+
44
+// return values:
45
+//  0 : OK
46
+// -1 : checksum error
47
+// -2 : timeout
48
+int dht::read22(uint8_t pin)
49
+{
50
+	// READ VALUES
51
+	int rv = read(pin);
52
+	if (rv != 0) return rv;
53
+
54
+	// CONVERT AND STORE
55
+	humidity    = word(bits[0], bits[1]) * 0.1;
56
+
57
+	int sign = 1;
58
+	if (bits[2] & 0x80) // negative temperature
59
+	{
60
+		bits[2] = bits[2] & 0x7F;
61
+		sign = -1;
62
+	}
63
+	temperature = sign * word(bits[2], bits[3]) * 0.1;
64
+
65
+
66
+	// TEST CHECKSUM
67
+	uint8_t sum = bits[0] + bits[1] + bits[2] + bits[3];
68
+	if (bits[4] != sum) return -1;
69
+
70
+	return 0;
71
+}
72
+
73
+/////////////////////////////////////////////////////
74
+//
75
+// PRIVATE
76
+//
77
+
78
+// return values:
79
+//  0 : OK
80
+// -2 : timeout
81
+int dht::read(uint8_t pin)
82
+{
83
+	// INIT BUFFERVAR TO RECEIVE DATA
84
+	uint8_t cnt = 7;
85
+	uint8_t idx = 0;
86
+
87
+	// EMPTY BUFFER
88
+	for (int i=0; i< 5; i++) bits[i] = 0;
89
+
90
+	// REQUEST SAMPLE
91
+	pinMode(pin, OUTPUT);
92
+	digitalWrite(pin, LOW);
93
+	delay(20);
94
+	digitalWrite(pin, HIGH);
95
+	delayMicroseconds(40);
96
+	pinMode(pin, INPUT);
97
+
98
+	// GET ACKNOWLEDGE or TIMEOUT
99
+	unsigned int loopCnt = TIMEOUT;
100
+	while(digitalRead(pin) == LOW)
101
+		if (loopCnt-- == 0) return -2;
102
+
103
+	loopCnt = TIMEOUT;
104
+	while(digitalRead(pin) == HIGH)
105
+		if (loopCnt-- == 0) return -2;
106
+
107
+	// READ THE OUTPUT - 40 BITS => 5 BYTES
108
+	for (int i=0; i<40; i++)
109
+	{
110
+		loopCnt = TIMEOUT;
111
+		while(digitalRead(pin) == LOW)
112
+			if (loopCnt-- == 0) return -2;
113
+
114
+		unsigned long t = micros();
115
+
116
+		loopCnt = TIMEOUT;
117
+		while(digitalRead(pin) == HIGH)
118
+			if (loopCnt-- == 0) return -2;
119
+
120
+		if ((micros() - t) > 40) bits[idx] |= (1 << cnt);
121
+		if (cnt == 0)   // next byte?
122
+		{
123
+			cnt = 7;   
124
+			idx++;      
125
+		}
126
+		else cnt--;
127
+	}
128
+
129
+	return 0;
130
+}
131
+//
132
+// END OF FILE
133
+//

+ 38
- 0
ISS_Project/ESP32_script/dht.h View File

@@ -0,0 +1,38 @@
1
+// 
2
+//    FILE: dht.h
3
+// VERSION: 0.1.00
4
+// PURPOSE: DHT Temperature & Humidity Sensor library for Arduino
5
+//
6
+//     URL: http://arduino.cc/playground/Main/DHTLib
7
+//
8
+// HISTORY:
9
+// see dht.cpp file
10
+// 
11
+
12
+#ifndef dht_h
13
+#define dht_h
14
+
15
+#if ARDUINO >= 100
16
+ #include "Arduino.h"
17
+#else
18
+ #include "WProgram.h"
19
+#endif
20
+
21
+#define DHT_LIB_VERSION "0.1.00"
22
+
23
+class dht
24
+{
25
+public:
26
+	int read11(uint8_t pin);
27
+    int read22(uint8_t pin);
28
+	double humidity;
29
+	double temperature;
30
+
31
+private:
32
+	uint8_t bits[5];  // buffer to receive data
33
+	int read(uint8_t pin);
34
+};
35
+#endif
36
+//
37
+// END OF FILE
38
+//

BIN
ISS_Project/Mobile_App.rar View File


Loading…
Cancel
Save