Sunday, July 29, 2018

Installing MicroPython on ESP8266 and ESP32

Windows 10
MacOS Mojave 10.14.1
MicroPython 1.9.4.8


Goals:

  • Use Python for Microcontrollers (MicroPython) on Wemos D1 mini (ESP8266) and ESP32.

Info:
  • MicroPython is a condensed, optimized code of Python 3 that has been loaded in hardware. This means rather than having to have an interpreter run on an operating system to execute the Python code, the MicroPython chip can run the Python code directly on the hardware. No operating system is needed. In fact, MicroPython has basic file I/O built in.
  • After a fresh install and boot the device configures itself as a WiFi access point (AP) that you can connect to. The ESSID is of the form MicroPython-xxxxxx where the x’s are replaced with part of the MAC address of your device (so will be the same every time, and most likely different for all ESP8266 chips). The password for the WiFi is micropythoN (case-sensitive). Its IP address will be 192.168.4.1 once you connect to its network.

Installing Micropython:
  • Install the latest EspTool (2.5.0+):
  • Windows: using Windows Terminal (cmd):
    • For using ESP32 on WIndows we need to install USB Driver:
      • Download the driver and follow the instructions here.
    • pip install esptool --upgrade
    • pip3 install adafruit-ampy --upgrade
    • set AMPY_PORT=COM?
  • Linux:
    • sudo pip install esptool --upgrade
    • pip3 install adafruit-ampy --upgrade
    • export AMPY_PORT=/dev/ttyUSB0
  • MacOS: using Terminal:
    • pip3 install esptool --upgrade
    • pip3 install adafruit-ampy --upgrade
    • export AMPY_PORT=/dev/tty.usbserial-1420
    • For using ESP32 on MacOS we need to install USB Driver:
  • Erase the flash (ESP8266 and ESP32):
    • Windows:
      • locate the latest esptool.py installed:
        • cd \
        • dir /S esptool.py
      • python <esptool-path>/esptool.py --port COM? erase_flash
    • Linux:
      • python <esptool-path>/esptool.py --port /dev/ttyUSB0 erase_flash
    • MacOS:
      • python <esptool-path>/esptool.py --port /dev/tty.usbserial-1420 erase_flash
  • Deploy the new firmware on ESP8266:
    • Download MicroPython image here:
    • Windows:
      • python <esptool-path>/esptool.py --port COM? --baud 460800 write_flash --verify -fs=detect -fm dio 0 \users\Marcus\Downloads\esp8266-20180511-v1.9.4.bin
    • Linux:
      • python <esptool-path>/esptool.py --port /dev/ttyUSB0 --baud 460800 write_flash --verify -fs=detect -fm dio 0 /home/marcus/esp8266-20180511-v1.9.4.bin
    • MacOS:
      • python <esptool-path>/esptool.py --port /dev/tty.usbserial-1420 --baud 460800 write_flash --verify -fs=detect -fm dio 0 /Users/marcus/Downloads/esp8266-20180511-v1.9.4.bin
  • Deploy the new firmware oESP32:
    • Download MicroPython image here:
    • Windows:
      • python <esptool-path>/esptool.py --chip esp32 --port COM4 write_flash -z 0x1000 \users\Marcus\Downloads\esp32-20180730-v1.9.4-410-g11a38d5dc.bin
    • Linux:
      • python <esptool-path>/esptool.py --chip esp32 --port /dev/ttyUSB0 write_flash -z 0x1000 /home/marcus/esp32-20180730-v1.9.4-410-g11a38d5dc.bin
    • MacOS:
      • python <esptool-path>/esptool.py --chip esp32 --port /dev/tty.SLAB_USBtoUART write_flash -z 0x1000 /Users/marcus/Downloads/esp32-20180730-v1.9.4-410-g11a38d5dc.bin
  • Installing Libraries not included in MicroPython using upip and REPL:
    • Connecting to Internet:
      • import network
      • station = network.WLAN(network.STA_IF)
      • station.active(True)
      • station.connect('ssid', 'password')
      • print('Connected: {}'.format(station.isconnected()))
      • ipadd=station.ifconfig()
      • print('IP: {}'.format(ipadd))
    • import upip
    • Use upip to install any library available on PyPI:
      • upip.install('micropython-umqtt.robust')
      • upip.install('micropython-umqtt.simple')
Testing:
  • List files on the board using Ampy, MFPShell or RShell:
    • ampy ls (needs AMPY_PORT env var)
    • mpfshell
      • open tty.SLAB_USBtoUART 
      • ls
    • rshell
      • ls -l
  • Connect and interact with a MicroPython board using a serial connection.
  • Connect with the Device REPL using a terminal. The baudrate of the REPL is 115200 (Data: 8 bit; Parity: None; Stop bits: 1 bit; Flow Control: None):
      • Windows:
        • Use Putty or any other terminal software.
      • Linux:
        • ToDo
      • MacOS:
        • miniterm.py /dev/tty.usbserial-1420 115200
    • Press Enter and the terminal should show Python prompt like >>>
  • Try a Hello World:
    • print("Hello World")
    • import esp
    • esp.flash_size()
    • import uos
    • uos.listdir()
  • Blink Example:
    • from machine import Pin
    • import utime as time
    • led_builtin = 2  # D4=gpio02
    • led = Pin(led_builtin, Pin.OUT)
    • while True:
      • led.on()
      • time.sleep(1)
      • led.off()
      • time.sleep(1)


Additional Information:

  • You can use the Arduino IDE to upload your C/C++ software to the ESP boards, even after the upload of the MicroPython firmware. You don't need to upload the original Expressif default firmware to use the Arduino IDE. However, if you really wants to upload the Expressif firmware, then these as the instructions for reverting board to the original Expressif default firmware to use with arduino C/C++ language:




      • ESP8266_NONOS_SDK-master/bin/blank.bin  at  0x7E000 and 0x3E000
      • ESP8266_NONOS_SDK-master/bin/esp_init_data_default_v08.bin  at  0x7C000
      • ESP8266_NONOS_SDK-master/bin/boot_v1.7.bin  at  0x0
      • ESP8266_NONOS_SDK-master/bin/at/1024+1024/user1.2048.new.5.bin  at  0x1000
    • ESP32:
    • Choose the right COM port
    • Choose Baud Rate 460800
    • Click "Start" button.
Tools:

References:

Friday, April 27, 2018

Installing MongoDB on Ubuntu 18.04 32bits

Ubuntu 18.04 Desktop
MongoDB Community Edition 3.2.19


Goals:

  • Install a mongodb server in Ubuntu 18.04 32bits.

Install:

  • curl -O https://fastdl.mongodb.org/linux/mongodb-linux-i686-3.2.19.tgz
  • tar -zxvf mongodb-linux-i686-3.2.19.tgz
  • mkdir -p ~/mongodb
  • cp -R -n mongodb-linux-i686-3.2.19/ mongodb
  • export PATH=~/mongodb/bin:$PATH
  • Starting MongoDB with previous config file and legacy storage engine. This way the databases from older version still active and with data already saved.
  • sudo ~/mongodb/bin/mongod --storageEngine=mmapv1 --config /etc/mongodb.conf

Testing:
Troubleshooting:

  • Error: solved Cannot start server. The default storage engine 'wiredTiger' is not available with this build of mongod.
    • force mmapv1 engine with option mongod --storageEngine=mmapv1

References:

If you like this content, feel free to

Tuesday, May 9, 2017

Wemos ESP8266 - mDNS and OTA firmware update

Windows 10
Ubuntu Linux 16.04
Arduino IDE 1.8.1


Goal:

  • Test Multicast DNS (mDNS) and Over The Air (OTA) firmware update for using with IoT devices.

Status:

2017.05.09 - It's working.

ToDo:
  • Test OTA functions onStart, onEnd, onProgress and onError.

Code:

#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <Hash.h>
// OTA firmware update
#include <ESP8266HTTPUpdateServer.h>
const String DEVICE_ID  = "Test_OTA_mDNS_v0.0.1";
const char*  ssid       = "SSID";
const char*  password   = "pass";
const int    httpPort   = 8080; // OTA firmware update
#define      DEBUG        true
#define ARRAY_SIZE 6
String activeClients[ARRAY_SIZE];
ESP8266WiFiMulti WiFiMulti;
// OTA firmware update
ESP8266WebServer httpServer(httpPort);
ESP8266HTTPUpdateServer httpUpdater;
void setup() {
    Serial.begin(115200);
    if (DEBUG) Serial.println(DEVICE_ID);
    for(uint8_t t = 4; t > 0; t--) {
        if (DEBUG) Serial.printf("[SETUP] BOOT WAIT %d...\n", t);
        Serial.flush();
        delay(1000);
    }
    // ESP Chip Id
    if (DEBUG) Serial.println(ESP.getChipId());
    WiFiMulti.addAP(ssid, password);
    while(WiFiMulti.run() != WL_CONNECTED) {
        delay(100);
    }
    // ESP client IP
    if (DEBUG) Serial.println(WiFi.localIP());
    // OTA
    httpUpdater.setup(&httpServer);
    httpServer.begin();
    if(MDNS.begin("OTASom4r")) {
        if (DEBUG) Serial.println("MDNS responder started");
    }
    // Add service to MDNS
    MDNS.addService("http", "tcp", httpPort);
}
void loop() {
  httpServer.handleClient();  
}
/*
void onStart(OTA_CALLBACK(fn)) {
  Serial.println("onStart");
}
void onEnd(OTA_CALLBACK(fn)) {
  Serial.println("onEnd");
}
void onProgress(OTA_CALLBACK_PROGRESS(fn)) {
  Serial.println("onProgress");
}
void onError(OTA_CALLBACK_ERROR (fn)) {
  Serial.println("onError");

}
*/

Installing mDNS:

  • Windows:
    • Install Bonjour (included with Safari Browser for Windows).
  • Linux:
    • sudo apt-get update
    • sudo upt-get install libnss-mdns

Testing:


Site to upload new firmware.
Serial log while device is rebooting.


References:
If you like this content, feel free to

Sunday, June 26, 2016

Installing WebPy, SQLAlchemy and SQLite on Termux

Android 5
Termux 0.34 (Terminal emulator and Linux environment for Android)
WebPy 0.37
SQLAlchemy 1.0.13
SQLite 3.13.0


Installing packages on Termux:
  • apt update
  • Installing Python 2:
  • apt install python2
  • apt install nano
  • apt install openssh
Installing (Easy way) on Termux:
  • apt install sqlite
  • wget http://webpy.org/static/web.py-0.38.tar.gz
  • tar -xzvf  web.py-0.38.tar.gz
  • cd web.py-0.38
  • pyhton2 setup.py install
  • download SQLAlchemy package (http://www.sqlalchemy.org/download.html)
  • tar -xzvf  SQLAlchemy-1.1.5.tar.gz
  • cd SQLAlchemy-1.1.5
  • python2 setup.py install
Installing (Hard way) on Termux:
  • Installing easy_setup:
  • wget https://bootstrap.pypa.io/ez_setup.py -O - | python
  • IF it doesn't work this way:
  • THEN:
    • python2
    • $ import urllib2
    • $ response = urllib2.urlopen('https://bootstrap.pypa.io/ez_setup.py')
    • $ html = response.read()
    • $ f = open('ez_setup.py','w')
    • $ f.write(html)
    • $ f.close()
    • $ exit()
    • python2 ez_setup.py
  • Install WebPy framework:
  • easy_install web.py
  • Install SQLAlchemy:
  • easy_install sqlalchemy
  • Install SQLite:
  • download and extract SQLite3 zip files (DLL and Shell/Tools) to new folder (ex: c:\development\sqlite):

Testing:
  • nano webpy_test.py
    • import web
    • urls = (
    •     '/(.*)', 'hello'
    • )
    • app = web.application(urls, globals())

    • class hello:        
    •     def GET(self, name):
    •         if not name: 
    •             name = 'World'
    •         return 'Hello, ' + name + '!'

    • if __name__ == "__main__":
    •     app.run()
  • python2 webpy_test.py 8090
  • Connect using a REST client.


Links:

If you like this content, feel free to

Sunday, May 15, 2016

Updating firmware of Wi-Fi ESP8266 Module using FTDI

"This is one procedure for downloading and installing the latest available firmware on an ESP8266 using the flash tool provided by Espressif, the chip manufacturer."


Windows 10
PuTTY 0.67



Hint:

  • The operation of the ESP8266 outside of stated limits may be unstable and unreliable - you could damage your sanity.
  • The ESP8266 may draw more current than the 3.3V regulator on your Arduino can supply - you could damage your Arduino. Use a separate 3.3v power source.

Hardware:

  • ESP8266 - ESP01 module
  • FTDI - FT232RL module
  • StepDown 5v-3.3v module

Schematics:
Schematics using FTDI of 5v and a stepdown module




Downloads:


Installing the Firmware:

  • Power up your ESP connecting it to your PC.
  • Press and hold the Reset button, and then press and hold the Flash button. Release the Reset button, and then release the Flash button.
  • Click the box in the flash download tool GUI window labeled "SpiAutoSet," which will cause the download tool to automatically select the correct flash size and crystal frequency.
  • In the flash download tool GUI window, select checkbox on the left and click the START button.

Flash Download Tool DOS window

Flash Download Tool GUI window

  • Press and hold the Reset button, and then press and hold the Flash button. Release the Reset button, and then release the Flash button. Click the START button in the Flash Download Tool GUI window. The download should begin, and its progress should be shown in the Flash Download Tool GUI window and the log window, as depicted below.
Loading firmware

  • When the flash operation is complete, close the Flash Download Tool. Remove power from the ESP board, and then reconnect the power.

Testing:

  • Open PuTTY, and click the Serial radio button. Enter the COM port number (which must be less than 10) and the baud rate (which will most likely be 115200 or 9600.)
  • Click the Open button, and a PuTTY terminal session window should open.


  • Enable Caps Lock on your PC, and type AT, but don't press Enter. You should see AT in the PuTTY terminal window. If you don't, you may have selected the wrong COM port or the wrong baud rate. Close PuTTY and start again at the top of this section. The permissible baud rates are: 9600, 19200, 38400, 74880, 115200, 230400, 460800, and 921600; try each one in turn until you find the one that works.
  • Enable Caps Lock on your PC, and type AT, but don't press Enter. You should see AT in the PuTTY terminal window. Type a + sign followed by GMR. When you see AT+GMR in the PuTTY terminal window, while holding the Ctrl key down, press the M key followed by the J key. Release the Ctrl key. You should see the ESP8266 firmware information in the PuTTY terminal window similar to that shown in the picture below.




Links:

If you like this content, feel free to

Sunday, March 27, 2016

Install OpenCV and WebPy on Raspberry Pi

Raspbian
OpenCV


Install:
  • Install Raspberry Pi updates:
  • sudo apt-get update
  • sudo apt-get upgrade
  • sudo rpi-update
  • Reboot:
  • Install webpy:
  • cd ~
  • wget http://webpy.org/static/web.py-0.37.tar.gz
  • gzip -d -r web.py-0.37.tar.gz
  • tar -xvf web.py-0.37.tar 
  • cd ./web.py-0.37
  • sudo python setup.py install
  • Install OpenCV requirements:
  • sudo apt-get install build-essential cmake pkg-config
  • sudo apt-get install libgtk2.0-dev
  • sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
  • sudo apt-get install libatlas-base-dev gfortran
  • wget https://bootstrap.pypa.io/get-pip.py
  • sudo python get-pip.py
  • sudo pip install virtualenv virtualenvwrapper
  • sudo rm -rf ~/.cache/pip
  • Then, update your ~/.profile  file to include the following 3 lines:
  • # virtualenv and virtualenvwrapper
  • export WORKON_HOME=$HOME/.virtualenvs
  • source /usr/local/bin/virtualenvwrapper.sh
  • Reload profile:
  • source ~/.profile
  • mkvirtualenv cv
  • sudo apt-get install python2.7-dev
  • pip install numpy  (zzz... 45min)
  • Download and Install OpenCV:
  • wget -O opencv-2.4.10.zip http://sourceforge.net/projects/opencvlibrary/files/opencv-unix/2.4.11/opencv-2.4.11.zip/download
  • unzip opencv-2.4.11.zip
  • cd opencv-2.4.11
  • mkdir build
  • cd build
  • cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D BUILD_NEW_PYTHON_SUPPORT=ON -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON  -D BUILD_EXAMPLES=ON ..
  • ???this or the last one??? cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=ON -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON -D WITH_QT=ON -D WITH_OPENGL=ON ..
  • Compile OpenCV:
  • make  (zzz... 9h)
  • sudo make install
  • sudo ldconfig
  • Check if installtion is ok:
  • ls /usr/local/lib/python2.7/dist-packages
  • But in order to utilize OpenCV within our cv  virtual environment, we first need to sym-link:
  • cd ~/.virtualenvs/cv/lib/python2.7/dist-packages/
  • ln -s /usr/local/lib/python2.7/dist-packages/cv2.so cv2.so
  • ln -s /usr/local/lib/python2.7/dist-packages/cv.py cv.py

Links:
If you like this content, feel free to

Monday, January 18, 2016

MobileSim - Simulator for Pioneer (MobileRobots) Robots

Windows 10

Aria 2.9.0.1  (32bits)
MobileSim 0.7.2

Status:
  • 2016.01.18 - Simulator (MobileSim) is working. Python example (simple.py) is working with MobileSim.

Todo:
  • Test java example.
Instalation on Windows:
  • Download and install ARIA SDK.
  • Configure PATH system variable - C:\Development\Aria_2.9.0-1_32bit\bin;C:\Development\Aria_2.9.0-1_32bit\python;C:\Development\Aria_2.9.0-1_32bit\java
  • Configure PYTHONPATH system variable - C:\Development\Aria_2.9.0-1_32bit\python
  • Download and install MobileSim.
Testing Python Examples:
  • cd \Development\Aria_2.9.0-1_32bit\PythonExamples
  • Start MobileSim, with C:\Development\Aria_2.9.0-1_32bit\maps\triangle.map
  • python simple.py




References:
If you like this content, feel free to