|
| 1 | +/** |
| 2 | + * @file Wemos - Advanced Example |
| 3 | + * This example reads data from a sensor and sends it to the hackAIR platform |
| 4 | + * using the Wemos integrated WiFi on a configurable frequency. This code |
| 5 | + * assumes a DHT11 humidity sensor connected to pin 2. |
| 6 | + * |
| 7 | + * @author Ilias Stavrakas |
| 8 | + * @author Thanasis Georgiou (Cleanup) |
| 9 | + * |
| 10 | + * This example is part of the hackAIR Arduino Library and is available |
| 11 | + * in the Public Domain. |
| 12 | + */ |
| 13 | + |
| 14 | +#include <Arduino.h> |
| 15 | +#include <DHT.h> |
| 16 | +#include <DHT_U.h> |
| 17 | +#include <DNSServer.h> |
| 18 | +#include <ESP8266WebServer.h> |
| 19 | +#include <ESP8266WiFi.h> |
| 20 | +#include <FS.h> |
| 21 | +#include <WiFiClientSecure.h> |
| 22 | +#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager |
| 23 | +#include <hackair.h> |
| 24 | + |
| 25 | +// Replace the string below with your Authorization Token from the hackAIR |
| 26 | +// platform |
| 27 | +#define AUTHORIZATION "AUTHORIZATION TOKEN HERE" |
| 28 | + |
| 29 | +// Sensor initialization |
| 30 | +hackAIR sensor(SENSOR_SDS011); |
| 31 | + |
| 32 | +// Setup the humidity sensor (pin 2) |
| 33 | +DHT dht(2, DHT11); |
| 34 | + |
| 35 | +// How often to measure (in minutes) |
| 36 | +const unsigned long minutes_time_interval = 120; |
| 37 | + |
| 38 | +// Setup ADC to measure Vcc (battery voltage) |
| 39 | +ADC_MODE(ADC_VCC); |
| 40 | + |
| 41 | +// Create a secure client for sending data using HTTPs |
| 42 | +WiFiClientSecure client; |
| 43 | + |
| 44 | +// Struct for storing sensor data |
| 45 | +struct hackAirData data; |
| 46 | + |
| 47 | +unsigned long previous_millis = 0; |
| 48 | +void setup() { |
| 49 | + // Open serial communications |
| 50 | + Serial.begin(9600); |
| 51 | + |
| 52 | + // Initialize the PM sensor |
| 53 | + sensor.begin(); |
| 54 | + sensor.enablePowerControl(); |
| 55 | + sensor.turnOn(); |
| 56 | + |
| 57 | + sensor.clearData(data); |
| 58 | + |
| 59 | + // Initialize temperature and humidity sensor |
| 60 | + dht.begin(); |
| 61 | + |
| 62 | + // Initialize the WiFi connection |
| 63 | + WiFiManager wifiManager; |
| 64 | + if (!wifiManager.autoConnect("hackAIR-AP")) { |
| 65 | + Serial.println("failed to connect, please push reset button and try again"); |
| 66 | + delay(3000); |
| 67 | + ESP.reset(); |
| 68 | + delay(10000); |
| 69 | + } |
| 70 | + // check if we have connected to the WiFi |
| 71 | + Serial.println("network connected:)"); |
| 72 | + Serial.println("Your local ip is:"); |
| 73 | + Serial.println(WiFi.localIP()); |
| 74 | +} |
| 75 | + |
| 76 | +void loop() { |
| 77 | + float vdd = ESP.getVcc() / 500.0; |
| 78 | + while (WiFi.status() != WL_CONNECTED) { |
| 79 | + delay(500); |
| 80 | + Serial.print("."); |
| 81 | + } |
| 82 | + |
| 83 | + // Measure data |
| 84 | + sensor.clearData(data); |
| 85 | + sensor.refresh(data); |
| 86 | + |
| 87 | + // Average readings (60 measurments) |
| 88 | + double pm25 = data.pm25; |
| 89 | + double pm10 = data.pm10; |
| 90 | + int error = 0; |
| 91 | + for (int i = 0; i < 59; i++) { |
| 92 | + // Read from the sensor |
| 93 | + sensor.refresh(data); |
| 94 | + |
| 95 | + // If error is not zero something went wrong with this measurment |
| 96 | + // and we should not send it. |
| 97 | + if (data.error == 0) { |
| 98 | + pm25 = (pm25 + data.pm25) / 2; |
| 99 | + pm10 = (pm10 + data.pm10) / 2; |
| 100 | + } else { |
| 101 | + error++; |
| 102 | + } |
| 103 | + delay(1000); // Wait one second |
| 104 | + } |
| 105 | + data.pm25 = pm25; |
| 106 | + data.pm10 = pm10; |
| 107 | + data.error = error; |
| 108 | + |
| 109 | + // Measure humidity and temperature |
| 110 | + float humidity = dht.readHumidity(); |
| 111 | + // Read temperature as Celsius (the default) |
| 112 | + float temperature = dht.readTemperature(); |
| 113 | + |
| 114 | + // Send the data to the hackAIR server |
| 115 | + String dataJson = "{\"reading\":{\"PM2.5_AirPollutantValue\":\""; |
| 116 | + dataJson += pm25; |
| 117 | + dataJson += "\",\"PM10_AirPollutantValue\":\""; |
| 118 | + dataJson += pm10; |
| 119 | + dataJson += "\"},\"battery\":\""; |
| 120 | + dataJson += vdd; |
| 121 | + dataJson += "\",\"tamper\":\""; |
| 122 | + dataJson += "0"; |
| 123 | + dataJson += "\",\"error\":\""; |
| 124 | + dataJson += "0"; |
| 125 | + dataJson += "\"}"; |
| 126 | + if (client.connect("api.hackair.eu", 443)) { |
| 127 | + Serial.println("connected"); |
| 128 | + client.print("POST /sensors/arduino/measurements HTTP/1.1\r\n"); |
| 129 | + client.print("Host: api.hackair.eu\r\n"); |
| 130 | + client.print("Connection: close\r\n"); |
| 131 | + client.print("Authorization: "); |
| 132 | + client.println(Authorization); |
| 133 | + client.print("Accept: application/vnd.hackair.v1+json\r\n"); |
| 134 | + client.print("Cache-Control: no-cache\r\n"); |
| 135 | + client.print("Content-Type: application/json\r\n"); |
| 136 | + client.print("Content-Length: "); |
| 137 | + client.println(dataJson.length() + 2); |
| 138 | + client.println(""); |
| 139 | + client.println(dataJson); |
| 140 | + Serial.println(dataJson); |
| 141 | + delay(500); |
| 142 | + while (client.available()) { |
| 143 | + char c = client.read(); |
| 144 | + Serial.print(c); |
| 145 | + } |
| 146 | + client.stop(); |
| 147 | + } |
| 148 | + delay(1000); |
| 149 | + |
| 150 | + // Turn off sensor and go to sleep |
| 151 | + sensor.turnOff(); |
| 152 | + unsigned long current_millis = millis(); |
| 153 | + while (current_millis < |
| 154 | + (previous_millis + (minutes_time_interval * 60 * 1000))) { |
| 155 | + delay(10000); |
| 156 | + current_millis = millis(); |
| 157 | + Serial.println(current_millis); |
| 158 | + } |
| 159 | + previous_millis = current_millis; |
| 160 | + sensor.turnOn(); |
| 161 | +} |
0 commit comments