Just a very short post concerning the main firmware. You can find the sketch in my GitHub. There are two sketches, CalibrateSniffingTrinket.ino for the calibration of the MQ135 sensor (see last post) and SniffingTrinket.ino as the general purpose sketch. You need a couple of libraries to get the sketches running:
- Adafruit NeoPixel Library
- Adafruit DHT11 Library
- A patched version of the Adafruit Trinket USB library (for the USB keyboard functionality)
- My MQ135 Library
Just install the libraries, compile the code and upload it to the trinket. I won't go much into details of the code, as the code itself is heavily commented and you should be pretty easily able to understand it. I will just give quick overview of the main loop of the SniffingTrinket.ino
At the beginning of the loop the sensor values are read out.
// 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(); // Read temperature as Celsius float t = dht.readTemperature(); // Read temperature as Fahrenheit - in case you still don't use the SI //float f = dht.readTemperature(true); // Check if any reads failed and exit early (to try again). if (isnan(h) || isnan(t)) { Serial.println("Failed to read from DHT sensor!"); return; } // Read out the Gas Sensor float ppm = gasSensor.getPPM();
The next step is setting the LEDs to the correct color value. This is done by calling a dedicated function for each LED.
// Set the LEDs accordingly setLedOutput(t, TEMPLED); setLedOutput(h, HUMLED); setLedOutput(ppm, AIRLED);Check if we have to sound the alarm
// Check if the alarm level has been reached
if(t > TEMPALARM ||
h > HUMALARM ||
ppm > CO2ALARM)
tone(ALARMPIN, 5000, 1000);
or if the button has been pressed.// Read if the button was pressed buttonState = digitalRead(BUTTONPIN); if(!buttonState) setMode();Thats about it. Now we just output some text to the serial/USB keyboard interface and redo this, forever (or till the batteries run out).
Have a look at the code and let me know if there is something that needs to be qualified. I have still some things in mind I want to change on the code, so have a look at the repository now and then!
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.