(help needed): Migrate from Python to MicroPython

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Post Reply
PNZ20
Posts: 4
Joined: Tue Mar 16, 2021 10:16 am

(help needed): Migrate from Python to MicroPython

Post by PNZ20 » Tue Mar 16, 2021 10:53 am

Hello everybody,
after a lot of improvements, I created a BTC and ETH price tiker for a 0.96" OLED.
I made it running from a raspberry PI 3B+, and it works. ^_^
During the coding, I cloned from github the Adafruit_Python_SSD1306 repository
and installed via pip3 install the modules:
cfscrape: https://pypi.org/project/cfscrape/
python-ev3dev2 https://pypi.org/project/python-ev3dev2/
oled-text https://pypi.org/project/oled-text/

Lots of step were necessary to make the raspberry to communicate with the oled, I think...
Now I want to miniaturize the hardware, so I plan to move to an awesome ESP32 board.
I managed how to flash it with micropython firmware and I have understood the boot.py and the main.py roles.
I have tried to use the

Code: Select all

import upip
upip.install('cfscrape')
but without success.
Now I am stuck with the libraries/modules. My code works on a raspberry Pi but I don't understand how replicate the pip install in the ESP32 board.

Can someone in the forum please review my project and show me the way?

Here there is the code of my project in Python.
Thank you very very very much.

Code: Select all

#-*- coding: utf-8 -*-
import json
import cfscrape
import time
import requests
from board import SCL, SDA
import busio
from oled_text import OledText, Layout64, SmallLine, BigLine
scraper = cfscrape.create_scraper()
i2c = busio.I2C(SCL, SDA)
#DIMENSIONI OLED 168 X 64
oled = OledText(i2c, 128, 64)

while True:
    url = "https://google.com"
    timeout = 5
    try:
        request = requests.get(url, timeout=timeout)
#        print("Connected to the Internet")
    
        t = time.localtime()
        current_time = time.strftime("%H:%M", t)
##ORGANIZZA LAYOUT
        oled.layout = {
#Layout64.layout_icon_only()
        0: SmallLine(17, 0, font="arial.ttf", size=11),    
        1: BigLine(0, 15, font="FAbrand.otf", size=20),
        2: BigLine(20, 12, font="arial.ttf", size=24),
        3: BigLine(107, 16, font="FAsolid.otf", size=20),
        4: BigLine(1, 40, font="FAbrand.otf", size=20),
        5: BigLine(20, 38, font="arial.ttf", size=24),
        6: BigLine(107, 42, font="FAsolid.otf", size=20),
        }

##BTC
        btcurl = 'https://api.pro.coinbase.com/products/BTC-eur/stats'
        cfurl = scraper.get(btcurl).content
        btcdata = json.loads(cfurl)
        bitcoin = btcdata['last']
        bitcoinopen = btcdata['open']
        strbitcoin = bitcoin.split(".")[0]
        nbitcoin = float(bitcoin)
        nbitcoinopen = float(bitcoinopen)
        nbtcchange = nbitcoin - nbitcoinopen
##ETH
        ethurl = 'https://api.pro.coinbase.com/products/ETH-eur/stats'
        cfurl = scraper.get(ethurl).content
        ethdata = json.loads(cfurl)
        ethcoin = ethdata['last']
        ethcoinopen = ethdata['open']
        strethcoin = ethcoin.split(".")[0]
        nethcoin = float(ethcoin)
        nethcoinopen = float(ethcoinopen)
        nethchange = nethcoin - nethcoinopen

##stampa il testo sull'OLED
#thumbsdown=f165 // chartUP=f201 // arrowUP=uf062 // arrowDOWN=uf063
        oled.text ("PNZ20 edition "+current_time, 0)
##BTC line
        oled.text ('\uf15a', 1)
        oled.text ("€ "+strbitcoin, 2)
        if nbtcchange > 0:
            oled.text ('\uf062', 3)
        else:
            oled.text ('\uf063', 3)


##ETH line
        oled.text ('\uf42e', 4)
        oled.text ("€   "+strethcoin, 5)
        if nethchange > 0:
            oled.text ('\uf062', 6)
        else:
            oled.text ('\uf063', 6)

        time.sleep (30)

#        oled.clear()
        
    except (requests.ConnectionError, requests.Timeout) as exception:
#        print("NO INTERNET CONNECTION")
        oled.layout = {
        8: BigLine(15, 0, font="arial.ttf", size=15),
        9: SmallLine(50, 20, font="FAsolid.otf", size=30),
        10: BigLine(15, 50, font="arial.ttf", size=15),
        }
        oled.text ("NO INTERNET", 8)
        oled.text ('\uf165', 9)
        oled.text ("CONNECTION", 10)
        time.sleep(5)
        oled.clear()

jdts
Posts: 36
Joined: Mon Dec 23, 2019 2:55 pm

Re: (help needed): Migrate from Python to MicroPython

Post by jdts » Sun Mar 21, 2021 4:04 pm

Complicated python libraries don't typically "just work" in MicroPython without some porting, since more advanced supporting libraries are pared down. Looks like your scraper library uses requests. There are several µPy adaptations of this library called "urequests". So you'd need to explore how to port the scraper code to use urequests, which may require some minor simplification and a good bit of testing.

PNZ20
Posts: 4
Joined: Tue Mar 16, 2021 10:16 am

Re: (help needed): Migrate from Python to MicroPython

Post by PNZ20 » Mon Mar 22, 2021 9:17 am

Thanks, so this is why upip does not find lots of packages that work with pyton.
I'll give a try with urequest instead of cfscrape.
Thanks a lot

Post Reply