Skip to content

Instantly share code, notes, and snippets.

@ItKindaWorks
Created July 19, 2018 00:49
Show Gist options
  • Save ItKindaWorks/04b7e2442dee6be2082ab9de30d5dc4c to your computer and use it in GitHub Desktop.
Save ItKindaWorks/04b7e2442dee6be2082ab9de30d5dc4c to your computer and use it in GitHub Desktop.
Demo code for using a waveshare/sharp dust sensor with an ESP8266
/*
dustSensorDemo.ino
Copyright (c) 2018 ItKindaWorks All right reserved.
github.com/ItKindaWorks
dustSensorDemo.ino is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
dustSensorDemo.ino is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with dustSensorDemo.ino. If not, see <http://www.gnu.org/licenses/>.
*/
#include "ESPHelper.h"
#include <Metro.h>
#include "WaveshareSharpDustSensor.h"
//setup macros for time
#define SECOND 1000L
#define MINUTE (SECOND * 60L)
#define HOUR (MINUTE * 60L)
netInfo homeNet = { .mqttHost = "YOUR MQTT-IP", //can be blank if not using MQTT
.mqttUser = "YOUR MQTT USERNAME", //can be blank
.mqttPass = "YOUR MQTT PASSWORD", //can be blank
.mqttPort = 1883, //default port for MQTT is 1883 - only chance if needed.
.ssid = "YOUR SSID",
.pass = "YOUR NETWORK PASS"};
ESPHelper myESP(&homeNet);
//mqtt topic to post dust readings to
const char* postTopic = "/home/dust";
//pin defs
const int sensorLED = D1;
const int sensorInPin = A0;
//sensor instance
WaveshareSharpDustSensor sensor;
//timer interval for posting to mqtt
Metro sensorPostTimer = Metro(10 * SECOND);
void setup() {
Serial.begin(115200); //start the serial line
delay(500);
pinMode(sensorLED, OUTPUT);
pinMode(sensorInPin, INPUT);
digitalWrite(sensorLED, LOW);
//start ESPHelper
Serial.println("Starting Up, Please Wait...");
myESP.begin();
Serial.println("Initialization Finished.");
}
void loop(){
//if connected and the timer to send the data has gone off then trigger a reading
if(myESP.loop() == FULL_CONNECTION && sensorPostTimer.check()){
//power up the sensor LED and wait the required 280 uS
digitalWrite(sensorLED, HIGH);
delayMicroseconds(280);
//take a raw ADC reading from the dust sensor
int adcvalue = analogRead(sensorInPin);
//Shut off the sensor LED
digitalWrite(sensorLED, LOW); // turn the LED off
//filter and convert from ADC reading to density (ug/m3)
adcvalue = sensor.Filter(adcvalue);
float density = sensor.Conversion(adcvalue);
//display the result in serial
Serial.print("The current dust concentration is: ");
Serial.print(density);
Serial.print(" ug/m3\n");
//post the result to mqtt
char postStr[10];
String tmp = String(density);
tmp.toCharArray(postStr, sizeof(postStr));
myESP.publish(postTopic, postStr);
}
delay(10);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment