From 0cc7c910c4d3d432f50b75865d5d5f1d293a9207 Mon Sep 17 00:00:00 2001 From: Mitja Felicijan Date: Mon, 7 Sep 2020 00:35:50 +0200 Subject: New blog post --- content/2020-08-15-systemd-disable-wake-onmouse.md | 47 +++++ content/2020-08-15-systemd.md | 47 ----- content/2020-09-06-esp-and-micropython.md | 203 +++++++++++++++++++++ 3 files changed, 250 insertions(+), 47 deletions(-) create mode 100644 content/2020-08-15-systemd-disable-wake-onmouse.md delete mode 100644 content/2020-08-15-systemd.md create mode 100644 content/2020-09-06-esp-and-micropython.md (limited to 'content') diff --git a/content/2020-08-15-systemd-disable-wake-onmouse.md b/content/2020-08-15-systemd-disable-wake-onmouse.md new file mode 100644 index 0000000..dc22220 --- /dev/null +++ b/content/2020-08-15-systemd-disable-wake-onmouse.md @@ -0,0 +1,47 @@ +~ title: Disable mouse wake from suspend with systemd service +~ description: Disable mouse wake from suspend with systemd service +~ slug: /disable-mouse-wake-from-suspend-with-systemd-service.html +~ date: 2020-08-15 +~ template: post +~ hide: false + +I recently bought [ThinkPad X220](https://www.laptopmag.com/reviews/laptops/lenovo-thinkpad-x220) just as a joke on eBay to test Linux distributions and play around with things and not destroy my main machine. Little to my knowledge I felt in love with it. Man, they really made awesome machines back then. + +After changing disk that came with it to SSD and installing Ubuntu to test if everything works I noticed that even after a single touch of my external mouse the system would wake up from sleep even though the lid was shut down. + +I wouldn't even noticed it if laptop didn't have [LED sleep indicator](https://support.lenovo.com/lk/en/solutions/~/media/Images/ContentImages/p/pd025386_x1_status_03.ashx?w=426&h=262). I already had a bad experience with Linux and it's power management. I had a [Dell Inspiron 7537](https://www.pcmag.com/reviews/dell-inspiron-15-7537) laptop with a touchscreen and while traveling it decided to wake up and started cooking in my backpack to the point that the digitizer responsible for touch actually glue off and the whole screen got wrecked. So, I am a bit touchy about this. + +I went on solution hunting and to my surprise there is no easy way to disable specific devices to perform wake up. Why is this not under the power management tab in setting is really strange. + +After googling for a solution I found [this nice article describing the solution](https://codetrips.com/2020/03/18/ubuntu-disable-mouse-wake-from-suspend/) that worked for me. The only problem with this solution was that he added his solution to `.bashrc` and this triggers `sudo` that asks for a password each time new terminal is opened, which get annoying quickly since I open a lot of terminals all the time. + +I followed his instructions and got to solution `sudo sh -c "echo 'disabled' > /sys/bus/usb/devices/2-1.1/power/wakeup"`. + +I created a system service file `sudo nano /etc/systemd/system/disable-mouse-wakeup.service` and removed `sudo` and replaced `sh` with `/usr/bin/sh` and pasted all that in `ExecStart`. + +```ini +[Unit] +Description=Disables wakeup on mouse event +After=network.target +StartLimitIntervalSec=0 + +[Service] +Type=simple +Restart=always +RestartSec=1 +User=root +ExecStart=/usr/bin/sh -c "echo 'disabled' > /sys/bus/usb/devices/2-1.1/power/wakeup" + +[Install] +WantedBy=multi-user.target +``` + +After that I enabled, started and checked status of service. + +```sh +sudo systemctl enable disable-mouse-wakeup.service +sudo systemctl start disable-mouse-wakeup.service +sudo systemctl status disable-mouse-wakeup.service +``` + +This will permanently disable that device from wakeing up you computer on boot. If you have many devices you would like to surpress from waking up your machine I would create a shell script and call that instead of direclty doing it in service file. diff --git a/content/2020-08-15-systemd.md b/content/2020-08-15-systemd.md deleted file mode 100644 index dc22220..0000000 --- a/content/2020-08-15-systemd.md +++ /dev/null @@ -1,47 +0,0 @@ -~ title: Disable mouse wake from suspend with systemd service -~ description: Disable mouse wake from suspend with systemd service -~ slug: /disable-mouse-wake-from-suspend-with-systemd-service.html -~ date: 2020-08-15 -~ template: post -~ hide: false - -I recently bought [ThinkPad X220](https://www.laptopmag.com/reviews/laptops/lenovo-thinkpad-x220) just as a joke on eBay to test Linux distributions and play around with things and not destroy my main machine. Little to my knowledge I felt in love with it. Man, they really made awesome machines back then. - -After changing disk that came with it to SSD and installing Ubuntu to test if everything works I noticed that even after a single touch of my external mouse the system would wake up from sleep even though the lid was shut down. - -I wouldn't even noticed it if laptop didn't have [LED sleep indicator](https://support.lenovo.com/lk/en/solutions/~/media/Images/ContentImages/p/pd025386_x1_status_03.ashx?w=426&h=262). I already had a bad experience with Linux and it's power management. I had a [Dell Inspiron 7537](https://www.pcmag.com/reviews/dell-inspiron-15-7537) laptop with a touchscreen and while traveling it decided to wake up and started cooking in my backpack to the point that the digitizer responsible for touch actually glue off and the whole screen got wrecked. So, I am a bit touchy about this. - -I went on solution hunting and to my surprise there is no easy way to disable specific devices to perform wake up. Why is this not under the power management tab in setting is really strange. - -After googling for a solution I found [this nice article describing the solution](https://codetrips.com/2020/03/18/ubuntu-disable-mouse-wake-from-suspend/) that worked for me. The only problem with this solution was that he added his solution to `.bashrc` and this triggers `sudo` that asks for a password each time new terminal is opened, which get annoying quickly since I open a lot of terminals all the time. - -I followed his instructions and got to solution `sudo sh -c "echo 'disabled' > /sys/bus/usb/devices/2-1.1/power/wakeup"`. - -I created a system service file `sudo nano /etc/systemd/system/disable-mouse-wakeup.service` and removed `sudo` and replaced `sh` with `/usr/bin/sh` and pasted all that in `ExecStart`. - -```ini -[Unit] -Description=Disables wakeup on mouse event -After=network.target -StartLimitIntervalSec=0 - -[Service] -Type=simple -Restart=always -RestartSec=1 -User=root -ExecStart=/usr/bin/sh -c "echo 'disabled' > /sys/bus/usb/devices/2-1.1/power/wakeup" - -[Install] -WantedBy=multi-user.target -``` - -After that I enabled, started and checked status of service. - -```sh -sudo systemctl enable disable-mouse-wakeup.service -sudo systemctl start disable-mouse-wakeup.service -sudo systemctl status disable-mouse-wakeup.service -``` - -This will permanently disable that device from wakeing up you computer on boot. If you have many devices you would like to surpress from waking up your machine I would create a shell script and call that instead of direclty doing it in service file. diff --git a/content/2020-09-06-esp-and-micropython.md b/content/2020-09-06-esp-and-micropython.md new file mode 100644 index 0000000..b5002ce --- /dev/null +++ b/content/2020-09-06-esp-and-micropython.md @@ -0,0 +1,203 @@ +~ title: Getting started with MicroPython and ESP8266 +~ description: Getting started with MicroPython and ESP8266 +~ slug: /esp8266-and-micropython-guide.html +~ date: 2020-09-06 +~ template: post +~ hide: false + +**Table of contents** + +1. [Introduction](#introduction) +2. [Flashing the SOC](#flashing-the-soc) +3. [Install better tooling](#install-better-tooling) +4. [Additional resources](#additional-resources) + + +## Introduction + +A while ago I bought some [ESP8266](https://www.espressif.com/en/products/socs/esp8266) and [ESP32](https://www.espressif.com/en/products/socs/esp32) dev boards to play around with and I finally found a project to try it out. + +For my project, I used [ESP32](https://www.espressif.com/en/products/socs/esp32) but I could easily choose [ESP8266](https://www.espressif.com/en/products/socs/esp8266). This guide contains which tools I use and how I prepared my workspace to code for [ESP8266](https://www.espressif.com/en/products/socs/esp8266). + +![ESP8266 and ESP32 boards](/assets/esp8366-micropython/boards.jpg) + +This guide covers: +- flashing SOC +- install proper tooling +- deploying a simple script + +> Make sure that you are using **a good USB cable**. I had some problems with mine and once I replaced it everything started to work. + +## Flashing the SOC + +Plug your ESP8266 to USB port and check if the device was recognized with executing `dmesg | grep ch341-uart`. + +Then check if the device is available under `/dev/` by running `ls /dev/ttyUSB*`. + +> **Linux users**: if a device is not available be sure you are in `dialout` group. You can check this by executing `groups $USER`. You can add a user to `dialout` group with `sudo adduser $USER dialout`. + +After these conditions are meet go to the navigate to [https://micropython.org/download/esp8266/](https://micropython.org/download/esp8266/) and download `esp8266-20200902-v1.13.bin`. + +```sh +mkdir esp8266-test +cd esp8266-test + +wget https://micropython.org/resources/firmware/esp8266-20200902-v1.13.bin +``` + +After obtaining firmware we will need some tooling to flash the firmware to the board. + +```sh +sudo pip3 install esptool +``` + +You can read more about `esptool` at [https://github.com/espressif/esptool/](https://github.com/espressif/esptool/). + +Before flashing the firmware we need to erase the flash on device. Substitute `USB0` with the device listed in output of `ls /dev/ttyUSB*`. + +```sh +esptool.py --port /dev/ttyUSB0 erase_flash +``` + +If flash was successfully erased it is now time to flash the new firmware to it. + +```sh +esptool.py --port /dev/ttyUSB0 --baud 460800 write_flash --flash_size=detect 0 esp8266-20200902-v1.13.bin +``` + +If everything went ok you can try accessing MicroPython REPL with `screen /dev/ttyUSB0 115200` or `picocom /dev/ttyUSB0 -b115200`. + +> Sometimes you will need to press `ENTER` in `screen` or `picocom` to access REPL. + +When you are in REPL you can test if all is working properly following steps. + +```py +> import machine +> machine.freq() +``` + +This should output a number representing a frequency of the CPU (mine was `80000000`). + +When you are in `screen` or `picocom` these can help you a bit. + +| Key | Command | +| -------- | -------------------- | +| CTRL+d | preforms soft reboot | +| CTRL+a x | exits picocom | +| CTRL+a \ | exits screen | + + +## Install better tooling + +Now, to make our lives a little bit easier there are couple of additional tools that will make this whole experience a little more bearable. + +There are twq cool ways of uploading local files to SOC flash. + +- ampy → [https://github.com/scientifichackers/ampy](https://github.com/scientifichackers/ampy) +- rshell → [https://github.com/dhylands/rshell](https://github.com/dhylands/rshell) + +### ampy + +```bash +# installing ampy +sudo pip3 install adafruit-ampy +``` + +Listed below are some common commands I used. + +```bash + +# uploads file to flash +ampy --delay 2 --port /dev/ttyUSB0 put boot.py + +# lists file on flash +ampy --delay 2 --port /dev/ttyUSB0 ls + +# outputs contents of file on flash +ampy --delay 2 --port /dev/ttyUSB0 cat boot.py +``` + +> I added `delay` of 2 seconds because I had problems with executing commands. + +### rshell + +Even though `ampy` is a cool tool I opted with `rshell` in the end since it's much more polished and feature rich. + +```bash +# installing ampy +sudo pip3 install rshell +``` + +Now that `rshell` is installed we can connect to the board. + +```bash +rshell --buffer-size=30 -p /dev/ttyUSB0 -a +``` + +This will open a shell inside bash and from here you can execute multiple commands. You can check what is supported with `help` once you are inside of a shell. + +```bash +m@turing ~/Junk/esp8266-test +$ rshell --buffer-size=30 -p /dev/ttyUSB0 -a + +Using buffer-size of 30 +Connecting to /dev/ttyUSB0 (buffer-size 30)... +Trying to connect to REPL connected +Testing if ubinascii.unhexlify exists ... Y +Retrieving root directories ... /boot.py/ +Setting time ... Sep 06, 2020 23:54:28 +Evaluating board_name ... pyboard +Retrieving time epoch ... Jan 01, 2000 +Welcome to rshell. Use Control-D (or the exit command) to exit rshell. +/home/m/Junk/esp8266-test> help + +Documented commands (type help ): +======================================== +args cat connect date edit filesize help mkdir rm shell +boards cd cp echo exit filetype ls repl rsync + +Use Control-D (or the exit command) to exit rshell. +``` + +> Inside a shell `ls` will display list of files on your machine. To get list of files on flash folder `/pyboard` is remapped inside the shell. To list files on flash you must perform `ls /pyboard`. + +#### Moving files to flash + +To avoid copying files all the time I used `rsync` function from the inside of `rshell`. + +```bash +rsync . /pyboard +``` + +#### Executing scripts + +It is a pain to continuously reboot the device to trigger `/pyboard/boot.py` and there is a better way of testing local scripts on remote device. + +Lets assume we have `src/freq.py` file that displays CPU frequency of a remote device. + +```py +# src/freq.py + +import machine +print(machine.freq()) +``` + +Now lets upload this and execute it. + +```bash +# syncs files to remove device +rsync ./src /pyboard + +# goes into REPL +repl + +# we import file by importing it without .py extension and this will run the script +> import freq + +# CTRL+x will exit REPL +``` + +## Additional resources + +- [https://randomnerdtutorials.com/getting-started-micropython-esp32-esp8266/](https://randomnerdtutorials.com/getting-started-micropython-esp32-esp8266/) +- [http://docs.micropython.org/en/latest/esp8266/quickref.html](http://docs.micropython.org/en/latest/esp8266/quickref.html) -- cgit v1.2.3