Hi all!
I need Your help getting temperature and humidity reading from DHT11 sensor. I have tried a lot of different things and none of them work
I have Electron upgraded to device OS 1.4.4
I’ve connected LED to D6, Photosensor to D5 and D0 (as per manuals). I’ve also added a sound sensor to A1. All of these seem to work. I then added DHT11 connected to D2 with 10kOm pull-up to 3v3.
On the Particle desktop IDE:
I’ve tried Adafruit_DHT 0.0.4 and PietteTech_DHT 0.0.12 and flashing their corresponding simple examples (after selecting DHT11 device and D2 pin). I then connect to serial but see nothing at all! I’ve checked all COM ports and don’t see anything with putty either.
On the web IDE:
I want to get the values into Particle variables. So once I read the values I then convert them to string and send them to the variables. With Adafruit I always got 255 for both variables, with Piette I always get -7!
What am I missing here?
Sidenote - why can’t I see variables and functions on the console if I flash the same code from desktop IDE?
Thanks for any help!
// This #include statement was automatically added by the Particle IDE.
#include <PietteTech_DHT.h>
#define DHTTYPE DHT11 // Sensor type DHT11/21/22/AM2301/AM2302
#define DHTPIN D2 // Digital pin for communications
// We're going to start by declaring which pins everything is plugged into.
int led = D6; // This is where your LED is plugged in. The other side goes to a resistor connected to GND.
int photoresistor = A0; // This is where your photoresistor is plugged in. The other side goes to the "power" pin (below).
int microphone = A1;
int power = A5; // This is the other end of your photoresistor. The other side is plugged into the "photoresistor" pin (above).
// The reason we have plugged one side into an analog pin instead of to "power" is because we want a very steady voltage to be sent to the photoresistor.
// That way, when we read the value from the other side of the photoresistor, we can accurately calculate a voltage drop.
int lightlevel; // Here we are declaring the integer variable analogvalue, which we will use later to store the value of the photoresistor.
int soundvolume;
float h;
float t;
char h_char[10];
char t_char[10];
PietteTech_DHT DHT(DHTPIN, DHTTYPE);
// Next we go into the setup function.
void setup() {
// First, declare all of our pins. This lets our device know which ones will be used for outputting voltage, and which ones will read incoming voltage.
pinMode(led,OUTPUT); // Our LED pin is output (lighting up the LED)
pinMode(photoresistor,INPUT); // Our photoresistor pin is input (reading the photoresistor)
pinMode(microphone,INPUT);
pinMode(power,OUTPUT); // The pin powering the photoresistor is output (sending out consistent power)
// Next, write the power of the photoresistor to be the maximum possible, so that we can use this for power.
digitalWrite(power,HIGH);
// We are going to declare a Particle.variable() here so that we can access the value of the photoresistor from the cloud.
Particle.variable("light_level", lightlevel);
Particle.variable("sound_volume", soundvolume);
Particle.variable("temperature", t_char);
Particle.variable("humidity", h_char);
// This is saying that when we ask the cloud for "analogvalue", this will reference the variable analogvalue in this app, which is an integer variable.
// We are also going to declare a Particle.function so that we can turn the LED on and off from the cloud.
Particle.function("led",ledToggle);
// This is saying that when we ask the cloud for the function "led", it will employ the function ledToggle() from this app.
DHT.begin();
}
// Next is the loop function...
void loop() {
delay(2500);
// check to see what the value of the photoresistor is and store it in the int variable analogvalue
lightlevel = analogRead(photoresistor);
soundvolume = analogRead(microphone);
h = DHT.getHumidity();
// Read temperature as Celsius
t = DHT.getCelsius();
sprintf(t_char, "%.2f", t);
sprintf(h_char, "%.2f", h);
}
// Finally, we will write out our ledToggle function, which is referenced by the Particle.function() called "led"
int ledToggle(String command) {
if (command=="on") {
digitalWrite(led,HIGH);
return 1;
}
else if (command=="off") {
digitalWrite(led,LOW);
return 0;
}
else {
return -1;
}
}