vincent.vandeputte.60@gmail.com /*This test code is write for Arduino AVR Series(UNO, Leonardo, Mega) If you want to use with LinkIt ONE, please connect the module to D0/1 and modify: // #include // SoftwareSerial s_serial(2, 3); // TX, RX #define sensor Serial1 */ #include SoftwareSerial s_serial(2, 3); // TX, RX #define sensor s_serial const unsigned char cmd_get_sensor[] = { 0xff, 0x01, 0x86, 0x00, 0x00, 0x00, 0x00, 0x00, 0x79 }; unsigned char dataRevice[9]; int temperature; int CO2PPM; bool dataRecieve(void) { byte data[9]; int i = 0; //transmit command data for(i = 0; i < sizeof(cmd_get_sensor); i++) { sensor.write(cmd_get_sensor[i]); } delay(10); //begin reveiceing data if(sensor.available()) { while(sensor.available()) { for(int i=0;i<9; i++) { data[i] = sensor.read(); } } } if((i != 9) || (1 + (0xFF ^ (byte)(data[1] + data[2] + data[3] + data[4] + data[5] + data[6] + data[7]))) != data[8]) { return false; } CO2PPM = (int)data[2] * 256 + (int)data[3]; temperature = (int)data[4] - 40; return true; } // Example testing sketch for various DHT humidity/temperature sensors // Written by ladyada, public domain #include "DHT.h" #define DHTPIN A0 // what pin we're connected to // Uncomment whatever type you're using! //#define DHTTYPE DHT11 // DHT 11 #define DHTTYPE DHT22 // DHT 22 (AM2302) //#define DHTTYPE DHT21 // DHT 21 (AM2301) // Connect pin 1 (on the left) of the sensor to +5V // Connect pin 2 of the sensor to whatever your DHTPIN is // Connect pin 4 (on the right) of the sensor to GROUND // Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor DHT dht(DHTPIN, DHTTYPE); void setup() { Serial.begin(115200); Serial.println("DHTxx test!"); dht.begin(); sensor.begin(9600); Serial.begin(115200); Serial.println("get a 'g', begin to read from sensor!"); Serial.println("********************************************************"); Serial.println(); } void loop() { // Reading temperature or humidity takes about 250 milliseconds! // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor) float h = dht.readHumidity(); float t = dht.readTemperature(); // check if returns are valid, if they are NaN (not a number) then something went wrong! if (isnan(h)) Serial.println("Failed to read from DHT"); else { if(dataRecieve()) { Serial.print("{");Serial.print(t);Serial.print(",");Serial.print(CO2PPM);Serial.print(", ");Serial.print(h);Serial.println("}|"); } else Serial.println("Failed to read from MH-Z16"); } delay(1000); }