当前位置:网站首页>Measure the current temperature

Measure the current temperature

2022-06-25 08:22:00 yangsf_

Measure the current temperature

We use Mongoose OS + esp8266 + DHT11 To do it .

  • Mongoose OS: An Internet of things The firmware Development framework . Official documents

  • esp8266:ESP8266 Is a A serial port WiFi modular .

  • DHT11: A temperature sensor .

  • The firmware : It can be understood as the operating system of an electronic product , It is the software at the bottom of the hardware .

  • A serial port : Look at the picture directly

     Insert picture description here

     Insert picture description here

    You can send eight bits of data at one time , Mutual interference , If one bit of parallel port transmission error occurs, it is necessary to resend the eight bit data , One bit of serial port transmission error only needs to be sent again .

One 、Hello World

  1. download mongoose OS The official tool of mos.exe, Double click or enter a command mos ui You can start it UI Interface . If it doesn't start , Open Command Prompt , Input cd c:\mos then mos --start-webview=false. Need to close when closing 1992 Service of port ( Otherwise, double clicking again will not open ui Interface ).

     Insert picture description here

  2. With a data cable ( It must be a data line that can transmit data , Some cables can only be charged ) Connect esp8266, Install the corresponding driver according to your own requirements .

    Installation driver :1. Right click this computer -> management -> Device manager -> Other equipment

    The yellow exclamation point is the driver we need to install , After installation, you can see which port the device is on in the port .

  3. open mos, Select port and model , The port and model shall be selected according to the actual situation , I use it esp8266 2m All choices of memory esp8266 flash 2m.

  4. stay ui There is a line of text box below which you can enter commands , Input mos clone https://github.com/mongoose-os-apps/demo-c app1 Clone a template . After cloning, we can see a in the corresponding directory app1 Folder , The contents of this catalog are src In the catalog main.c It's our code ,mos.yml It's a configuration file , Describe the entire application .

  5. open main.c, find  Insert picture description here

    It is amended as follows :

     Insert picture description here

  6. stay mos Of ui Input... In the interface mos build Compile code .

  7. After a long wait, enter mos flash Brush the firmware into .

  8. One sentence per second will be output after success hello world!

     Insert picture description here

Two 、 Use DHT11 Thermometry

First put our dht11 Connect to esp8266 On ,vcc or + Connect 3v The pin of , GND or - even GND, DAT or out even IO Pin (IO Just remember , Like I take it IO2 Pin , It will be configured as IO2 Pin ).

esp8266 Pin figure :

 View the source image

After the connection is completed, we just need to hello world The program is slightly modified :

  1. Add dependency :

    stay mos.yml Of libs Add below dht Driven dependency ( These drivers can be used in mongoose Find... On the official website ), if necessary rpc Services can be added rpc rely on .

    libs:
      - location: https://github.com/mongoose-os-libs/boards
      - location: https://github.com/mongoose-os-libs/demo-bundle
      - location: https://github.com/mongoose-os-libs/rpc-service-config
      - location: https://github.com/mongoose-os-libs/rpc-service-fs
      - location: https://github.com/mongoose-os-libs/rpc-uart
      - location: https://github.com/mongoose-os-libs/wifi
      - location: https://github.com/mongoose-os-libs/dht
    
  2. Configure pins , Modify the code :

    stay mos.yml Add :

    config_schema:
      - ["app.pin", "i", 2, {
          title: "GPIO pin a sensor is attached to"}]
      # Because just now I was io2 Pin , So this is 2
    

    main.c:

    #include "mgos.h"
    #include "mgos_dht.h"
    
    static void timer_cb(void *dht) {
          
      LOG(LL_INFO, ("Temperature: %lf", mgos_dht_get_temp(dht)));
    }
    
    
    enum mgos_app_init_result mgos_app_init(void) {
          
      struct mgos_dht *dht = mgos_dht_create(mgos_sys_config_get_app_pin(), DHT11);
      mgos_set_timer(1000, true, timer_cb, dht);
      return MGOS_APP_INIT_SUCCESS;
    }
    

    It seems complicated , There are actually only two important functions :

    mgos_dht_create(mgos_sys_config_get_app_pin(), DHT11);

    mgos_dht_get_temp(dht)

    initialization dht And get the temperature .

  3. compile & Firmware brush in

    stay mos Of ui Input... In the interface mos build After successful compilation, enter mos flash Brush in the firmware .

  4. effect :

     Insert picture description here

原网站

版权声明
本文为[yangsf_]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/176/202206250646599035.html