blynkkk / lib-python

Blynk IoT library for Python and Micropython
MIT License
236 stars 81 forks source link

virtual_write for map widget point updates #46

Open kindmartin opened 3 years ago

kindmartin commented 3 years ago

hi all. I'm struggling with this python lib trying to update points in a blynk app map/

I was reading https://community.blynk.cc/t/solved-maps-with-http-rest-api/15826/54 where a possible solution is to use that REST API to update. Also https://community.blynk.cc/t/blynk-map-interface-update-from-blynk-library-python/27289 without a solution.

how I can update a map widget here with python lib, I fact I'm using an ESP32 running last official Micropython 1.15. any hint? thanks in advance.

M

kindmartin commented 3 years ago

More details, for the REST API get method vs a virtualWrite @ arduino, I compiled a c/arduino sketch and all worked right. REST API I was trying this python code: ... value = '60,1,-34.754129,-56.6964227,test1' r = lib.urequests.get("http://192.168.1.2:8080/WXiw3UFznFCQxP3rcWpYZf9fV9ydNMYX/update/V60?value="+str(value)) ... it worked for a simple text input widget, not map.

For map, I also captured in input text wigget what was send to V60 virtual pin where I attached the widget map and get this:
'1,-34.754129,-56.6964227,test1'

But with Arduino I have got 0-34.507-56.128position#61 that seems the same but I note some control caracters between values like 0[non ascii]-34.507[non ascii]-56.128[non ascii]position#61

_#define BLYNK_PRINT Serial

include

include

include

char ssid[] = "PlantaAlta.2Ghz"; char pass[] = "****"; char auth[] = "**NMYX";

WidgetMap myMap(V60);

void setup() { // Debug console Serial.begin(115200);

Blynk.begin(auth, ssid, pass,IPAddress(192,168,1,2),8080);

int index = 0; float lat = -34.5074; float lon = -56.1278;

myMap.location(index, lat, lon, "position#2");

Blynk.virtualWrite(V61, 0, lat, lon, "position#61"); Serial.println( string(0), string(lat), string(lon), "position#61"); }

void loop() { Blynk.run(); }_

antohaUa commented 3 years ago

Hi Kindmartin! Simple blynk.virtual_write(pin, 2, -33.754129, -56.6964227, 'test2') should work for you. This should add point with certain location to map somewhere in Uruguay ))

When you can do it ( write value)?
Blynk.run means that here hidden auth connect , hadlers registration etc. For example you can add value widget to display pin value each 10 sec

@blynk.handle_event('read V11')
def read_virtual_pin_handler(pin):
     blynk.virtual_write(pin, 2, -33.754129, -56.6964227, 'test2')

To learn more about lib implementation capabilities I suggest you to examine examples for different operations and try to play with them - to get expertise for read/write pin calls https://github.com/blynkkk/lib-python/tree/master/examples Each example has great description and schema in header. I think they might be very helpful for your findings.

kindmartin commented 3 years ago

Thanks, Anto, I will re-try and update. I think I tested a lot before on blynk.virtual_write(pin, 2, -33.754129, -56.6964227, 'test2') and similar, but for an unknown something it doesn't works (probably due the usual silly reason I cannot see). I will update soon.

M

kindmartin commented 3 years ago

It worked.... thanks and sorry for taking time without more self testing, btw, Im from Buenos Aires Argentina so those coordinates are some ones I wrote ramdombly.

/M

kindmartin commented 3 years ago

one more thing before close this one. in arduino we can clean the widget map using myMap.clear() (related to WidgetMap myMap(Pin) )

how we could clean/delete old points ?

,M

antohaUa commented 3 years ago

Kindmartin!

how we could clean/delete old points ? As I can see within https://github.com/blynkkk/blynk-library/blob/master/src/WidgetMap.h

void clear() {
Blynk.virtualWrite(mPin, "clr");
}

Let's try this approach with python lib )) -> blynk.virtual_write(pin, "clr")

kindmartin commented 3 years ago

It worked charmingly!

Thanks Anto!

PD I still have to understand fix why I get some IOerrors when I try to virtual write vpins

_>>> bkwupdateBlynkLoc each 20 seg") mem free loop: 3738304

bkw.blynk.disconnect() [DISCONNECT_EVENT] .===>>>> memory pre update {'ram': {'lat': -34.52522, 'QtyWifis': 11, 'uid': 'fcf5c43cfb60', 'FlagA9GENA': True, 'FlagA9GData': False, 'timestamp': '20210510 13:39:25', 'QtyCells': 2, 'accuracy': 20, 'lon': -58.49915, 'FlagAlarmed': 0}}

Traceback (most recent call last): File "", line 1, in File "blynklib.py", line 303, in virtual_write File "blynklib.py", line 166, in send NameError: name 'IOError' isn't defined

bkw.blynk.virtual_write(60, "clr")mem free 2: 3734048

Traceback (most recent call last): File "", line 1, in File "blynklib.py", line 303, in virtual_write File "blynklib.py", line 166, in send NameError: name 'IOError' isn't defined

AGPS position -34.52527,-58.49914 2Cells 8wifis (accuracy= 20m, rt:3s) ===>>>> memory after update {'ram': {'lat': -34.52527, 'QtyWifis': 8, 'uid': 'fcf5c43cfb60', 'FlagA9GENA': True, 'FlagA9GData': False, 'timestamp': '20210510 13:39:53', 'QtyCells': 2, 'accuracy': 20, 'lon': -58.49914, 'FlagAlarmed': 0}} [CONNECT_EVENT] -34.52527 -58.49914 Home #2,20210510 13:39:53 bkw.blynk.virtualwrite(60, "clr") 14 updateBlynkLoc each 20 seg mem free loop: 3597632

now as a workaround I force to blynk.disconnect() then connect() then do the virtual writes.

Like:

lib. is a module where I just import libraries for this project

bkw. is another module where I have all hw related real pins declarations and class instances fun. is just a module with all non task especific functions. That’s fun!

….

**async def loop_garbageC(each_s=60):

print('async loop_garbageC')

while True:
    await lib.uasyncio.sleep(each_s)
    lib.gc.collect()
    print('mem free loop:',lib.gc.mem_free())

async def updateBlynkLoc(each_s=5): p = 0 while True: print("updateBlynkLoc each {} seg".format(each_s)) print('mem free loop:',lib.gc.mem_free())

    print('>>>>> bkw.blynk.disconnect()')
    bkw.blynk.disconnect()
    try:
        print('===>>>> memory pre update', bkw.memory)
        (lat,lon,QtyCells,QtyWifis,accuracy,responseTime) = fun.getAGPS(apikey =  'AIzaSyCW-qcNeQc_-***- _M')
        bkw.memory["ram"]["lat"] = lat
        bkw.memory["ram"]["lon"] = lon
        bkw.memory["ram"]["QtyCells"] = QtyCells
        bkw.memory["ram"]["QtyWifis"] = QtyWifis
        bkw.memory["ram"]["accuracy"] = accuracy  
        bkw.memory["ram"]["timestamp"] = fun.nowString()
        print('===>>>> memory after update', bkw.memory)
    except Exception as error:
        print('===>> error')
        print(error)
        print('-----------')
    bkw.blynk.connect()
    while bkw.blynk.connected() is False:
        pass
    info = 'Home #'+str(p)+','+fun.nowString( lib.time.localtime(lib.time.time() + bkw.TMZ ) )
    bkw.blynk.virtual_write(60, p, bkw.memory["ram"]["lat"], bkw.memory["ram"]["lon"], info )
    print(bkw.memory["ram"]["lat"], bkw.memory["ram"]["lon"],info)
    p += 1
    bkw.blynk.virtual_write(51, 'lat: {:.5f}'.format(bkw.memory["ram"]["lat"])) 
    bkw.blynk.virtual_write(52, 'lon: {:.5f}'.format(bkw.memory["ram"]["lon"])) 
    bkw.blynk.virtual_write(53, 'qtyCells: {}'.format(bkw.memory["ram"]["QtyCells"]))
    bkw.blynk.virtual_write(54, 'qtyWifis: {}'.format(bkw.memory["ram"]["QtyWifis"])) 
    bkw.blynk.virtual_write(55, 'accuracy: {}'.format(bkw.memory["ram"]["accuracy"]))
    bkw.blynk.virtual_write(56, 'timestamp: {}'.format(bkw.memory["ram"]["timestamp"]))
    await lib.uasyncio.sleep(each_s)

async def loop_blynk(each_ms=500): while True: bkw.blynk.run() await lib.uasyncio.sleep_ms(each_ms)**

antohaUa commented 3 years ago

Kindmartin!

NameError: name 'IOError' isn't defined

Can be related to HW python specific.

I fact I'm using an ESP32 running last official Micropython 1.15

You mentioned micro_python - thus you need to import not c_python but micro_python lib in your script https://github.com/blynkkk/lib-python/blob/master/blynklib_mp.py