Programming ESP32 with Rust: OTA firmware update

We at AgriConnect have been using Rust to develop firmware for ESP32 (and even part of our IoT platform backend). From today we start a series to help fellows on the same journey. "Over-the-air firmware update" is the first sharing because it was the topic that one of the authors of esp-idf-hal suggested me to write.

For OTA firmware update, esp-idf-svc already provides API. What it lacks is a documentation how to use the API, how to prepare partitions to use with that API. This post will complement that.

(Bản tiếng Việt ở đây.)

Prepare partition

...

Configure proxy for APT to download faster

As an Ubuntu user, I often retrieve softwares from PPA in addition to official Ubuntu repositories. My own QR scanner software, CoBang, is also hosted there. But one issue with PPAs is that, it is slow to download for people from developing countries like Viet Nam. We can overcome that by using a domestic proxy server.

The idea of using proxy is that, we use the Internet line at datacenter (where proxy server stays) to download files from PPA, which is fast for international connection, then the files are delivered to our home via domestic line, which is fast for domestic connection. So the requirement is that, the proxy server must be in the same country, or in near neighbor country.

If we have a VPS, we can install Dante to have a proxy server. I have another post about how to install and setup Dante. This guide applies for SOCKv5 server, like Dante. If you use another type of proxy, I cannot help.

proxy

...


Làm việc từ xa bằng máy tính ở nhà

Mình là người hay làm việc bên ngoài văn phòng, cụ thể là quán cafe hay trên đường đi du lịch. Thế nhưng laptop thì không mạnh bằng máy bàn nên mình vẫn thực sự dùng máy tính bàn ở nhà cho công việc, dù đang dùng màn hình và bàn phím của laptop. Vậy mình làm thế nào?

Dù làm về lập trình và có thâm niên nhưng mình vẫn chỉ sử dụng máy móc bình dân. Cụ thể laptop của mình chỉ là Dell Vostro 5568 với CPU Core i5 (2 nhân 4 luồng).

Laptop

Trong thời gian bị phong tỏa vì CoVid, mình cứ băn khoăn lỡ laptop này bị hư thì làm sao đi sửa để tiếp tục làm việc được, nên sau khi được mở cửa lại, mình cũng mua luôn một máy bàn để dự phòng, lỡ bị phong tỏa nữa. Máy bàn mà mình mua, mặc dù rẻ hơn laptop nhưng mạnh hơn nhiều, là con ASRock DeskMini X300, với CPU Ryzen 5 5600G (6 nhân 12 luồng). Máy chạy nhanh, mát, ít tốn điện nên mình cho nó bật 24/24 và làm công cụ kiếm cơm chính luôn. Dù đi đâu thì mình vẫn truy cập từ xa vào nó. Đặc biệt, công việc của mình cũng thường phải chạy biên dịch code Rust, build ảnh Docker, là những tác vụ ngốn CPU, nên việc tận dụng sức mạnh của máy bàn sẽ giúp tiết kiệm thời gian hơn, làm việc năng suất hơn.

...

PlatformIO hangs when installing packages

These days, I am back with embedded programming. At AgriConnect, we use PlatformIO to setup and build embedded projects. This time, I want to evaluate a new chip and new software framework, so I tell PlatformIO to install new packages with the command pio pkg install, but it hangs.

Turn out that it is because of network problem between Vietnamese ISP and the CDN services that PlatformIO Labs use to mirror its package repositories.

The solution is to quickly setup a temporary SOCKS proxy, and let PlatformIO tool reach those CDN through the proxy. The requirement is that you have a server that is outside Viet Nam, and you can access it via SSH. The steps is simple:

  • First, make a SSH connection to our server, activating its SOCKS proxy feature. Assume that we use port 1337 for proxy.
...

Setup EdgeDB for single-server deployment

Today, many people talk about microservice. The new-born EdgeDB also doesn't stay outside that trend. But not every website is applicable for microservice. Due to low traffic, low budget, some websites still prefer single-server deployment, meaning that everything, application and databases, are hosted in the same server. But EdgeDB lacks a traightforward documentation for this setup. This post is to guide how to do.

Install EdgeDB package

Follow this guide to install EdgeDB package on your server, but hold on before "Enable a systemd unit​" section.

Setup simple authentication for EdgeDB

...

"Oxy hóa" nền tảng IoT nông nghiệp bằng Rust

Trong giới lập trình, "oxy hóa" là một cách nói vui ám chỉ việc viết lại (một phần hoặc toàn bộ) một phần mềm bằng ngôn ngữ Rust, đây là một lối chơi chữ, vì "Rust" còn có nghĩa là "rỉ sét", một hiện tượng do sự oxy hóa gây nên. Gần đây mình cũng mạnh dạn oxy hóa một phần nền tảng IoT nông nghiệp của AgriConnect.

Động lực khiến mình viết lại nền tảng IoT của AgriConnect bằng Rust là để giảm tải hệ thống, tăng cường khả năng chịu áp lực trong tương lai. Phần mềm mình đang nói đến ở đây có tên mã là "Hạt Thóc". Nghe tên khiêm tốn, nhỏ bé thôi nhưng nó vận hành theo kiểu SaaS (Software as a Service), tức một phần mềm sẽ vận hành cùng lúc nhiều trang trại khách hàng. Mỗi khách hàng sẽ có một không gian riêng khi thao tác, quản lý trang trại của mình, thậm chí có tên miền riêng, nhưng thực ra tất cả đều đang được phục vụ bởi một chương trình trên server. Phần mềm này vốn được viết bằng ngôn ngữ Python, framework Django, được chia ra nhiều thành phần, mỗi thành phần chạy dưới dạng một process, một service riêng. Trong hoàn cảnh đặc thù của "Hạt Thóc" thì thì mình không "oxy hoá" theo kiểu, viết lại một vài hàm nào đó bằng Rust, biên dịch dưới dạng thư viện, rồi dùng Python import thư viện đó, mà viết lại toàn bộ thành phần con luôn. "Hạt Thóc" có ba thành phần chính:

  • Collector: Giao tiếp để thu thập dữ liệu cảm biến, trạng thái bật tắt của các tải, và lưu vào database.
  • ControlView: Cung cấp giao diện web để người dùng vào xem dữ liệu, cấu hình trang trại, đặt lịch, hay bật tắt tải bằng tay.
  • ControlCenter: Chạy ngầm để phân tích lịch, dữ liệu cảm biến để ra lệnh bật, tắt tải, kiểm tra tình trạng bất thường và phát đi cảnh báo.
...

Filter out bot visits from Gunicorn log

Our web is often written in Python, and to run the web app on production, we often use Gunicorn. Its log is also a resource for incident investigation. But the log of bot visits is so noisy. How to exclude them?

When running Gunicorn, we often have a config file for Gunicorn. We often name it gunicorn_conf.py, with content like this:

proc_name = 'awesome-web'
workers = 6
...

How to find the largest files

Recently, my company website wants to host media files (user uploaded files) to some cloud storage service. But in preparation, we realized that our media folder is too big, about 48GiB. We need to delete unnecessary files to save the storage cost. Part of it, we need to hunt down big files.

I looked around to find a command to list largest files in Linux. Most of the tutorials on Internet tell you to use a combination of commands, which is often du | sort | head, or find | sort | head. None of them satisfies me, because:

  • Combination with du only gives result of directories, not files.

...

Theo dõi bài mới qua feeds

Vừa rồi có bạn đề nghị mình thêm tính năng RSS Feeds vào website để bạn ấy dễ theo dõi khi nào có bài mới, nên mình tranh thủ làm trong dịp nghỉ lễ. Bây giờ thì tính năng đã sẵn sàng.

Tuy gọi là RSS Feeds nhưng mình cung cấp feeds ở định dạng Atom và JSONFeed, thay vì RSS.

Trên đầu trang bạn sẽ thấy icon của feeds, để lấy URL

...