A call to help me learn code the right way on MicroPhyton

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
iotmar
Posts: 8
Joined: Wed Sep 04, 2019 8:56 pm

A call to help me learn code the right way on MicroPhyton

Post by iotmar » Thu Sep 19, 2019 3:14 am

G'day reader, after about 2 months trying things out, learning, doing the RTF thing, I still get the feeling that i'm drifting away
from what microphyton should be, a good understandable language that with some good effort is to master.

- I had no problem flashing the firmware on a esp8266 or esp32, all fine. Than I needed to figure out how to communicate with the
board and put my first hello world on it.

- You would think for an organisation as this, there would be some kind of ide available which a new user can use for the first year.
But ehrm no, not such thing from MIcrophyton.

I did find out however on this site there was a book who would learn microphyton and spoke of a IDE, the uPycract, great for beginners. So
I bought that course, lookes nice, but when it came to installing the uPycraft it went downhill fast... It was for Ubuntu 16! only. Could not get it working on 18.4. And so I downgraded to 16.04, uPycraft works than fine, but other software in your distro demands newer versions, like pip, phyton and so on. So that was a showstopper again.

Further was it kinda frustrating that the docs section is ehm short. and it's focus is on the let's say the function itself.

You have something like this from Networking
A useful function for connecting to your local WiFi network is:

def do_connect():
import network
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
if not wlan.isconnected():
print('connecting to network...')
wlan.connect('essid', 'password')
while not wlan.isconnected():
pass
print('network config:', wlan.ifconfig())
Once the network is established the socket module can be used to create and use TCP/UDP sockets as usual, and the urequests module for convenient HTTP requests.
If I look up the documentation under sockets / sockets, I get a technical manual from several pages, written by experts for experts.

So after a few days I did that put aside as well, it's great that i learned how to connect to the internet, but the next steps, how to setup up a socket, call an api etc. I could not find it.

So after a coffee break, I fired up two monitors again and started scaving youtube for " instructions" microphyton with esp8266 / esp32.
There where a buch , bet even more where not so easy to reproduce and to learn from. Sourcecode not up to date if the video is a few months old... so you want to run it... you can't due to errors.
Verry funny one, was an error i kept getting with the sdd1306 OLed driver. Every video was using different ones, i was ploughing on git through the libs of the Microphyton git itself and found an official one, just 4 days ago edited. Used that library and it workd fine.

So I got my esp32 working, with firmware, correct pins in boot.py stated for the attached oled. you have to declare pin16 first, then pull it down, then pull it up, then give it value 1. before you can do a thing with your display. Creative idea from the manufactor I think, pin 16 the rst pin :)
So I finally was able to put some text on that little display \0/ now was it time to start bringing in the information for my project, the reason why I want to learn this all.. A Microphycosmic project, that tracks items and peoples in space, from Nasa and Esa and Xspace.
Depending on what is happening in outerspace, several units running microphyton will handle tasks.
- If the ISS flies over my house and is visible, a nice diaroma comes more to life with blinky leds etc. :-)

With a raspberry pi besides it, with a nice 7" screen a microphyten device could trigger that screen and popup the livestream when SpaceX shoots another awesome rocket in the sky.

As I'm totally new in programming also a reason why i choose microphyton I thought i made a good choice, but i'm not so shure anymore.
For example I was trying to grab the data from the nasa api about iss station passes and i could not access it in the way it should.

With my request.get i did get the .json file I desired but was not able to manipulate it. phyton says it's a str and it has to be an obj.
So since i'm not giving up that easy, i did reasearch again, but I drifted farther and farter away from the core microphyten.
at some moment I was importing libraries from phyton in the hope it would work in microphyton. urllib is such a famous on.

And again some good working tutorials about some fun missing projects are missing in the microphyton docs here.

--how to connect your microphyton to the GCP and upload data from connected sensors and visualise them?
-- or the otherway around, you have on your desk an esp32 who receives data from the cloud and depending what it receives comes
in to action gives a warning signal, or switches the coffeemaker on?

-- what about some nice tutorials about little insects / cars you could make with microphyton? -- and if you have make 4 of 5 of them, they follow each other as a snake body.

Computers are for 32 years now a part of my life, it still amazes me everyday what people can do with them, what great programs they can write. that's something that has always been out of my reach, but now I have the time , the will and see the fun in it with creating software. I can build and maintain computers, administer them, Linux and Windows (sorry no Macs) have servers running, but that last oh so important part is missing.... a bit of programming ;-)

-------
So I wrote all this down not as a frustrated person, who is about to through it all away. I had for shure some hours and hours of fun diving through the heaps of information but the endresult was not always that good. That's why I noted everything down what came down on my path the last ehm 2 months *\/* from 0 i'm now at .25 microphyton. knowledge

-So I hope there is a good IDE in the near future where I can upload code directly to my boards (esp8266 & sm32 & RPiB+)
With the goodies that come with an IDE.

Also to help me really out, if possible I would like to start a topic here on this forum where others could join in to help me write code or point me to the right direction. It might be looking a big project, but that's the end result. I want to make module for module.
I hope for some good feedback and I hope that I gave the organisation a few ones.
from all the options out in the world, i do believe this is the best option. Mongoose Os is nice for starting and to get your device easy on GCP but otherwise .. PlatformIO does promis and advertise a lot.. but what a messss when you install it in VSC, Extensions who don't like to work with eachother, you can't compile cause the great big Intellisense sees a mistake, run the code elsewhere and it's all fine.

So my last remark .. keep microphyton simple and keep it microphyton or maybe..maybe.. combine it with nodejs. npm
just to keep me sain i followd this tutorial https://thisdavej.com/node-js-iot-tracking-the-iss-through-the-sky/ and it was a breeze, it worked.
after that tut I put credentials in from my other site altered the code a bit and voila:
{ info:
{ satid: 25544,
satname: 'SPACE STATION',
transactionscount: 0,
passescount: 4 },
passes:
[ { startAz: 206.59,
startAzCompass: 'SSW',
startEl: 0.04,
startUTC: 1569090875,
maxAz: 144.17,
maxAzCompass: 'SE',
maxEl: 16.48,
maxUTC: 1569091160,
endAz: 82.21,
endAzCompass: 'E',
endEl: 14.47,
endUTC: 1569091440,
mag: -0.2,
duration: 350 },
{ startAz: 194.34,
startAzCompass: 'S',
startEl: 0.14,
startUTC: 1569174405,
maxAz: 139.78,
maxAzCompass: 'SE',
maxEl: 11.44,
maxUTC: 1569174665,
endAz: 84.94,
endAzCompass: 'E',
endEl: 4.3,
endUTC: 1569174925,
mag: 0.2,
duration: 445 },
It shows the next passes of the ISS above my house, took about 15 minutes I think. but that was with Nodejs & Npm
I hope something can also be adchieved with microphyton. I do!!

Thank you for ready my first and lang post ;)
And if you have something to say.. please do.. all information is welcome, my microphyton learningsponge is dry ;-))

Martin

torwag
Posts: 216
Joined: Fri Dec 13, 2013 9:25 am

Re: A call to help me learn code the right way on MicroPhyton

Post by torwag » Thu Sep 19, 2019 6:12 am

Hi Martin,

welcome to the forum and with that you might have taken the correct way to step in.
Just to clarify there is nothing like "the organisation". Damien the founder of Micropython runs a small business selling pyboards.
There are a few other small companies relying on Micropython. However, there is no commercial player no big company behind Micropython, the language, itself.

Most of your problems and challenges can be solved here. There was an IRC chat as well, did not connect to this for a long time.

You are right with the fact, that many docs are more technical related. That is a typical phenomenon of open source projects where the devs are busy getting the code right and take less effort writing tutorials. However, Micropython is on a good way. As you noticed yourself, people start to write books about it and this shows, that more and more people help to buld the ecosystem around the coding tasks.

As for now, a good way to learn and something that Micropython is ideal for, look in projects which interest you. Could be the little wifi-switch, the irrigation system, the little robot, the tiny html-server.... on github and in the forum you will find a lot of projects. As python is easy readable you can look how it was done, start tinker with it, test it out and learn on that route. As Micropython tries to be as close as possible to the "real" Python, many libs try to do the same. Thus, if you have rather generic libs, which have a counterpart in the cpython libs, you can often find that they are mostly identical, in terms of parameters. You might find that the Micropython-lib is only a subset of the bigger brother but often tutorials and docs of the bigger brother can be used to get a grip on what to do.

Yep sometimes it is frustrating not to find the right documentation but that is something everyone coming to this point can help to improve over time.

As for the IDE there is nothing like THE IDE.... many people with different tastes. However, some bigger IDEs started to build in Micropython support. Then again it depends on your taste of what kind of editor you like.... as you know this kind of discussion can start a war in that region of the internet ;)

Hope that refills a bit your Micropython-Battery

User avatar
BradenM
Posts: 7
Joined: Thu Jul 11, 2019 6:29 am
Contact:

Re: A call to help me learn code the right way on MicroPhyton

Post by BradenM » Thu Sep 19, 2019 7:40 am

Hi Martin,

Welcome to the community. I hope you are able to persist through the learning curve and continue to explore micropython/programming despite your frustrations. I highly doubt there is anyone here who has not had a similar experience to yours at some point in their learning path. Just gotta keep pushing through!

Here are some resources that I hope will help you out:
  • LeMaRiva Tech - Awesome blog with over 25 MicroPython posts/projects.
As for your troubles with an IDE, I suggest reading LemaRiva's #MicroPython: VSCode IntelliSense, Autocompletion & Linting capabilities blog post. Full disclosure, I am the author of micropy-cli (an extension used in the article), but I do believe its currently one the most effective methods of managing a micropython environment :)

Don't give up, we've all been there before!

rpr
Posts: 75
Joined: Sat Oct 27, 2018 5:17 pm

Re: A call to help me learn code the right way on MicroPhyton

Post by rpr » Thu Sep 19, 2019 8:00 am

Hi Martin,

Welcome!! I am enjoying my journey with MicroPython. Sure it is challenging but you can do a lot. To get started here is some code that I just wrote in MicroPython.

File is called whereisiss.py

Code: Select all

# Make sure you have installed urllib.urequest as follows from within Micropython
# >>> import upip
# >>> upip.install('urllib.urequest')

import json
import urllib.urequest as urlreq

def getss():
    x = urlreq.urlopen("http://api.open-notify.org/iss-now.json")
    return json.loads(x.read())
In order for the code to work, you need an additional library that can be installed using 'upip'. See the first comment.

Once you have the file whereisiss.py on the board, do the following:

Code: Select all

>>> from whereisiss import getss
>>> y =  getss()
{'message': 'success', 'iss_position': {'longitude': '155.7770', 'latitude': '17.5533'}, 'timestamp': 1568878850}
>>> y['iss_position']['longitude']
'-151.0533'
Hopefully this can get you started. Happy coding. :)

User avatar
pythoncoder
Posts: 3655
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: A call to help me learn code the right way on MicroPhyton

Post by pythoncoder » Thu Sep 19, 2019 8:05 am

Another place to look for drivers/libraries is the wiki.
Peter Hinch

iotmar
Posts: 8
Joined: Wed Sep 04, 2019 8:56 pm

Re: A call to help me learn code the right way on MicroPhyton

Post by iotmar » Fri Sep 20, 2019 12:11 pm

First, thank you all for the warm welcome,

second, the given answers combined gave me the exact package I was hoping for, so a big thank you for you all.

The battery did get a boost, I have an IDE with the right / good extensions, working lines of code on which I can build. :D

On the code part, I had to tweak it a little, the library did not import as

"import urllib.urequest as urlreq"

But I knew from my earlier attempts with that library that inside was a file urequests.py, so when I replaced urllib.requests with urequests the code worked fine. Can't recall exactly what the reason is for it, did read something about it on using phyton libraries in microphyton I think but I did read a lot of scattered articles on the web the past weeks :-)
I also noted it got installed in the /lib folder now, earlier I copied that library to the root not in a folder and a folder named /libs. That might have also been a reason it did not work at that time :-) But now it does, yay! :-)

Thanks again,
Martin

User avatar
ta1db
Posts: 37
Joined: Mon Sep 02, 2019 12:05 pm
Contact:

Re: A call to help me learn code the right way on MicroPhyton

Post by ta1db » Fri Sep 20, 2019 1:30 pm

rpr wrote:
Thu Sep 19, 2019 8:00 am
File is called whereisiss.py

Code: Select all

# Make sure you have installed urllib.urequest as follows from within Micropython
# >>> import upip
# >>> upip.install('urllib.urequest')

import json
import urllib.urequest as urlreq

def getss():
    x = urlreq.urlopen("http://api.open-notify.org/iss-now.json")
    return json.loads(x.read())
worked on STM32F7DISC flawlessly !.

iotmar
Posts: 8
Joined: Wed Sep 04, 2019 8:56 pm

Re: A call to help me learn code the right way on MicroPhyton

Post by iotmar » Fri Sep 20, 2019 2:10 pm

ta1db wrote:
Fri Sep 20, 2019 1:30 pm

worked on STM32F7DISC flawlessly !.
Might be the board, I was using an esp8266, will try an stm32 later. But with the small edit I got it working :-)

One small step... :-)

User avatar
ta1db
Posts: 37
Joined: Mon Sep 02, 2019 12:05 pm
Contact:

Re: A call to help me learn code the right way on MicroPhyton

Post by ta1db » Fri Sep 20, 2019 2:50 pm

One small step... :-)
This small step is very important ! Congratulations...

BTW of course I did something extra on top of that code such as:

Code: Select all

rshell
#you have to create a /flash/lib directory first
cp upip.py /flash/lib
cp upip_utarfile.py /flash/lib
repl
import network
lan=network.LAN()
lan.active(True)
lan.ifconfig("dhcp")
import socket
s=socket.socket()
Last edited by ta1db on Sat Sep 21, 2019 6:13 am, edited 1 time in total.

rpr
Posts: 75
Joined: Sat Oct 27, 2018 5:17 pm

Re: A call to help me learn code the right way on MicroPhyton

Post by rpr » Fri Sep 20, 2019 8:05 pm

ta1db wrote:
Fri Sep 20, 2019 2:50 pm
iotmar wrote: One small step... :-)
This small step is very important ! Congratulations...
Python/MicroPython is a very powerful tool. It does have a small learning curve but once you get past you realize you can do a lot with it. Being able to just do prototype development interactively is a huge plus since you don't have to compile, build, flash every single time. Very easy to add modules as you test and write.

The one downside is that it does not have a nice custom GUI IDE such as the one for the arduino. There are people who have made it work with VS Code (see Braden's post earlier in this thread).

Personally I'm comfortable with command line and I prefer working with it using tools such as rshell. Using the CLI is great especially when you have to connect remotely.

Post Reply