Showing posts with label MicroPython. Show all posts
Showing posts with label MicroPython. Show all posts

Saturday, 17 November 2018

Develop Micropython Firmware -- 01: Pure Python Based Driver

Micropython is almost like a mini operating system on a MCU and I'd like to build my own project standing on its shoulder.

Step 1: Setting up the environment
Just follow:
or

The awesome thing is that all the environment is in a virtual machine. Your host is safe. So finish configure the vagrant virtual box first and then compile ESP-toochain and Micropython source code in it.

pitfall 1: 
Depending on what kind of error you might run into, the following commands might help:

- vagrant box update
- vagrant box add ubuntu/trusty32 https://atlas.hashicorp.com/ubuntu/boxes/
vagrant provision

Step 2: coding the hello driver
Going into ~/micropython/drivers/, I found there are modules with C codes based and pure python based. In this tutorial, we focus on the pure python based drivers. For example, onewire and display.

step 2.1
mkdir hello_mpy

step 2.2
code hello_mpy.py:

def my_hello_mpy():
  print('This is my first micropython module.')

step 2.3
link it to esp8266 port.

cd ~/micropython/ports/esp8266/modules
ln -s ../../../drivers/hello_mpy/hello_mpy.py hello_mpy.py
ls -l
total 40
-rw-rw-r-- 1 vagrant vagrant  470 Nov 15 04:12 apa102.py
-rw-rw-r-- 1 vagrant vagrant  219 Nov 15 04:12 _boot.py
lrwxrwxrwx 1 vagrant vagrant   27 Nov 15 04:12 dht.py -> ../../../drivers/dht/dht.py
lrwxrwxrwx 1 vagrant vagrant   35 Nov 15 04:12 ds18x20.py -> ../../../drivers/onewire/ds18x20.py
-rw-rw-r-- 1 vagrant vagrant 1110 Nov 15 04:12 flashbdev.py
lrwxrwxrwx 1 vagrant vagrant   39 Nov 17 04:03 hello_mpy.py -> ../../../drivers/hello_mpy/hello_mpy.py
-rw-rw-r-- 1 vagrant vagrant 1425 Nov 15 04:12 inisetup.py
-rw-rw-r-- 1 vagrant vagrant  836 Nov 15 04:12 neopixel.py
-rw-rw-r-- 1 vagrant vagrant  850 Nov 15 04:12 ntptime.py
lrwxrwxrwx 1 vagrant vagrant   35 Nov 15 04:12 onewire.py -> ../../../drivers/onewire/onewire.py
-rw-rw-r-- 1 vagrant vagrant 1094 Nov 15 04:12 port_diag.py
lrwxrwxrwx 1 vagrant vagrant   22 Nov 15 04:12 upip.py -> ../../../tools/upip.py
lrwxrwxrwx 1 vagrant vagrant   31 Nov 15 04:12 upip_utarfile.py -> ../../../tools/upip_utarfile.py
-rw-rw-r-- 1 vagrant vagrant 2118 Nov 15 04:12 webrepl.py
-rw-rw-r-- 1 vagrant vagrant 2748 Nov 15 04:12 webrepl_setup.py
-rw-rw-r-- 1 vagrant vagrant 1624 Nov 15 04:12 websocket_helper.py

step 2.4 compile again
make axtls
make

step 2.5 
Get the .bin file out of vm.

pitfall 2:
I wasn't able to get my compiled firmware-combined.bin out from vm to host.
I eventually used vagrant-scp tool and it worked.
            https://github.com/invernizzi/vagrant-scp
It works both way, host to vm or vm to host.


Step 3: flash the new firmware
esptool.py -p /dev/ttyUSB0 --baud 460800 write_flash --flash_size=detect 0 firmware-combined.bin

Step 4: use it
>>>import hello_mpy
>>>hello_mpy.my_hello_mpy()
This is my first micropython module.









Thursday, 15 November 2018

vagrant up issue

Ran in to problem when running:
$vagrant up

Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'hashicorp/precise64'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'hashicorp/precise64' is up to date...
==> default: Setting the name of the VM: aws_default_1449506528859_
==> default: Fixed port collision for 22 => 2222. Now on port 2200.
==> default: Clearing any previously set network interfaces...
==> default: Specific bridge 'en0' not found. You may be asked to s
==> default: which network to bridge to.
==> default: Preparing network interfaces based on configuration...
default: Adapter 1: nat
default: Adapter 2: bridged
==> default: Forwarding ports...
default: 22 => 2200 (adapter 1)
==> default: Running 'pre-boot' VM customizations...
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minut
The guest machine entered an invalid state while waiting for it
to boot. Valid states are 'starting, running'. The machine is in th
'poweroff' state. Please verify everything is configured
properly and try again.
If the provider you're using has a GUI that comes with it,
it is often helpful to open that and watch the machine, since the
GUI often has more helpful error messages than Vagrant can retrieve
For example, if you're using VirtualBox, run vagrant up while the
VirtualBox GUI is open.
The primary issue for this error is that the provider you're using
is not properly configured. This is very rarely a Vagrant issue.
#######################################################
You simply need to turn on the virtualization setting in your BIOS.



Thursday, 16 November 2017

MicroPython + NodeMCU Getting Started


(0) sudo pip install esptool
(1) esptool.py --port /dev/ttyUSB0 erase_flash

esptool.py v2.1
Connecting....
Detecting chip type... ESP8266
Chip is ESP8266
Uploading stub...
Running stub...
Stub running...
Erasing flash (this may take a while)...
Chip erase completed successfully in 7.8s
Hard resetting...

(2) esptool.py --port /dev/ttyUSB0 --baud 460800 write_flash --flash_size=detect 0 esp8266-20171101-v1.9.3.bin 

esptool.py v2.1
Connecting....
Detecting chip type... ESP8266
Chip is ESP8266
Uploading stub...
Running stub...
Stub running...
Changing baud rate to 460800
Changed.
Configuring flash size...
Auto-detected Flash size: 4MB
Flash params set to 0x0040
Compressed 600888 bytes to 392073...
Wrote 600888 bytes (392073 compressed) at 0x00000000 in 8.9 seconds (effective 542.3 kbit/s)...
Hash of data verified.

Leaving...
Hard resetting...

(3) picocom /dev/ttyUSB0 -b 115200

picocom v1.7

port is        : /dev/ttyUSB0
flowcontrol    : none
baudrate is    : 115200
parity is      : none
databits are   : 8
escape is      : C-a
local echo is  : no
noinit is      : no
noreset is     : no
nolock is      : no
send_cmd is    : sz -vv
receive_cmd is : rz -vv
imap is        : 
omap is        : 
emap is        : crcrlf,delbs,

Terminal ready

////////////////////////////////////////////////
I have to unplug and plug the USB back in. Resetting the device won't work. I got the following error message:

picocom v1.7

port is        : /dev/ttyUSB0
flowcontrol    : none
baudrate is    : 115200
parity is      : none
databits are   : 8
escape is      : C-a
local echo is  : no
noinit is      : no
noreset is     : no
nolock is      : no
send_cmd is    : sz -vv
receive_cmd is : rz -vv
imap is        : 
omap is        : 
emap is        : crcrlf,delbs,


FATAL: cannot open /dev/ttyUSB0: Device or resource busy

This could also be a bad usb cable. Use a good one with a data line on it.


///////////////////////////////////////////////////
///////////////////////////////////////////////////
///////////////////////////////////////////////////
Also you might need to press enter a few times to see the Python prompt:

.......
imap is        : 
omap is        : 
emap is        : crcrlf,delbs,

Terminal ready

>>> 
>>> 
>>> 

(4) Start playing around
Hookup an LED on D7 which is mapped to GPIO13.

>>> import machine
>>> pin = machine.Pin(13, machine.Pin.OUT)
>>> pin.on()
>>> pin.off()

You can see the LED goes on and off now. Happy tinkering!


ref:
https://docs.micropython.org/en/latest/esp8266/esp8266/tutorial/intro.html
https://dev.to/kenwalger/micropython-and-the-nodemcu-esp8266
http://www.instructables.com/id/MicroPython-Basics-Using-NodeMCU-ESP8266/
https://hackaday.com/2016/07/21/micropython-on-the-esp8266-kicking-the-tires/