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
| #include <SoftwareSerial.h>
#include <Wire.h>
#include "rgb_lcd.h" // Bibliothèque pour faire fonctionner l'afficheur LCD
rgb_lcd afficheur; // Création de l'objet afficheur
const int pinRx = 8;
const int pinTx = 7;
SoftwareSerial sensor(pinTx,pinRx);
unsigned char flg_get = 0; // if get sensor data
const unsigned char cmd_get_sensor[] = {
0xff, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79};
bool sendCmdGetDta(int *gas_strength, int *temperature)
{
for(int i=0; i<sizeof(cmd_get_sensor); i++)
{
sensor.write(cmd_get_sensor[i]);
}
long cnt_timeout = 0;
while(!sensor.available()) // wait for data
{
delay(1);
cnt_timeout++;
if(cnt_timeout>1000)return 0; // time out
}
int len = 0;
unsigned char dta[20];
while(sensor.available())
{
dta[len++] = sensor.read();
}
if((9 == len) && (0xff == dta[0]) && (0x86 == dta[1])) // data ok
{
*gas_strength = 256*dta[2]+dta[3];
*temperature = dta[4] - 40;
return 1;
}
return 0;
}
void setup()
{
Serial.begin(115200);
sensor.begin(9600);
pinMode(3,OUTPUT); // diode sur le port D3
pinMode(4,OUTPUT); // Buzzer sur le port D4
afficheur.begin(16, 2); // initialisation de l'objet afficheur
afficheur.setRGB(255,255,255); // couleur de fond de l'afficheur : Blanc = R + V + B
}
void loop()
{
flg_get = 0;
int gas, temp;
if(sendCmdGetDta(&gas, &temp)) // get data ok
{
Serial.print("gas_strength = ");
Serial.println(gas);
Serial.print("\ttemperature = ");
Serial.println(temp);
}
else
{
Serial.println("get data error");
}
//Afficheur
afficheur.clear(); // Remise à zéro de l'afficheur
afficheur.print("Tx CO2: "); // Affichage sur l'écran "Taux de CO2:"
afficheur.print(gas); // Affichage de la valeur
afficheur.print("ppm"); // ppm
afficheur.setCursor(0,1); // On se met sur la ligne du bas à gauche
afficheur.print("Temp: "); // Affichage sur l'écran "Temp: "
afficheur.print(temp); // temp
afficheur.print(" C"); // °C
if (gas > 1500)
{
digitalWrite(3, HIGH); // Allume la diode sur le port D3
digitalWrite(4, HIGH); // Allume le buzzer sur le port D4
afficheur.setRGB(255,0,0); // Le fond d'écran de l'afficheur est rouge
}
else
{
digitalWrite(3, LOW); // Eteint la diode sur le port D3
digitalWrite(4, LOW); // Eteint le buzzer sur le port D4
afficheur.setRGB(255,255,255); // Le fond d'écran de l'afficheur est blanc
}
delay(1000);
}
void serialEvent()
{
while (Serial.available())
{
char c = Serial.read();
if(c == 'g')flg_get = 1;
}
} |
Partager