ShIoTiny: nodes, links and events or features of drawing programs

Key points or what this article is about

The topic of the article is the visual programming of the ShIoTiny PLC for the smart home described here: ShIoTiny: small automation, the Internet of things, or “six months before the holidays” .

Such concepts as nodes , communications , events , as well as features of loading and executing a visual program on ESP8266 , which is the basis of ShIoTiny PLC, are very briefly reviewed .

Introduction or a couple of organizational questions

In a previous article about my development, I gave a brief overview of the capabilities of the ShIoTiny controller.

Oddly enough, the public showed a rather strong interest and asked me a lot of questions. Some comrades even immediately immediately suggested I buy a controller. No, I am not opposed to earning a little money, but my conscience does not allow me to sell a thing that is still very crude in terms of software.

Therefore, I posted on GitHub the firmware binaries and the device diagram: firmware + shortest instruction + diagram + examples .

Now everyone can flash ESP-07 and play with the firmware on their own. If anyone really wants just such a board as in the photo, then I have several of them. Write to . But, as the unforgettable Cucumbers used to say: "I am not responsible for anything!"

So, let's get to the point: what is a “ node ” (node) and “ event ”? How is the program executed?

As usual - let's start in order: by downloading the program.

How does the program load

To begin with, what happens when we click the Upload button in the ElDraw editor and our circuit program, consisting of beautiful squares, flies into the device.

Firstly, on the basis of the diagram drawn by us, its description in text form is built.
Secondly, it is checked whether all inputs of the nodes are connected to the outputs. There should be no “hanging” inputs. If such an input is detected, the circuit in ShIoTiny will not load, and the editor will display a warning.

If everything went well, the editor sends a text description of the circuit to one node in ShIoTiny. Of course, the existing schema from ShIoTiny is previously removed. The resulting text description is saved in the FLASH memory.

By the way, if you want to remove the circuit from the device, then simply load an empty circuit (which does not contain any node elements) into it.

As soon as the entire program circuit is loaded into the ShIoTiny PLC, it starts to “run”. What does it mean?

Note that the processes of loading the circuit from the FLASH memory when the power is turned on and when the circuit is received from the editor are identical.

First comes the creation of node objects based on their description.
Then the arrangement of connections between nodes is made. That is, links of outputs to inputs and inputs to outputs are generated.

And only after all this, the main program execution cycle starts.

I wrote for a long time, but the whole process — from “loading” the circuit from the FLASH memory to the start of the main loop — takes a split second for a circuit of 60-80 nodes.

How does the main loop work? Very simple. First, he waits for the event to occur in any node, then he processes this event. And so without end. Well, or until they upload a new scheme to ShIoTiny.

Already several times I mentioned things like events , nodes and connections . But what is it from a software point of view? We’ll talk about this today.

Nodes, Connections, and Events

Just look at the examples of circuit programs for ShIoTiny to understand that a circuit consists of only two entities - nodes (or elements) and the relationships between them.

A node , a node, or a circuit element is a virtual representation of some action on data. This may be an arithmetic operation, a logical operation, or any operation whatever comes to our mind. The main thing is that the node has an input and an output.

An input is where the node receives data. Input images are points that are always on the left side of the node.

The output is the place where the result of the node operation is extracted from. Output images are points that are always on the right side of the node.

Some nodes have no inputs. Such nodes generate a result within themselves. For example, a constant node or a sensor node: they do not need data from other nodes to report the result.

Other nodes, in contrast, have no exits. These are nodes that display, for example, actuators (relays or some other similar ones). They accept data, but do not generate the result of calculations available to other nodes.

In addition, there is also a unique comment node. He does nothing, has neither inputs nor outputs. Its purpose is to be an explanation in the diagram.

What is an “ event ”? An event is the occurrence of new data in a node. For example, events include: changing the input state ( Input node), receiving data from another device ( MQTT and UDP nodes), the expiration of a specified period of time ( Timer and Delay nodes), and so on.

What are events for? Yes, in order to determine in which node the new data appeared and the states of which nodes need to be changed in connection with the receipt of new data. The event, as it were, “passes” along the chain of nodes until it has bypassed all the nodes whose state must be checked and changed.

All nodes can be divided into two categories.
The nodes that can generate events are called “ active nodes ”.
The nodes that cannot generate events are called “ passive nodes ”.

When a node generates an event (that is, new data appears on its output), then in general the state of the entire chain of nodes connected to the output of the event generator node changes.

To make it clear, consider the example in the figure.

The active nodes here are Input1, Input2 and Input3. The remaining nodes are passive. Consider what happens when this or that input closes. For convenience, the results are tabulated.

As you can see, when an event occurs, a chain is built from the source node of the event to the end node. The state of those nodes that do not fall into the chain does not change.

A legitimate question arises, but what will happen if two or even several events occur simultaneously?

As a lover of creativity, Gleb Anfilov, I am attracted to send a curious questioner to his book “Flight from Surprise”. This is such a "theory of relativity for the smallest", in which it is well told what is "simultaneously" and how to live with it.

But purely practically everything is much simpler: when two or even several events occur, all chains from each event source are sequentially built and processed in turn and no miracles.

The next quite legitimate question of a curious reader is what will happen if the nodes are connected in a ring? Or, as it is customary to say among these wiseacres of yours, to introduce feedback. That is, connect the output of one of the nodes with the input of the previous node so that the output state of this node affects the state of its input. ElDraw editor will not allow you to directly connect the node output to its own input. But indirectly, as in the figure below - this can be done.

So what will be in this case? The answer will be very “definite”: depending on which nodes. Consider the example in the figure.

When the contacts of the input Input1 are open at the upper input of node A - 0. At the output of node A, also 0. At the output of node B - 1. And finally, at the lower input of node A - 1. Everything is clear. And to whom it is not clear - look below for a description of how the nodes "AND" and "NOT" work.

Now close the contacts of the input Input1, that is, we will supply one to the upper input of node A. Those who are familiar with electronics know that in fact we get a classic logic generator circuit. And in theory, such a scheme should endlessly produce at the output of elements A and B the sequences 1-0-1-0-1-0 .... and 0-1-0-1-0-1-1 ... After all, an event must constantly change the state of nodes A and B, running in a circle 2-3-2-3- ...!

But in fact this does not happen. The circuit will fall into a random state - or the relay will remain on or off, or it may buzz slightly on and off several times in a row. It all depends on the weather at the south pole of Mars. And that’s why this happens.

An event from node Input1 changes the state of node A, then node B, and so on in a circle several times. The program determines the "looping" of the event and forcibly terminates this carnival. After this, the state changes of nodes A and B are blocked until a new event occurs. The moment at which the program decides - “stop spinning in a circle!” - generally depends on many factors and can be considered random.
Be careful when connecting nodes in a ring - the effects will not always be obvious! Well imagine what and why you are doing!
But is it possible to build a generator on the nodes accessible to us? Yes you can! But this requires a node that itself knows how to generate events. And there is such a node - this is the “delay line”. Let's see how the generator works with a period of 6 seconds in the figure below.

A key element of the generator is node A, the delay line. If you change the input state of the delay line from 0 to 1, then 1 at the output will not appear immediately, but only after a specified time. In our case, it is 3 seconds. Similarly, if you change the input status of the delay line from 1 to 0, then 0 will appear at the output after the same 3 seconds. The delay time is set in tenths of a second. That is, the value is 30 and means - 3 seconds.

A feature of the delay line is that it generates an event after the delay time has elapsed.

Suppose that initially the output of the delay line was 0. After passing through node B, the inverter, this 0 turns into 1 and enters the input of the delay line. Nothing happens right away. At the output of the delay line, as it was, 0 will remain, but then the delay time countdown is turned on. It takes 3 seconds. And then the delay line generates an event. At the output, she has 1. This unit, after passing through node B, the inverter, turns into 0 and goes to the input of the delay line. Another 3 seconds pass ... and the process repeats. That is, every 3 seconds the output status of the delay line changes from 0 to 1 and then from 1 to 0. The relay clicks. The generator is working. The pulse period is 6 seconds (3 seconds at the output of zero and 3 seconds - one).

But, in real circuits, you usually do not need to use this example. There are special timer nodes that perfectly and without outside help generate a sequence of pulses with a given period. The duration of “zero” and “units” in these pulses is equal to half the period.
To set periodic actions, use timer nodes.
I note that such digital signals, where the duration of “zero” and “units” are equal, are called a “meander”.

I hope I clarified the question a bit about how events are distributed between nodes and what should not be done?

Conclusion and references

The article turned out to be short, but this article is the answer to the questions raised on nodes and events.

As the firmware evolves and new examples appear, I will write about how to program ShIoTiny small articles, as long as it will be interesting to people.

As before, the scheme, firmware, examples, description of nodes and everything else is here .

Questions, wishes, criticism - this is here:


All Articles