RPi and DHT11 with Python

hello, I need some help. :pray: :pray: :pray:
I want to do a project using Raspberry Pi model B + DHT11 sensor + Blynk + Python.
I know that everything is ready in “Node.js” code but I need to use Python, can you help me?

I started by moving your question into its own topic… not hijacking one that is five years old! :stuck_out_tongue:

Next, lookup how to use that DHT11 with Python… This is a necessary step as Blynk will just display your results, not actually query the sensor to get them.

Then once you have that, and understand how it works, you can add in the relevant code to a basic Blynk Python sketch from the examples from the official Blynk Python source.

I’m still not getting it. It turns out that in this project I have the following scenario:

1x Raspberry Pi controlling a Relay 8 doors where it will light 4 lamps. (This step is OK and working with Blynk). This same Raspberry also controls a 9g Micro Servo Motor (this step is also working with Blynk too). The whole previous process is working with Python code.

Now what I’m not getting is adding the temperature and humidity data from my DHT11 to Blynk to the code.

Here is my code in Python so far: (the virtual pin I use for Temperature is V4 and for humidity it is V5).

import RPi.GPIO as GPIO
import blynklib
import Adafruit_DHT
import time

sensor_type = Adafruit_DHT.DHT11 #define o tipo de sensor que stou usando

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(18, GPIO.OUT)#Servo_Motor_Físico_12_GPIO_18
GPIO.setup(17, GPIO.OUT)#Relay_1_Físico_11_GPIO_17
GPIO.setup(27, GPIO.OUT)#Relay_2_Fisico_13_GPIO_27
GPIO.setup(4, GPIO.OUT)#Relay_3_Fisico_07_GPIO_4
GPIO.setup(23, GPIO.OUT)#Relay_4_Fisico_16_GPIO_23

p=GPIO.PWM(18,50)
p.start(7.5)

auth_token = 'your token'

# Initialize Blynk

blynk = blynklib.Blynk ('your token')


@blynk.handle_event('write V0') #Físico 12 & GPIO 18
def write_handler_pin_handler(pin, value):
    Doorlock = (format(value[0]))
    if Doorlock =="0":
        p.ChangeDutyCycle(12.5)
        print("Portão Aberto")
    elif Doorlock =="1":
        p.ChangeDutyCycle(7.5)
        print("Portão Fechado")
        
@blynk.handle_event('write V1') #Físico 11 & GPIO 17
def write_handler_pin_handler(pin, value):
        if value[0] >= "1":
            GPIO.output(17, 1)
            print("Luz Apagada")
        else:
            GPIO.output(17, 0)
            print("Luz Acesa")

@blynk.handle_event('write V2') #Físico 13 & GPIO 27
def write_handler_pin_handler(pin, value):
        if value[0] >= "1":
            GPIO.output(27, 1)
            print("Luz Apagada")
        else:
            GPIO.output(27, 0)
            print("Luz Acesa")

@blynk.handle_event('write V3') #Físico 07 & GPIO 4
def write_handler_pin_handler(pin, value):
        if value[0] >= "1":
            GPIO.output(4, 1)
            print("Luz Apagada")
        else:
            GPIO.output(4, 0)
            print("Luz Acesa")

@blynk.handle_event('write V6') #Físico 16 & GPIO 23
def write_handler_pin_handler(pin, value):
        if value[0] >= "1":
            GPIO.output(23, 1)
            print("Luz Apagada")
        else:
            GPIO.output(23, 0)
            print("Luz Acesa")

try:
    while True:
        blynk.run()

except KeyboardInterrupt:
GPIO.cleanup()

I had to fix your code formatting in your post…

Blynk - FTFC

I don’t see any clearly assigned GPIO or Blynk code for your DHT11…

Instead of merging all the servos and relays and sensors together, just work on Blynkifing the DHT11 code until you figure it out.

hello I hope you are well!

the inclusion of the Python code to receive the data from my DHT11 and view it in Blynk is exactly what I am not able to do … this code that I posted does not really have the DHT11 code because I cannot include it in a functional way. (I started recently with Python programming and this is a big challenge).

I found this code here on the forum, it works perfectly when I run only for DHT11:

[Unformatted code removed by moderator]

Is there any way to include it in the first python code (along with Servo Motor etc.) ??

This is my Blynk inteface:

@AugustoSilva91 once again, you have posted Unformatted code, so this time it has been deleted.
You must put triple backticks at the beginning and end of your code so that it displays correctly, otherwise it will be deleted.

Triple backticks look like this:
```

Pete.

1 Like

As your issue is “how to program in Python?” related, more so than “How to learn Blynk?”, and we are not really here to teach Python programming, perhaps just study the code you had posted above (after properly formatting it, if you wish others to see it) and experiment on your own first with modifying it with servo additions and so on
.