2023-06-20

AWS network inbound rules: All traffic is no traffic

I found a very weird problem in AWS' network inbound rules, that although the default rule is to allow all traffic, it actually allows no traffic. At least is the case for PostgreSQL's port 5432 and SSH's port 22. 

To fix, I had to manually add rules to allow inbound traffic to certain ports, like below:

This sounds redundant but I had to do this to make it work. 

I could set the source in the first rule to 0.0.0.0/0. This was the error I got: 

If you have an explanation, please feel free to let me know. 

2021-12-27

Loading or including images or asset files in Webpack or ReactJS

In Webpack, or pretty much any frontend framework that relies on it, like ReactJS, if you want to include an image or any asset file, like a PDF, you will need a loader to handle it. You cannot simply write a line of HTML like <img src="pipeline_demo.gif"/>, which will not move the asset file to the compiled output, making asset files inaccessible (404 error).

Instead, you need to use the file-loader module and import an asset file into a variable before using it. Just follow the official instructions and you will a really good result. There are lots of good examples at the end of the official doc. Many answers on Stackoverflow are noises -- like you need to have both file-loader and url-loader.

One thing that you want to pay attention to is that:

ℹ️ By default the filename of the resulting file is the hash of the file's contents with the original extension of the required resource.

So if you do not use any option for file-loader, it will result in a pretty big HTML file with the asset files being super long hash strings, like this: < img src="data:image/gif;base64,R0lG0......" > Hence, you want to use the name option unless you really want the hash strings, e.g., when you only have storage access to HTML files.

Lastly, as someone who is new to MODERN front-end development, I feel front-end development has become so complicated. There are so much stuff to learn at once to start. So many points work together. It's very newbie-unfriendly.

2021-09-01

When Tensorflow cannot match the checkpoint file ERROR:tensorflow:Couldn't match files for checkpoint

It seems that Tensorflow hardcodes the path to a checkpoint file using an absolute path. So if you move the checkpoints from one computer to another, you may run into errors like below: 

ERROR:tensorflow:Couldn't match files for checkpoint /root/result_base/cnn_dailymail/sent_delete/model.ckpt-273863

To fix it, it's very simple. Just open the checkpoint file in the folder that stores Tensorflow result from an experiment, and replace the paths to checkpoints with the ones fit for the new computer.

For example, from

model_checkpoint_path: "/root/result_base/cnn_dailymail/sent_delete/model.ckpt-273863"
all_model_checkpoint_paths: "/root/result_base/cnn_dailymail/sent_delete/model.ckpt-273863"

to

model_checkpoint_path: "/mnt/12T/data/NLP/result_base/cnn_dailymail/sent_delete/model.ckpt-273863"
all_model_checkpoint_paths: "/mnt/12T/data/NLP/result_base/cnn_dailymail/sent_delete/model.ckpt-273863"

2020-09-29

Linux WiFi disabled after a long period of no activities: bad wifi powersave

I have Lubuntu 18.04 on my Dell Inspiron 5000 laptop. A recurring problem is that the WiFi becomes unavailable after I leave my laptop on for a long period. The problem won't appear if I always put it back to sleep before leaving it idle for a long time. Once the problem happens, I have to turn off and turn on the computer exactly twice to fix it. Rebooting (e.g., sudo reboot) won't even fix it. 

Today I was so annoyed by this problem so I googled around. I am not alone. Nearly everyone who had the issue points to WiFi power management. Some say that it just took a really long time, e.g., 30 seconds, for the WiFi adapter to wake up from sleep. Some has the problem after system wake up from suspension -- but I don't have this problem. 

 It turns out that the most effective fix is to simply disable WiFi power management to prevent the WiFi adapter from going into powersave mode. Just edit

/etc/NetworkManager/conf.d/default-wifi-powersave-on.conf
and change the line
wifi.powersave = 3
into
wifi.powersave = 2

However, this also means that you will waste energy on WiFi when the computer is idle. 

Below are some links for those who want more information. 

  • https://www.reddit.com/r/Lubuntu/comments/54endb/help_new_install_but_wifi_disconnects_randomly/ 
  • https://core.docs.ubuntu.com/en/stacks/network/network-manager/docs/reference/snap-configuration/wifi-powersave

  • https://askubuntu.com/questions/1022203/how-to-prevent-wifi-sleep-after-suspend 
  • https://askubuntu.com/questions/695867/disable-wifi-power-management/961460
  • https://askubuntu.com/questions/1030653/wifi-randomly-disconnected-on-ubuntu-18-04-lts

2020-05-17

Installing CircuitPython on Arduino MKR WIFI

Part I: Installing CircuitPython

In this part, we will be flashing two bootloaders sequentially onto MKR.

1. Download and install the latest Arduino IDE https://www.arduino.cc/en/Main/Software
2. Open the IDE. Go to Tools -> Boards -> Boards Manager. In Boards Manager, search "Arduino SAMD Boards (32-bits AMR Cortex-M0+)" and then click Install. After installation, select Tools -> Boards -> MKR WIFI 1010
3. Plug-in MKR WIFI into the computer via a USB cable, and select Tools -> Port -> MKR WIFI or the only USB device when MKR is plugged in.
4. Download latest INO for MKR Zero's bootloader from https://github.com/adafruit/uf2-samdx1/releases to your computer, e.g., https://github.com/adafruit/uf2-samdx1/releases/download/v3.9.0/update-bootloader-mkrzero-v3.9.0.ino
5. Open the INO file just downloaded and upload it to MKR. You should see something like this on your Arduino IDE:

Sketch uses 21112 bytes (8%) of program storage space. Maximum is 262144 bytes.
Global variables use 3464 bytes of dynamic memory.
Atmel SMART device 0x10010005 found
Device : ATSAMD21G18A
Chip ID : 10010005
Version : v2.0 [Arduino:XYZ] Mar 19 2018 09:45:14
Address : 8192
Pages : 3968
Page Size : 64 bytes
Total Size : 248KB
Planes : 1
Lock Regions : 16
Locked : none
Security : false
Boot Flash : true
BOD : true
BOR : true
Arduino : FAST_CHIP_ERASE
Arduino : FAST_MULTI_PAGE_WRITE
Arduino : CAN_CHECKSUM_MEMORY_BUFFER
Erase flash
done in 0.823 seconds

Write 21112 bytes to flash (330 pages)

[===== ] 19% (64/330 pages)
[=========== ] 38% (128/330 pages)
[================= ] 58% (192/330 pages)
[======================= ] 77% (256/330 pages)
[============================= ] 96% (320/330 pages)
[==============================] 100% (330/330 pages)
done in 0.128 seconds

Verify 21112 bytes of flash with checksum.
Verify successful
done in 0.018 seconds
CPU reset.


6. MKR will restart. If not, press the reset button. A USB storage device called MKRZEROBOOT shall pop-up on your computer now. If a file called
CURRENT.UF2
is there, then you are good.

7. Download the CircuitPython image for MKR from https://circuitpython.org/board/arduino_mkrzero/, the UF2 file. Then copy it to the MKRZEROBOOT device. Then the MKR will restart and the new USB storage device's name will become CIRCUITPY. If it doesn't restart automatically, press the reset button.


Part II: Play around with CircuitPython

1. Download and install Mu editor. For Windows and Mac, go to https://codewith.mu/en/download . For Linux, or Python on other platforms, you can simply use pip: pip3 install mu-editor --user Then start the Mu editor.

2. Upon initial start, select "Adafruit CircuitPython". Then you see an blank editor. Now, click the "Serial" button at the top of the window. And you shall see the "Adafruit CircuitPython REPL" box. Click in it, and press any key to activate the Python console. Then, you can enjoy the Python shell.

3. To run a script, in the Mu editor, paste the code below and save it as a file on your CIRCUITPY storage device and make sure it is the only file there. After saving, the MKR will restart on its own -- if not, press the reset button. And you shall see the orange LED blinking.

import board
import digitalio
import time

led = digitalio.DigitalInOut(board.D6)
led.direction = digitalio.Direction.OUTPUT

while True:
print("Hello, CircuitPython!")
led.value = True
time.sleep(1)
led.value = False
time.sleep(1)


Note that the interactive mode Python shell and the scripting mode (i.e., running code from a script file) cannot coexist.