Skip to content

Commit

Permalink
Merge pull request #2 from MecaHumArduino/Adafruit-dht
Browse files Browse the repository at this point in the history
Use Adafruit Unified Sensor Library
  • Loading branch information
isbkch committed Sep 25, 2020
2 parents 6de7c91 + 53decc6 commit a77a90b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 27 deletions.
2 changes: 1 addition & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ board = uno
framework = arduino
lib_extra_dirs = ~/Documents/Arduino/libraries
monitor_speed = 9600
serial_port = /dev/cu.usbserial-A50285BI
lib_deps = adafruit/Adafruit Unified Sensor@^1.1.4
65 changes: 39 additions & 26 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@
*/

#include <Arduino.h>
#include <dht11.h>
#include <DHT.h>
#include <LiquidCrystal.h>
#include <ArduinoJson.h> //https://github.com/bblanchon/ArduinoJson (use v6.xx)
#include <SoftwareSerial.h>

#define DEBUG true
#define DHT11PIN 4

dht11 sensor;
#define DHT_PIN 4 // pin connected to data pin of DHT11
#define DHT_TYPE DHT11 // Type of the DHT Sensor, DHT11/DHT22

DHT sensor(DHT_PIN, DHT_TYPE);

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
Expand All @@ -24,18 +26,20 @@ LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
SoftwareSerial wifi(2, 3);

// declaring custom function to follow C++ validation rules
String prepareDataForWiFi(float humidity, float temperature);
// **************
String sendDataToWiFi(String command, const int timeout, boolean debug);
String sendDataToWiFi(String command, const int timeout, boolean debug);
String prepareDataForWiFi(float humidity, float temperature, float headIndex);
// **************


String prepareDataForWiFi(float humidity, float temperature)
String prepareDataForWiFi(float humidity, float temperature, float headIndex)
{

StaticJsonDocument<200> doc;

doc["humidity"] = (String)humidity;
doc["temperature"] = (String)temperature;
doc["humidity"] = String(humidity);
doc["temperature"] = String(temperature);
doc["head_index"] = String(headIndex);

char jsonBuffer[512];
serializeJson(doc, jsonBuffer);
Expand Down Expand Up @@ -108,25 +112,34 @@ void loop() {
}

// read from the digital pin
int check = sensor.read(DHT11PIN);
float temperature = (float)sensor.temperature;
float humidity = (float)sensor.humidity;

// display Humidity on the LCD screen
lcd.setCursor(0, 0);
lcd.print("Humidity (%): ");
lcd.print(humidity, 2);

// display Temperature on the LCD screen
lcd.setCursor(0, 1);
lcd.print("Temp (C): ");
lcd.print(temperature, 2);

String preparedData = prepareDataForWiFi(humidity, temperature);
if (DEBUG == true) {
Serial.println(preparedData);
sensor.begin();

float temperature = sensor.readTemperature(); // return temperature in °C
float humidity = sensor.readHumidity(); // return humidity in %

// Compute heat index in Celsius (isFahrenheit = false)
float heatIndex = sensor.computeHeatIndex(temperature, humidity, false);

// check whether reading was successful or not
if (temperature == NAN || humidity == NAN) { // NAN means no available data
lcd.print("Reading failed.");
} else {
// display Humidity on the LCD screen
lcd.setCursor(0, 0);
lcd.print("Humidity (%): ");
lcd.print(String(humidity));

// display Temperature on the LCD screen
lcd.setCursor(0, 1);
lcd.print("Temp (C): ");
lcd.print(String(temperature));

String preparedData = prepareDataForWiFi(humidity, temperature, heatIndex);
if (DEBUG == true) {
Serial.println(preparedData);
}
sendDataToWiFi(preparedData, 1000, DEBUG);
}
sendDataToWiFi(preparedData, 1000, DEBUG);

delay(2000); // take measurements every 2 sec
}

0 comments on commit a77a90b

Please sign in to comment.