Skip to content

Mailbox Sketch

Denis Stepanov edited this page Feb 28, 2023 · 6 revisions

Dual Sketch

You would probably notice that the mailbox sketch has unusual design, where mailbox.ino contains just:

#include "local.ino.h"        // Local module
#include "remote.ino.h"       // Remote module

local.ino.h and remote.ino.h are the actual sketches for the receiver and transmitter correspondingly. The selection of which one is active is made in MySystem.h with the help of DS_MAILBOX_REMOTE define. The reason for this is that both sketches have significant piece of code to share:

  1. ESP-DS-System library (System.h and System.cpp);
  2. communication protocol (MailBoxMessage.h and MailBoxMessage.cpp);
  3. common mailbox definition (MailBox.h and MailBox.cpp);
  4. some common definitions for transmitter and receiver (Transceiver.h and Transceiver.cpp).

Having these files as copies in two sketches would be impractical for updates and version control. A standard Arduino IDE answer to this is to make your own library and place it out of the sketch. Here, however, it would have little sense to make a library, because the code is application-specific. While developing on Linux, I put the shared code in a separate folder and used symbolic links to let the two sketches know where the code is. However, on Windows symlinks are not supported, so the design would have had to be adapted for publishing. Combining two sketches into one looked like the only viable option.

File Inventory

Class Diagram

Most of the files are present in pairs .h / .cpp, where .h contains class declaration, and .cpp contains implementation. Sometimes there is only .h or only .cpp.

Shared Files

  • SystemESP-DS-System library (not sketch-specific);
  • MySystem — sketch-specific settings; selection of local / remote module to build. See details in Getting Started;
  • MailBoxMessagecommunication protocol definition;
  • Transceiver — base class for Transmitter and Receiver;
  • MailBox — base class for PhysicalMailBox and VirtualMailBox;
  • mailbox.ino — sketch entry point.

Remote Module (Transmitter, Mailbox)

  • TransmitterHC-12 transmission interface, in particular procedures for data sending and sleep mode handling;
  • PhysicalMailBox — physical interface to mailbox door sensor. Also includes battery management;
  • remote.ino — main loop. Also takes care of deep sleep for ESP8266 controller.

Local Module (Receiver, House)

  • Receiver — HC-12 receiver interface. Also includes transmission emulation mode;
  • VirtualMailBox — virtual representation of the physical mailbox on receiver side;
  • MailBoxManager — support for multiple mailboxes on receiver side;
  • webweb site interface. This is not a class per se, but a collection of functions to support web server;
  • TelegramTelegram interface;
  • GoogleAssistantGoogle Home interface;
  • local.ino — main loop.