#include "application.h" #include "DHT.h" // System defines // Arduino #define DHTPIN 2 // Data pin #define PWRPIN D5 // Power pin #define ERRPIN D7 // Error pin #define DHTTYPE DHT22 // DHT 22 (AM2302) // IoTCloud #define SLEEP_TIME 15 // Sleep time in seconds #define RETRY_SLEEP_TIME 5 // Sleep time in seconds #define RETRY_TIME 10000 // stop trying after 10 seconds #define CONNECTION_DELAY 2100000 // Need to delay after connecting WiFi to wait for sensor // Initialize DHT sensor for normal 16mhz Arduino DHT dht(DHTPIN, DHTTYPE); // Globals // IoTCloud char keyBuffer[80]; char dataBuffer[80]; int64_t machineId; void setup() { // TODO: // 1) This code reads from the sensor and publishes // the results on the cloud. // 2) If you want to use the Serial library, // please install "screen" on your machine and run // it on the serial port right after flashing the // firmware onto the Particle board. // e.g. sudo screen /dev/ttyACM0 // We use one I/O pin to power the sensor so // that we can make it go to sleep when we go into // deep sleep. // TODO: Profiling! //Serial.begin(); //Serial.print("Time begin setup: "); //Serial.println(micros()); // Turn on sensor pinMode(PWRPIN, OUTPUT); digitalWrite(PWRPIN, HIGH); // Arduino DHT dht.begin(); // Prepare device key from MAC (just last 2 of 6 digits) byte mac[6]; WiFi.macAddress(mac); // TODO: Uncomment the following block to print MAC //for (int i=0; i<6; i++) { // Serial.printf("%02x%s", mac[i], i != 5 ? ":" : ""); //} //Serial.println(); // Prepare machine ID int64_t mac4 = (int64_t) mac[4]; int64_t mac5 = (int64_t) mac[5]; machineId = (mac4 * 256) + mac5; //machineId = (int64_t) mac[5]; // Shorter version of machine ID // TODO: Profiling //Serial.print("Time end setup: "); //Serial.println(micros()); } void loop() { // Wait until sensor is ready delay(1500); // TODO: Profiling //Serial.print("Time begin loop: "); //Serial.println(micros()); // Read humidity float humid = dht.readHumidity(); // Read temperature as Fahrenheit float tempF = dht.readTemperature(true); // Check if any reads failed and exit early (to try again). if (isnan(humid) || isnan(tempF)) return; // Machine ID sprintf(keyBuffer, "%04x", machineId); // Humidity + Temperature sprintf(dataBuffer, "%0.2f-%0.2f", humid, tempF); // Publish on PHOTON's cloud Particle.publish(keyBuffer, dataBuffer); delay(1000); // Turn off sensor digitalWrite(PWRPIN, LOW); // TODO: Profiling //Serial.print("Time end loop: "); //Serial.println(micros()); //while(true) { } System.sleep(SLEEP_MODE_DEEP, SLEEP_TIME); }