1+ /*
2+ * Example for how to use SinricPro Air Quality Sensor with Sharp Dust Sensor (GP2Y1014AU0F) connected to WemosD1 Mini
3+ * More information is here
4+ * https://github.com/sharpsensoruser/sharp-sensor-demos/wiki/Application-Guide-for-Sharp-GP2Y1014AU0F-Dust-Sensor
5+ *
6+ * If you encounter any issues:
7+ * - check the readme.md at https://github.com/sinricpro/esp8266-esp32-sdk/blob/master/README.md
8+ * - ensure all dependent libraries are installed
9+ * - see https://github.com/sinricpro/esp8266-esp32-sdk/blob/master/README.md#arduinoide
10+ * - see https://github.com/sinricpro/esp8266-esp32-sdk/blob/master/README.md#dependencies
11+ * - open serial monitor and check whats happening
12+ * - check full user documentation at https://sinricpro.github.io/esp8266-esp32-sdk
13+ * - visit https://github.com/sinricpro/esp8266-esp32-sdk/issues and check for existing issues or open a new one
14+ */
15+
16+ // Uncomment the following line to enable serial debug output
17+ // #define ENABLE_DEBUG
18+
19+ #ifdef ENABLE_DEBUG
20+ #define DEBUG_ESP_PORT Serial
21+ #define NODEBUG_WEBSOCKETS
22+ #define NDEBUG
23+ #endif
24+
25+ #include < GP2YDustSensor.h> // https://github.com/luciansabo/GP2YDustSensor
26+
27+ #include < Arduino.h>
28+ #ifdef ESP8266
29+ #include < ESP8266WiFi.h>
30+ #endif
31+ #ifdef ESP32
32+ #include < WiFi.h>
33+ #endif
34+
35+ #include " SinricPro.h"
36+ #include " SinricProAirQualitySensor.h"
37+
38+ #define WIFI_SSID " "
39+ #define WIFI_PASS " "
40+ #define APP_KEY " " // Should look like "de0bxxxx-1x3x-4x3x-ax2x-5dabxxxxxxxx"
41+ #define APP_SECRET " " // Should look like "5f36xxxx-x3x7-4x3x-xexe-e86724a9xxxx-4c4axxxx-3x3x-x5xe-x9x3-333d65xxxxxx"
42+ #define DEVICE_ID " " // Should look like "5dc1564130xxxxxxxxxxxxxx"
43+ #define BAUD_RATE 9600 // Change baudrate to your need
44+
45+ // Air quality sensor event dispatch time. Min is every 1 min.
46+ #define MIN (1000UL * 60 * 1 )
47+ unsigned long dispatchTime = millis() + MIN;
48+
49+ const uint8_t SHARP_LED_PIN = D5; // Sharp Dust/particle sensor Led Pin
50+ const uint8_t SHARP_VO_PIN = A0; // Sharp Dust/particle analog out pin used for reading
51+
52+ GP2YDustSensor dustSensor (GP2YDustSensorType::GP2Y1014AU0F, SHARP_LED_PIN, SHARP_VO_PIN);
53+
54+ // setup function for WiFi connection
55+ void setupWiFi () {
56+ Serial.printf (" \r\n [Wifi]: Connecting" );
57+ WiFi.begin (WIFI_SSID, WIFI_PASS);
58+
59+ while (WiFi.status () != WL_CONNECTED) {
60+ Serial.printf (" ." );
61+ delay (250 );
62+ }
63+ Serial.printf (" connected!\r\n [WiFi]: IP-Address is %s\r\n " , WiFi.localIP ().toString ().c_str ());
64+ }
65+
66+ // setup function for SinricPro
67+ void setupSinricPro () {
68+ // add device to SinricPro
69+ SinricProAirQualitySensor& mySinricProAirQualitySensor = SinricPro[DEVICE_ID];
70+
71+ // set callback function to device
72+
73+
74+ // setup SinricPro
75+ SinricPro.onConnected ([](){ Serial.printf (" Connected to SinricPro\r\n " ); });
76+ SinricPro.onDisconnected ([](){ Serial.printf (" Disconnected from SinricPro\r\n " ); });
77+ SinricPro.begin (APP_KEY, APP_SECRET);
78+ }
79+
80+ void setupDustSensor () {
81+ // dustSensor.setBaseline(0.4); // set no dust voltage according to your own experiments
82+ // dustSensor.setCalibrationFactor(1.1); // calibrate against precision instrument
83+ dustSensor.begin ();
84+ }
85+
86+
87+ void setup () {
88+ Serial.begin (BAUD_RATE); Serial.printf (" \r\n\r\n " );
89+ setupWiFi ();
90+ setupSinricPro ();
91+ setupDustSensor ();
92+ }
93+
94+
95+ void loop () {
96+ SinricPro.handle ();
97+
98+ if ((long )(millis () - dispatchTime) >= 0 ) {
99+ Serial.print (" Dust density: " );
100+ Serial.print (dustSensor.getDustDensity ());
101+ Serial.print (" ug/m3; Running average: " );
102+ Serial.print (dustSensor.getRunningAverage ());
103+ Serial.println (" ug/m3" );
104+
105+ SinricProAirQualitySensor &mySinricProAirQualitySensor = SinricPro[DEVICE_ID]; // get air q sensor device
106+
107+ int pm1=0 ;
108+ int pm2_5 = dustSensor.getRunningAverage ();
109+ int pm10=0 ;
110+
111+ mySinricProAirQualitySensor.sendAirQualityEvent (pm1, pm2_5, pm10, " PERIODIC_POLL" );
112+ dispatchTime += MIN;
113+
114+ Serial.println (" Sending Air Quality event .." );
115+ }
116+ }
0 commit comments