Skip to content

Instantly share code, notes, and snippets.

@BenDLH
Created December 11, 2016 13:12
Show Gist options
  • Save BenDLH/a3bd4b37831565b054640d43f5d9a2bc to your computer and use it in GitHub Desktop.
Save BenDLH/a3bd4b37831565b054640d43f5d9a2bc to your computer and use it in GitHub Desktop.
Reads temp and humidity from DHT22 sensor and writes values to LCD panel, Particle server and Serial.
#include "Adafruit_DHT.h"
#include "LiquidCrystal.h"
//
// Ben De La Haye
// CloudTemp-v5.ino
//
// Reads temp and humidity from DHT22 sensor and writes values to LCD panel, Particle
// server and Serial.
#define TEMP_SENSOR_PIN 0 // DHT22 Sensor
#define SERVO_PIN A4
DHT tempSensor(TEMP_SENSOR_PIN, DHT22);
LiquidCrystal lcdPanel(7, 6, 4, 3, 2, 1);
Servo servo;
double temp;
double humidity;
int heaterState;
void setup() {
// Start Serial
Serial.begin(9600);
Serial.println("CloudTemp-v5.ino");
// Start up the temp sensor
tempSensor.begin();
// Start up the LCD
lcdPanel.begin(16, 2);
// Set up the servo
servo.attach(SERVO_PIN);
// Setup Particle variables
Particle.variable("temperature", temp);
Particle.variable("humidity", humidity);
Particle.variable("heaterState", heaterState);
// Setup cloud function
Particle.function("setHeater", setHeaterState);
}
void loop() {
// We cannot read from the temperature sensor more than once a second
// and Particle has a 4 second publish burst limit
delay(4000);
// Read from the sensor
temp = (double) tempSensor.getTempCelcius();
humidity = (double) tempSensor.getHumidity();
// Read from the servo
heaterState = servo.read() == 0;
// Make sure we have a valid reading
if (isnan(humidity) || isnan(temp)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Print values to Serial
Serial.print("Temp: ");
Serial.print(temp, 1);
Serial.print("C, Humidity: ");
Serial.print(humidity, 1);
Serial.println("%");
printToLcd(temp, humidity);
}
// Particle Cloud function
int setHeaterState(String state) {
Serial.print("setHeaterState(");
Serial.print(state);
Serial.println(")");
if (state == "true") {
servo.write(0);
heaterState = 1;
return 0;
} else if (state == "false") {
servo.write(180);
heaterState = 0;
return 180;
}
return -1;
}
void printToLcd(double tepm, double humidity) {
// Print temp to LCD
lcdPanel.setCursor(0, 0);
lcdPanel.print("Temp: ");
lcdPanel.print(temp, 1);
lcdPanel.print("C");
// Print humidity to LCD
lcdPanel.setCursor(0, 1);
lcdPanel.print("Humidity: ");
lcdPanel.print(humidity, 1);
lcdPanel.print("%");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment