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