1 #include "application.h"
4 SYSTEM_MODE(SEMI_AUTOMATIC)
9 #define DHTPIN 2 // Data pin
10 #define PWRPIN D5 // Power pin
11 #define ERRPIN D7 // Error pin
12 #define DHTTYPE DHT22 // DHT 22 (AM2302)
14 #define SLEEP_TIME 15 // Sleep time in seconds
15 #define RETRY_SLEEP_TIME 5 // Sleep time in seconds
16 #define RETRY_TIME 10000 // stop trying after 10 seconds
17 #define CONNECTION_DELAY 2100000 // Need to delay after connecting WiFi to wait for sensor
19 // Initialize DHT sensor for normal 16mhz Arduino
20 DHT dht(DHTPIN, DHTTYPE);
30 // 1) This code reads from the sensor and publishes
31 // the results on the cloud.
32 // 2) If you want to use the Serial library,
33 // please install "screen" on your machine and run
34 // it on the serial port right after flashing the
35 // firmware onto the Particle board.
36 // e.g. sudo screen /dev/ttyACM0
38 // We use one I/O pin to power the sensor so
39 // that we can make it go to sleep when we go into
44 //Serial.print("Time begin setup: ");
45 //Serial.println(micros());
47 pinMode(PWRPIN, OUTPUT);
48 digitalWrite(PWRPIN, HIGH);
51 // Connect to WiFi and Particle cloud
52 // Wait for a maximum amount of time - sleep if WiFi is not connected
53 // Wake up and try again afterwards
54 // TODO: either use this or just WiFi connection (below)
56 if (!waitFor(Particle.connected, RETRY_TIME))
57 System.sleep(SLEEP_MODE_DEEP, RETRY_SLEEP_TIME);
59 // Prepare device key from MAC (just last 2 of 6 digits)
63 // TODO: Uncomment the following block to print MAC
64 //for (int i=0; i<6; i++) {
65 // Serial.printf("%02x%s", mac[i], i != 5 ? ":" : "");
69 int64_t mac4 = (int64_t) mac[4];
70 int64_t mac5 = (int64_t) mac[5];
71 machineId = (mac4 * 256) + mac5;
72 //machineId = (int64_t) mac[5]; // Shorter version of machine ID
75 //Serial.print("Time end setup: ");
76 //Serial.println(micros());
80 // Wait until sensor is ready
83 //Serial.print("Time begin loop: ");
84 //Serial.println(micros());
86 float humid = dht.readHumidity();
87 // Read temperature as Fahrenheit
88 float tempF = dht.readTemperature(true);
89 // Check if any reads failed and exit early (to try again).
90 if (isnan(humid) || isnan(tempF))
94 sprintf(keyBuffer, "%04x", machineId);
95 // Humidity + Temperature
96 sprintf(dataBuffer, "%0.2f-%0.2f", humid, tempF);
98 // Publish on PHOTON's cloud
99 Particle.publish(keyBuffer, dataBuffer);
103 digitalWrite(PWRPIN, LOW);
106 //Serial.print("Time end loop: ");
107 //Serial.println(micros());
109 System.sleep(SLEEP_MODE_DEEP, SLEEP_TIME);