Engee documentation
Notebook

Code generation for ESP 8266 ("Smart" socket)

This demo develops a Engee model to control a digital output depending on the current time values received from an NTP server, followed by code generation for the ESP 8266 microcontroller.

Introduction

The purpose of this example is to familiarise the operation of a conditional design in the Engee model based on the block If and an atomic subsystem with a control port block of the version Action Port.
The essence of the algorithm reproduced in the model is as follows. The controller in each cycle of the model calculation polls the NTP server, receiving data about the current time from it. When a certain time is reached (in the example it is 15 hours), the controller switches one of its digital outputs to the state of logical "1", thus switching on the power contacts of the electromechanical relay, reproducing the operation of the "smart" socket.
The term "smart" in this example does not carry the connotation of "intelligent", but only briefly characterises the automatic switching according to a set time, which is the case in most of the devices of this class on the market.

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 electromechanical relay module JCQ-3FF (10A 250V) is used for switching the power circuit.
The reference voltage level of the microcontroller is 3.3 Volts and that of the relay is 5 Volts, and in addition, a low level control signal is required to switch the relay. A Schmitt trigger chip SN74HC14N is used to match the voltage levels and invert the control signal.
The power normally open contacts of the relay switch the phase of the 220 Volt mains voltage, thus providing power to the smart socket. The AC voltage is supplied from the 220 Volt input.
The wiring diagram for this example is shown in the figure below.

esp_smart_socket_bb.png

** To reproduce the hardware system described in this example, we strongly recommend following the requirements of regulations in the field of occupational health and safety, electrical safety, fire safety. If the necessary competences, personal protective equipment, serviceable and safe components are not available, we strongly recommend that you contact specialists in the relevant field.**

Model description

The model of this example esp8266_smart_socket.engee uses 4 blocks C Function to interact with peripherals:

  • WiFiConnect - establishes a WiFi connection using the SSID and password specified in the user sketch. If the connection is not established within Resp_Time_ms = 15000 мс reboots the WiFi module.
  • NTP_Client - initialises and polls NTP-server, sends to the outputs HH, MM and SS integer values of current hours, minutes, seconds. In the process of modelling the block NTP_Client forms a cyclic change of time, which allows to test the work of the algorithm.
  • ToSerial - initialises the serial port with a baud rate of 115200 baud, outputs messages to the serial port.
  • GPIO4 - initialises digital pin #4 of the microcontroller (pin D2 on the debug board) as an output, sets its state state.

Explanation of the user code of C Function blocks is given in the code comments.

The model structure of this example is shown in the figure below.

image_2.png

Condition block Check_time checks the value Hours at the input for compliance with the condition written in it - equality 15. If the condition is met, it initiates the execution of the atomic subsystem SET_HIGH on the execution control port.
In view of the fact that at removal of the logical one from the input Action port the state of the output HIGH_LEV of the subsystem SET_HIGH is preserved, in the execution mode of this subsystem the algorithm of formation of single pulses is implemented, which changes the logical level of the subsystem output at each step of calculation. In order to retain the signal in case of execution of the subsystem and resetting in case of its non-execution the function "EXCEPT OR" is implemented on the subsystem output, comparing the current and previous output values of the subsystem.

Modelling results

Let's verify the performance of the algorithm by loading and executing the assembled model.

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

data = engee.run(m);

Let's build graphs of the current hour change - signal Hours and relay switching signal - Hold_and_Reset.

In [ ]:
using Plots
plotlyjs()
plot(data["Hours"].time, data["Hours"].value,
    label="Время, часы", size=(900,300), lw=2, st=:step)
plot!(data["Hold_and_Reset"].time, data["Hold_and_Reset"].value,
    label="Вкл. розетки", size=(900,300), lw=2, st=:step)
Out[0]:

As can be seen from the graphs, at cyclic change of the current hour the relay control signal transitions and holds the state of logical one only at 15 o'clock. Now let's consider the execution of the model on the target device.

Loading code into ESP 8266

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

In [ ]:
engee.generate_code( "$(@__DIR__)/esp8266_smart_socket.engee",
                     "$(@__DIR__)/esp8266_smart_socket_code")
[ Info: Generated code and artifacts: /user/new/esp8266_smart_socket/esp8266_smart_socket_code

The files generated in the specified directory will be connected in the user sketch esp8266_smart_socket.ino. Also in the sketch macros and variables are defined, libraries and ntp-server are connected, delay of the calculation cycle is set. Download these files and load them into ESP8266 using Arduino IDE.

The external libraries used for this example are C: NTPClient, ESP8266WiFi, WiFiUdp. The NTP server is accessed at "pool.ntp.org ".

Explanations of the user sketch code operation are given in its comments.

Code execution on ESP 8266

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

smart_socket.gif

The "Socket is on" message in the diagnostic output is accompanied by switching on of the input signal status LED on the relay module and switching of the relay power contacts.

Conclusion

This demo describes the development of the Engee model and its execution on the target device - the ESP 8266 controller. This model implements interaction with the controller peripherals - serial port, digital output and WiFi module, and also implements NTP-server polling and processing of the received values. The result of the model and the programme based on it is a reproduced principle of smart socket operation.