Experimenting with a few things…

Posted: July 20, 2016 in General

Once again, time has gone on and I’ve not added much for a while to the site.  However I’ve been playing around with a few things which have been lots of fun.

So I’ll keep adding to this post a little at a time and take you through a few of the interesting things I’ve got my hands on…  No spoilers..yet..but a few really excellent things (at least I think so).

Using Micro-Python with the NodeMCU Board

As usual I’ve written myself some notes on getting this setup, and I fully intend to share (once I get time to write it properly).  However some very quick lessons learnt…

Lesson 1: Micro Python is very nice, and you can use a Python-like terminal with the device by connecting through the serial.  Very swish…but frankly no cigar yet.

Lesson 2: Micro Python with a Wifi device like the ESP8266 on the NodeMCU board is great…but remember the ESP8266 Wifi settings are separate to the firmware, so you may need to ensure it is set to station mode first, before the micropython firmware will reset it.

Lesson 3: When you get webREPL running…we are getting better  – developing over Wifi yes please!  If it isn’t running once you’ve programmed the firmware (which it wasn’t on mine) you’ll never be able to connect using the webREPL console page (which is a html page – you can download a local copy or visit http://micropython.org/webrepl/).  Don’t get annoyed it doesn’t work until you’ve started it!

Lesson 4: Use the serial python console to start webREPL on the device as follows:

>> import webrepl
>>> webrepl.start()
WebREPL daemon started on ws://192.168.4.1:8266
Started webrepl in setup mode
>>>

Lesson 5: You must connect to the device via Wifi to use the web console!  Connect to the device via the AP it provides “MicroPython-ffffff” with default password “micropythoN).

First time you connect via the console (using the address shown when it starts) you’ll set a new password.  Then reset the board and reload webrepl as before.

Lesson 6: Unless you want to always manually start webrepl use the boot.py file and upload it.  This will run on power up!

boot.py

import webrepl
webrepl.start()

Lesson 7: Micro Python is a little annoying…it supports files so you can upload .py files and import them…BUT there is no editing via the webREPL console.  So edit and upload? No not quite!

Lesson 8: Uploading new files and “import mymodule” isn’t enough to update your code!  Import will not reload a module if it is already loaded…deleting a module (using “del mymodule”) will remove it but it is still cached so it will still not update.  WHAT A PAIN!

Lesson 9: After a few experiments (and a hint from @mnelsoneorm1 to checkout https://forum.micropython.org) managed to work out a way to do it.  Sometime later, I wrapped up all the useful bits into a util.py file, which I loaded at startup as well.

boot.py

import webrepl
import util
webrepl.start()

util.py

def readfile(file="boot.py"):
  with open(file,'r') as thefile:
    data = thefile.read()
    print(data)

def delfile(file):
  import os
  os.remove(file)

def reload(module):
  import sys
  del sys.modules[module]

Lesson 10: The upload might not upload a new version of your file…You may need to upload a different file first to ensure the newer one is reloaded into the browser.

Final lesson is NeoPixels are simply awesome (and deceptively easy)!

neo.py

import machine,neopixel
v=0.5
print(v)
NUM=24
np=neopixel.NeoPixel(machine.Pin(4),NUM)
OFF=off=(0,0,0)
R=r=(255,0,0)
G=g=(0,255,0)
B=b=(0,0,255)
Y=y=(255,255,0)
C=c=(0,255,255)

def clear():
  all(OFF)

def all(val):
  for i in range(NUM):
    np[i]=val
    np.write()

def pattern(val1,val2,val3,val4):
  for i in range(NUM/4):
    np[(i*4)]=val1
    np[(i*4)+1]=val2
    np[(i*4)+2]=val3
    np[(i*4)+3]=val4
  np.write()

def demo():
  import time
  for i in range(10):
    pattern(R,R,G,B)
    time.sleep(0.1)
    pattern(B,R,R,G)
    time.sleep(0.1)
    pattern(G,B,R,R)
    time.sleep(0.1)
    pattern(R,G,B,R)
    time.sleep(0.1)

np[0]=(255,0,0)
np[1]=(0,128,0)
np[2]=(0,0,64)
np.write()

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.