Skip to content

Commit 61fd0f5

Browse files
committed
Add advanced example for the hackAIR WiFi shield
1 parent 0873c7a commit 61fd0f5

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/**
2+
* @file hackAIR WiFi Shield - Advanced Example
3+
* This example reads data from a sensor and sends it to the hackAIR platform
4+
* using the hackAIR WiFi Shield on a configurable frequency.
5+
*
6+
* @author Thanasis Georgiou
7+
*
8+
* This example is part of the hackAIR Arduino Library and is available
9+
* in the Public Domain.
10+
*/
11+
12+
#include <hackair.h>
13+
#include <hackair_wifi.h>
14+
15+
// How often to measure (in minutes)
16+
#define MEASURING_DELAY 60
17+
18+
// Define sensor
19+
hackAIR sensor(SENSOR_DFROBOT);
20+
21+
// Struct for storing data
22+
struct hackAirData data;
23+
24+
void setup() {
25+
// Initialize the sensor
26+
sensor.enablePowerControl();
27+
sensor.turnOn();
28+
sensor.begin();
29+
sensor.clearData(data);
30+
31+
// Boot WiFi module
32+
wifi_begin();
33+
// Wait for WiFi connection
34+
// At this point you should use your mobile phone to setup the WiFi connection
35+
wifi_waitForReady();
36+
// Set authentication token
37+
wifi_setToken("AUTHORIZATION TOKEN");
38+
}
39+
40+
void loop() {
41+
// At this point we are either starting up for the first time or after a
42+
// sleep period.
43+
// Wait for sensor to settle
44+
delay(1000 * 30);
45+
46+
// Refresh data
47+
sensor.refresh(data);
48+
49+
// Average readings
50+
double pm25 = data.pm25;
51+
double pm10 = data.pm10;
52+
int error = 0;
53+
54+
// We will take 60 averages
55+
for (int i = 0; i < 60; i++) {
56+
// Read from the sensor
57+
sensor.refresh(data);
58+
59+
// If error is not zero something went wrong with this measurment
60+
// and we should not send it.
61+
if (data.error == 0) {
62+
// Calculate average between the new reading and the old average
63+
pm25 = (pm25 + data.pm25) / 2;
64+
pm10 = (pm10 + data.pm10) / 2;
65+
} else {
66+
error++;
67+
}
68+
69+
delay(1000); // Wait one second
70+
}
71+
72+
// Send data to the hackAIR server
73+
data.pm25 = pm25;
74+
data.pm10 = pm10;
75+
data.error = error;
76+
wifi_sendData(data);
77+
78+
// Turn off sensor while we wait the specified time
79+
sensor.turnOff();
80+
delay(MEASURING_DELAY * 60 * 1000UL);
81+
sensor.turnOn();
82+
}

0 commit comments

Comments
 (0)