|
| 1 | +/** |
| 2 | + * @file Ethernet - Advanced Example |
| 3 | + * This example sets up the Ethernet shield and sends data to the hackAIR |
| 4 | + * platform on a configurable frequency. The sensor is turned off while not |
| 5 | + * measuring to extend lifespan. For a simpler example check out |
| 6 | + * `Ethernet-Simple`. |
| 7 | + * |
| 8 | + * @author Thanasis Georgiou |
| 9 | + * |
| 10 | + * This example is part of the hackAIR Arduino Library and is available |
| 11 | + * in the Public Domain. |
| 12 | + */ |
| 13 | + |
| 14 | +#include <Ethernet.h> |
| 15 | +#include "hackair.h" |
| 16 | +#include "hackair_ethernet.h" |
| 17 | + |
| 18 | +// How often to measure (in minutes) |
| 19 | +#define MEASURING_DELAY 60 |
| 20 | + |
| 21 | +// MAC Address of the Ethernet Shield |
| 22 | +// Newer shields should have a MAC address printed on a sticker |
| 23 | +byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; |
| 24 | + |
| 25 | +// Initialize the Ethernet client library and the hackAir helper class |
| 26 | +EthernetClient client; |
| 27 | +// Write your API key here |
| 28 | +hackAirEthernet hackAirNet(client, "AUTHORIZATION TOKEN"); |
| 29 | + |
| 30 | +// Specify your sensor |
| 31 | +hackAIR sensor(SENSOR_SDS011); |
| 32 | + |
| 33 | +void setup() { |
| 34 | + // Open serial communications and wait for port to open |
| 35 | + Serial.begin(9600); |
| 36 | + while (!Serial) {;} |
| 37 | + |
| 38 | + // Start the ethernet connection |
| 39 | + if (Ethernet.begin(mac) == 0) { |
| 40 | + Serial.println("Failed to configure Ethernet using DHCP"); |
| 41 | + while (true) {;} |
| 42 | + } |
| 43 | + |
| 44 | + // Give the Ethernet shield a second to initialize |
| 45 | + delay(1000); |
| 46 | + |
| 47 | + // Initialize the sensor |
| 48 | + sensor.enablePowerControl(); |
| 49 | + sensor.turnOn(); |
| 50 | + sensor.begin(); |
| 51 | +} |
| 52 | + |
| 53 | +void loop() { |
| 54 | + // At this point, we are either booting or after the sleep period |
| 55 | + // Wait for the sensor to settle |
| 56 | + delay(1000 * 10); |
| 57 | + |
| 58 | + // Check DHCP lease |
| 59 | + // This is required to maintain the internet connection for long periods of time |
| 60 | + Ethernet.maintain(); |
| 61 | + |
| 62 | + // Collect data |
| 63 | + struct hackAirData data; |
| 64 | + sensor.clearData(data); |
| 65 | + sensor.refresh(data); |
| 66 | + |
| 67 | + // Average readings |
| 68 | + double pm25 = data.pm25; |
| 69 | + double pm10 = data.pm10; |
| 70 | + int error = 0; |
| 71 | + |
| 72 | + // We will take 60 averages |
| 73 | + for (int i = 0; i < 60; i++) { |
| 74 | + // Read from the sensor |
| 75 | + sensor.refresh(data); |
| 76 | + |
| 77 | + // If error is not zero something went wrong with this measurment |
| 78 | + // and we should not send it. |
| 79 | + if (data.error == 0) { |
| 80 | + // Calculate average between the new reading and the old average |
| 81 | + pm25 = (pm25 + data.pm25) / 2; |
| 82 | + pm10 = (pm10 + data.pm10) / 2; |
| 83 | + } else { |
| 84 | + error++; |
| 85 | + } |
| 86 | + |
| 87 | + delay(1000); // Wait one second |
| 88 | + } |
| 89 | + |
| 90 | + // Send with ethernet shield |
| 91 | + data.pm25 = pm25; |
| 92 | + data.pm10 = pm10; |
| 93 | + data.error = error; |
| 94 | + hackAirNet.sendData(data); |
| 95 | + |
| 96 | + // Turn off sensor while we wait the specified time |
| 97 | + sensor.turnOff(); |
| 98 | + delay(MEASURING_DELAY * 60UL * 1000UL); |
| 99 | + sensor.turnOn(); |
| 100 | +} |
0 commit comments