Device Offline (Micropython) (New Blynk)

Hi,

I have configured everything in the blynk console as well as the mobile app.

Please help me in getting my device online.

Here is the code -

import blynklib

import network

import esp

import gc

import time

import machine

from machine import Pin, PWM

esp.osdebug(None)

gc.collect()

wifi_status = Pin(2, Pin.OUT)

wht_pin = PWM(Pin(13), freq=1000, duty=0)

ylw_pin = PWM(Pin(12), freq=1000, duty=0)

ssid_01 = 'thunderACT'

ssid_02 = 'A8'

password = '###'

wlan = network.WLAN(network.STA_IF)

wlan.active(True)

avl_networks = wlan.scan()

print(avl_networks)

print('{} networks available!'.format(len(avl_networks)))

for i in range(len(avl_networks)):

    while (wlan.isconnected() == False):

        wifi_status.off()

        if b'thunderACT' in avl_networks[i]:

            wlan.connect(ssid_01, password)

            break

        else:

            wlan.connect(ssid_02, password)

            break

    break

print('Connected with {}'.format(wlan.config('essid')))

wifi_status.on()

print('IP Configuration: {}'.format(wlan.ifconfig()))

BLYNK_AUTH = '###'

# initialize Blynk

blynk = blynklib.Blynk(BLYNK_AUTH)

WRITE_EVENT_PRINT_MSG = "[WRITE_VIRTUAL_PIN_EVENT] Pin: V{} Value: '{}'"

# register handler for virtual pin V4 write event

@blynk.handle_event('write V1')

def write_virtual_pin_handler(ylw_pin, value):

    print(WRITE_EVENT_PRINT_MSG.format(1, value))

@blynk.handle_event('write V0')

def write_virtual_pin_handler(wht_pin, value):

    print(WRITE_EVENT_PRINT_MSG.format(0, value))

###########################################################

# infinite loop that waits for event

###########################################################

while True:

    blynk.run()

@vcr3at3 this is the second “need help…” topic that you’ve created today without providing the information that is requested with this type of topic. This information is requested for a good reason - so that forum members can provide you with assistance without having to ask lots of additional questions.

You’ve also chosen to ignore this information about how to correctly format your code when you post it to the forum.
This is the pre-populated information that you’ve chosen to ignore…

Before creating the topic

  1. Search forum for similar topics
  2. Check http://docs.blynk.cc and http://help.blynk.cc/
  3. Add details :
    • Hardware model + communication type. For example: Arduino UNO with Ethernet Shield
    • Smartphone OS (iOS or Android) + version
    • Blynk server or local server
    • Blynk Library version
    • Add your sketch code. :point_up:Code should be formatted as example below.

Simply paste your code between ``` If you don’t format your code, your topic can be deleted by moderators.

```cpp

void loop()

```

Maybe you would like to edit your initial post in this topic and fill-in the blanks, and re-post your code in the requested format?

Pete.

2 Likes

You just lost a potential business.

I did not chose to ignore the info, I just missed it.

I think you are the community forum moderator and I am just a guy who is looking for help.

this is the second “need help…” topic that you’ve created today without providing the information that is requested with this type of topic.
You’ve also chosen to ignore this information about how to correctly format your code when you post it to the forum.

Kindly frame your sentences politely.

@vcr3at3 - Pete is a well respected member of this community, no need for snotty comments. everyone here are community and want to help, loss of business to blynk is a poor comment and really not much else out there that offers the pricing and value, so either work with us to help you or move swiftly along.

Let’s look at you code now:
Looking at you code provided, you are not allowing for checks and balances:

  1. some ESP32 wifi connections can take +/- 20-30 seconds to connect.
  2. which Blynk Library version are you using?
  3. Are you using the new Blynk app and not the old one?
  4. What errors are you getting (from your print statements?
3 Likes

• ESP 32 Dev Module as a WiFi Station
• Android 10
• Using Blynk server
• Blynk Library version https://github.com/blynkkk/lib-python
• Adding code

I am using the new Blynk Version.

I do not get any errors, simpy the Blynk Logo that comes when we initialize Blynk
import blynklib

import network

import esp

import gc

import time

import machine

from machine import Pin, PWM

esp.osdebug(None)

gc.collect()

wifi_status = Pin(2, Pin.OUT)

wht_pin = PWM(Pin(13), freq=1000, duty=0)

ylw_pin = PWM(Pin(12), freq=1000, duty=0)

ssid_01 = 'thunderACT'

ssid_02 = 'A8'

password = '###'

wlan = network.WLAN(network.STA_IF)

wlan.active(True)

avl_networks = wlan.scan()

print(avl_networks)

print('{} networks available!'.format(len(avl_networks)))

for i in range(len(avl_networks)):

    while (wlan.isconnected() == False):

        wifi_status.off()

        if b'thunderACT' in avl_networks[i]:

            wlan.connect(ssid_01, password)

            break

        else:

            wlan.connect(ssid_02, password)

            break

    break

print('Connected with {}'.format(wlan.config('essid')))

wifi_status.on()

print('IP Configuration: {}'.format(wlan.ifconfig()))

BLYNK_AUTH = '###'

# initialize Blynk

blynk = blynklib.Blynk(BLYNK_AUTH)

WRITE_EVENT_PRINT_MSG = "[WRITE_VIRTUAL_PIN_EVENT] Pin: V{} Value: '{}'"

# register handler for virtual pin V4 write event

@blynk.handle_event('write V1')

def write_virtual_pin_handler(ylw_pin, value):

    print(WRITE_EVENT_PRINT_MSG.format(1, value))

@blynk.handle_event('write V0')

def write_virtual_pin_handler(wht_pin, value):

    print(WRITE_EVENT_PRINT_MSG.format(0, value))

###########################################################

# infinite loop that waits for event

###########################################################

while True:

    blynk.run()

Check that you are connected to the “internet” here is a code snippet:

if WIFI.isconnected():
				print('---> Network Config:', WIFI.ifconfig())
				GenRes = WIFI.status('rssi')
# Get External IP Address
				try:
					EXTIP = usocket.getaddrinfo("google.com",1)[0][-1][0]
					print ('---> External IP: ' + str(EXTIP))
					WIFISTATUS = 1
					WIFIFAILED = 0
				except:
					print ('---> No Internet Access On: ' ,WIFISSID)
					WIFI.disconnect()						# Disconnect
					WIFI.active(False)						# De-activate The Wi-Fi Module```

here is the latest Blynk Lib for Python: [Latest Blynk Lib](https://github.com/vshymanskyy/blynk-library-python/blob/master/BlynkLib.py)

@SmsAlert 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:
```

Pete.

Thanks Pete, apostrophe… triple back ticks… knew it was one of them, thanks for the help :wink:

Thanks for navigating me to the latest library.

I’ll try this one

@vcr3at3 here is the link: Latest Blynk Python Library

@PeteKnight

My sincere apologies for everything I said earlier.

I tried the new library.

Here is my code

import time

BLYNK_AUTH = '###'

# initialize Blynk
blynk = blynklib.Blynk(BLYNK_AUTH)

tmr_start_time = time.time()
while True:
    blynk.run()

    t = time.time()
    if t - tmr_start_time > 1:
        print("1 sec elapsed, sending data to the server...")
        blynk.virtual_write(0, "time:" + str(t))
        tmr_start_time += 1

The errors are shown below in the screenshot -

Please help me in figuring out whats wrong.

I checked that my network was not connected properly. After getting a proper connection, I ran the following code snippet

if WIFI.isconnected():
				print('---> Network Config:', WIFI.ifconfig())
				GenRes = WIFI.status('rssi')
# Get External IP Address
				try:
					EXTIP = usocket.getaddrinfo("google.com",1)[0][-1][0]
					print ('---> External IP: ' + str(EXTIP))
					WIFISTATUS = 1
					WIFIFAILED = 0
				except:
					print ('---> No Internet Access On: ' ,WIFISSID)
					WIFI.disconnect()						# Disconnect
					WIFI.active(False)						# De-activate The Wi-Fi

The code ran successfully.

After that, I again ran this code below -

# Initialize Blynk
import blynklib, blynk_timer
import time

BLYNK_AUTH = '###'

# initialize Blynk
blynk = blynklib.Blynk(BLYNK_AUTH)

tmr_start_time = time.time()
while True:
    blynk.run()

    t = time.time()
    if t - tmr_start_time > 1:
        print("1 sec elapsed, sending data to the server...")
        blynk.virtual_write(0, "time:" + str(t))
        tmr_start_time += 1

Now I get the following error -

Connecting to blynk.cloud:443...
.Traceback (most recent call last):
  File "<stdin>", line 8, in <module>
  File "/lib/blynklib.py", line 214, in __init__
  File "/lib/blynklib.py", line 81, in __init__
  File "/lib/blynklib.py", line 226, in connect
OSError: -202

I also had a few issues in the beginning and had to make quite a few changes to the new blynk library, go to the blynk library and change the following code at line 223 to 226 to the below code snippet:

def connect(self):
		self.log('Connect: Server: "%s" : Port: "%d"' % (self.server, self.port))
		self.s = None
		self.conn = None
		self.s = socket.socket()
#		self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
		self.s.connect(socket.getaddrinfo(self.server, self.port)[0][-1])
		self.log('Connect: Socket Setup')

you can try the socket settings option just comment out the line above it, like this

#		self.s = socket.socket()
		self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

On the OS Error issue: OSError is a built-in exception in Python and serves as the error class for the os module, which is raised when an os specific system function returns a system-related error, including I/O failures such as “file not found” or “disk full”.

you can also try the following when initialising the Blynk app non secure connection on port 80:

blynk = blynklib.Blynk(BLYNK_AUTH, insecure=True, server='blynk-cloud.com')

Meant to add that you must also chek that you are running uPython Ver 1.17 or higher on your ESP32

Thanks. I’ll try this

I tried changing the new blynk library code as you showed above but now I am getting this new error while executing the virtual_write example in the library.


    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ for Python v1.0.0 (esp32)

Traceback (most recent call last):
  File "<stdin>", line 7, in <module>
  File "/lib/BlynkLib.py", line 222, in __init__
  File "/lib/BlynkLib.py", line 85, in __init__
  File "/lib/BlynkLib.py", line 253, in connect
NameError: name 's' isn't defined

After pasting the new code snippet shared by you, my blynk library connect function looks like this -

    def connect(self):
        self.log('Connect: Server: "%s" : Port: "%d"' %
                 (self.server, self.port))
        self.s = None
        self.conn = None
        self.s = socket.socket()
        #		self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.s.connect(socket.getaddrinfo(self.server, self.port)[0][-1])
        self.log('Connect: Socket Setup')
        try:
            s.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
        except:
            pass
        if self.insecure:
            self.conn = s
        else:
            try:
                import ussl
                ssl_context = ussl
            except ImportError:
                import ssl
                ssl_context = ssl.create_default_context()
            self.conn = ssl_context.wrap_socket(s, server_hostname=self.server)
        try:
            self.conn.settimeout(SOCK_TIMEOUT)
        except:
            s.settimeout(SOCK_TIMEOUT)
        BlynkProtocol.connect(self)

Please help me here. In VS code, when I hover my mouse cursor over s, it shows s is not defined. But everything is properly indented. Snapshot below -

Please help me out here. What could be wrong?

The device went online once. But again I am getting OSError

My code -

import BlynkLib
import time

BLYNK_AUTH = '###'

# initialize Blynk
blynk = BlynkLib.Blynk(BLYNK_AUTH)

tmr_start_time = time.time()
while True:
    blynk.run()

    t = time.time()
    if t - tmr_start_time > 1:
        print("1 sec elapsed, sending data to the server...")
        blynk.virtual_write(0, "time:" + str(t))
        tmr_start_time += 1

Error output -

Connecting to blynk.cloud:443...
Traceback (most recent call last):
  File "<stdin>", line 7, in <module>
  File "/lib/BlynkLib.py", line 214, in __init__
  File "/lib/BlynkLib.py", line 81, in __init__
  File "/lib/BlynkLib.py", line 240, in connect
OSError: [Errno 12] ENOMEM
```

Since you’re using esp32, I recommend you to use the C++ library instead.