Last commit with ISS Project
This commit is contained in:
parent
af07af6085
commit
16abbedf3e
6 changed files with 1139 additions and 0 deletions
134
ISS_Project/ESP32_script/ESP32_script.ino
Normal file
134
ISS_Project/ESP32_script/ESP32_script.ino
Normal file
|
@ -0,0 +1,134 @@
|
|||
|
||||
#include "BluetoothSerial.h"
|
||||
#include "dht.h"
|
||||
#include <Wire.h>
|
||||
#include "MutichannelGasSensor.h"
|
||||
#define dht_pin 4
|
||||
#define mq2_pin 2
|
||||
|
||||
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
|
||||
#error Bluetooth is not enabled! Please run 'make menuconfig' to and enable it
|
||||
#endif
|
||||
|
||||
BluetoothSerial SerialBT;
|
||||
float co;
|
||||
float no2;
|
||||
float ethanol ;
|
||||
float hydrogene;
|
||||
float methane;
|
||||
float propane;
|
||||
float isobutane;
|
||||
int temp;
|
||||
int hum;
|
||||
int delay_secondes = 1000;
|
||||
dht DHT;
|
||||
|
||||
void setup(){
|
||||
|
||||
Serial.begin(115200);
|
||||
SerialBT.begin("Scarty");
|
||||
gas.begin(0x04);
|
||||
gas.powerOn();
|
||||
delay(500);//Delay to let system boot
|
||||
delay(1000);//Wait before accessing Sensor
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
void loop(){
|
||||
|
||||
DHT.read11(dht_pin);
|
||||
temp = DHT.temperature;
|
||||
temp = temp + 1000;
|
||||
hum = DHT.humidity;
|
||||
hum = hum + 1200;
|
||||
|
||||
co = gas.measure_CO();
|
||||
co = co + 2000;
|
||||
no2 = gas.measure_NO2();
|
||||
no2 = no2 + 3500;
|
||||
//ethanol = gas.measure_NH3;
|
||||
//ethanol = ethanol + 4000;
|
||||
hydrogene = gas.measure_H2();
|
||||
hydrogene = hydrogene +5000;
|
||||
methane = gas.measure_CH4();
|
||||
methane = methane + 10000;
|
||||
propane = gas.measure_C3H8();
|
||||
propane = propane + 20000;
|
||||
isobutane = gas.measure_C4H10();
|
||||
isobutane = isobutane + 30000;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//Temperature
|
||||
SerialBT.println(temp);
|
||||
if (Serial.available()){
|
||||
SerialBT.write(Serial.read());
|
||||
}
|
||||
delay(delay_secondes);
|
||||
|
||||
//Humidite
|
||||
SerialBT.println(hum);
|
||||
if (Serial.available()){
|
||||
SerialBT.write(Serial.read());
|
||||
}
|
||||
delay(delay_secondes);
|
||||
|
||||
//CO
|
||||
Serial.println(co);
|
||||
SerialBT.println(co);
|
||||
if (Serial.available()){
|
||||
SerialBT.write(Serial.read());
|
||||
|
||||
}
|
||||
delay(delay_secondes);
|
||||
|
||||
//NO2
|
||||
SerialBT.println(no2);
|
||||
if (Serial.available()){
|
||||
SerialBT.write(Serial.read());
|
||||
}
|
||||
delay(delay_secondes);
|
||||
|
||||
//Ethanol
|
||||
SerialBT.println(ethanol);
|
||||
if (Serial.available()){
|
||||
SerialBT.write(Serial.read());
|
||||
}
|
||||
delay(delay_secondes);
|
||||
|
||||
//Hydrogene
|
||||
SerialBT.println(hydrogene);
|
||||
if (Serial.available()){
|
||||
SerialBT.write(Serial.read());
|
||||
}
|
||||
delay(delay_secondes);
|
||||
|
||||
//Methane
|
||||
SerialBT.println(methane);
|
||||
if (Serial.available()){
|
||||
SerialBT.write(Serial.read());
|
||||
}
|
||||
delay(delay_secondes);
|
||||
|
||||
//Propane
|
||||
SerialBT.println(propane);
|
||||
if (Serial.available()){
|
||||
SerialBT.write(Serial.read());
|
||||
}
|
||||
delay(delay_secondes);
|
||||
|
||||
//Iso-Butane
|
||||
SerialBT.println(isobutane);
|
||||
if (Serial.available()){
|
||||
SerialBT.write(Serial.read());
|
||||
}
|
||||
delay(delay_secondes);
|
||||
|
||||
|
||||
|
||||
}
|
683
ISS_Project/ESP32_script/MutichannelGasSensor.cpp
Normal file
683
ISS_Project/ESP32_script/MutichannelGasSensor.cpp
Normal file
|
@ -0,0 +1,683 @@
|
|||
/*
|
||||
MutichannelGasSensor.cpp
|
||||
2015 Copyright (c) Seeed Technology Inc. All right reserved.
|
||||
|
||||
Author: Jacky Zhang
|
||||
2015-3-17
|
||||
http://www.seeed.cc/
|
||||
modi by Jack, 2015-8
|
||||
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2015 Seeed Technology Inc.
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include <math.h>
|
||||
#include <Wire.h>
|
||||
#include <Arduino.h>
|
||||
#include "MutichannelGasSensor.h"
|
||||
|
||||
/*********************************************************************************************************
|
||||
** Function name: begin
|
||||
** Descriptions: initialize I2C
|
||||
*********************************************************************************************************/
|
||||
void MutichannelGasSensor::begin(int address)
|
||||
{
|
||||
__version = 1; // version 1/2
|
||||
r0_inited = false;
|
||||
|
||||
Wire.begin();
|
||||
i2cAddress = address;
|
||||
__version = getVersion();
|
||||
}
|
||||
|
||||
unsigned char MutichannelGasSensor::getVersion()
|
||||
{
|
||||
if(get_addr_dta(CMD_READ_EEPROM, ADDR_IS_SET) == 1126) // get version
|
||||
{
|
||||
__version = 2;
|
||||
Serial.println("version = 2");
|
||||
return 2;
|
||||
}
|
||||
|
||||
__version = 1;
|
||||
Serial.println("version = 1");
|
||||
return 1;
|
||||
}
|
||||
|
||||
void MutichannelGasSensor::begin()
|
||||
{
|
||||
begin(DEFAULT_I2C_ADDR);
|
||||
}
|
||||
|
||||
/*********************************************************************************************************
|
||||
** Function name: sendI2C
|
||||
** Descriptions: send one byte to I2C Wire
|
||||
*********************************************************************************************************/
|
||||
void MutichannelGasSensor::sendI2C(unsigned char dta)
|
||||
{
|
||||
Wire.beginTransmission(i2cAddress); // transmit to device #4
|
||||
Wire.write(dta); // sends one byte
|
||||
Wire.endTransmission(); // stop transmitting
|
||||
}
|
||||
|
||||
|
||||
unsigned int MutichannelGasSensor::get_addr_dta(unsigned char addr_reg)
|
||||
{
|
||||
START:
|
||||
Wire.beginTransmission(i2cAddress);
|
||||
Wire.write(addr_reg);
|
||||
Wire.endTransmission(); // stop transmitting
|
||||
|
||||
Wire.requestFrom(i2cAddress, 2);
|
||||
|
||||
unsigned int dta = 0;
|
||||
|
||||
unsigned char raw[10];
|
||||
int cnt = 0;
|
||||
|
||||
while(Wire.available())
|
||||
{
|
||||
raw[cnt++] = Wire.read();
|
||||
}
|
||||
|
||||
if(cnt == 0)goto START;
|
||||
|
||||
dta = raw[0];
|
||||
dta <<= 8;
|
||||
dta += raw[1];
|
||||
|
||||
switch(addr_reg)
|
||||
{
|
||||
case CH_VALUE_NH3:
|
||||
|
||||
if(dta > 0)
|
||||
{
|
||||
adcValueR0_NH3_Buf = dta;
|
||||
}
|
||||
else
|
||||
{
|
||||
dta = adcValueR0_NH3_Buf;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case CH_VALUE_CO:
|
||||
|
||||
if(dta > 0)
|
||||
{
|
||||
adcValueR0_CO_Buf = dta;
|
||||
}
|
||||
else
|
||||
{
|
||||
dta = adcValueR0_CO_Buf;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case CH_VALUE_NO2:
|
||||
|
||||
if(dta > 0)
|
||||
{
|
||||
adcValueR0_NO2_Buf = dta;
|
||||
}
|
||||
else
|
||||
{
|
||||
dta = adcValueR0_NO2_Buf;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
default:;
|
||||
}
|
||||
return dta;
|
||||
}
|
||||
|
||||
unsigned int MutichannelGasSensor::get_addr_dta(unsigned char addr_reg, unsigned char __dta)
|
||||
{
|
||||
|
||||
START:
|
||||
Wire.beginTransmission(i2cAddress);
|
||||
Wire.write(addr_reg);
|
||||
Wire.write(__dta);
|
||||
Wire.endTransmission(); // stop transmitting
|
||||
|
||||
Wire.requestFrom(i2cAddress, 2);
|
||||
|
||||
unsigned int dta = 0;
|
||||
|
||||
unsigned char raw[10];
|
||||
int cnt = 0;
|
||||
|
||||
while(Wire.available())
|
||||
{
|
||||
raw[cnt++] = Wire.read();
|
||||
}
|
||||
|
||||
if(cnt == 0)goto START;
|
||||
|
||||
dta = raw[0];
|
||||
dta <<= 8;
|
||||
dta += raw[1];
|
||||
|
||||
|
||||
return dta;
|
||||
}
|
||||
|
||||
void MutichannelGasSensor::write_i2c(unsigned char addr, unsigned char *dta, unsigned char dta_len)
|
||||
{
|
||||
Wire.beginTransmission(addr);
|
||||
for(int i=0; i<dta_len; i++)
|
||||
{
|
||||
Wire.write(dta[i]);
|
||||
}
|
||||
Wire.endTransmission();
|
||||
}
|
||||
|
||||
|
||||
/*********************************************************************************************************
|
||||
** Function name: readData
|
||||
** Descriptions: read 4 bytes from I2C slave
|
||||
*********************************************************************************************************/
|
||||
int16_t MutichannelGasSensor::readData(uint8_t cmd)
|
||||
{
|
||||
uint16_t timeout = 0;
|
||||
uint8_t buffer[4];
|
||||
uint8_t checksum = 0;
|
||||
int16_t rtnData = 0;
|
||||
|
||||
//send command
|
||||
sendI2C(cmd);
|
||||
//wait for a while
|
||||
delay(2);
|
||||
//get response
|
||||
Wire.requestFrom(i2cAddress, (uint8_t)4); // request 4 bytes from slave device
|
||||
while(Wire.available() == 0)
|
||||
{
|
||||
if(timeout++ > 100)
|
||||
return -2;//time out
|
||||
delay(2);
|
||||
}
|
||||
if(Wire.available() != 4)
|
||||
return -3;//rtnData length wrong
|
||||
buffer[0] = Wire.read();
|
||||
buffer[1] = Wire.read();
|
||||
buffer[2] = Wire.read();
|
||||
buffer[3] = Wire.read();
|
||||
checksum = (uint8_t)(buffer[0] + buffer[1] + buffer[2]);
|
||||
if(checksum != buffer[3])
|
||||
return -4;//checksum wrong
|
||||
rtnData = ((buffer[1] << 8) + buffer[2]);
|
||||
|
||||
return rtnData;//successful
|
||||
}
|
||||
|
||||
/*********************************************************************************************************
|
||||
** Function name: readR0
|
||||
** Descriptions: read R0 stored in slave MCU
|
||||
*********************************************************************************************************/
|
||||
int16_t MutichannelGasSensor::readR0(void)
|
||||
{
|
||||
int16_t rtnData = 0;
|
||||
|
||||
rtnData = readData(0x11);
|
||||
|
||||
if(rtnData > 0)
|
||||
res0[0] = rtnData;
|
||||
else
|
||||
return rtnData; //unsuccessful
|
||||
|
||||
rtnData = readData(0x12);
|
||||
if(rtnData > 0)
|
||||
res0[1] = rtnData;
|
||||
else
|
||||
return rtnData; //unsuccessful
|
||||
|
||||
rtnData = readData(0x13);
|
||||
if(rtnData > 0)
|
||||
res0[2] = rtnData;
|
||||
else
|
||||
return rtnData; //unsuccessful
|
||||
|
||||
return 1;//successful
|
||||
}
|
||||
|
||||
/*********************************************************************************************************
|
||||
** Function name: readR
|
||||
** Descriptions: read resistance value of each channel from slave MCU
|
||||
*********************************************************************************************************/
|
||||
int16_t MutichannelGasSensor::readR(void)
|
||||
{
|
||||
int16_t rtnData = 0;
|
||||
|
||||
rtnData = readData(0x01);
|
||||
if(rtnData >= 0)
|
||||
res[0] = rtnData;
|
||||
else
|
||||
return rtnData;//unsuccessful
|
||||
|
||||
rtnData = readData(0x02);
|
||||
if(rtnData >= 0)
|
||||
res[1] = rtnData;
|
||||
else
|
||||
return rtnData;//unsuccessful
|
||||
|
||||
rtnData = readData(0x03);
|
||||
if(rtnData >= 0)
|
||||
res[2] = rtnData;
|
||||
else
|
||||
return rtnData;//unsuccessful
|
||||
|
||||
return 0;//successful
|
||||
}
|
||||
|
||||
/*********************************************************************************************************
|
||||
** Function name: readR
|
||||
** Descriptions: calculate gas concentration of each channel from slave MCU
|
||||
** Parameters:
|
||||
gas - gas type
|
||||
** Returns:
|
||||
float value - concentration of the gas
|
||||
*********************************************************************************************************/
|
||||
float MutichannelGasSensor::calcGas(int gas)
|
||||
{
|
||||
|
||||
float ratio0, ratio1, ratio2;
|
||||
if(1 == __version)
|
||||
{
|
||||
if(!r0_inited)
|
||||
{
|
||||
if(readR0() >= 0) r0_inited = true;
|
||||
else return -1.0f;
|
||||
}
|
||||
|
||||
if(readR() < 0)
|
||||
return -2.0f;
|
||||
|
||||
ratio0 = (float)res[0] / res0[0];
|
||||
ratio1 = (float)res[1] / res0[1];
|
||||
ratio2 = (float)res[2] / res0[2];
|
||||
}
|
||||
else if(2 == __version)
|
||||
{
|
||||
// how to calc ratio/123
|
||||
ledOn();
|
||||
int A0_0 = get_addr_dta(6, ADDR_USER_ADC_HN3);
|
||||
int A0_1 = get_addr_dta(6, ADDR_USER_ADC_CO);
|
||||
int A0_2 = get_addr_dta(6, ADDR_USER_ADC_NO2);
|
||||
|
||||
int An_0 = get_addr_dta(CH_VALUE_NH3);
|
||||
int An_1 = get_addr_dta(CH_VALUE_CO);
|
||||
int An_2 = get_addr_dta(CH_VALUE_NO2);
|
||||
|
||||
ratio0 = (float)An_0/(float)A0_0*(1023.0-A0_0)/(1023.0-An_0);
|
||||
ratio1 = (float)An_1/(float)A0_1*(1023.0-A0_1)/(1023.0-An_1);
|
||||
ratio2 = (float)An_2/(float)A0_2*(1023.0-A0_2)/(1023.0-An_2);
|
||||
|
||||
}
|
||||
|
||||
float c = 0;
|
||||
|
||||
switch(gas)
|
||||
{
|
||||
case CO:
|
||||
{
|
||||
c = pow(ratio1, -1.179)*4.385; //mod by jack
|
||||
break;
|
||||
}
|
||||
case NO2:
|
||||
{
|
||||
c = pow(ratio2, 1.007)/6.855; //mod by jack
|
||||
break;
|
||||
}
|
||||
case NH3:
|
||||
{
|
||||
c = pow(ratio0, -1.67)/1.47; //modi by jack
|
||||
break;
|
||||
}
|
||||
case C3H8: //add by jack
|
||||
{
|
||||
c = pow(ratio0, -2.518)*570.164;
|
||||
break;
|
||||
}
|
||||
case C4H10: //add by jack
|
||||
{
|
||||
c = pow(ratio0, -2.138)*398.107;
|
||||
break;
|
||||
}
|
||||
case CH4: //add by jack
|
||||
{
|
||||
c = pow(ratio1, -4.363)*630.957;
|
||||
break;
|
||||
}
|
||||
case H2: //add by jack
|
||||
{
|
||||
c = pow(ratio1, -1.8)*0.73;
|
||||
break;
|
||||
}
|
||||
case C2H5OH: //add by jack
|
||||
{
|
||||
c = pow(ratio1, -1.552)*1.622;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
if(2==__version)ledOff();
|
||||
return isnan(c)?-3:c;
|
||||
}
|
||||
|
||||
/*********************************************************************************************************
|
||||
** Function name: changeI2cAddr
|
||||
** Descriptions: change I2C address of the slave MCU, and this address will be stored in EEPROM of slave MCU
|
||||
*********************************************************************************************************/
|
||||
void MutichannelGasSensor::changeI2cAddr(uint8_t newAddr)
|
||||
{
|
||||
Wire.beginTransmission(i2cAddress); // transmit to device
|
||||
Wire.write(0x23); // sends one byte
|
||||
Wire.write(newAddr); // sends one byte
|
||||
Wire.endTransmission(); // stop transmitting
|
||||
i2cAddress = newAddr;
|
||||
}
|
||||
|
||||
/*********************************************************************************************************
|
||||
** Function name: doCalibrate
|
||||
** Descriptions: tell slave to do a calibration, it will take about 8s
|
||||
after the calibration, must reread the R0 values
|
||||
*********************************************************************************************************/
|
||||
void MutichannelGasSensor::doCalibrate(void)
|
||||
{
|
||||
|
||||
if(1 == __version)
|
||||
{
|
||||
START:
|
||||
|
||||
sendI2C(0x22);
|
||||
if(readR0() > 0)
|
||||
{
|
||||
for(int i=0; i<3; i++)
|
||||
{
|
||||
Serial.print(res0[i]);
|
||||
Serial.print('\t');
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
delay(5000);
|
||||
Serial.println("continue...");
|
||||
for(int i=0; i<3; i++)
|
||||
{
|
||||
Serial.print(res0[i]);
|
||||
Serial.print('\t');
|
||||
}
|
||||
Serial.println();
|
||||
goto START;
|
||||
}
|
||||
}
|
||||
else if(2 == __version)
|
||||
{
|
||||
unsigned int i, a0, a1, a2;
|
||||
while(1)
|
||||
{
|
||||
a0 = get_addr_dta(CH_VALUE_NH3);
|
||||
a1 = get_addr_dta(CH_VALUE_CO);
|
||||
a2 = get_addr_dta(CH_VALUE_NO2);
|
||||
|
||||
Serial.print(a0);
|
||||
Serial.print('\t');
|
||||
Serial.print(a1);
|
||||
Serial.print('\t');
|
||||
Serial.print(a2);
|
||||
Serial.println('\t');
|
||||
ledOn();
|
||||
|
||||
int cnt = 0;
|
||||
for(i=0; i<20; i++)
|
||||
{
|
||||
if((a0 - get_addr_dta(CH_VALUE_NH3)) > 2 || (get_addr_dta(CH_VALUE_NH3) - a0) > 2)cnt++;
|
||||
if((a1 - get_addr_dta(CH_VALUE_CO)) > 2 || (get_addr_dta(CH_VALUE_CO) - a1) > 2)cnt++;
|
||||
if((a2 - get_addr_dta(CH_VALUE_NO2)) > 2 || (get_addr_dta(CH_VALUE_NO2) - a2) > 2)cnt++;
|
||||
|
||||
if(cnt>5)
|
||||
{
|
||||
break;
|
||||
}
|
||||
delay(1000);
|
||||
}
|
||||
|
||||
ledOff();
|
||||
if(cnt <= 5)break;
|
||||
delay(200);
|
||||
}
|
||||
|
||||
Serial.print("write user adc value: ");
|
||||
Serial.print(a0);Serial.print('\t');
|
||||
Serial.print(a1);Serial.print('\t');
|
||||
Serial.print(a2);Serial.println('\t');
|
||||
|
||||
unsigned char tmp[7];
|
||||
|
||||
tmp[0] = 7;
|
||||
|
||||
tmp[1] = a0>>8;
|
||||
tmp[2] = a0&0xff;
|
||||
|
||||
tmp[3] = a1>>8;
|
||||
tmp[4] = a1&0xff;
|
||||
|
||||
tmp[5] = a2>>8;
|
||||
tmp[6] = a2&0xff;
|
||||
|
||||
write_i2c(i2cAddress, tmp, 7);
|
||||
}
|
||||
}
|
||||
|
||||
/*********************************************************************************************************
|
||||
** Function name: powerOn
|
||||
** Descriptions: power on sensor heater
|
||||
*********************************************************************************************************/
|
||||
void MutichannelGasSensor::powerOn(void)
|
||||
{
|
||||
if(__version == 1)
|
||||
sendI2C(0x21);
|
||||
else if(__version == 2)
|
||||
{
|
||||
dta_test[0] = 11;
|
||||
dta_test[1] = 1;
|
||||
write_i2c(i2cAddress, dta_test, 2);
|
||||
}
|
||||
}
|
||||
|
||||
/*********************************************************************************************************
|
||||
** Function name: powerOff
|
||||
** Descriptions: power off sensor heater
|
||||
*********************************************************************************************************/
|
||||
void MutichannelGasSensor::powerOff(void)
|
||||
{
|
||||
if(__version == 1)
|
||||
sendI2C(0x20);
|
||||
else if(__version == 2)
|
||||
{
|
||||
dta_test[0] = 11;
|
||||
dta_test[1] = 0;
|
||||
write_i2c(i2cAddress, dta_test, 2);
|
||||
}
|
||||
}
|
||||
|
||||
void MutichannelGasSensor::display_eeprom()
|
||||
{
|
||||
if(__version == 1)
|
||||
{
|
||||
Serial.println("ERROR: display_eeprom() is NOT support by V1 firmware.");
|
||||
return ;
|
||||
}
|
||||
|
||||
Serial.print("ADDR_IS_SET = "); Serial.println(get_addr_dta(CMD_READ_EEPROM, ADDR_IS_SET));
|
||||
Serial.print("ADDR_FACTORY_ADC_NH3 = "); Serial.println(get_addr_dta(CMD_READ_EEPROM, ADDR_FACTORY_ADC_NH3));
|
||||
Serial.print("ADDR_FACTORY_ADC_CO = "); Serial.println(get_addr_dta(CMD_READ_EEPROM, ADDR_FACTORY_ADC_CO));
|
||||
Serial.print("ADDR_FACTORY_ADC_NO2 = "); Serial.println(get_addr_dta(CMD_READ_EEPROM, ADDR_FACTORY_ADC_NO2));
|
||||
Serial.print("ADDR_USER_ADC_HN3 = "); Serial.println(get_addr_dta(CMD_READ_EEPROM, ADDR_USER_ADC_HN3));
|
||||
Serial.print("ADDR_USER_ADC_CO = "); Serial.println(get_addr_dta(CMD_READ_EEPROM, ADDR_USER_ADC_CO));
|
||||
Serial.print("ADDR_USER_ADC_NO2 = "); Serial.println(get_addr_dta(CMD_READ_EEPROM, ADDR_USER_ADC_NO2));
|
||||
Serial.print("ADDR_I2C_ADDRESS = "); Serial.println(get_addr_dta(CMD_READ_EEPROM, ADDR_I2C_ADDRESS));
|
||||
}
|
||||
|
||||
float MutichannelGasSensor::getR0(unsigned char ch) // 0:CH3, 1:CO, 2:NO2
|
||||
{
|
||||
if(__version == 1)
|
||||
{
|
||||
Serial.println("ERROR: getR0() is NOT support by V1 firmware.");
|
||||
return -1;
|
||||
}
|
||||
|
||||
int a = 0;
|
||||
switch(ch)
|
||||
{
|
||||
case 0: // CH3
|
||||
a = get_addr_dta(CMD_READ_EEPROM, ADDR_USER_ADC_HN3);
|
||||
Serial.print("a_ch3 = ");
|
||||
Serial.println(a);
|
||||
break;
|
||||
|
||||
case 1: // CO
|
||||
a = get_addr_dta(CMD_READ_EEPROM, ADDR_USER_ADC_CO);
|
||||
Serial.print("a_co = ");
|
||||
Serial.println(a);
|
||||
break;
|
||||
|
||||
case 2: // NO2
|
||||
a = get_addr_dta(CMD_READ_EEPROM, ADDR_USER_ADC_NO2);
|
||||
Serial.print("a_no2 = ");
|
||||
Serial.println(a);
|
||||
break;
|
||||
|
||||
default:;
|
||||
}
|
||||
|
||||
float r = 56.0*(float)a/(1023.0-(float)a);
|
||||
return r;
|
||||
}
|
||||
|
||||
float MutichannelGasSensor::getRs(unsigned char ch) // 0:CH3, 1:CO, 2:NO2
|
||||
{
|
||||
|
||||
if(__version == 1)
|
||||
{
|
||||
Serial.println("ERROR: getRs() is NOT support by V1 firmware.");
|
||||
return -1;
|
||||
}
|
||||
|
||||
int a = 0;
|
||||
switch(ch)
|
||||
{
|
||||
case 0: // NH3
|
||||
a = get_addr_dta(1);
|
||||
break;
|
||||
|
||||
case 1: // CO
|
||||
a = get_addr_dta(2);
|
||||
break;
|
||||
|
||||
case 2: // NO2
|
||||
a = get_addr_dta(3);
|
||||
break;
|
||||
|
||||
default:;
|
||||
}
|
||||
|
||||
float r = 56.0*(float)a/(1023.0-(float)a);
|
||||
return r;
|
||||
}
|
||||
|
||||
// 1. change i2c address to 0x04
|
||||
// 2. change adc value of R0 to default
|
||||
void MutichannelGasSensor::factory_setting()
|
||||
{
|
||||
|
||||
unsigned char tmp[7];
|
||||
|
||||
unsigned char error;
|
||||
unsigned char address = 0;
|
||||
|
||||
for(address = 1; address < 127; address++ )
|
||||
{
|
||||
// The i2c_scanner uses the return value of
|
||||
// the Write.endTransmisstion to see if
|
||||
// a device did acknowledge to the address.
|
||||
Wire.beginTransmission(address);
|
||||
error = Wire.endTransmission();
|
||||
|
||||
if (error == 0)
|
||||
{
|
||||
// change i2c to 0x04
|
||||
|
||||
Serial.print("I2C address is: 0x");
|
||||
Serial.println(address, HEX);
|
||||
Serial.println("Change I2C address to 0x04");
|
||||
|
||||
dta_test[0] = CMD_CHANGE_I2C;
|
||||
dta_test[1] = 0x04;
|
||||
write_i2c(address, dta_test, 2);
|
||||
|
||||
i2cAddress = 0x04;
|
||||
delay(100);
|
||||
getVersion();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
unsigned int a0 = get_addr_dta(CMD_READ_EEPROM, ADDR_FACTORY_ADC_NH3);
|
||||
unsigned int a1 = get_addr_dta(CMD_READ_EEPROM, ADDR_FACTORY_ADC_CO);
|
||||
unsigned int a2 = get_addr_dta(CMD_READ_EEPROM, ADDR_FACTORY_ADC_NO2);
|
||||
|
||||
tmp[0] = 7;
|
||||
tmp[1] = a0>>8;
|
||||
tmp[2] = a0&0xff;
|
||||
tmp[3] = a1>>8;
|
||||
tmp[4] = a1&0xff;
|
||||
|
||||
tmp[5] = a2>>8;
|
||||
tmp[6] = a2&0xff;
|
||||
delay(100);
|
||||
write_i2c(i2cAddress, tmp, 7);
|
||||
delay(100);
|
||||
}
|
||||
|
||||
void MutichannelGasSensor::change_i2c_address(unsigned char addr)
|
||||
{
|
||||
dta_test[0] = CMD_CHANGE_I2C;
|
||||
dta_test[1] = addr;
|
||||
write_i2c(i2cAddress, dta_test, 2);
|
||||
|
||||
|
||||
Serial.print("FUNCTION: CHANGE I2C ADDRESS: 0X");
|
||||
Serial.print(i2cAddress, HEX);
|
||||
Serial.print(" > 0x");
|
||||
Serial.println(addr, HEX);
|
||||
|
||||
i2cAddress = addr;
|
||||
}
|
||||
|
||||
MutichannelGasSensor gas;
|
||||
/*********************************************************************************************************
|
||||
END FILE
|
||||
*********************************************************************************************************/
|
151
ISS_Project/ESP32_script/MutichannelGasSensor.h
Normal file
151
ISS_Project/ESP32_script/MutichannelGasSensor.h
Normal file
|
@ -0,0 +1,151 @@
|
|||
/*
|
||||
MutichannelGasSensor.h
|
||||
2015 Copyright (c) Seeed Technology Inc. All right reserved.
|
||||
|
||||
Author: Jacky Zhang
|
||||
2015-3-17
|
||||
http://www.seeed.cc/
|
||||
modi by Jack, 2015-8
|
||||
|
||||
V2 by Loovee
|
||||
2016-11-11
|
||||
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2015 Seeed Technology Inc.
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef __MUTICHANNELGASSENSOR_H__
|
||||
#define __MUTICHANNELGASSENSOR_H__
|
||||
|
||||
#define DEFAULT_I2C_ADDR 0x04
|
||||
|
||||
#define ADDR_IS_SET 0 // if this is the first time to run, if 1126, set
|
||||
#define ADDR_FACTORY_ADC_NH3 2
|
||||
#define ADDR_FACTORY_ADC_CO 4
|
||||
#define ADDR_FACTORY_ADC_NO2 6
|
||||
|
||||
#define ADDR_USER_ADC_HN3 8
|
||||
#define ADDR_USER_ADC_CO 10
|
||||
#define ADDR_USER_ADC_NO2 12
|
||||
#define ADDR_IF_CALI 14 // IF USER HAD CALI
|
||||
|
||||
#define ADDR_I2C_ADDRESS 20
|
||||
|
||||
#define CH_VALUE_NH3 1
|
||||
#define CH_VALUE_CO 2
|
||||
#define CH_VALUE_NO2 3
|
||||
|
||||
#define CMD_ADC_RES0 1 // NH3
|
||||
#define CMD_ADC_RES1 2 // CO
|
||||
#define CMD_ADC_RES2 3 // NO2
|
||||
#define CMD_ADC_RESALL 4 // ALL CHANNEL
|
||||
#define CMD_CHANGE_I2C 5 // CHANGE I2C
|
||||
#define CMD_READ_EEPROM 6 // READ EEPROM VALUE, RETURN UNSIGNED INT
|
||||
#define CMD_SET_R0_ADC 7 // SET R0 ADC VALUE
|
||||
#define CMD_GET_R0_ADC 8 // GET R0 ADC VALUE
|
||||
#define CMD_GET_R0_ADC_FACTORY 9 // GET FACTORY R0 ADC VALUE
|
||||
#define CMD_CONTROL_LED 10
|
||||
#define CMD_CONTROL_PWR 11
|
||||
|
||||
enum{CO, NO2, NH3, C3H8, C4H10, CH4, H2, C2H5OH};
|
||||
|
||||
class MutichannelGasSensor{
|
||||
|
||||
private:
|
||||
|
||||
int __version;
|
||||
unsigned char dta_test[20];
|
||||
|
||||
unsigned int readChAdcValue(int ch);
|
||||
unsigned int adcValueR0_NH3_Buf;
|
||||
unsigned int adcValueR0_CO_Buf;
|
||||
unsigned int adcValueR0_NO2_Buf;
|
||||
|
||||
public:
|
||||
|
||||
uint8_t i2cAddress; //I2C address of this MCU
|
||||
uint16_t res0[3]; //sensors res0
|
||||
uint16_t res[3]; //sensors res
|
||||
bool r0_inited;
|
||||
|
||||
|
||||
inline unsigned int get_addr_dta(unsigned char addr_reg);
|
||||
inline unsigned int get_addr_dta(unsigned char addr_reg, unsigned char __dta);
|
||||
inline void write_i2c(unsigned char addr, unsigned char *dta, unsigned char dta_len);
|
||||
|
||||
void sendI2C(unsigned char dta);
|
||||
int16_t readData(uint8_t cmd);
|
||||
int16_t readR0(void);
|
||||
int16_t readR(void);
|
||||
float calcGas(int gas);
|
||||
|
||||
public:
|
||||
|
||||
void begin(int address);
|
||||
void begin();
|
||||
void changeI2cAddr(uint8_t newAddr);
|
||||
void powerOn(void);
|
||||
void powerOff(void);
|
||||
void doCalibrate(void);
|
||||
|
||||
//get gas concentration, unit: ppm
|
||||
float measure_CO(){return calcGas(CO);}
|
||||
float measure_NO2(){return calcGas(NO2);}
|
||||
float measure_NH3(){return calcGas(NH3);}
|
||||
float measure_C3H8(){return calcGas(C3H8);}
|
||||
float measure_C4H10(){return calcGas(C4H10);}
|
||||
float measure_CH4(){return calcGas(CH4);}
|
||||
float measure_H2(){return calcGas(H2);}
|
||||
float measure_C2H5OH(){return calcGas(C2H5OH);}
|
||||
|
||||
float getR0(unsigned char ch); // 0:CH3, 1:CO, 2:NO2
|
||||
float getRs(unsigned char ch); // 0:CH3, 1:CO, 2:NO2
|
||||
|
||||
public:
|
||||
|
||||
void ledOn()
|
||||
{
|
||||
dta_test[0] = CMD_CONTROL_LED;
|
||||
dta_test[1] = 1;
|
||||
write_i2c(i2cAddress, dta_test, 2);
|
||||
}
|
||||
|
||||
void ledOff()
|
||||
{
|
||||
dta_test[0] = CMD_CONTROL_LED;
|
||||
dta_test[1] = 0;
|
||||
write_i2c(i2cAddress, dta_test, 2);
|
||||
}
|
||||
|
||||
void display_eeprom();
|
||||
void factory_setting();
|
||||
void change_i2c_address(unsigned char addr);
|
||||
unsigned char getVersion();
|
||||
};
|
||||
|
||||
extern MutichannelGasSensor gas;
|
||||
|
||||
#endif
|
||||
|
||||
/*********************************************************************************************************
|
||||
END FILE
|
||||
*********************************************************************************************************/
|
133
ISS_Project/ESP32_script/dht.cpp
Normal file
133
ISS_Project/ESP32_script/dht.cpp
Normal file
|
@ -0,0 +1,133 @@
|
|||
//
|
||||
// FILE: dht22.cpp
|
||||
// VERSION: 0.1.00
|
||||
// PURPOSE: DHT22 Temperature & Humidity Sensor library for Arduino
|
||||
//
|
||||
// DATASHEET:
|
||||
//
|
||||
// HISTORY:
|
||||
// 0.1.0 by Rob Tillaart (01/04/2011)
|
||||
// inspired by DHT11 library
|
||||
//
|
||||
|
||||
#include "dht.h"
|
||||
|
||||
#define TIMEOUT 10000
|
||||
|
||||
/////////////////////////////////////////////////////
|
||||
//
|
||||
// PUBLIC
|
||||
//
|
||||
|
||||
|
||||
// return values:
|
||||
// 0 : OK
|
||||
// -1 : checksum error
|
||||
// -2 : timeout
|
||||
int dht::read11(uint8_t pin)
|
||||
{
|
||||
// READ VALUES
|
||||
int rv = read(pin);
|
||||
if (rv != 0) return rv;
|
||||
|
||||
// CONVERT AND STORE
|
||||
humidity = bits[0]; // bit[1] == 0;
|
||||
temperature = bits[2]; // bits[3] == 0;
|
||||
|
||||
// TEST CHECKSUM
|
||||
uint8_t sum = bits[0] + bits[2]; // bits[1] && bits[3] both 0
|
||||
if (bits[4] != sum) return -1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// return values:
|
||||
// 0 : OK
|
||||
// -1 : checksum error
|
||||
// -2 : timeout
|
||||
int dht::read22(uint8_t pin)
|
||||
{
|
||||
// READ VALUES
|
||||
int rv = read(pin);
|
||||
if (rv != 0) return rv;
|
||||
|
||||
// CONVERT AND STORE
|
||||
humidity = word(bits[0], bits[1]) * 0.1;
|
||||
|
||||
int sign = 1;
|
||||
if (bits[2] & 0x80) // negative temperature
|
||||
{
|
||||
bits[2] = bits[2] & 0x7F;
|
||||
sign = -1;
|
||||
}
|
||||
temperature = sign * word(bits[2], bits[3]) * 0.1;
|
||||
|
||||
|
||||
// TEST CHECKSUM
|
||||
uint8_t sum = bits[0] + bits[1] + bits[2] + bits[3];
|
||||
if (bits[4] != sum) return -1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////
|
||||
//
|
||||
// PRIVATE
|
||||
//
|
||||
|
||||
// return values:
|
||||
// 0 : OK
|
||||
// -2 : timeout
|
||||
int dht::read(uint8_t pin)
|
||||
{
|
||||
// INIT BUFFERVAR TO RECEIVE DATA
|
||||
uint8_t cnt = 7;
|
||||
uint8_t idx = 0;
|
||||
|
||||
// EMPTY BUFFER
|
||||
for (int i=0; i< 5; i++) bits[i] = 0;
|
||||
|
||||
// REQUEST SAMPLE
|
||||
pinMode(pin, OUTPUT);
|
||||
digitalWrite(pin, LOW);
|
||||
delay(20);
|
||||
digitalWrite(pin, HIGH);
|
||||
delayMicroseconds(40);
|
||||
pinMode(pin, INPUT);
|
||||
|
||||
// GET ACKNOWLEDGE or TIMEOUT
|
||||
unsigned int loopCnt = TIMEOUT;
|
||||
while(digitalRead(pin) == LOW)
|
||||
if (loopCnt-- == 0) return -2;
|
||||
|
||||
loopCnt = TIMEOUT;
|
||||
while(digitalRead(pin) == HIGH)
|
||||
if (loopCnt-- == 0) return -2;
|
||||
|
||||
// READ THE OUTPUT - 40 BITS => 5 BYTES
|
||||
for (int i=0; i<40; i++)
|
||||
{
|
||||
loopCnt = TIMEOUT;
|
||||
while(digitalRead(pin) == LOW)
|
||||
if (loopCnt-- == 0) return -2;
|
||||
|
||||
unsigned long t = micros();
|
||||
|
||||
loopCnt = TIMEOUT;
|
||||
while(digitalRead(pin) == HIGH)
|
||||
if (loopCnt-- == 0) return -2;
|
||||
|
||||
if ((micros() - t) > 40) bits[idx] |= (1 << cnt);
|
||||
if (cnt == 0) // next byte?
|
||||
{
|
||||
cnt = 7;
|
||||
idx++;
|
||||
}
|
||||
else cnt--;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
//
|
||||
// END OF FILE
|
||||
//
|
38
ISS_Project/ESP32_script/dht.h
Normal file
38
ISS_Project/ESP32_script/dht.h
Normal file
|
@ -0,0 +1,38 @@
|
|||
//
|
||||
// FILE: dht.h
|
||||
// VERSION: 0.1.00
|
||||
// PURPOSE: DHT Temperature & Humidity Sensor library for Arduino
|
||||
//
|
||||
// URL: http://arduino.cc/playground/Main/DHTLib
|
||||
//
|
||||
// HISTORY:
|
||||
// see dht.cpp file
|
||||
//
|
||||
|
||||
#ifndef dht_h
|
||||
#define dht_h
|
||||
|
||||
#if ARDUINO >= 100
|
||||
#include "Arduino.h"
|
||||
#else
|
||||
#include "WProgram.h"
|
||||
#endif
|
||||
|
||||
#define DHT_LIB_VERSION "0.1.00"
|
||||
|
||||
class dht
|
||||
{
|
||||
public:
|
||||
int read11(uint8_t pin);
|
||||
int read22(uint8_t pin);
|
||||
double humidity;
|
||||
double temperature;
|
||||
|
||||
private:
|
||||
uint8_t bits[5]; // buffer to receive data
|
||||
int read(uint8_t pin);
|
||||
};
|
||||
#endif
|
||||
//
|
||||
// END OF FILE
|
||||
//
|
BIN
ISS_Project/Mobile_App.rar
Normal file
BIN
ISS_Project/Mobile_App.rar
Normal file
Binary file not shown.
Loading…
Reference in a new issue