Engee documentation
Notebook

Code generation for ESP 8266 (Sound sensor in Telegram bot)

This demo describes the development of a model Engee to process a signal from a sound sensor and then generate code from the model for ESP 8266 microcontroller controlled via Telegram-bot.

Introduction

The purpose of this example is to implement in Engee modelling followed by code generation of a simple algorithm applicable in home automation and Internet of Things. The target device, a microcontroller with an embedded WiFi module, will be controlled via a Telegram bot, which is also provided by the Engee model. The Telegram bot will receive from the controller the sound level at the sensor location, averaged using the moving average method.

Hardware

The target device used in this example is a NodeMCU v1.0 debug board with a ESP12E WiFi module on a ESP8266 microcontroller. The analogue signal to the device input comes from the sound sensor implemented on the LM393 chip. The sensor is powered by the microcontroller. The connection diagram of the devices of this example is shown below.

image.png

The microcontroller is programmed via USB port, the programme is loaded into Flash-memory. For further operation, the controller and the sensor are powered from the power supply unit.
Compilation of the user sketch and programming of the controller is carried out via Arduino IDE 2.3.2. To work with NodeMCU v1.0 (ESP-12E) the Arduino IDE also has a library of boards based on ESP 8266 chip from ESP8266 Community.

Telegram bot

Telegram-bot "ESP_test" (@EngeeESPTestBot) was created as a user interface to control the controller. For further work with the bot you will need its token for access to HTTP API (generated when creating the bot) and user Chat ID.

There are two commands configured in the used bot: /hi - to output a text message, /sound - to output an average sound level.

The processes of creating a Telegram bot and obtaining a user's Chat ID are trivial, publicly available and are not considered in this example.

It should be noted that the user code for processing incoming Telegram-bot messages for this example uses an ID for only one specified user. To access the processing of commands from any user, you should change the bot's incoming message processing function.

Engee model description

The control algorithm for the controller is reproduced in the Engee model. Its structure is shown in the figure below.

image_2.png

Here the blocks MovingAverage and AverageDiv calculate the moving average of 10 signal values. Interaction with the controller periphery is performed by blocks C Function:

  • AnalogInput - receives the audio level signal from the analogue input;
  • ToSerial - initialises the serial port and transmits to it the value of the averaged sound level;
  • TelegramBot - initialises Telegram-bot, checks for incoming messages;
  • WiFiConnect - establishes WiFi connection.

User chat ID MyChatID is transmitted to the input of TelegramBot block, and WiFiConnect input - connection waiting time ResponseTime, which should be increased in case of weak WiFi signal.

Any available library can be used to work with Telegram-bot. In this example the C++ library FastBot is used.

To connect to WiFi network in block WiFiConnect macros WIFI_SSID, WIFI_PASS are used - SSID and password values of WiFi network, which are further defined in the user sketch.

Detailed descriptions of algorithms of code operation in blocks C Function are given in block comments.

Modelling results

To simulate the moving average algorithm, a random output value between 8 and 10 is also generated at the output of the AnalogInput block. Let's check the algorithm performance by loading and executing the assembled model.

In [ ]:
if "esp8266_tg_sound_detect" in [m.name for m in engee.get_all_models()]
    m = engee.open( "esp8266_tg_sound_detect" );
else
    m = engee.load( "$(@__DIR__)/esp8266_tg_sound_detect.engee" );
end

data = engee.run(m);

Let's build graphs of analogue signal (generated random values) and the result of moving average calculation.

In [ ]:
using Plots
plotlyjs()
plot(data["SoundFromSens"].time, data["SoundFromSens"].value,
    label="Сигнал от датчика", size=(900,300), lw=2, st=:step)
plot!(data["SoundAvValue"].time, data["SoundAvValue"].value,
    label="Усредненный сигнал", size=(900,300), lw=2, st=:step,
    legend = :bottomright)
Out[0]:

The algorithm performs correct calculation, then let's proceed to code execution on the target device.

Loading the code into ESP 8266

To upload to ESP 8266, code must first be generated from the developed model.

In [ ]:
engee.generate_code( "$(@__DIR__)/esp8266_tg_sound_detect.engee",
                     "$(@__DIR__)/esp8266_tg_sound_detect_code")
[ Info: Generated code and artifacts: /user/start/examples/codegen/esp8266_tg_sound_detect/esp8266_tg_sound_detect_code

The files generated in the specified directory will be connected in the user sketch esp8266_tg_sound_detect.ino. Also in the sketch we define macros, variables and function for processing incoming messages, connect libraries and initialise Telegram-bot. Download these files and load them into ESP8266 using Arduino IDE.

Executing the code on ESP 8266

After successfully compiling the custom sketch and loading the code into the controller, the following output can be observed in the Arduino IDE port monitor:

sound_sens.gif

When sending messages containing specified commands to the Telegram bot, you can observe the following bot response:

sound_sens_t.gif

As can be seen from the results of the programme, the algorithm defined in the Engee model is reproduced on the target device.

Conclusion

In this demo we have considered the process of creating a Engee model, which supports working with the controller peripherals - analogue input, serial port and WiFi module. The model also implements: processing of analogue signal from the sound sensor, configuration and monitoring of Telegram-bot.

Blocks used in example