2024年2月1日木曜日

raspberry pi uart.py and c (tone,dht11,bme280,hc-sr04)

---- receive.py via hc05 ----------------------------------------

import serial

import time


SerDevice = serial.Serial('/dev/serial0', 9600, timeout=10)



#******************************

#Recieve Response

while True:

    #Read data one Line (top->'\r\n')

    L_RecieveData=SerDevice.readline()

    RecieveData = L_RecieveData.decode()


    #Get Data Length

    DataLength = len(L_RecieveData)

    print(DataLength) #cr/lfのみのとき2となる

    #If DataLength = 2, then Response END!!

    if DataLength==2: 

        break

    else:

        print(RecieveData)


#******************************

#Close Serial Device

SerDevice.close()


-------- send.py  via hc05  ------

import serial

import time

i=0

ser = serial.Serial('/dev/serial0', 9600)

while(i != 10):

    ser.write(b'10\n')

    time.sleep(1)

    i = i+1

ser.close()



ーーーwiringPi.setup.gpioーーー音出しーーー

https://torisky.com/%E3%83%A9%E3%82%BA%E3%83%91%E3%82%A4%EF%BC%9Ac%E8%A8%80%E8%AA%9E%E3%81%A7gpio%E3%83%9D%E3%83%BC%E3%83%88%E3%82%92%E5%88%B6%E5%BE%A1%E3%81%A7%E3%81%8D%E3%82%8Bwiringpi%E3%81%AE%E3%82%A4%E3%83%B3/

ーーーーdht11ーーーーーーーーーーーーーーーーー

https://www.circuitbasics.com/how-to-set-up-the-dht11-humidity-sensor-on-the-raspberry-p に以下も参考にしてsetupをsetupGpioにした!

https://sky-dream.xyz/2020/10/18/%E6%B8%A9%E6%B9%BF%E5%BA%A6%E3%82%92%E8%A8%88%E3%81%A3%E3%81%A6%E3%81%BF%E3%82%88%E3%81%86%E3%80%80%EF%BD%9Edht11%E7%B7%A8%E3%80%80%EF%BD%82%EF%BD%99c%EF%BD%9E/


#include <wiringPi.h>

#include <stdio.h>

#include <stdlib.h>

#include <stdint.h>

#define MAXTIMINGS 85

#define DHTPIN 4   // as gpio number

int dht11_dat[5] = { 0, 0, 0, 0, 0 };

void read_dht11_dat()

{

uint8_t laststate = HIGH;

uint8_t counter = 0;

uint8_t j = 0, i;

float f; 

 

dht11_dat[0] = dht11_dat[1] = dht11_dat[2] = dht11_dat[3] = dht11_dat[4] = 0;

  pinMode( DHTPIN, OUTPUT );

digitalWrite( DHTPIN, LOW );

delay( 18 );

digitalWrite( DHTPIN, HIGH );

delayMicroseconds( 40 );

pinMode( DHTPIN, INPUT );

 

for ( i = 0; i < MAXTIMINGS; i++ )

{

counter = 0;

while ( digitalRead( DHTPIN ) == laststate )

{

counter++;

delayMicroseconds( 1 );

if ( counter == 255 )

{

break;

}

}

laststate = digitalRead( DHTPIN );

 

if ( counter == 255 )

break;

 

if ( (i >= 4) && (i % 2 == 0) )

{

dht11_dat[j / 8] <<= 1;

if ( counter > 16 )

dht11_dat[j / 8] |= 1;

j++;

}

}

 

if ( (j >= 40) &&

     (dht11_dat[4] == ( (dht11_dat[0] + dht11_dat[1] + dht11_dat[2] + dht11_dat[3]) & 0xFF) ) )

{

f = dht11_dat[2] * 9. / 5. + 32;

printf( "Humidity = %d.%d %% Temperature = %d.%d C (%.1f F)\n",

dht11_dat[0], dht11_dat[1], dht11_dat[2], dht11_dat[3], f );

}else  {

printf( "Data not good, skip\n" );

}

}

 

int main( void )

{

printf( "Raspberry Pi wiringPi DHT11 Temperature test program\n" );

if ( wiringPiSetupGpio() == -1 )  // setupではgpioつかえない!

exit( 1 );

while ( 1 )

{

read_dht11_dat();

delay( 2000 ); 

}

  return(0);

}


ーーーーー超音波ーーーー

超音波距離センサー HC-SR04 - GreenLeaf (mydns.jp) 記述通り1kΩはなくても動いた

--------------BME280------------------------

https://tomosoft.jp/design/?p=6963 もあるが

温湿・気圧センサモジュール AE-BME280 - GreenLeaf (mydns.jp) OKだった


0 件のコメント:

コメントを投稿