Arduino DHT22 readings over serial

 Today I am going to add a DHT22 temperature humidity sensor to my existing battery voltage monitor project. Uploading and testing the code is done over USB cable, the HC-05 needs to be disconnected for this to work.

The DHT22 is connected to +5v, gnd and Digital Pin 10, I only chose pin10 as it is convenient for my veroboard layout.

The code for this is very simple :

<code>

#include "DHT.h"
#define DHTPIN 2 // The pin that your DHT is connected to
DHT dht(DHTPIN, DHT22);

void setup() {
dht.begin();
}

void loop(){
float h = dht.readHumidity();    //stores humidity value in float variable h
float t = dht.readTemperature();// temperature value
}

</code>

Don't forget to import the "DHT sensor library" in library manager in the Arduino IDE. 


We can then do whatever we need to with the values. I am going to send request them over serial via a HC-05 Bluetooth device from a Python script on a Raspberry Pi.  For testing  am using a laptop running Linux Mint. Please have a look at my other blog posts if you want more info about the code below.  

<code>

 #include "DHT.h"

#define DHTPIN 10 // The pin that your DHT is connected to
DHT dht(DHTPIN, DHT22);

const int pinA0 = A0;  // Analogue input pin that the battery is attached to

int vBattery = 0;       // This will be our voltage reading.
int incomingByte = 0;   //
void setup() {
  // initialize serial communications at 9600 bps:
  Serial.begin(9600);
  analogReference(EXTERNAL); // use AREF for reference voltage
  dht.begin();
  delay(1000);
  Serial.println("Request reading by sending a letter");
  Serial.println("Pin A0, Battery = b");
  Serial.println("Pin D10 DHT, Temp = t, Humidity = h, devide by 100 for value");
}

void loop() {
  // read the analogue value:
  vBattery = analogRead(pinA0);
  delay(250);                 // No need to stress anything
  if (Serial.available() > 0) {
  // read the incoming byte:
  incomingByte = Serial.read();
  if (incomingByte == 98) {
    Serial.println(vBattery);
  }
  if (incomingByte == 116) {  //t = temperature
    float t = dht.readTemperature();
    int(dt) = t * 100;        // I need to send an integer over serial not float
    Serial.println(dt);
  }
  if (incomingByte == 104) {  //h = humidity
    float h = dht.readHumidity();
    int(dh) = h *100;
    Serial.println(dh);
  }
 
  }
}
</code>

The Arduino checks for input on the serial connection, if it detects a 116 or 104 it then requests the value, converts it to an integer and sends it back over the serial connection. I sent t then h and receive the below.

Looking good so far, lets reconnect the HC-05 and edit the Python code to request the values.

<code>

import bluetooth
import time
import sys
bd_addr = "98:D3:61:F6:0B:E8"   #mac address of HC-05
port = 1
sock = bluetooth.BluetoothSocket( bluetooth.RFCOMM )
sock.connect((bd_addr, port))
print ("Connected")
sock.settimeout(5.0)    #will search for 5 seconds

def readValue(value):
    count = 0
    data = ""
    sock.send(value.to_bytes(8,'big'))      #sends passed value over bluetooth to HC-05
    while (count < 5):  #this ensures we receive the complete variable
        rcv = sock.recv(1024)           #receives data from the socket
        data = data + rcv.decode("utf-8")       #bytes to string
        if r"\r\n" in str(rcv):         #checks for \r\n in the bytes value
            return int(data)
        count += 1

#print(readValue(98))
print("Temperature = " + str(readValue(116)/100)) #Calls function with value, divides return by 100 and prints result.
print("Humidity = " + str(readValue(104)/100))

sock.close()

</code>

I have changed the while loop into a function so that we can request any required value.

The output is :



Comments

Popular posts from this blog

Intro and Welcome

ScreenGrab

Exchange 2013 Event ID:15021, OWA blank screen