A weather clock in a handwritten font
=====================================

![A weather clock in a handwritten font](./clock.jpg "A weather clock in a handwritten font")

[Русская версия](./README-rus.md)

A clock on an ESP32 with a 320x240 screen. The time, the temperature and the
name of the city are written in a handwritten font made of Bézier curves: the
digits of the time flow one into another, the temperature and the city are
written stroke by stroke. The weather symbol is made of dots and flows smoothly
from one into another.

[![Video: the clock starts](./video-start.jpg)](https://youtu.be/5kWO57Guf_s)

This is the second version of a clock from 2021. The first one is described in
an [article on Habr](https://habr.com/ru/articles/818873/) (in Russian) and
stays under the tag `2021`. The idea and the layout of the screen come from it;
the code is written anew. This version, with the font and the library it is
made of, is described in [another article](https://habr.com/ru/articles/1093548/)
(in Russian), and in English in the project
[Bringing text to life on an ESP32](https://hackaday.io/project/206968-bringing-text-to-life-on-an-esp32)
on Hackaday.io.

# Documentation

  1. [What is on the screen](docs/en/screen.md): the time, the weather, the
     name of the place, the signs of the start, day and night, the forecast;
  2. [Setting it up](docs/en/setup.md): the network from a phone, the page of
     settings, what the clock keeps;
  3. [Boards](docs/en/hardware.md): the three boards, the touch panel of the
     10.1-inch one, the radio beside the ESP32-P4, another board;
  4. [Building](docs/en/building.md): ESP-IDF, a preset for each board, the
     options, what the build makes;
  5. [The place, the time and the weather](docs/en/place-time-weather.md):
     what the clock asks of the Internet, and how it finds the time zone;
  6. [How the code works](docs/en/how-it-works.md): the tasks, the one that
     owns the display, the page cut up at build time, NVS, memory.

# What is on the screen

The time is at the top, the weather under it: a symbol and the temperature, and
the name of the city at the bottom.

  1. The digits of the time do not switch, they flow one into another;
  2. The temperature and the name of the city are written by a pen, letter by
     letter, joined up. The font has the Latin alphabet, the German "ä", "ö",
     "ü", "ß" and the Russian alphabet;
  3. The weather symbol is made of dots. When the weather changes, the dots of
     one symbol flow into the other;
  4. The time is silver by day and copper by night. The change starts half an
     hour before sunrise or sunset and ends half an hour after it;
  5. The color of the temperature and of the weather symbol depends on the
     temperature: 23 degrees Celsius are taken as the "comfortable" level,
     below it the color goes to blue, above it to red;
  6. After the start three signs in the top line show its stages: the WiFi
     sign for the connection to the network, "Z" for the time from the
     network, "W" for the weather. When its stage is passed, a sign leaves;
  7. On a board with a touch panel and PSRAM a swipe on the weather symbol
     leafs through the forecast for a day, in steps of 3 hours; the time of
     each moment stands beside the symbol.

The weather symbols flowing one into another, close up:

[![Video: weather symbols made of dots flow one into another](./video-dots.jpg)](https://youtu.be/bh8ax5zkCpk)

# Hardware

The board is a CYD (ESP32-2432S028), also known as the Cheap Yellow Display:
an ESP32 and a 2.8" 320x240 screen on the ILI9341 controller. Everything is
connected on it already:

| Display | Pin |
|---------|-----|
| MOSI    | 13  |
| MISO    | 12  |
| CLK     | 14  |
| CS      | 15  |
| DC      | 2   |
| BLK     | 21  |

The button is GPIO 0, the BOOT button of a CYD.

The second board is a Guition JC1060P470: an ESP32-P4, a 7" 1024x600 display
on a JD9165 controller over MIPI-DSI, WiFi and BLE in an ESP32-C6 beside it,
the button GPIO 35. Tested on one board with a chip of revision 1.3.

The third board is a Guition JC8012P4A1C: an ESP32-P4, a 10.1" 800x1280
display on a JD9365 controller over MIPI-DSI (the panel is portrait, the
clock turns it to 1280x800 as the 7-inch one lies), WiFi and BLE in an
ESP32-C6 beside it, the button again GPIO 35. Its touch panel, a GSL3680,
keeps no firmware of its own: the one who builds makes
`main/jc8012p4a1c_touch.fw` from the board's package (the header
`esp_lcd_touch_gsl3680.h` of `JC8012P4A1C_I_W_Y.zip`) by DGX-DI's
`tools/gsl_firmware.py --range 1664x896` — the vendor's array carries no
licence to give away, so it is not in this repository:

    python managed_components/jef-sure__dgx-di/tools/gsl_firmware.py esp_lcd_touch_gsl3680.h main/jc8012p4a1c_touch.fw --range 1664x896

Without the file the clock still builds and runs; only the forecast cannot be
leafed through on this board.

None of these pins is in the code of the clock. Each board is described once,
as data, by [jef-sure/boards](https://github.com/jef-sure/esp32-component-boards):
its display, its touch panel and its button, their pins and buses, the way the
display lies and the calibration of the touch measured on such a board. The
clock asks for a display, a touch panel and a button and gets those of the
board it is built for. Another board is one of the catalog of the component,
or a board of the program described the same way in `components/local-boards/`
beside the clock, see
[the docs of the component](https://github.com/jef-sure/esp32-component-boards/blob/main/docs/en/modules.md).

# Build

ESP-IDF 5.5 or newer is needed, the version jef-sure/boards takes. I built
under 6.1. The components,
[dgx](https://github.com/jef-sure/esp32-component-dgx), the latest version, and
[boards](https://github.com/jef-sure/esp32-component-boards) included, are
downloaded by the component manager, and a build takes only what its board
needs: the driver of the touch panel of the 7-inch board comes with that board
alone. The weather icons are made from their font
during the build by the `font2c` utility of dgx, which is built on the spot:
the computer needs a C compiler, `pkg-config` and the FreeType development
files (`libfreetype-dev` on Debian and Ubuntu).

A board is built by its preset of `CMakePresets.json`, in a directory of its
own, `build/<board>`, with its file of settings, `sdkconfig.defaults.<board>`,
read before the clock's own: no `set-target`, no `menuconfig`, and nothing
rebuilds when you go from one board to another. The names are those of the
catalog of jef-sure/boards: `cyd_s028r` for the CYD, `jc1060p470c` for the
7-inch, `jc8012p4a1c` for the 10.1-inch. ESP-IDF 6 reads the presets:
`idf.py build` builds the current board, the 10.1-inch, and `--preset` names
another:

    idf.py build
    idf.py --preset cyd_s028r build
    idf.py --preset cyd_s028r -p /dev/ttyUSB0 flash monitor

ESP-IDF 5 does not read the presets; there the board is named on the command
line:

    idf.py -B build/cyd_s028r -D SDKCONFIG=sdkconfig.cyd_s028r -D BOARD=cyd_s028r build

The files of the boards and the presets are made by the component from its
data, and made again the same way after it is updated:

    python3 managed_components/jef-sure__boards/tools/board_select.py cyd_s028r jc1060p470c jc8012p4a1c .
    python3 managed_components/jef-sure__boards/tools/board_select.py --again .

`board_select.py --current <board> .` makes another board the current one.
The file of the 7-inch board is for a chip of revision 1.3, the one of the
10.1-inch for revision 3.2, and a build for a chip below revision 3 does not
start on one of revision 3 or later, nor the other way round. A board with a
chip of another revision is named with `--revision`, for example
`board_select.py jc8012p4a1c --revision 1.3 .`. The firmware of the ESP32-C6
of the board is brought to the esp_hosted of the build when the clock is
flashed: with the factory 2.3.2 the setup of the network over BLE did not
start.

The list of time zones is made during the build from the tz database of the
machine that builds: the script `misc/generate_tzones.py` takes it from
`/usr/share/zoneinfo` or, if there is no such directory, from the Python
package `tzdata`.

# Setup

The firmware holds neither the network nor the keys of the services: the clock
keeps them in NVS, and the settings survive reflashing. No computer is needed
for the setup.

## Network

A clock that knows no network shows a QR code. It is meant for the ESP BLE
Provisioning app by Espressif, available on Google Play and in the App Store:
the app reads the code from the screen and sends the name of the WiFi network
and the password to the clock over BLE. Having got the network, the clock
restarts and works without BLE from then on.

## Settings page

The BOOT button restarts the clock into the settings mode; on a board with a
touch panel (the ESP32-P4) a swipe down from the top edge of the screen does
the same — the button hides behind the back cover. How long the
button is pressed does not matter. The clock connects to the network and
shows the address of the page as text and as a QR code:

![The settings screen: the address of the page as a QR code and as text](./settings-screen.jpg "The settings screen: the address of the page as a QR code and as text")

The page waits for 5 minutes, then the clock goes back to work. Pressing the
button again closes it too, and so does a swipe up from the bottom edge of
the screen on a board with a touch panel. Entering the settings on video:

[![Video: entering the settings](./video-settings.jpg)](https://youtu.be/48G7s-aJNIw)

The page itself:

<img src="./settings-page.png" alt="The settings page" width="320">

| Field | Meaning |
|-------|---------|
| Openweathermap.org API key | The key for the weather. It is free and is given out at [openweathermap.org](https://openweathermap.org/). Without it the clock shows the time only |
| Guess location from IP Info | Find the place by the IP address through [ipinfo.io](https://ipinfo.io). Off until it is turned on: an error of tens of kilometers is usual for this way |
| IP Info API key | The key of ipinfo.io, needed for this way only |
| Manual location | The place: coordinates ("52.52,13.40"), a city and a country ("Berlin,DE") or a postal code and a country ("10115,DE") |
| Name on the screen | A name of your own for the place on the screen. Empty: the name from the service |
| Manual timezone | The time zone. If none is chosen, the clock finds it by the place |
| Host name | The name in the network; under it the clock also answers as `<name>.local`. Before the setup it is `weather-clock-` and the last three bytes of the MAC address |
| Clear WiFi data | Forget the WiFi network: the clock shows the QR code for the setup from a phone again |
| Clear all settings | Forget everything: the network, the keys of the services, the place and its name. For giving the clock to somebody else |

Saved settings take effect with a restart.

The time comes from pool.ntp.org, the weather from OpenWeatherMap every 5
minutes.

# How it is made

Everything noticeable on the screen is done by
[dgx](https://github.com/jef-sure/esp32-component-dgx), my graphics library for
displays on microcontrollers: the handwritten font, writing at the pace of a
pen, morphing of the digits and of the dot symbols, 8-bit virtual screens with
a table of colors. Its repository has
[examples](https://github.com/jef-sure/esp32-component-dgx/tree/main/examples),
almost all of them built for the same CYD board.

The font is drawn in the [hw-fonts](https://github.com/jef-sure/hw-fonts)
editor: it keeps a letter as the path of a pen, not as an outline.

The settings page is an ordinary HTML file, `misc/index.html`. During the build
the script `misc/split_to_template.py` cuts it into pieces at places like
`%HOST_NAME%`, and the clock sends the page piece by piece straight from flash.

# Trade-offs

  1. The forecast is leafed through only on a board with a touch panel and
     PSRAM, the two Guition boards; the CYD shows the weather of now alone;
  2. The time zone by the nearest place is sometimes wrong where a zone without
     summer time is near: El Paso gets the zone of Phoenix. Then the zone must
     be chosen on the page;
  3. Letters of other languages with diacritics are written without them in
     the name of the city: "Kraków" as "Krakow". The font has no CJK characters
     and no Arabic script: such a name is written in part or not written at
     all;
  4. The settings page is styled with Bootstrap, and the browser loads its CSS
     file from the Internet, from a CDN: the clock does not hold it. Without
     the Internet on the phone the page works but looks like bare HTML;
  5. The page has no password. It exists only after the button is pressed and
     only in the local network.

# Third-party resources

  1. The weather symbols are the [Weather Icons](https://github.com/erikflowers/weather-icons)
     font by Erik Flowers, licensed under SIL OFL 1.1. The font itself is in
     `misc`; the symbols of the table of `main/weather.c` are turned into code
     from it during the build, by the `font2c` utility of dgx;
  2. JSON is parsed by [mjson](https://github.com/cesanta/mjson), Cesanta, MIT,
     as the component [jef-sure/mjson](https://components.espressif.com/components/jef-sure/mjson/);
  3. QR codes are made by the [QR Code generator](https://github.com/nayuki/QR-Code-generator)
     library of Project Nayuki, MIT. Its two C files are in `components/qrcodegen` as they are.

# License

MIT, see [LICENSE](./LICENSE).
