Adding IR-sensor for our testbed (both Particle and Fidelius clouds).
[iotcloud.git] / version2 / src / others / ino / Temp-Sensor / Temp-Sensor_Particle-Cloud.ino
1 #include "application.h"
2 #include "DHT.h"
3
4 SYSTEM_MODE(SEMI_AUTOMATIC)
5 SYSTEM_THREAD(ENABLED)
6
7 // System defines
8 // Arduino
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)
13 // IoTCloud
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
18
19 // Initialize DHT sensor for normal 16mhz Arduino
20 DHT dht(DHTPIN, DHTTYPE);
21
22 // Globals
23 // IoTCloud
24 char keyBuffer[80];
25 char dataBuffer[80];
26 int64_t machineId;
27
28 void setup() {
29         // TODO: 
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
37
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
40         // deep sleep.
41         
42         // TODO: Profiling!
43         //Serial.begin();
44         //Serial.print("Time begin setup: ");
45         //Serial.println(micros());
46         // Turn on sensor
47         pinMode(PWRPIN, OUTPUT);
48         digitalWrite(PWRPIN, HIGH);
49         // Arduino DHT
50         dht.begin();
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)
55         Particle.connect();
56         if (!waitFor(Particle.connected, RETRY_TIME))
57                 System.sleep(SLEEP_MODE_DEEP, RETRY_SLEEP_TIME);
58
59         // Prepare device key from MAC (just last 2 of 6 digits)
60         byte mac[6];
61         WiFi.macAddress(mac);
62
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 ? ":" : "");
66         //}
67         //Serial.println();
68         // Prepare machine ID
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
73         
74     // TODO: Profiling  
75         //Serial.print("Time end setup: ");
76         //Serial.println(micros());
77 }
78
79 void loop() {
80         // Wait until sensor is ready
81         delay(1500);
82     // TODO: Profiling
83         //Serial.print("Time begin loop: ");
84         //Serial.println(micros());
85         // Read humidity
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))
91                 return;
92
93         // Machine ID
94         sprintf(keyBuffer, "%04x", machineId);
95         // Humidity + Temperature
96         sprintf(dataBuffer, "%0.2f-%0.2f", humid, tempF);
97
98         // Publish on PHOTON's cloud
99         Particle.publish(keyBuffer, dataBuffer);
100         delay(1000);
101
102         // Turn off sensor
103         digitalWrite(PWRPIN, LOW);
104
105         // TODO: Profiling
106         //Serial.print("Time end loop: ");
107         //Serial.println(micros());
108         //while(true) { }
109         System.sleep(SLEEP_MODE_DEEP, SLEEP_TIME);
110 }
111