First time project - can't connect to server

Rasp Pi 4 using Python 3.7.3. Connected via wifi and confirmed connection to the web.
Blynk Library v0.2.6

Hi,
I’m trying to create a simple project to monitor temperature and humidity on a Raspberry Pi4. The hardware works well. All attempts to connect to the Blynk server for the past 7 days (and multiple different versions of python code) have failed. I can create my device on the console and have 2 virtual pins (0 for temperature, 1 for humidity). I have the Template ID, Template name and the Token. When I try to initialize Blynk, I get the console splash screen (Blynk for Python v0.2.6) but no indication that I am connected to the server and the code pauses there.

The Pi is definitely connected to the internet as I can browse the web from the device.

Any help would be appreciated as my brain is fried. I have followed the Quickstart guide daily. Do I keep overlooking something obvious?

Jeff

import smbus2
import bme280     #Temperature and Humidity sensor
import time
from datetime import datetime

import blynklib
global blynk

class Counter:
    cycle = 0

BLYNK_TEMPLATE_ID = "TMPLmHsIOcLN"
BLYNK_TEMPLATE_NAME = "TempHume"
BLYNK_AUTH_TOKEN = "my tokenxxxxxxxx"

# Initialize Blynk
blynk = blynklib.Blynk(BLYNK_AUTH_TOKEN)

T_CRI_VALUE = 12.8  # 55 F
T_CRI_MSG = 'Low TEMP!!!'
T_CRI_COLOR = '#c0392b'

T_COLOR = '#f5b041'
H_COLOR = '#85c1e9'
ERR_COLOR = '#444444'

T_VPIN = 0
H_VPIN = 1

# Initialize the bme280 temperature sensor
port = 1
address = 0x76
bus = smbus2.SMBus(port)
calibration_params = bme280.load_calibration_params(bus, address)

@blynk.handle_event('read V{}'.format(T_VPIN))
def read_handler(vpin):

    data = bme280.sample(bus, address, calibration_params)
    tempf = format(data.temperature * 9.0 / 5.0 +32.0, ".2f")
    hume = format(data.humidity, ".2f")

    # check that values are not False (mean not None)
    if all([tempf, hume]):
        print(" Temperature : ",tempf, "  -  humidity : " , hume, "%")
        blynk.set_property(T_VPIN, 'color', T_COLOR)
        blynk.set_property(H_VPIN, 'color', H_COLOR)
        blynk.virtual_write(T_VPIN, tempf)
        blynk.virtual_write(H_VPIN, hume)
    else:
        print('[ERROR] reading BMP180 sensor data')
        blynk.set_property(T_VPIN, 'color', ERR_COLOR)  # show aka 'disabled' that mean we errors on data read
        blynk.set_property(H_VPIN, 'color', ERR_COLOR)

###########################################################
# infinite loop that waits for event
###########################################################
while True:
    blynk.run()

@JeffG Please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```

Copy and paste these if you can’t find the correct symbol on your keyboard.

Pete.

Thanks, I couldn’t figure out why my code looked so strange…

You need to be using the Blynk Python library version 1.0.0.
This isn’t available as a bundled release, but is available by copying the individual files from here:

Pete.

Pete,
Thank you, Thank you!!!

I am finally getting data to the server and to my phone!!

I have other questions regarding the blynk timer but just found your outstanding guide online and will come back if I fail to answer my own question

Jeff

Pete,
Thanks again. I have a working first project and immediately started tweaking my code :slight_smile:

My main problem was not using the blynktimer. I found your excellent guide (for C) and worked it into Python. My issues are likely due to my ancient programming skills (I was actually a CS major 30+ yrs ago). I really should be using the C++ code instead of Python but was hoping to learn Python at the same time as learning to use Blynk. Lots of the Python examples online don’t use the timer.

While my code is working, I was wondering if you could explain something to me that I just can’t comprehend? If not, I understand and will get some smart-ass college kid to explain it to me. In my working code, this is the main working section…

@timer.register(vpin_num=0, interval=60, run_once=False)
def write_to_virtual_pin(vpin_num=1):
    # check that values are not False (mean not None)
    if all([tempf, hume]):
        print(" Temperature : ",tempf, "  -  humidity : " , hume, "%")
        blynk.set_property(T_VPIN, 'color', T_COLOR)
        blynk.set_property(H_VPIN, 'color', H_COLOR)
        blynk.virtual_write(T_VPIN, tempf)
        blynk.virtual_write(H_VPIN, hume)
    else:
        print('[ERROR] reading BMP180 sensor data')
        blynk.set_property(T_VPIN, 'color', ERR_COLOR)  # we have errors on data read
        blynk.set_property(H_VPIN, 'color', ERR_COLOR)

###########################################################
# infinite loop that waits for event
###########################################################
while True:
    blynk.run()
    timer.run()

I looked through the python blynktimer module and dont see ‘register’ under the timer class definition. Am I missing something obvious?

Thanks again,
Jeff

I know nothing about Python I’m afraid.
I’d say that 99.9% of Blynk users probably use C++, me included.

Also, the RPI is also an awful platform to use for most projects, it requires an OS and suitable storage (SD cards will fail at some point) and are bulky, expensive and slow to boot. That’s why most people use ESP8266 or ESP32 devices instead.

Pete.

Understood! It does appear that most everyone is using C++ on the boards and i can mostly follow that code.

I’ll tell you a funny story about C; I graduated college in the late 80’s. When I took my 24 yr old son to college 3 yrs ago, he was was scheduled to take a course in C. We went to the bookstore to get his books. As he grabs the recommended books, I took one out of the pile and put it back on the shelf. It was a book on C that I still had from 1986 and mailed it to him :).

This is a sample project just to make sure my computer-brain had not completely atrophied over the past 35 years. I owned the Pi4 so I used it. I fully intend to move my subsequent projects to a smaller board (ie ESP32).

Thanks again for your help.

Jeff

Depending on what sort of stuff you ultimately want to do, you could take a look at running Node-Red on your RPI, and isinglass this (via the Blynk Node-Red plug-in) as your connection to Blynk.

Any ESP devices you then add can talk to your RPI using MQTT messaging. This approach is very powerful, as Node-Red acts as your logic controller/rules engine and makes interfacing with devices like Amazon Alexa or Google Assistant very easy.

More info here…

Pete.

This syntax belongs to version 0.2.6 of the Blynk Python library. In the new version of the Blynk Python library 1.0.0, the syntax has been changed to

timer.set_interval(time, your_function)

Here’s an example

import BlynkLib
from BlynkTimer import BlynkTimer

BLYNK_AUTH = 'YourAuthToken'

# Initialize Blynk
blynk = BlynkLib.Blynk(BLYNK_AUTH)

# Create BlynkTimer Instance
timer = BlynkTimer()


# Will only run once after 2 seconds
def hello_world():
    print("Hello World!")


# Will Print Every 5 Seconds
def print_me():
    print("Thanks!")


# Add Timers
timer.set_timeout(2, hello_world)
timer.set_interval(5, print_me)


while True:
    blynk.run()
    timer.run()
1 Like