Projektidee: Sauerstoffampel (de)

2018-11-02

Other projects

Current state

This is the current state. Power is supplied:

click to enlarge

What needs to be done: Measure the voltage between Aout and Ground. Keep in mind that the arduino can only handle voltages up to 5 Volt. So wee need a voltage divider to be on the safe side.

Set up the Nodemcu

Parts

Ubuntu 18.04 Bionic Beaver, Nodemcu v1... ESP8266EX 4MB, usb cable, Python (/usr/bin/python3, v3.6.6), pip, esptool.py (~/.local/bin/esptool.py),

Steps

  1. Connect usb cable

  2. Verify that device ttyUSBx is available: ls -la /dev/ttyUSB*

    ➜  ~ ls -la /dev/ttyUSB*
    crw-rw---- 1 root dialout 188, 0 Okt 31 08:02 /dev/ttyUSB0
    
  3. Verify that we have the latest version of PIP, the Python package manager:

    ➜  ~ pip --version
    pip 18.1 from ~/.local/lib/python3.6/site-packages/pip (python 3.6)
    
    ➜  ~ pip install --upgrade --user pip
    Requirement already up-to-date: pip in ./.local/lib/python3.6/site-packages (18.1)
    
  4. Verify that we have the latest version of esptool.py:

    ➜  ~ pip install --upgrade --user esptool
    
    Requirement already up-to-date: esptool in ./.local/lib/python3.6/site-packages (2.5.1)
    Requirement already satisfied, skipping upgrade: ecdsa in ./.local/lib/python3.6/site-packages (from esptool) (0.13)
    Requirement already satisfied, skipping upgrade: pyaes in ./.local/lib/python3.6/site-packages (from esptool) (1.6.1)
    Requirement already satisfied, skipping upgrade: pyserial>=3.0 in ./.local/lib/python3.6/site-packages (from esptool) (3.4)
    
  5. Erase the existing flash memory:

    ➜  ~ esptool.py --port /dev/ttyUSB0 erase_flash
    
    esptool.py v2.5.1
    Serial port /dev/ttyUSB0
    Connecting....
    Detecting chip type... ESP8266
    Chip is ESP8266EX
    Features: WiFi
    MAC: 00:00:00:xx:yy:zz
    Uploading stub...
    Running stub...
    Stub running...
    Erasing flash (this may take a while)...
    Chip erase completed successfully in 12.2s
    Hard resetting via RTS pin...
    ➜  ~
    
  6. Download the latest stable Micropython firmware for the ESP8266:

    ➜  ~ wget http://micropython.org/resources/firmware/esp8266-20180511-v1.9.4.bin \
              -O ~/Downloads/MicroPython/esp8266-20180511-v1.9.4.bin
    
  7. Upload the firmware to the ESP8266 flash memory:

    ➜  ~ esptool.py --port /dev/ttyUSB0 --baud 115200 write_flash \
                    --flash_size=detect -fm dio 0 \
                    ~/Downloads/MicroPython/esp8266-20180511-v1.9.4.bin
    
    esptool.py v2.5.1
    Serial port /dev/ttyUSB0
    Connecting....
    Detecting chip type... ESP8266
    Chip is ESP8266EX
    Features: WiFi
    MAC: 00:00:00:xx:yy:zz
    Uploading stub...
    Running stub...
    Stub running...
    Configuring flash size...
    Auto-detected Flash size: 4MB
    Flash params set to 0x0240
    Compressed 604872 bytes to 394893...
    Wrote 604872 bytes (394893 compressed) at 0x00000000 in 34.9 seconds (effective 138.6 kbit/s)...
    Hash of data verified.
    
    Leaving...
    Hard resetting via RTS pin...
    ➜  ~
    
  8. Open the firmware running on the ESP8266 in a terminal window using ‘screen’. Use ctrl+a ctrl+d to leave the screen terminal:

    screen /dev/ttyUSB0 115200  # press ctrl+a ctrl+d to return
    

    In that window press ‘enter’ and more:

    >>> 1+1
    2
    

    Ask for help:

    >>> help()
    Welcome to MicroPython!
    
    For online docs please visit http://docs.micropython.org/en/latest/esp8266/ .
    For diagnostic information to include in bug reports execute 'import port_diag'.
    
    Basic WiFi configuration:
    
    import network
    sta_if = network.WLAN(network.STA_IF); sta_if.active(True)
    sta_if.scan()                             # Scan for available access points
    sta_if.connect("<AP_name>", "<password>") # Connect to an AP
    sta_if.isconnected()                      # Check for successful connection
    # Change name/password of ESP8266's AP:
    ap_if = network.WLAN(network.AP_IF)
    ap_if.config(essid="<AP_NAME>", authmode=network.AUTH_WPA_WPA2_PSK, password="<password>")
    
    Control commands:
      CTRL-A        -- on a blank line, enter raw REPL mode
      CTRL-B        -- on a blank line, enter normal REPL mode
      CTRL-C        -- interrupt a running program
      CTRL-D        -- on a blank line, do a soft reset of the board
      CTRL-E        -- on a blank line, enter paste mode
    
    For further help on a specific object, type help(obj)
    >>>
    
    Gather diagnostics information::
    
       >>> import port_diag
       FlashROM:
       Flash ID: 1640e0 (Vendor: e0 Device: 4016)
       Flash bootloader data:
       Byte @2: 02
       Byte @3: 40 (Flash size: 4MB Flash freq: 40MHZ)
       Firmware checksum:
       size: 604856
       md5: 53b9852a38ac9f9e27694b3123fd1420
       True
    
       Networking:
       STA ifconfig: ('0.0.0.0', '0.0.0.0', '0.0.0.0', '208.67.222.222')
       AP ifconfig: ('192.168.4.1', '255.255.255.0', '192.168.4.1', '208.67.222.222')
       Free WiFi driver buffers of type:
       0: 8 (1,2 TX)
       1: 0 (4 Mngmt TX(len: 0x41-0x100))
       2: 8 (5 Mngmt TX (len: 0-0x40))
       3: 4 (7)
       4: 7 (8 RX)
       lwIP PCBs:
       Active PCB states:
       Listen PCB states:
       Local port 8266, foreign port 31308 snd_nxt 1869116537 rcv_nxt 1073725424 State: LISTEN
       TIME-WAIT PCB states:
       >>>
    

    Find out what modules are built in:

    >>> help('modules')
    __main__          http_client_ssl   sys               urandom
    _boot             http_server       time              ure
    _onewire          http_server_ssl   uasyncio/__init__ urequests
    _webrepl          inisetup          uasyncio/core     urllib/urequest
    apa102            json              ubinascii         uselect
    array             lwip              ucollections      usocket
    btree             machine           uctypes           ussl
    builtins          math              uerrno            ustruct
    dht               micropython       uhashlib          utime
    ds18x20           neopixel          uheapq            utimeq
    errno             network           uio               uzlib
    esp               ntptime           ujson             webrepl
    example_pub_button                  onewire           umqtt/robust      webrepl_setup
    example_sub_led   os                umqtt/simple      websocket
    flashbdev         port_diag         uos               websocket_helper
    framebuf          select            upip
    gc                socket            upip_utarfile
    http_client       ssd1306           upysh
    Plus any modules on the filesystem
    >>>
    

    Press ctrl+a ctrl+d to return to the host’s commandline:

    ➜  ~ screen /dev/ttyUSB0 115200
    [detached from 12762.pts-1.MachineName]
    

    Reattach to that screen:

    ➜  ~ screen -r 12762
    
    >>> 2+2
    4
    >>> # pressing ctrl+a ctrl+d to leave the ESP8266
    

    Find the instance of screen that still may be running for reconnect:

    ps aux | grep -i screen
    

    And finally kill that process:

    kill 12762
    
  9. Configure webrepl:

    import webrepl_setup
    

Configure Wifi

After flashing you can connect to a Wifi with ssid ‘MicroPython-xxyyzz’ made up by the ESP8266 at 192.168.4.1. Password is ‘micropythoN’ (note the capital N).

To be written

repl, webrepl, configure wifi

MicroPython on my Nodemcu board

Toggle built in LED

In repl: ctrl+e to enter paste mode, then ‘insert’ to paste, ctrl+d to finish pasting:

import machine
import time
pin = machine.Pin(2, machine.Pin.OUT)
pin.on()   # turn led OFF
pin.off()  # turn led ON
for i in range(10):
   time.sleep_ms(250)
   pin.on()
   time.sleep_ms(250)
   pin.off()
pin.on()

Sensor

Schlagworte

Archive

Sprachen

Neue Einträge

Diese Seite