BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

After import BlynkLib, import RPi.GPIO as GPIO not working?


#1

Hi Blynkers!

first of all:
im using a RaspberryPi 3b and a relay
my project is to switch a relay at a certain temp on, and on another off (between these two marks, the relay stays on whatever side it was switched before).

so i wrote this script down below and it all works like a charm.

    import RPi.GPIO as GPIO
    from time import sleep
    import Adafruit_DHT

    GPIO.setmode(GPIO.BOARD)
    GPIO.setup(12, GPIO.OUT)

    sensor = Adafruit_DHT.AM2302
    pin = 2


    while True:
        humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
        sleep(5)
        if temperature > 25.0:
            GPIO.output(12, GPIO.LOW)
            print("ON")
        elif temperature < 23:
            GPIO.output(12, GPIO.HIGH)
            print("OFF")
        else:
            print("temp ok")
        continue

now i just wanted to add a few virtual pins to it, so i can see on a “history graph” a line going on 1 when the relay is set to HIGH and on 0 when its on LOW.

here is what i wrote so far:

    import BlynkLib
    import RPi.GPIO as GPIO
    import time
    import Adafruit_DHT
    from time import sleep

    BLYNK_AUTH = 'xxxxxxxxxxxxxxxxxxxxxxx'

    blynk = BlynkLib.Blynk(BLYNK_AUTH)


    @blynk.VIRTUAL_READ(3)  #temp
    @blynk.VIRTUAL_READ(4)  #humid
    @blynk.VIRTUAL_READ(10) #FAN

    GPIO.setmode(GPIO.BOARD)
    GPIO.setup(12, GPIO.OUT)

    sensor = Adafruit_DHT.AM2302
    pin = 2

    def v3_read_handler():
        blynk.virtual_write(3, temperature)
    def v4_read_handler():
        blynk.virtual_write(4, humidity)


    while True:
        humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
        sleep(5)
        if temperature > 25:
            GPIO.output(12, GPIO.LOW)
            blynk.virtual_write(4, 1)
            print("ON")
        elif temperature < 23:
            GPIO.output(12, GPIO.HIGH)
            blynk.virtual_write(4, 0)
            print("OFF")
        else:
            print("temp ok")
        continue


    blynk.run()

so when i run this script, i get “invalid syntax” on GPIO.setmode(GPIO.BOARD). i also tried BCM, but no mater what it seems like import RPi.GPIO as GPIO is not working :confused:

do i have to set the paths to the modules manually? how do i do that? pls help!


#2

Hello and thank you for the formatting attempt on the pasted code… however can you use the method as described in the Welcome Topic, as the “built in” </> doesn’t always display correctly. (I have already edited your post, so you can go back and see how it is done). Thank you.

Blynk - FTFC

Can you paste the exact error here (as shown in the CLI) as that can sometimes point out the area of the syntax?

Also, it appears you are using the WiringPi C++ method with your RPi, correct? If so, it appears you are missing all the libraries, and void() structure and, well, everything else that would make it look “normal” :stuck_out_tongue: … but that might have been due to how you formatted the code?

If so you might need to repost it in full with the above formatting method. Thanks.


#3

you are totally right… im a bloody newb and i have no clue about programming… i tought this myself in the last couple of day…

“but that might have been due to how you formatted the code?” - No thats just because of my lack of programming skills :wink:

but hey my first script is running… but why wont the second do so?
okay, so heres my script again:

import BlynkLib
import RPi.GPIO as GPIO
import time
import Adafruit_DHT
from time import sleep

BLYNK_AUTH = 'afad18114e814a2d942dc8a158de30d6'

blynk = BlynkLib.Blynk(BLYNK_AUTH)


@blynk.VIRTUAL_READ(3)  #temp
@blynk.VIRTUAL_READ(4)  #humid
@blynk.VIRTUAL_READ(10) #ACF


GPIO.setmode(GPIO.BOARD)
GPIO.setup(12, GPIO.OUT)

sensor = Adafruit_DHT.AM2302
pin = 2

def v3_read_handler():
    blynk.virtual_write(3, temperature)
def v4_read_handler():
    blynk.virtual_write(4, humidity)



while True:
    humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
    sleep(2)
    blynk.virtual_write(3, temperature)
    blynk.virtual_write(4, humidity)





while True:
    humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
    sleep(5)
    if temperature > 29.0:
        GPIO.output(12, GPIO.LOW)
        blynk.virtual_write(4, 1)
        print("ACF ON")
    elif temperature < 28.8:
        GPIO.output(12, GPIO.HIGH)
        blynk.virtual_write(4, 0)
        print("ACF OFF")
    else:
        print("temp ok")
    continue

the error is:

  File "switch.py", line 17
    GPIO.setmode(GPIO.BOARD)
       ^
SyntaxError: invalid syntax

#4

A bit of Googling your command GPIO.setmode(GPIO.BOARD) seems to indicate that it is python related, not javascript or WiringPi?.. Unfortunately I don’t have the knowledge in any of those to assist, and it appears js/Blynk is in holding pattern and py/Blynk is still beta.

In fact, I’m still trying to determine the validity of pursuing RPi/Blynk interfacing on a client level myself as I suspect ESP is the hardware focus.

While not the answer you may be looking for… pershap look at changing your hardware for your learning process and better forum assistance… we (the forum) know Blynk/ESP8266 very well :stuck_out_tongue:


#5

thx for that info, sadly i dont have that much time to start learning JS from start at the moment…


#6

Not necessary to learn JS if you already are learning python (which I am now guessing was the case?) so keep working in that direction. And updating your title to reflect the “Python” syntax issue might help draw the right help.

Unfortunately figuring out issues like yours becomes a challenge when very few seem to use that programming language with Blynk.

Have you seen this topic yet?

Also, some searching of this forum for the keyword python, does show even some older topics about it… it all might help?