Get data from dht22 with hm10

Hello, I am not to clued up on app inventor and am currently creating my first project.
I have no idea where to start with receiving data from a dht22 temp and humidity sensor via hm10 ble. Can anyone point me in the correct direction on how to receive and display temperature and humidity values.
Thanks

Also try the search facility if you need more, looking for tags hm10 or dht22.

1 Like

@Tommy_Benham examples with HM-10 and Arduino UNO.

1 Like

Do you already have connected and configured arduino, dht22 sensor and hm10 module?

1 Like

Hello I have an arduino configured with an hm10 controlling 8 relays via app inventor.
I am struggling to teach myself how to add a dht22 sensor into the app to display humidity and temperature. I am very new to this and have been relying on google.

So based on google you need to add the dht22 library to your code. Then, export the data from the sensor to the uart where the hm10 module is connected. The rest to be done on the appinventor side.

1 Like

I have been following Martyn Currey Arduino, HM-10 and App Inventor 2 guide to get me to this stage

1 Like

Hello, I tried this but the language difference was confusing me even further, thank you

thank you, I have been using this from the start but just struggling with sensor data

Thank you, I have this built into my arduino code and can see humidity and and temp data in monitor. How do I transfer this data to my app inventor app using hm10. I have spend hours trying to find instructions on how to do this, I have no idea which doesn't help

So paste your arduino code here so you can see what you are doing with the data on the arduino side. Then we will adjust the appinventor blocks.

1 Like

I thought I had incorporated the two codes together, turns out I haven't successfully. When incorporated with the existing code I loose control of relays.
It is code used from Martyn project, Some parts are not required like switch......I haven't cleaned it up (which probably isn't helping)
I have pasted the code I need to integrate dht22 with

/*

  • Arduino, HM-10, App Inventor 2
  • Example Project Part 5: 3 leds and 3 switches
  • By Martyn Currey. www.martyncurrey.com
  • Turn LEDs on and off from an Android app via a HM-10 or from a physical switches connected to the Arduino
  • Uses the following pins
  • D9 - AltsoftSerial RX
  • D8 - AltsoftSerial TX
  • D7 - LED + resistor
  • D6 - LED + resistor
  • D5 - LED + resistor
  • D4 - Button switch
  • D3 - Button switch
  • D2 - Button switch
    */

// AltSoftSerial uses D9 for RX and D8 for TX. While using AltSoftSerial D10 cannot be used for PWM.
// Remember to use a voltage divider on the Arduino TX pin / Bluetooth RX pin
// Download from https://www.pjrc.com/teensy/td_libs_AltSoftSerial.html

#include <AltSoftSerial.h>
AltSoftSerial BTserial;

// Variables used for incoming data
const byte maxDataLength = 20;
char receivedChars[21] ;
boolean newData = false;

// Constants for hardware
const byte SWITCH_PIN[] = {};
const byte LED_PIN[] = {2,3,4,5,6,7,11,12};

// general variables
boolean LED_State[] = {false,false,false,false};
boolean switch_State[] = {false,false,false,false};
boolean oldswitch_State[] = {false,false,false,false};

void setup()
{
for (byte pin = 0; pin < 8; pin++)
{
// Set the button switch pins for input
pinMode(SWITCH_PIN[pin], INPUT);

     // Set the LED pins for output and make them LOW
     pinMode(LED_PIN[pin], OUTPUT);  digitalWrite(LED_PIN[pin],LOW);
}

// open serial communication for debugging
Serial.begin(9600);
Serial.print("Sketch:   ");   Serial.println(__FILE__);
Serial.print("Uploaded: ");   Serial.println(__DATE__);
Serial.println(" ");

//  open software serial connection to the Bluetooth module.
BTserial.begin(9600); 
Serial.println("AltSoftSerial started at 9600"); 

newData = false;

} // void setup()

void loop()
{
for (byte switchNum = 1; switchNum < 1; switchNum++)
{
checkSwitch(switchNum);
}
recvWithStartEndMarkers(); // check to see if we have received any new commands
if (newData) { processCommand(); } // if we have a new command do something about it
}

/*


  • Function checkSwitch()
  • checks the status of a button switch and turns an LED on or off depending on the switch status
  • passed:
  • global:
  •  switch_State[]
    
  •  LED_State[]
    
  •  oldswitch_State[]
    
  • Returns:
  • Sets:
  •  switch_State[]
    
  •  LED_State[]
    
  •  oldswitch_State[]
    

*/
void checkSwitch( byte pos)
{
// pos = 1,2,3. Array pos = 0,1,2 so convert by subtracting 1
pos = pos-1;

 // very simple debouce.
 boolean state1 = digitalRead(SWITCH_PIN[pos]); delay(1);
 boolean state2 = digitalRead(SWITCH_PIN[pos]); delay(1);
 boolean state3 = digitalRead(SWITCH_PIN[pos]); delay(1);
 if ((state1 == state2) && (state1==state3))   
 { 
      switch_State[pos] = state1;  
      if ( (switch_State[pos] == HIGH) && (oldswitch_State[pos] == LOW) )
      {
           LED_State[pos] = ! LED_State[pos];  // flip the status.

           // Rather than have a long list of possible commands I create the command as required.
           // Values in the temp command are replaced depending on which button switch has been pressed.
           char TMPcmd[8] = "[L,1,0]";
           TMPcmd[3] = pos+1+48;             // pos+1 should be 1,2, or 3.   convert a numeric value to ascii by adding 48. 
           TMPcmd[5] = LED_State[pos]+48;    // LED_State should be 0 or 1
           BTserial.print(TMPcmd);

           digitalWrite(LED_PIN[pos],LED_State[pos]);  
           Serial.println(TMPcmd);                    
      }          
      oldswitch_State[pos] = switch_State[pos];
  }

}

/*


  • Function processCommand
  • parses data commands contained in receivedChars[]
  • receivedChars[] has not been checked for errors
  • passed:
  • global:
  •   receivedChars[]
    
  •   newData
    
  • Returns:
  • Sets:
  •   receivedChars[]
    
  •   newData
    

*/
void processCommand()
{
Serial.print("receivedChars = "); Serial.println(receivedChars);

if (receivedChars[0] == 'L')      // do we have a LED command?
{
    // we know the LED command has a fixed length "L10"
    // and the value at pos 1 is the LED and the value at pos 2 is 0 or 1 (on/off). 
    // 0 and 1 is the same as LOW and HIGH so we can use 0/1 instead of LOW/HIGH

    byte LEDnum = receivedChars[1] - 48;          // convert ascii to value by subtracting 48
    boolean LEDstatus = receivedChars[2] - 48;

    digitalWrite(LED_PIN[LEDnum-1],LEDstatus);
    LED_State[LEDnum-1] = LEDstatus;
}

receivedChars[0] = '\0';
newData = false;

}

// function recvWithStartEndMarkers by Robin2 of the Arduino forums
// See http://forum.arduino.cc/index.php?topic=288234.0
/*


  • Function recvWithStartEndMarkers
  • reads serial data and returns the content between a start marker and an end marker.
  • passed:
  • global:
  •   receivedChars[]
    
  •   newData
    
  • Returns:
  • Sets:
  •   newData
    
  •   receivedChars
    

*/
void recvWithStartEndMarkers()
{
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '[';
char endMarker = ']';
char rc;

 if (BTserial.available() > 0) 
 {
      rc = BTserial.read();
      if (recvInProgress == true) 
      {
           if (rc != endMarker) 
           {
                receivedChars[ndx] = rc;
                ndx++;
                if (ndx > maxDataLength) { ndx = maxDataLength; }
           }
           else 
           {
                 receivedChars[ndx] = '\0'; // terminate the string
                 recvInProgress = false;
                 ndx = 0;
                 newData = true;
           }
      }
      else if (rc == startMarker) { recvInProgress = true; }
 }

}

  • D12 - Relay
  • D11 - Relay
  • D9 - AltsoftSerial RX
  • D8 - AltsoftSerial TX
  • D7 - Relay
  • D6 - Relay
  • D5 - Relay
  • D4 - Relay
  • D3 - Relay
  • D2 - Relay

Upload this code to arduino. I didn't change anything in your code, I just added DHT. The link to the ZIP library is in the code.

/*

Arduino, HM-10, App Inventor 2
Example Project Part 5: 3 leds and 3 switches
By Martyn Currey. www.martyncurrey.com
Turn LEDs on and off from an Android app via a HM-10 or from a physical switches connected to the Arduino
Uses the following pins
D10 - DHT22 data
D9 - AltsoftSerial RX
D8 - AltsoftSerial TX
D7 - LED + resistor
D6 - LED + resistor
D5 - LED + resistor
D4 - Button switch
D3 - Button switch
D2 - Button switch
*/
// AltSoftSerial uses D9 for RX and D8 for TX. While using AltSoftSerial D10 cannot be used for PWM.
// Remember to use a voltage divider on the Arduino TX pin / Bluetooth RX pin
// Download from https://www.pjrc.com/teensy/td_libs_AltSoftSerial.html

// DHT22 uses D10 for data.
// Download from https://botland.com.pl/index.php?controller=attachment&id_attachment=1211

#include <AltSoftSerial.h>
#include <dht.h>;

AltSoftSerial BTserial;

dht DHT22;
#define DHT22PIN 10



// Variables used for incoming data
const byte maxDataLength = 20;
char receivedChars[21] ;
boolean newData = false;

// Constants for hardware
const byte SWITCH_PIN[] = {};
const byte LED_PIN[] = {2,3,4,5,6,7,11,12};

// general variables
boolean LED_State[] = {false,false,false,false};
boolean switch_State[] = {false,false,false,false};
boolean oldswitch_State[] = {false,false,false,false};
unsigned long memory_Time = 0;

void setup()
{
for (byte pin = 0; pin < 8; pin++)
{
// Set the button switch pins for input
pinMode(SWITCH_PIN[pin], INPUT);

     // Set the LED pins for output and make them LOW
     pinMode(LED_PIN[pin], OUTPUT);  digitalWrite(LED_PIN[pin],LOW);
}

// open serial communication for debugging
Serial.begin(9600);
Serial.print("Sketch:   ");   Serial.println(__FILE__);
Serial.print("Uploaded: ");   Serial.println(__DATE__);
Serial.println(" ");

//  open software serial connection to the Bluetooth module.
BTserial.begin(9600); 
Serial.println("AltSoftSerial started at 9600"); 

newData = false;
} // void setup()

void loop()
{
for (byte switchNum = 1; switchNum < 1; switchNum++)
{
checkSwitch(switchNum);
}
recvWithStartEndMarkers(); // check to see if we have received any new commands
if (newData) { processCommand(); } // if we have a new command do something about it

if ((millis() - memory_Time) >= 1000UL){

//Read data and store it to variables hum and temp
    
  BTserial.print("Humidity (%): ");              
  BTserial.print((float)DHT22.humidity, 2);
  BTserial.print(",");
  BTserial.print("Temp. (C): ");           
  BTserial.println((float)DHT22.temperature, 2);
  memory_Time = millis();
  }
    

}

/*

Function checkSwitch()
checks the status of a button switch and turns an LED on or off depending on the switch status
passed:
global:
 switch_State[]
 LED_State[]
 oldswitch_State[]
Returns:
Sets:
 switch_State[]
 LED_State[]
 oldswitch_State[]
*/
void checkSwitch( byte pos)
{
// pos = 1,2,3. Array pos = 0,1,2 so convert by subtracting 1
pos = pos-1;

 // very simple debouce.
 boolean state1 = digitalRead(SWITCH_PIN[pos]); delay(1);
 boolean state2 = digitalRead(SWITCH_PIN[pos]); delay(1);
 boolean state3 = digitalRead(SWITCH_PIN[pos]); delay(1);
 if ((state1 == state2) && (state1==state3))   
 { 
      switch_State[pos] = state1;  
      if ( (switch_State[pos] == HIGH) && (oldswitch_State[pos] == LOW) )
      {
           LED_State[pos] = ! LED_State[pos];  // flip the status.

           // Rather than have a long list of possible commands I create the command as required.
           // Values in the temp command are replaced depending on which button switch has been pressed.
           char TMPcmd[8] = "[L,1,0]";
           TMPcmd[3] = pos+1+48;             // pos+1 should be 1,2, or 3.   convert a numeric value to ascii by adding 48. 
           TMPcmd[5] = LED_State[pos]+48;    // LED_State should be 0 or 1
           BTserial.print(TMPcmd);

           digitalWrite(LED_PIN[pos],LED_State[pos]);  
           Serial.println(TMPcmd);                    
      }          
      oldswitch_State[pos] = switch_State[pos];
  }
}

/*

Function processCommand
parses data commands contained in receivedChars[]
receivedChars[] has not been checked for errors
passed:
global:
  receivedChars[]
  newData
Returns:
Sets:
  receivedChars[]
  newData
*/
void processCommand()
{
Serial.print("receivedChars = "); Serial.println(receivedChars);

if (receivedChars[0] == 'L')      // do we have a LED command?
{
    // we know the LED command has a fixed length "L10"
    // and the value at pos 1 is the LED and the value at pos 2 is 0 or 1 (on/off). 
    // 0 and 1 is the same as LOW and HIGH so we can use 0/1 instead of LOW/HIGH

    byte LEDnum = receivedChars[1] - 48;          // convert ascii to value by subtracting 48
    boolean LEDstatus = receivedChars[2] - 48;

    digitalWrite(LED_PIN[LEDnum-1],LEDstatus);
    LED_State[LEDnum-1] = LEDstatus;
}

receivedChars[0] = '\0';
newData = false;
}

// function recvWithStartEndMarkers by Robin2 of the Arduino forums
// See http://forum.arduino.cc/index.php?topic=288234.0
/*

Function recvWithStartEndMarkers
reads serial data and returns the content between a start marker and an end marker.
passed:
global:
  receivedChars[]
  newData
Returns:
Sets:
  newData
  receivedChars
*/
void recvWithStartEndMarkers()
{
static boolean recvInProgress = false;
static byte ndx = 0;
char startMarker = '[';
char endMarker = ']';
char rc;

 if (BTserial.available() > 0) 
 {
      rc = BTserial.read();
      if (recvInProgress == true) 
      {
           if (rc != endMarker) 
           {
                receivedChars[ndx] = rc;
                ndx++;
                if (ndx > maxDataLength) { ndx = maxDataLength; }
           }
           else 
           {
                 receivedChars[ndx] = '\0'; // terminate the string
                 recvInProgress = false;
                 ndx = 0;
                 newData = true;
           }
      }
      else if (rc == startMarker) { recvInProgress = true; }
 }
}
1 Like

that's brilliant thank you, I'm learning slowly :laughing:

How to I send and read the temp and humidity in app inventor?

I really appreciate your help

Thank you

I think you already have blocks in your application that connect to the hm10 module?

1 Like

Yes, I am connected and can turn relays on & off via toggle buttons in the app. Im just unsure how to take the temp and humidity data and display in app.

This is where I currently am in the app

CI_ADDING_TEMP.aia (978.1 KB)

After connecting to the module, register the receiver to receive the strings.

You also need to add a string receiver in which we process the data and display it on the labels.

1 Like