Publishing sensor values on PHOTON cloud instead of using Fidelius---needs more delay...
[iotcloud.git] / version2 / src / C / Sensor-Arduino_Photon-Cloud.ino
1 #include "Table.h"
2 #include "IoTString.h"
3 #include "TimingSingleton.h"
4 #include "TransactionStatus.h"
5 #include "DHT.h"
6
7 SYSTEM_MODE(SEMI_AUTOMATIC)
8 SYSTEM_THREAD(ENABLED)
9
10 // System defines
11 // Arduino
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)
16 // IoTCloud
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
21
22 // Initialize DHT sensor for normal 16mhz Arduino
23 DHT dht(DHTPIN, DHTTYPE);
24
25 // Globals
26 // IoTCloud
27 bool foundError;
28 MyVector<TransactionStatus *> * transStatusList;
29 char keyBuffer[80];
30 char dataBuffer[80];
31 Table *t1;
32 int64_t machineId;
33
34 void setup() {
35         // TODO: 
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
43
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
46         // deep sleep.
47         
48         // TODO: Profiling!
49         //Serial.begin();
50         //Serial.print("Time begin setup: ");
51         //Serial.println(micros());
52         // Turn on sensor
53         pinMode(PWRPIN, OUTPUT);
54         digitalWrite(PWRPIN, HIGH);
55         // Arduino DHT
56         dht.begin();
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)
61         Particle.connect();
62         if (!waitFor(Particle.connected, RETRY_TIME))
63                 System.sleep(SLEEP_MODE_DEEP, RETRY_SLEEP_TIME);
64
65         // Prepare device key from MAC (just last 2 of 6 digits)
66         byte mac[6];
67         WiFi.macAddress(mac);
68
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 ? ":" : "");
72         //}
73         //Serial.println();
74         // Prepare machine ID
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
79         
80     // TODO: Profiling  
81         //Serial.print("Time end setup: ");
82         //Serial.println(micros());
83 }
84
85 void loop() {
86         // Wait until sensor is ready
87         delay(1500);
88     // TODO: Profiling
89         //Serial.print("Time begin loop: ");
90         //Serial.println(micros());
91         // Read humidity
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))
97                 return;
98
99         // Machine ID
100         sprintf(keyBuffer, "%04x", machineId);
101         // Humidity + Temperature
102         sprintf(dataBuffer, "%0.2f-%0.2f", humid, tempF);
103
104         // Publish on PHOTON's cloud
105         Particle.publish(keyBuffer, dataBuffer);
106         delay(1000);
107
108         // Turn off sensor
109         digitalWrite(PWRPIN, LOW);
110
111         // TODO: Profiling
112         //Serial.print("Time end loop: ");
113         //Serial.println(micros());
114         //while(true) { }
115         System.sleep(SLEEP_MODE_DEEP, SLEEP_TIME);
116 }
117