乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      樹莓派進(jìn)階之路 (015) - 樹莓派使用DS18B20模塊測(cè)量溫度

       戴維圖書館 2019-05-08

      參考:http://shumeipai./2013/10/03/raspberry-pi-temperature-sensor-monitors.html

      第一步,允許單總線接口

      sudo raspi-config進(jìn)入interfacingoptions

       

      enable 1-wire interface

       

      第二步,接線 

      接BCM編碼為4即圖上物理引腳7

      第三步,升級(jí)內(nèi)核

      sudo apt-get update
      sudo apt-get upgrade
      pi@raspberrypi:~$ cd /sys/bus/w1/devices/
      pi@raspberrypi:/sys/bus/w1/devices$ ls
      28-00000xxxxxx w1_bus_master1

      第四步,查看當(dāng)前溫度

      復(fù)制代碼
      cd 28-00000xxxxxx cat w1_slave
      顯示:
      46 01 4b 46 7f ff 0c 10 2f : crc=2f YES
      46 01 4b 46 7f ff 0c 10 2f t=20375
      第二行的t=20375就是當(dāng)前的溫度值,要換算成攝氏度,除以1000,即當(dāng)前溫度為20
      復(fù)制代碼

       

      python

      復(fù)制代碼
       1 #!/usr/bin/python3
       2 import os,time
       3 
       4 device_file ='/sys/bus/w1/devices/28-031681e171ff/w1_slave'
       5 
       6 def read_temp_raw():
       7     f = open(device_file,'r')
       8     lines = f.readlines()
       9     f.close()
      10     return lines
      11 
      12 def read_temp():
      13     lines = read_temp_raw()
      14     while lines[0].strip()[-3:] != 'YES':
      15         time.sleep(0.2)
      16         lines = read_temp_raw()
      17     equals_pos = lines[1].find('t=')
      18     if equals_pos != -1:
      19         temp_string = lines[1][equals_pos+2:]
      20         temp_c = float(temp_string)/1000.0
      21     return temp_c
      22 
      23 while True:
      24     print('temp C = %f'%read_temp())
      25     time.sleep(1)
      復(fù)制代碼

      打印結(jié)果:

      復(fù)制代碼
      1 pi@raspberrypi:~/myPython $ ./temp_ds18b20.py 
      2 temp C = 20.687000
      3 temp C = 20.687000
      4 temp C = 20.687000
      5 temp C = 20.750000
      6 temp C = 20.750000
      7 temp C = 20.750000
      復(fù)制代碼

       

      C語言代碼:

      復(fù)制代碼
       1 #include <stdio.h>
       2 #include <stdlib.h>
       3 #include <string.h>
       4 #include <dirent.h>
       5 #include <unistd.h>
       6 #include <sys/types.h>
       7 #include <sys/stat.h>
       8 #include <fcntl.h>
       9 #include <hiredis/hiredis.h>
      10 
      11 
      12 int Open_send(char *base){                      //打開發(fā)送數(shù)據(jù)
      13     int fd, size;
      14     char buffer[1024];
      15         fd = open(base,O_RDONLY);
      16         lseek(fd,69,SEEK_SET);
      17         size = read(fd,buffer,sizeof(buffer));
      18         close(fd);
      19         printf("temp C = %f\n",(float)atoi(buffer)/1000.0);
      20     return 0;
      21 }
      22 
      23 int readFileList(char *basePath)                //文件查找
      24 {
      25     DIR *dir;
      26     struct dirent *ptr;
      27     char base[1024];
      28 
      29     if ((dir=opendir(basePath)) == NULL){
      30         perror("Open dir error...");
      31         exit(1);
      32     }
      33     while ((ptr=readdir(dir)) != NULL)
      34     {
      35         if(strcmp(ptr->d_name,".")==0 || strcmp(ptr->d_name,"..")==0)    ///current dir OR parrent dir
      36             continue;
      37         else if(ptr->d_type == 10){
      38             memset(base,'\0',sizeof(base));
      39             sprintf(base,"%s",ptr->d_name);
      40             if((strcmp("27",base)<0)&&(strcmp("29",base)>0)){
      41                 sprintf(base,"%s/%s/w1_slave",basePath,ptr->d_name);
      42                 //printf("%s\n",base);
      43                 while(1)
      44                 Open_send(base);
      45             }
      46         }
      47     }
      48     closedir(dir);
      49     return 1;
      50 }
      51 
      52 int main(void)
      53 {
      54     DIR *dir;
      55     char basePath[1024];
      56     memset(basePath,'\0',sizeof(basePath));
      57     strcpy(basePath,"/sys/bus/w1/devices");
      58     readFileList(basePath);
      59     return 0;
      60 }
      復(fù)制代碼

       

      =======================================華麗的分割線=======================================

      由于根據(jù)官方的說法,在2015-02-16之后的Raspbain版本,為了防止GPIO的沖突,使用了新的dt策略,更改了原來單總線gpio的配置方法,需要在配置文件中添加gpiopin=8,配置單總線的gpio引腳。

      修改配置:

      sudo vim /boot/config.txt

      在最后一行手動(dòng)添加這個(gè),保存并重啟樹莓派。

      dtoverlay=w1-gpio-pullup,gpiopin=8

      在通過配置sudo raspi-config進(jìn)入interfacingoptions配置單總線時(shí),系統(tǒng)會(huì)在/boot/config.txt文件添加dtoverlay=w1-gpio-pullupz,只需要在后面通過gpiopin指定引腳。

       

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶 評(píng)論公約

        類似文章 更多