-#include "Table.h"
-#include "IoTString.h"
-#include "TimingSingleton.h"
-#include "TransactionStatus.h"
-#include "DHT.h"
-
-SYSTEM_MODE(SEMI_AUTOMATIC)
-SYSTEM_THREAD(ENABLED)
-
-// 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
-bool foundError;
-MyVector<TransactionStatus *> * transStatusList;
-char keyBuffer[80];
-char dataBuffer[80];
-Table *t1;
-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();
- // Connect to WiFi and Particle cloud
- // Wait for a maximum amount of time - sleep if WiFi is not connected
- // Wake up and try again afterwards
- // TODO: either use this or just WiFi connection (below)
- Particle.connect();
- if (!waitFor(Particle.connected, RETRY_TIME))
- System.sleep(SLEEP_MODE_DEEP, RETRY_SLEEP_TIME);
-
- // 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);
-}
-