Arduino implements real-time voice broadcast of current temperature and humidity

    The temperature has been extremely high these days, and Chongqing has exceeded 44°C. Hangzhou, where I live, has also reached the highest temperature of 41.8°C since there are meteorological records. So on a whim, I made a simple and quick voice broadcast of the current measured temperature, and see the current temperature. What is the temperature?

    I happen to have a DHT11 temperature and humidity measurement module on hand. This module uses a dedicated digital module acquisition technology and temperature and humidity sensing technology to ensure that the product has extremely high reliability and excellent long-term stability. The sensor includes a resistive humidity sensing element and an NTC temperature measuring element, and is connected with a high-performance 8-bit microcontroller. Therefore, the product has the advantages of excellent quality, ultra-fast response, strong anti-interference ability, and high cost performance. Each DHT11 sensor is calibrated in an extremely precise humidity calibration chamber. The calibration coefficients are stored in the OTP memory in the form of a program, and these calibration coefficients are called in the sensor during the processing of the detection signal. Single-wire serial interface makes system integration easy and fast. Ultra-small size, extremely low power consumption, the signal transmission distance can reach more than 20 meters. The key is that the price is very cheap, you can buy it for less than 3 yuan on a certain treasure, and it also includes free shipping.

    Okay, let’s not talk about gossip, the voice broadcast module uses LU-ASR01, although LU-ASR01 is an offline intelligent voice recognition module, but it is also very good as a voice broadcast module.

    The hardware is an Arduino UNO, a DHT11 temperature and humidity measurement module, a LU-ASR01 offline intelligent voice recognition module, and then several DuPont cables.

    Hardware connection method: Arduino still uses hardware serial communication, using TX and RX ports, LU-ASR01 uses IO6 port as soft TX for sending data, and IO7 port as soft RX for receiving data. Arduino provides 5V power supply to LU-ASR01 (red and black 2 wires in the figure below), Arduino’s TX is connected to LU-ASR01’s IO7 port (soft RX), Arduino’s RX is connected to LU-ASR01’s IO6 port (soft TX ), DHT11’s 1 pin (power positive pole) is connected to Arduino’s 3.3V (DHT11 can work at 3.3V or 5V), 4 pins (ground) are connected to Arduino’s ground, 2 pins (DATA) are connected to Arduino’s D12. The connection diagram is as follows:

     In order to simplify the program writing, the program on the Arduino side calls a simple library file DHT.h for reading DHT11 data. The library file can be downloaded from the website: https://github.com/markruys/arduino-DHT, and the download is completely free Yes, there is no need to register, log in, etc. After opening the webpage, click the green box "Code" to drop down, and then click the last item "Download ZIP". The web interface is shown in the figure below:

    We still use 3-byte messages for communication (for details, please refer to "Arduino's Attempt to Realize Serial Communication Through Simple Messages" written by me before ), this time we have customized 2 messages sent from Arduino to LU-ASR01 Command, one is a message to send temperature data, the format is defined as follows:

        The first byte: 0x23 (35 in decimal) to send temperature data

        Byte 2: Integer part of temperature

        Byte 3: 2 decimal places for temperature

    Article 2 is a message for sending humidity data, the format is defined as follows:

        The first byte: 0x24 (36 in decimal) to send humidity data

        Byte 2: Integer part of humidity

        Byte 3: 2 decimal places for humidity

    In addition, a message command sent by LU-ASR01 to Arduino is defined, requesting Arduino to send the current temperature or humidity. The message format is defined as follows:

        The first byte: 0x25 (37 in decimal) requests to send the current temperature or humidity

        Byte 2: not used

        The third byte: =1 requires sending temperature, =2 requires sending humidity

    When power on, call "current temperature", then LU-ASR01 sends a request temperature data message, Arduino reads the temperature value of DHT11 after receiving it, and sends it to LU-ASR01, LU-ASR01 receives the temperature data Then broadcast "the current temperature is XX degrees". You can also get the humidity report by calling "current humidity".

    The complete program on the Arduino side is as follows:

/*

   This program obtains the current temperature and humidity through DHT11, and connects with ASR01 through serial communication, and then broadcasts by ASR01

   Current temperature and humidity.

   The communication message length is 3 bytes, and the message format is:

   The first byte: command number The second and third bytes: data

   The message of this program uses three commands:

   Send temperature message: number: 0x23 (byte 1), byte 2 temperature integer part, byte 3 temperature 2 decimal places

   Send humidity message: number: 0x24 (byte 1), byte 2 humidity integer part, byte 3 humidity 2 decimal places

   Receive request to send temperature and humidity message: number: 0x25 (byte 1), byte 2 is not used, byte 3 =1 temperature, =2 humidity

*/

#include "DHT.h"

DHT dht;

#define MLEN 3 //Define the message length as 3

const int DHTPin = 12; // Define the pin connected to DHTPin as D12

unsigned char Txbyte[MLEN]; //The character data sent by the serial port, the length is MLEN

unsigned char Rxbyte[MLEN]; //The character data read by the serial port, the length is MLEN

float thtmp; //

//initialization

void setup() {

  Serial.begin(9600); //Set the serial port baud rate to 9600

  pinMode(DHTPin, INPUT); //Set DHTPin    

  dht.setup(DHTPin); // Set the pin for DHT11 data transmission

}

//Send message subroutine

//The parameter txb is the message data to be sent, and n is the message length

void txmss(unsigned char txb[],int n){

  int i;

  for(i=0;i<n;++i){

    Serial.write(txb[i]);

    delay(2);  

  }

}

//Receive message subroutine, the return value is true if the message is successfully received, otherwise it is false

//The parameter rxb is the returned message data, n is the message length

bool rxmss(unsigned char rxb[],int n){

int i,dn; //dn is the timeout counter

  for (i=0; i<n; ++i) {

    dn=0; //Reset the timeout count variable

    while(1){   //  

      if (dn<=50){ //Set the maximum time for waiting to read a byte to 50ms, if there is no timeout

        if(Serial.available() > 0){ //When the serial buffer has data

          rxb[i]=Serial.read(); //read a byte

          break; //Break out of while(1) loop

        }

        else{ //If there is no data in the serial port buffer and there is no timeout, then the timeout counter will be +1

          delay(1);

          dn+=1; //timeout counter +1

        }

      }

      else return false; //When receiving timeout, the function rxmss exits and returns false

    }

  }

  return true; // normally receive n bytes, the function rxmss returns true

}

//main program

void loop() {

  delay(dht.getMinimumSamplingPeriod()); //Get the minimum sampling period of DHT11

  if (Serial.available() > 0){ //When the serial buffer has data

    if (rxmss(Rxbyte,MLEN)){ //If a message is successfully received (that is, the 3-byte data has been received in the array Rxbyte)

      if(Rxbyte[0]==0x25) { //If the received Rxbyte[0] is 0x25, it is required to send the temperature and humidity command

        if(Rxbyte[2]==1){ //What is required to send is temperature

          // fill in the message

          Txbyte[0]=0x23; //command code 0x23 means sending temperature data

          thtmp=(float) dht.getTemperature(); //Read temperature from DHT11

          Txbyte[1]=(int) thtmp; //The integer part of the temperature is placed in Txbyte[1]

          Txbyte[2]=(int) (thtmp-Txbyte[1]*100); //Two decimal places of temperature are placed in Txbyte[2]

          txmss(Txbyte,MLEN); //Send message, send temperature data to LU-ASR01

        }

        if(Rxbyte[2]==2){ //What is required to send is humidity

          // fill in the message

          Txbyte[0]=0x24; //command code 0x24 means sending temperature data

          thtmp=(float) dht.getHumidity(); //Read humidity from DHT11

          Txbyte[1]=(int) thtmp; //The integer part of the humidity is placed in Txbyte[1]

          Txbyte[2]=(int) (thtmp-Txbyte[1]*100); //Two decimal places of humidity are placed in Txbyte[2]

          txmss(Txbyte,MLEN); //Send message, send humidity data to LU-ASR01

        }

      }

    }

  }

}

    LU-ASR01 still adopts the graphical programming platform of "Tianwen block", the following is the complete program on "Tianwen block":

     After I finished, I immediately took it outside to test it, and it reached 45 degrees all of a sudden, it was too hot, so I quickly terminated the test and fled back to the air-conditioned room, haha...

My online name is "Mr. Unforgiveness". The articles published on CSDN are all my original works, and they are only published on the CSDN website. The reprints on other websites have not been authorized by me.

Guess you like

Origin blog.csdn.net/m0_61543203/article/details/126368384