Friday, June 28, 2019

Building Espruino for ESP8266 with storage support

Espruino ESP8266 firmware with Storage support

Note:  Storage is now included, in 2v13

Prerequesites

PC or VM with Ubuntu 18.04 LTS
ESP8266 with 4 MB flash and USB port

The following code is removed from the build: Remote access via telnet, remote firmware update, the graphics and crypto library

Basic steps

Compiling Espruino for ESP8266 on Ubuntu 18.04 LTS

Reference:
https://github.com/espruino/Espruino/blob/master/README_Building.md#for-esp8266
  • development-tools
    • sudo apt-get update && apt-get upgrade
    • sudo apt-get install make unrar-free autoconf automake libtool gcc g++ gperf flex bison texinfo gawk ncurses-dev libexpat-dev python-dev python python-serial python-pip sed git unzip bash help2man wget bzip2 libtool-bin build-essential git esptool
    • sudo pip install --upgrade pip
  • esp-open-sdk
  • Espressif NONOS SDK (version 2.2.1)
  • Espruino
    •  git clone https://github.com/espruino/Espruino.git
  • Test build
    • cd Espruino
    • export BOARD=ESP8266_4MB
    • export FLASH_4MB=1
    • export ESP8266_SDK_ROOT=/<path>/ESP8266_NONOS_SDK-2.2.1
      • Replace <path>  with the folder containing the Expressiv NONOS SDK
    • export PATH=$PATH:/<path>/xtensa-lx106-elf/bin/
      • Replace <path> with the folder containing the esp-open-sdk
    • export COMPORT=/dev/ttyUSB0
      • Replace  ttyUSB0 if you're using another port
    • make clean && make $*

Additional steps

Within the Espruino folder go to boards, make a backup copy and edit the build libraries in ESP8266_4MB.py:
'build' : {
   'libraries' : [
     'NET',
     #'TELNET',
     #'GRAPHICS',
     #'CRYPTO',
     'NEOPIXEL',
     'FILESYSTEM',
     'FLASHFS'
],
This will disable TELNET (personally I never used it), GRAPHICS (OK when building a web server) and CRYPTO (that supports SHA256 only anyway), saving storage space for the now enabled FILESYSTEM and FLASHFS.

Within the Espruino folder edit /targets/esp8266/user_main.c and comment out lines 30 (to //#include <ota.h>) and 241 (to //otaInit(88);) to remove remote firmware update from the code.

Within the Espruino folder edit Makefile and activate lines 24 (to RELEASE=1). Remove the trailing space in line 507 (to libs/network/esp8266/pktbuf.c) and remove line 508 (libs/network/esp8266/ota.c) to prevent OTA from being included in the code.


To get "make flash" to work you'll have to
  • Adjust the link to esptool.py within the Expressiv NONOS SDK subfolder (delete current link (rm /<path1>/esptool/esptool.py), add new link (ln -s /usr/share/esptool/esptool.py / <<path1>/esptool/esptool.py)

  • Grand your user access to /dev/ttyUSB0:
    • Add your user to the dialout group (sudo usermod -a -G dialout $USER)
    • Eventually stop and remove modemmanager
    • Logout and login after these changes
    • If this doesn't work do it as root
  • Flash the ESP8266
    • cd Espruino
    • export BOARD=ESP8266_4MB
    • export FLASH_4MB=1
    • export ESP8266_SDK_ROOT=/<path>/ESP8266_NONOS_SDK-2.2.1
    • export PATH=$PATH:/<path>/xtensa-lx106-elf/bin/
    • export COMPORT=/dev/ttyUSB0
    • make clean && make $*
    • make flash

If flashing doesn't produce a bootable devices then it's most likely because of the use of the option "--flash_mode qio" in the esptool.py command line, some flash modules don't support this. With "--flash_mode dio" it'll produce bootable devices. Change line 20 in the file make/family/ESP8266.make to ESP_FLASH_MODE ?= 2 # 0->QIO, 2->DIO and it'll work.)


To flash the ESP8266 using Windows copy the two binaries
espruino_esp8266_user1.bin
espruino_esp8266_user2.bin 
to Windows, and use them together with
blank.bin, boot_v1.6.bin, esp_init_data_default.bin
from  https://www.espruino.com/binaries/espruino_2v03_esp8266_4mb/ for flashing.

Testing

Connect the ESP8266 to the IDE and execute a few commands
1+1;
=2

process.env
={
  VERSION: "2v03.38",
  GIT_COMMIT: "0b21c5d4",
  BOARD: "ESP8266_4MB_FS",
  FLASH: 0, RAM: 81920,
  SERIAL: "dc4f2218-6db0",
  CONSOLE: "Serial1",
  MODULES: "Flash,Storage,hea" ... ",ESP8266,neopixel",
  EXPTR: 1073643644 }

console.log(process.env.MODULES);
Flash,Storage,heatshrink,net,dgram,http,NetworkJS,Wifi,ESP8266,neopixel
=undefined

const Storage = require("Storage");
Storage.getFree();

=196608

Storage.list();
=[  ]

//a test with the wrong length
Storage.write("name", [1,2,3], 0, 5);
=true

Storage.list();
=[
  "name"
 ]

Storage.read("name");
="\1\2\3\xFF\xFF"

Storage.getFree();
=196584

//a test with the correct length
Storage.write("name", "123", 0, 3);
=true

Storage.read("name");
="123"

// cleanup after the test
Storage.erase("name");

=undefined

Storage.list();
=[  ]

Storage.getFree();
=196608

Wednesday, June 26, 2019

ESP32-CAM with OV2640 camera

Flashing the example application


Prerequisites

ESP32-CAM with OV2640 camera (USD 5.59 + 1.98 shipping)
A serial programmer with 3.3V (e.g. CH341 or CH341A work fine)
Arduino IDE, version 1.9.8
5V (or 3.3V) power supply
Wifi Access Point
Six jumper wires
Windows-PC with an USB port

Setup Arduino IDE

File -> Preferences -> Additional Boards Manager URLs
Add these URLs (URLs are comma separated):
http://arduino.esp8266.com/stable/package_esp8266com_index.json, https://dl.espressif.com/dl/package_esp32_index.json
(Note: package_esp8266com_index is not needed for the ESP32-CAM)

Tools -> Board -> Boards Manager
Search for esp32 in the Boards Manager windows, select it and click Install to install it.

Tools -> Board -> ESP32 Wrover Module


Tools -> Partition Scheme -> Huge App (3MB No OTA)

Tools -> COM Port -> COMxyz
(Select the COM port of your programmer)

Tools -> Serial Monitor
This starts a serial monitor on the selected COM port (not active during flashing, set it to 115200 baud.

Tools -> Programmer -> AVRISP mkII
File -> Examples -> ESP32 -> Camera -> CameraWebServer
In line 14 insert // at the beginning and in line 18 remove // at the beginning defining the camera as CAMERA_MODEL_AI_THINKER.
Enter the SSID and password of your Wifi Access Point editing lines 22 and 23 of the file CameraWebServer and save it as MyCameraWebServer.





Wiring the ESP32-CAM

PINs o the ESP32-CAM board:
3.3V - IO16 - IO0 - GND - VCC - U0R - U0T - GND
IO4 - IO2 - IO14 - IO15 - IO13 - IO12 - GND - 5V 

Connections to be made:
ESP32-CAM pin GND -> GND of the 3.3V serial programmer
ESP32-CAM pin U0T -> RX of the 3.3V serial programmer
ESP32-CAM pin U0R ->  TX of the 3.3V serial programmer
ESP32-CAM pin GND -> GND of the power supply
ESP32-CAM pin 5 V    -> 5V of the power supply(*)

(*):When using a 3.3V power supply wire the pin 3.3V to the 3.3V power supply instead.

Press the RST button and you should see the log output of the pre-installed firmware in the Arduino IDE serial monitor.


Flashing ESP32-CAM 

To flash the software connect the last jumper cable:
ESP32-CAM pin GND <-> ESP32-CAM pin IO0

Then press RST to enter flashing mode.

In Arduino IDE press the upload button (Sketch -> Upload)
During flashing the output should be like this:


Global variables use 52696 bytes (16%) of dynamic memory, leaving 274984 bytes for local variables. Maximum is 327680 bytes.
esptool.py v2.6
Serial port COMxyz
Connecting........
Chip is ESP32D0WDQ5 (revision 1)
Features: WiFi, BT, Dual Core, 240MHz, VRef calibration in efuse, Coding Scheme None
MAC: 12:34:45:56:9a:bc
Uploading stub...
Running stub...
Stub running...
Configuring flash size...
Auto-detected Flash size: 4MB
Compressed 8192 bytes to 47...

Writing at 0x0000e000... (100 %)
Wrote 8192 bytes (47 compressed) at 0x0000e000 in 0.0 seconds (effective 7281.7 kbit/s)...
Hash of data verified.
Compressed 16832 bytes to 10888...

Writing at 0x00001000... (100 %)
Wrote 16832 bytes (10888 compressed) at 0x00001000 in 1.0 seconds (effective 140.3 kbit/s)...
Hash of data verified.
Compressed 2242064 bytes to 1795618...

Writing at 0x00010000... (0 %)
Writing at 0x00014000... (1 %)
[...]
Writing at 0x001c0000... (99 %)
Writing at 0x001c4000... (100 %)
Wrote 2242064 bytes (1795618 compressed) at 0x00010000 in 158.1 seconds (effective 113.4 kbit/s)...
Hash of data verified.
Compressed 3072 bytes to 134...

Writing at 0x00008000... (100 %)
Wrote 3072 bytes (134 compressed) at 0x00008000 in 0.0 seconds (effective 1536.0 kbit/s)...
Hash of data verified.

Leaving...
Hard resetting via RTS pin...
Then disconnect the last jumper wire and press the RST button. The output shall be (in the Arduino IDE serial monitor):
00:00:07.879 -> ets Jun  8 2016 00:22:57
00:00:07.879 ->
00:00:07.879 -> rst:0x1 (POWERON_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
00:00:07.913 -> configsip: 0, SPIWP:0xee
00:00:07.913 -> clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
00:00:07.913 -> mode:DIO, clock div:1
00:00:07.913 -> load:0x3fff0018,len:4
00:00:07.913 -> load:0x3fff001c,len:1100
00:00:07.913 -> load:0x40078000,len:9232
00:00:07.913 -> load:0x40080400,len:6400
00:00:07.913 -> entry 0x400806a8
00:00:09.951 ->
00:00:11.373 -> .....
00:00:13.375 -> WiFi connected
00:00:13.375 -> Starting web server on port: '80'
00:00:13.375 -> Starting stream server on port: '81'
00:00:13.375 -> Camera Ready! Use 'http://192.168.43.125' to connect
Copy the URL given in the last line, http://192.168.43.125 in this example, to your web browser.

When getting an image the output shall be:

00:01:52.537 -> JPG: 3461B 43ms
When getting an stream the output shall be:
00:02:32.723 -> MJPG: 3454B 3ms (333.3fps), AVG: 3ms (333.3fps), 0+0+0+0=0 0
00:02:32.757 -> MJPG: 3441B 30ms (33.3fps), AVG: 16ms (62.5fps), 0+0+0+0=0 0
00:02:32.791 -> MJPG: 3468B 49ms (20.4fps), AVG: 27ms (37.0fps), 0+0+0+0=0 0
When disconnecting power the output shall be (also happens when using a weak power supply):
00:03:36.486 -> 
00:03:36.486 -> Brownout detector
If you receive the following lines during startup of the module remove power from the module, reconnect power and press the RST button. Pressing the RST button while powered on will not fix this issue:
00:00:44.576 -> [E][camera.c:205] skip_frame(): Timeout waiting for VSYNC
00:00:44.576 -> [E][camera.c:1270] esp_camera_init(): Camera init failed with error 0x20003

Done

The camera stream in the browser looks like this:













TLDR

The video quality is really bad, it has a lot of noise. And for me the camera module crashes rather often, even when using a stabilized power supply. Might be a software issue, or whatever. Having two modules with the same issue I'm about to drop the camera module to the waste bin and keep the ESP32 as micro-controller with Espruino.

Wednesday, January 30, 2019

OrangePi Lite - armbian wifi/wlan setup II

Update with new version of armbian, version used: Armbian_5.69_Orangepilite_Debian_stretch_next_4.19.13

Set up wifi with the menu armbian-config
1. Network -> IP -> DHCP
2. Network -> Wifi -> Select your SSID and enter the password if necessary

Back to the shell check if you received an ip address with ipconfig -a

For WPA you still need to edit the network configuration, prepare it with wpa_passphrase using your SSID and WLAN password:

root@orangepilite:~# wpa_passphrase mySSIDhere myPasswordHere
network={
        ssid="mySSIDhere"
        #psk="myPasswordHere"
        psk=04817c03bb5fd9d1c6d561a881f192da77501b47d0ac80598eb44191c603c516
}
root@orangepilite:~#


Then write the network configuration using the psk value:

echo "# Local loopback
auto lo
iface lo inet loopback

# Wlan
auto wlan0
iface wlan0 inet dhcp
      wpa-ssid yourSSIDhere
      wpa-psk  04817c03bb5fd9d1c6d561a881f192da77501b47d0ac80598eb44191c603c516" > /etc/network/interfaces


Also write the WLAN registration domain (replace it with your region):
echo "REGDOMAIN=CH" > /etc/default/crda

Then add a DNS server (usually your router, often 192.168.0.1) to the resolv.conf:

echo "nameserver 192.168.0.1" > /etc/resolv.conf

Now you should be able to resolve addresses with nslookup, e.g:
root@orangepilite:~# nslookup 8.8.8.8
Server:         192.168.0.1
Address:        192.168.0.1#53

Non-authoritative answer:
8.8.8.8.in-addr.arpa    name = google-public-dns-a.google.com.

Authoritative answers can be found from:

root@orangepilite:~#


That's it.