From 8bb2df3dd0bdfc262501e17b26525b564a6f3cc5 Mon Sep 17 00:00:00 2001 From: rtrimana Date: Fri, 13 Apr 2018 16:52:32 -0700 Subject: [PATCH] Adding DHT22/AM2302 sensor libraries (Arduino and PietteTech) but using Arduino for now (more accurate reading); Sensor-Arduino.ino now works with sensor reading, cloud update, and sleep for 30 seconds before repeating the cycle (specific device key for Table is obtained from device's WiFi MAC for convenience) --- version2/src/C/Sensor-Arduino.ino | 91 ++++++ .../src/others/{DHT => Arduino_DHT}/DHT.cpp | 0 .../src/others/{DHT => Arduino_DHT}/DHT.h | 0 .../others/{DHT => Arduino_DHT}/README.txt | 0 .../examples/DHTtester/DHTtester.ino | 0 version2/src/others/PietteTech_DHT/DHT22.ino | 131 ++++++++ .../others/PietteTech_DHT/PietteTech_DHT.cpp | 298 ++++++++++++++++++ .../others/PietteTech_DHT/PietteTech_DHT.h | 105 ++++++ version2/src/others/PietteTech_DHT/README.md | 15 + version2/src/others/ino/DHTtester.ino | 71 +++++ version2/src/others/ino/README | 5 + version2/src/others/ino/Sensor-Arduino.ino | 91 ++++++ version2/src/others/ino/Sensor-PietteTech.ino | 138 ++++++++ version2/src/others/ino/Sensor.ino | 53 ++++ 14 files changed, 998 insertions(+) create mode 100644 version2/src/C/Sensor-Arduino.ino rename version2/src/others/{DHT => Arduino_DHT}/DHT.cpp (100%) rename version2/src/others/{DHT => Arduino_DHT}/DHT.h (100%) rename version2/src/others/{DHT => Arduino_DHT}/README.txt (100%) rename version2/src/others/{DHT => Arduino_DHT}/examples/DHTtester/DHTtester.ino (100%) create mode 100644 version2/src/others/PietteTech_DHT/DHT22.ino create mode 100644 version2/src/others/PietteTech_DHT/PietteTech_DHT.cpp create mode 100644 version2/src/others/PietteTech_DHT/PietteTech_DHT.h create mode 100644 version2/src/others/PietteTech_DHT/README.md create mode 100644 version2/src/others/ino/DHTtester.ino create mode 100644 version2/src/others/ino/README create mode 100644 version2/src/others/ino/Sensor-Arduino.ino create mode 100644 version2/src/others/ino/Sensor-PietteTech.ino create mode 100644 version2/src/others/ino/Sensor.ino diff --git a/version2/src/C/Sensor-Arduino.ino b/version2/src/C/Sensor-Arduino.ino new file mode 100644 index 0000000..99bacdc --- /dev/null +++ b/version2/src/C/Sensor-Arduino.ino @@ -0,0 +1,91 @@ +#include "Table.h" +#include "IoTString.h" +#include "TimingSingleton.h" +#include "TransactionStatus.h" +#include "DHT.h" + +// System defines +// Arduino +#define DHTPIN 2 // what pin we're connected to +#define DHTTYPE DHT22 // DHT 22 (AM2302) +// IoTCloud +#define SLEEP_TIME 30 // Sleep time in seconds + +// Initialize DHT sensor for normal 16mhz Arduino +DHT dht(DHTPIN, DHTTYPE); + +// Globals +// IoTCloud +TimingSingleton *timer; +bool foundError; +MyVector * transStatusList; +char keyBuffer[80]; +char dataBuffer[80]; +Table *t1; +IoTString *iKeyA; +IoTString *iValueA; + + +void setup() { + // TODO: 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 + + // 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 + //Serial.begin(); + //for (int i=0; i<6; i++) { + // Serial.printf("%02x%s", mac[i], i != 5 ? ":" : ""); + //} + //Serial.println(); + + int deviceKey = (int) mac[4] + mac[5]; + // IoTCloud library + timer = TimingSingleton_getInstance(); + foundError = false; + transStatusList = new MyVector(); + IoTString *baseurl = new IoTString("http://dc-6.calit2.uci.edu/test.iotcloud/"); + IoTString *password = new IoTString("reallysecret"); + t1 = new Table(baseurl, password, deviceKey, -1); + t1->rebuild(); + + baseurl->releaseRef(); + password->releaseRef(); + + // Arduino DHT + dht.begin(); +} + +void loop() { + + // Wait until sensor is ready + delay(2000); + // Read temperature as Fahrenheit + float tempF = dht.readTemperature(true); + // Check if any reads failed and exit early (to try again). + if (isnan(tempF)) + return; + + // Key + sprintf(keyBuffer, "sensor0"); + iKeyA = new IoTString(keyBuffer); + + // Do updates for the temperature + sprintf(dataBuffer, "%f", tempF); + iValueA = new IoTString(dataBuffer); + + t1->startTransaction(); + t1->put(iKeyA, iValueA); + transStatusList->add(t1->commitTransaction()); + t1->update(); + + iKeyA->releaseRef(); + iValueA->releaseRef(); + System.sleep(SLEEP_MODE_DEEP, SLEEP_TIME); +} + diff --git a/version2/src/others/DHT/DHT.cpp b/version2/src/others/Arduino_DHT/DHT.cpp similarity index 100% rename from version2/src/others/DHT/DHT.cpp rename to version2/src/others/Arduino_DHT/DHT.cpp diff --git a/version2/src/others/DHT/DHT.h b/version2/src/others/Arduino_DHT/DHT.h similarity index 100% rename from version2/src/others/DHT/DHT.h rename to version2/src/others/Arduino_DHT/DHT.h diff --git a/version2/src/others/DHT/README.txt b/version2/src/others/Arduino_DHT/README.txt similarity index 100% rename from version2/src/others/DHT/README.txt rename to version2/src/others/Arduino_DHT/README.txt diff --git a/version2/src/others/DHT/examples/DHTtester/DHTtester.ino b/version2/src/others/Arduino_DHT/examples/DHTtester/DHTtester.ino similarity index 100% rename from version2/src/others/DHT/examples/DHTtester/DHTtester.ino rename to version2/src/others/Arduino_DHT/examples/DHTtester/DHTtester.ino diff --git a/version2/src/others/PietteTech_DHT/DHT22.ino b/version2/src/others/PietteTech_DHT/DHT22.ino new file mode 100644 index 0000000..9723114 --- /dev/null +++ b/version2/src/others/PietteTech_DHT/DHT22.ino @@ -0,0 +1,131 @@ +/* + * FILE: DHT_example.ino + * VERSION: 0.3 + * PURPOSE: Example that uses DHT library with two sensors + * LICENSE: GPL v3 (http://www.gnu.org/licenses/gpl.html) + * + * Example that start acquisition of DHT sensor and allows the + * loop to continue until the acquisition has completed + * It uses DHT.acquire and DHT.acquiring + * + * Change DHT_SAMPLE_TIME to vary the frequency of samples + * + * Scott Piette (Piette Technologies) scott.piette@gmail.com + * January 2014 Original Spark Port + * October 2014 Added support for DHT21/22 sensors + * Improved timing, moved FP math out of ISR + */ + +#include "PietteTech_DHT.h" + +// system defines +#define DHTTYPE DHT22 // Sensor type DHT11/21/22/AM2301/AM2302 +#define DHTPIN D5 // Digital pin for communications +#define DHT_SAMPLE_INTERVAL 2000 // Sample every two seconds + +//declaration +void dht_wrapper(); // must be declared before the lib initialization + +// Lib instantiate +PietteTech_DHT DHT(DHTPIN, DHTTYPE, dht_wrapper); + +// globals +unsigned int DHTnextSampleTime; // Next time we want to start sample +bool bDHTstarted; // flag to indicate we started acquisition +int n; // counter + +void setup() +{ + Serial.begin(9600); + while (!Serial.available()) { + Serial.println("Press any key to start."); + delay (1000); + } + Serial.println("DHT Example program using DHT.acquire and DHT.aquiring"); + Serial.print("LIB version: "); + Serial.println(DHTLIB_VERSION); + Serial.println("---------------"); + + DHTnextSampleTime = 0; // Start the first sample immediately +} + + +// This wrapper is in charge of calling +// mus be defined like this for the lib work +void dht_wrapper() { + DHT.isrCallback(); +} + +void loop() +{ + // Check if we need to start the next sample + if (millis() > DHTnextSampleTime) { + if (!bDHTstarted) { // start the sample + Serial.print("\n"); + Serial.print(n); + Serial.print(": Retrieving information from sensor: "); + DHT.acquire(); + bDHTstarted = true; + } + + int result = DHT.getStatus(); + if (!DHT.acquiring()) { // has sample completed? + + // get DHT status + int result = DHT.getStatus(); + + Serial.print("Read sensor: "); + switch (result) { + case DHTLIB_OK: + Serial.println("OK"); + break; + case DHTLIB_ERROR_CHECKSUM: + Serial.println("Error\n\r\tChecksum error"); + break; + case DHTLIB_ERROR_ISR_TIMEOUT: + Serial.println("Error\n\r\tISR time out error"); + break; + case DHTLIB_ERROR_RESPONSE_TIMEOUT: + Serial.println("Error\n\r\tResponse time out error"); + break; + case DHTLIB_ERROR_DATA_TIMEOUT: + Serial.println("Error\n\r\tData time out error"); + break; + case DHTLIB_ERROR_ACQUIRING: + Serial.println("Error\n\r\tAcquiring"); + break; + case DHTLIB_ERROR_DELTA: + Serial.println("Error\n\r\tDelta time to small"); + break; + case DHTLIB_ERROR_NOTSTARTED: + Serial.println("Error\n\r\tNot started"); + break; + default: + Serial.println("Unknown error"); + break; + } + + Serial.print("Humidity (%): "); + Serial.println(DHT.getHumidity(), 2); + + Serial.print("Temperature (oC): "); + Serial.println(DHT.getCelsius(), 2); + + Serial.print("Temperature (oF): "); + Serial.println(DHT.getFahrenheit(), 2); + + Serial.print("Temperature (K): "); + Serial.println(DHT.getKelvin(), 2); + + Serial.print("Dew Point (oC): "); + Serial.println(DHT.getDewPoint()); + + Serial.print("Dew Point Slow (oC): "); + Serial.println(DHT.getDewPointSlow()); + + n++; // increment counter + bDHTstarted = false; // reset the sample flag so we can take another + DHTnextSampleTime = millis() + DHT_SAMPLE_INTERVAL; // set the time for next sample + } + } +} diff --git a/version2/src/others/PietteTech_DHT/PietteTech_DHT.cpp b/version2/src/others/PietteTech_DHT/PietteTech_DHT.cpp new file mode 100644 index 0000000..2b8c3dd --- /dev/null +++ b/version2/src/others/PietteTech_DHT/PietteTech_DHT.cpp @@ -0,0 +1,298 @@ +/* + * FILE: PietteTech_DHT.cpp + * VERSION: 0.3 + * PURPOSE: Spark Interrupt driven lib for DHT sensors + * LICENSE: GPL v3 (http://www.gnu.org/licenses/gpl.html) + * + * S Piette (Piette Technologies) scott.piette@gmail.com + * January 2014 Original Spark Port + * October 2014 Added support for DHT21/22 sensors + * Improved timing, moved FP math out of ISR + * + * Based on adaptation by niesteszeck (github/niesteszeck) + * Based on original DHT11 library (http://playgroudn.adruino.cc/Main/DHT11Lib) + * + * + * This library supports the DHT sensor on the following pins + * D0, D1, D2, D3, D4, A0, A1, A3, A5, A6, A7 + * http://docs.spark.io/firmware/#interrupts-attachinterrupt + * + */ + +/* + Timing of DHT22 SDA signal line after MCU pulls low for 1ms + https://github.com/mtnscott/Spark_DHT/AM2302.pdf + + - - - - ----- -- - - -- ------- - - + \ / \ / \ \ / + + / + / + + / + \ / \ / \ \ / + ------ ----- -- - -------- + ^ ^ ^ ^ ^ + | Ts | Tr | Td | Te | + + Ts : Start time from MCU changing SDA from Output High to Tri-State (Hi-Z) + Spec: 20-200us Tested: < 65us + Tr : DHT response to MCU controlling SDA and pulling Low and High to + start of first data bit + Spec: 150-170us Tested: 125 - 200us + Td : DHT data bit, falling edge to falling edge + Spec: '0' 70us - 85us Tested: 60 - 110us + Spec: '1' 116us - 130us Tested: 111 - 155us + Te : DHT releases SDA to Tri-State (Hi-Z) + Spec: 45-55us Not Tested + */ + +#include "application.h" +#include "math.h" +#include "PietteTech_DHT.h" + +// Thanks to Paul Kourany for this word type conversion function +uint16_t word(uint8_t high, uint8_t low) { + uint16_t ret_val = low; + ret_val += (high << 8); + return ret_val; +} + +PietteTech_DHT::PietteTech_DHT(uint8_t sigPin, uint8_t dht_type, void (*callback_wrapper)()) { + begin(sigPin, dht_type, callback_wrapper); + _firstreading = true; +} + +void PietteTech_DHT::begin(uint8_t sigPin, uint8_t dht_type, void (*callback_wrapper) ()) { + _sigPin = sigPin; + _type = dht_type; + isrCallback_wrapper = callback_wrapper; + + pinMode(sigPin, OUTPUT); + digitalWrite(sigPin, HIGH); + _lastreadtime = 0; + _state = STOPPED; + _status = DHTLIB_ERROR_NOTSTARTED; +} + +int PietteTech_DHT::acquire() { + // Check if sensor was read less than two seconds ago and return early + // to use last reading + unsigned long currenttime = millis(); + if (currenttime < _lastreadtime) { + // there was a rollover + _lastreadtime = 0; + } + if (!_firstreading && ((currenttime - _lastreadtime) < 2000 )) { + // return last correct measurement, (this read time - last read time) < device limit + return DHTLIB_ACQUIRED; + } + + if (_state == STOPPED || _state == ACQUIRED) { + /* + * Setup the initial state machine + */ + _firstreading = false; + _lastreadtime = currenttime; + _state = RESPONSE; + +#if defined(DHT_DEBUG_TIMING) + /* + * Clear the debug timings array + */ + for (int i = 0; i < 41; i++) _edges[i] = 0; + _e = &_edges[0]; +#endif + + /* + * Set the initial values in the buffer and variables + */ + for (int i = 0; i < 5; i++) _bits[i] = 0; + _cnt = 7; + _idx = 0; + _hum = 0; + _temp = 0; + + /* + * Toggle the digital output to trigger the DHT device + * to send us temperature and humidity data + */ + pinMode(_sigPin, OUTPUT); + digitalWrite(_sigPin, LOW); + if (_type == DHT11) + delay(18); // DHT11 Spec: 18ms min + else + delayMicroseconds(1500); // DHT22 Spec: 0.8-20ms, 1ms typ + pinMode(_sigPin, INPUT); // Note Hi-Z mode with pullup resistor + // will keep this high until the DHT responds. + /* + * Attach the interrupt handler to receive the data once the DHT + * starts to send us data + */ + _us = micros(); + attachInterrupt(_sigPin, isrCallback_wrapper, FALLING); + + return DHTLIB_ACQUIRING; + } else + return DHTLIB_ERROR_ACQUIRING; +} + +int PietteTech_DHT::acquireAndWait() { + acquire(); + while(acquiring()) ; + return getStatus(); +} + +void PietteTech_DHT::isrCallback() { + unsigned long newUs = micros(); + unsigned long delta = (newUs - _us); + _us = newUs; + + if (delta > 6000) { + _status = DHTLIB_ERROR_ISR_TIMEOUT; + _state = STOPPED; + detachInterrupt(_sigPin); + return; + } + switch(_state) { + case RESPONSE: // Spec: 80us LOW followed by 80us HIGH + if(delta < 65) { // Spec: 20-200us to first falling edge of response + _us -= delta; + break; //do nothing, it started the response signal + } if(125 < delta && delta < 200) { +#if defined(DHT_DEBUG_TIMING) + *_e++ = delta; // record the edge -> edge time +#endif + _state = DATA; + } else { + detachInterrupt(_sigPin); + _status = DHTLIB_ERROR_RESPONSE_TIMEOUT; + _state = STOPPED; +#if defined(DHT_DEBUG_TIMING) + *_e++ = delta; // record the edge -> edge time +#endif + } + break; + case DATA: // Spec: 50us low followed by high of 26-28us = 0, 70us = 1 + if(60 < delta && delta < 155) { //valid in timing + _bits[_idx] <<= 1; // shift the data + if(delta > 110) //is a one + _bits[_idx] |= 1; +#if defined(DHT_DEBUG_TIMING) + *_e++ = delta; // record the edge -> edge time +#endif + if (_cnt == 0) { // we have completed the byte, go to next + _cnt = 7; // restart at MSB + if(++_idx == 5) { // go to next byte, if we have got 5 bytes stop. + detachInterrupt(_sigPin); + // Verify checksum + uint8_t sum = _bits[0] + _bits[1] + _bits[2] + _bits[3]; + if (_bits[4] != sum) { + _status = DHTLIB_ERROR_CHECKSUM; + _state = STOPPED; + } else { + _status = DHTLIB_OK; + _state = ACQUIRED; + _convert = true; + } + break; + } + } else _cnt--; + } else if(delta < 10) { + detachInterrupt(_sigPin); + _status = DHTLIB_ERROR_DELTA; + _state = STOPPED; + } else { + detachInterrupt(_sigPin); + _status = DHTLIB_ERROR_DATA_TIMEOUT; + _state = STOPPED; + } + break; + default: + break; + } +} + +void PietteTech_DHT::convert() { + // Calculate the temperature and humidity based on the sensor type + switch (_type) { + case DHT11: + _hum = _bits[0]; + _temp = _bits[2]; + break; + case DHT22: + case DHT21: + _hum = word(_bits[0], _bits[1]) * 0.1; + _temp = (_bits[2] & 0x80 ? + -word(_bits[2] & 0x7F, _bits[3]) : + word(_bits[2], _bits[3])) * 0.1; + break; + } + _convert = false; +} + +bool PietteTech_DHT::acquiring() { + if (_state != ACQUIRED && _state != STOPPED) + return true; + return false; +} + +int PietteTech_DHT::getStatus() { + return _status; +} + +float PietteTech_DHT::getCelsius() { + DHT_CHECK_STATE; + return _temp; +} + +float PietteTech_DHT::getHumidity() { + DHT_CHECK_STATE; + return _hum; +} + +float PietteTech_DHT::getFahrenheit() { + DHT_CHECK_STATE; + return _temp * 9 / 5 + 32; +} + +float PietteTech_DHT::getKelvin() { + DHT_CHECK_STATE; + return _temp + 273.15; +} + +/* + * Added methods for supporting Adafruit Unified Sensor framework + */ +float PietteTech_DHT::readTemperature() { + acquireAndWait(); + return getCelsius(); +} + +float PietteTech_DHT::readHumidity() { + acquireAndWait(); + return getHumidity(); +} + +// delta max = 0.6544 wrt dewPoint() +// 5x faster than dewPoint() +// reference: http://en.wikipedia.org/wiki/Dew_point +double PietteTech_DHT::getDewPoint() { + DHT_CHECK_STATE; + double a = 17.271; + double b = 237.7; + double temp_ = (a * (double) _temp) / (b + (double) _temp) + log( (double) _hum/100); + double Td = (b * temp_) / (a - temp_); + return Td; +} + +// dewPoint function NOAA +// reference: http://wahiduddin.net/calc/density_algorithms.htm +double PietteTech_DHT::getDewPointSlow() { + DHT_CHECK_STATE; + double a0 = (double) 373.15 / (273.15 + (double) _temp); + double SUM = (double) -7.90298 * (a0-1.0); + SUM += 5.02808 * log10(a0); + SUM += -1.3816e-7 * (pow(10, (11.344*(1-1/a0)))-1) ; + SUM += 8.1328e-3 * (pow(10,(-3.49149*(a0-1)))-1) ; + SUM += log10(1013.246); + double VP = pow(10, SUM-3) * (double) _hum; + double T = log(VP/0.61078); // temp var + return (241.88 * T) / (17.558-T); +} diff --git a/version2/src/others/PietteTech_DHT/PietteTech_DHT.h b/version2/src/others/PietteTech_DHT/PietteTech_DHT.h new file mode 100644 index 0000000..dc5b216 --- /dev/null +++ b/version2/src/others/PietteTech_DHT/PietteTech_DHT.h @@ -0,0 +1,105 @@ +/* + * FILE: PietteTech_DHT.h + * VERSION: 0.3 + * PURPOSE: Spark Interrupt driven lib for DHT sensors + * LICENSE: GPL v3 (http://www.gnu.org/licenses/gpl.html) + * + * S Piette (Piette Technologies) scott.piette@gmail.com + * January 2014 Original Spark Port + * October 2014 Added support for DHT21/22 sensors + * Improved timing, moved FP math out of ISR + * + * Based on adaptation by niesteszeck (github/niesteszeck) + * Based on original DHT11 library (http://playgroudn.adruino.cc/Main/DHT11Lib) + * + * + * With this library connect the DHT sensor to the following pins + * D0, D1, D2, D3, D4, A0, A1, A3, A5, A6, A7 + * http://docs.spark.io/firmware/#interrupts-attachinterrupt + * + */ + +#ifndef __PIETTETECH_DHT_H__ +#define __PIETTETECH_DHT_H__ + +// There appears to be a overrun in memory on this class. For now please leave DHT_DEBUG_TIMING enabled +#define DHT_DEBUG_TIMING // Enable this for edge->edge timing collection + +#include "application.h" + +#define DHTLIB_VERSION "0.3" + +// device types +#define DHT11 11 +#define DHT21 21 +#define AM2301 21 +#define DHT22 22 +#define AM2302 22 + +// state codes +#define DHTLIB_OK 0 +#define DHTLIB_ACQUIRING 1 +#define DHTLIB_ACQUIRED 2 +#define DHTLIB_RESPONSE_OK 3 + +// error codes +#define DHTLIB_ERROR_CHECKSUM -1 +#define DHTLIB_ERROR_ISR_TIMEOUT -2 +#define DHTLIB_ERROR_RESPONSE_TIMEOUT -3 +#define DHTLIB_ERROR_DATA_TIMEOUT -4 +#define DHTLIB_ERROR_ACQUIRING -5 +#define DHTLIB_ERROR_DELTA -6 +#define DHTLIB_ERROR_NOTSTARTED -7 + +#define DHT_CHECK_STATE \ + if(_state == STOPPED) \ + return _status; \ + else if(_state != ACQUIRED) \ + return DHTLIB_ERROR_ACQUIRING; \ + if(_convert) convert(); + +class PietteTech_DHT +{ +public: + PietteTech_DHT(uint8_t sigPin, uint8_t dht_type, void (*isrCallback_wrapper)()); + void begin(uint8_t sigPin, uint8_t dht_type, void (*isrCallback_wrapper)()); + void isrCallback(); + int acquire(); + int acquireAndWait(); + float getCelsius(); + float getFahrenheit(); + float getKelvin(); + double getDewPoint(); + double getDewPointSlow(); + float getHumidity(); + bool acquiring(); + int getStatus(); + float readTemperature(); + float readHumidity(); +#if defined(DHT_DEBUG_TIMING) + volatile uint8_t _edges[41]; +#endif + +private: + void (*isrCallback_wrapper)(void); + void convert(); + + enum states{RESPONSE=0,DATA=1,ACQUIRED=2,STOPPED=3,ACQUIRING=4}; + volatile states _state; + volatile int _status; + volatile uint8_t _bits[5]; + volatile uint8_t _cnt; + volatile uint8_t _idx; + volatile unsigned long _us; + volatile bool _convert; +#if defined(DHT_DEBUG_TIMING) + volatile uint8_t *_e; +#endif + int _sigPin; + int _type; + unsigned long _lastreadtime; + bool _firstreading; + float _hum; + float _temp; +}; +#endif diff --git a/version2/src/others/PietteTech_DHT/README.md b/version2/src/others/PietteTech_DHT/README.md new file mode 100644 index 0000000..a1b032f --- /dev/null +++ b/version2/src/others/PietteTech_DHT/README.md @@ -0,0 +1,15 @@ +# Photon-DHT22 +Configures a DHT22 humidity/temp sensor to work with a Particle Photon and publish readings to the Particle Dashboard. + +I've included a copy of the PietteTech_DHT library, feel free to grab a more recent copy and replace what is bundled in if it's outdated. + +The connection setup for the DHT22 is as follows... + +* PIN 1 = 3.3V or 5V +* PIN 2 = Data (D1 is default for this project) +* PIN 3 = Unused +* PIN 4 = Ground + +Between PIN 1 and PIN 2 I'm running a 10K pulldown resistor + +Note: I did run into issues when trying to use D0 to connect to the DHT22 even though the library says it's supported. diff --git a/version2/src/others/ino/DHTtester.ino b/version2/src/others/ino/DHTtester.ino new file mode 100644 index 0000000..021107f --- /dev/null +++ b/version2/src/others/ino/DHTtester.ino @@ -0,0 +1,71 @@ +// Example testing sketch for various DHT humidity/temperature sensors +// Written by ladyada, public domain + +#include "DHT.h" + +#define DHTPIN 2 // what pin we're connected to + +// Uncomment whatever type you're using! +//#define DHTTYPE DHT11 // DHT 11 +#define DHTTYPE DHT22 // DHT 22 (AM2302) +//#define DHTTYPE DHT21 // DHT 21 (AM2301) + +// Connect pin 1 (on the left) of the sensor to +5V +// NOTE: If using a board with 3.3V logic like an Arduino Due connect pin 1 +// to 3.3V instead of 5V! +// Connect pin 2 of the sensor to whatever your DHTPIN is +// Connect pin 4 (on the right) of the sensor to GROUND +// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor + +// Initialize DHT sensor for normal 16mhz Arduino +DHT dht(DHTPIN, DHTTYPE); +// NOTE: For working with a faster chip, like an Arduino Due or Teensy, you +// might need to increase the threshold for cycle counts considered a 1 or 0. +// You can do this by passing a 3rd parameter for this threshold. It's a bit +// of fiddling to find the right value, but in general the faster the CPU the +// higher the value. The default for a 16mhz AVR is a value of 6. For an +// Arduino Due that runs at 84mhz a value of 30 works. +// Example to initialize DHT sensor for Arduino Due: +//DHT dht(DHTPIN, DHTTYPE, 30); + +void setup() { + Serial.begin(9600); + Serial.println("DHTxx test!"); + + dht.begin(); +} + +void loop() { + // Wait a few seconds between measurements. + delay(2000); + + // Reading temperature or humidity takes about 250 milliseconds! + // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor) + float h = dht.readHumidity(); + // Read temperature as Celsius + float t = dht.readTemperature(); + // Read temperature as Fahrenheit + float f = dht.readTemperature(true); + + // Check if any reads failed and exit early (to try again). + if (isnan(h) || isnan(t) || isnan(f)) { + Serial.println("Failed to read from DHT sensor!"); + return; + } + + // Compute heat index + // Must send in temp in Fahrenheit! + float hi = dht.computeHeatIndex(f, h); + + Serial.print("Humidity: "); + Serial.print(h); + Serial.print(" %\t"); + Serial.print("Temperature: "); + Serial.print(t); + Serial.print(" *C "); + Serial.print(f); + Serial.print(" *F\t"); + Serial.print("Heat index: "); + Serial.print(hi); + Serial.println(" *F"); +} diff --git a/version2/src/others/ino/README b/version2/src/others/ino/README new file mode 100644 index 0000000..d5a051e --- /dev/null +++ b/version2/src/others/ino/README @@ -0,0 +1,5 @@ +================ +About .ino files +================ +1) One .ino file is needed as the main file to generate a .bin (firmware) file for Particle PHOTON code compilation. +2) Please put one of these .ino files into the C code folder before compiling it with the Particle CLI compiler. diff --git a/version2/src/others/ino/Sensor-Arduino.ino b/version2/src/others/ino/Sensor-Arduino.ino new file mode 100644 index 0000000..99bacdc --- /dev/null +++ b/version2/src/others/ino/Sensor-Arduino.ino @@ -0,0 +1,91 @@ +#include "Table.h" +#include "IoTString.h" +#include "TimingSingleton.h" +#include "TransactionStatus.h" +#include "DHT.h" + +// System defines +// Arduino +#define DHTPIN 2 // what pin we're connected to +#define DHTTYPE DHT22 // DHT 22 (AM2302) +// IoTCloud +#define SLEEP_TIME 30 // Sleep time in seconds + +// Initialize DHT sensor for normal 16mhz Arduino +DHT dht(DHTPIN, DHTTYPE); + +// Globals +// IoTCloud +TimingSingleton *timer; +bool foundError; +MyVector * transStatusList; +char keyBuffer[80]; +char dataBuffer[80]; +Table *t1; +IoTString *iKeyA; +IoTString *iValueA; + + +void setup() { + // TODO: 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 + + // 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 + //Serial.begin(); + //for (int i=0; i<6; i++) { + // Serial.printf("%02x%s", mac[i], i != 5 ? ":" : ""); + //} + //Serial.println(); + + int deviceKey = (int) mac[4] + mac[5]; + // IoTCloud library + timer = TimingSingleton_getInstance(); + foundError = false; + transStatusList = new MyVector(); + IoTString *baseurl = new IoTString("http://dc-6.calit2.uci.edu/test.iotcloud/"); + IoTString *password = new IoTString("reallysecret"); + t1 = new Table(baseurl, password, deviceKey, -1); + t1->rebuild(); + + baseurl->releaseRef(); + password->releaseRef(); + + // Arduino DHT + dht.begin(); +} + +void loop() { + + // Wait until sensor is ready + delay(2000); + // Read temperature as Fahrenheit + float tempF = dht.readTemperature(true); + // Check if any reads failed and exit early (to try again). + if (isnan(tempF)) + return; + + // Key + sprintf(keyBuffer, "sensor0"); + iKeyA = new IoTString(keyBuffer); + + // Do updates for the temperature + sprintf(dataBuffer, "%f", tempF); + iValueA = new IoTString(dataBuffer); + + t1->startTransaction(); + t1->put(iKeyA, iValueA); + transStatusList->add(t1->commitTransaction()); + t1->update(); + + iKeyA->releaseRef(); + iValueA->releaseRef(); + System.sleep(SLEEP_MODE_DEEP, SLEEP_TIME); +} + diff --git a/version2/src/others/ino/Sensor-PietteTech.ino b/version2/src/others/ino/Sensor-PietteTech.ino new file mode 100644 index 0000000..ac065ca --- /dev/null +++ b/version2/src/others/ino/Sensor-PietteTech.ino @@ -0,0 +1,138 @@ +#include "Table.h" +#include "IoTString.h" +#include "TimingSingleton.h" +#include "TransactionStatus.h" +#include "PietteTech_DHT.h" + +// System defines +// PietteTech +#define DHTTYPE DHT22 // Sensor type DHT11/21/22/AM2301/AM2302 +#define DHTPIN D2 // Digital pin for communications +#define DHT_SAMPLE_INTERVAL 2000 // Sample every two seconds +// IoTCloud +#define SLEEP_TIME 30 // Sleep time in seconds + +// PietteTech +// Declaration +void dht_wrapper(); // must be declared before the lib initialization +// Library instantiation +PietteTech_DHT DHT(DHTPIN, DHTTYPE, dht_wrapper); + +// Globals +// PietteTech +unsigned int DHTnextSampleTime; // Next time we want to start sample +bool bDHTstarted; // flag to indicate we started acquisition +int n; // counter +// IoTCloud +TimingSingleton *timer; +bool foundError; +MyVector * transStatusList; +char keyBuffer[80]; +char dataBuffer[80]; +Table *t1; +IoTString *iKeyA; +IoTString *iValueA; + +void setup() { + // TODO: 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 + + // IoTCloud library + timer = TimingSingleton_getInstance(); + foundError = false; + transStatusList = new MyVector(); + IoTString *baseurl = new IoTString("http://dc-6.calit2.uci.edu/test.iotcloud/"); + IoTString *password = new IoTString("reallysecret"); + t1 = new Table(baseurl, password, 321, -1); + t1->rebuild(); + + baseurl->releaseRef(); + password->releaseRef(); + + // PietteTech DHT library + DHTnextSampleTime = 0; // Start the first sample immediately +} + +// This function gets and prints sensor status for debugging +void getSensorStatus(int result) { + + Serial.print("Read sensor: "); + switch (result) { + case DHTLIB_OK: + Serial.println("OK"); + break; + case DHTLIB_ERROR_CHECKSUM: + Serial.println("Error\n\r\tChecksum error"); + break; + case DHTLIB_ERROR_ISR_TIMEOUT: + Serial.println("Error\n\r\tISR time out error"); + break; + case DHTLIB_ERROR_RESPONSE_TIMEOUT: + Serial.println("Error\n\r\tResponse time out error"); + break; + case DHTLIB_ERROR_DATA_TIMEOUT: + Serial.println("Error\n\r\tData time out error"); + break; + case DHTLIB_ERROR_ACQUIRING: + Serial.println("Error\n\r\tAcquiring"); + break; + case DHTLIB_ERROR_DELTA: + Serial.println("Error\n\r\tDelta time to small"); + break; + case DHTLIB_ERROR_NOTSTARTED: + Serial.println("Error\n\r\tNot started"); + break; + default: + Serial.println("Unknown error"); + break; + } +} + +// This wrapper is in charge of calling +// must be defined like this for the library to work +void dht_wrapper() { + DHT.isrCallback(); +} + +void loop() { + + // Wait until sensor is ready + delay(2000); + // Check if we need to start the next sample + if (millis() > DHTnextSampleTime) { + + if (!bDHTstarted) { // Start the sample + DHT.acquire(); + bDHTstarted = true; + } + + // Has sample completed? + if (!DHT.acquiring()) { + // TODO: get DHT status (for debugging) + //int result = DHT.getStatus(); + //getSensorStatus(result); + + // Key + sprintf(keyBuffer, "sensor0"); + iKeyA = new IoTString(keyBuffer); + + float tempF = DHT.getFahrenheit(); + // Do updates for the temperature + sprintf(dataBuffer, "%f", tempF); + iValueA = new IoTString(dataBuffer); + + t1->startTransaction(); + t1->put(iKeyA, iValueA); + transStatusList->add(t1->commitTransaction()); + t1->update(); + + iKeyA->releaseRef(); + iValueA->releaseRef(); + System.sleep(SLEEP_MODE_DEEP, SLEEP_TIME); + } + } +} + diff --git a/version2/src/others/ino/Sensor.ino b/version2/src/others/ino/Sensor.ino new file mode 100644 index 0000000..c98c6f3 --- /dev/null +++ b/version2/src/others/ino/Sensor.ino @@ -0,0 +1,53 @@ +#include "Table.h" +#include "IoTString.h" +#include "TimingSingleton.h" +#include "TransactionStatus.h" + +TimingSingleton *timer; +bool foundError; +MyVector * transStatusList; +char keyBuffer[80]; +char dataBuffer[80]; + +Table *t1; +IoTString *iKeyA; +IoTString *iValueA; + +void setup() { + // TODO: This test uses 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 + Serial.begin(); + timer = TimingSingleton_getInstance(); + foundError = false; + transStatusList = new MyVector(); + IoTString *baseurl = new IoTString("http://dc-6.calit2.uci.edu/test.iotcloud/"); + IoTString *password = new IoTString("reallysecret"); + t1 = new Table(baseurl, password, 321, -1); + t1->rebuild(); + + baseurl->releaseRef(); + password->releaseRef(); +} + + +void loop() { + // Key + sprintf(keyBuffer, "sensor0"); + iKeyA = new IoTString(keyBuffer); + + // Do Updates for the keys + sprintf(dataBuffer, "data92617"); + iValueA = new IoTString(dataBuffer); + + t1->startTransaction(); + t1->put(iKeyA, iValueA); + transStatusList->add(t1->commitTransaction()); + t1->update(); + + iKeyA->releaseRef(); + iValueA->releaseRef(); + System.sleep(SLEEP_MODE_DEEP, 300); +} -- 2.34.1