Interactive feeder on Snap4Arduino

Although the principle of developing applications for all platforms is approximately the same and is based on the ideology of creating Scratch-style scripts using program blocks, there are differences and interesting features. These features are important to know when choosing a programming platform.

Most specialists who develop sketches (programs) for Arduino in C or Python using the popular integrated development environment Arduino IDE treat graphical programming using Scratch exclusively as a means of learning for beginners. Meanwhile, these platforms allow you to create projects that are extremely difficult to do using the Arduino IDE. For example, controlling a character on the monitor screen, creating interactive games or educational videos.

Table 1. Features of various visual programming environments

S4A

Snap4Arduino

mBlock3/5

ArduBlock 3.0

Mind+

https://s4a.cat/

https://snap4arduino.rocks/

https://www.mblock.cc/en/download/

http://ardublock.ru/download/

http://mindplus.cc/download-en.html

1

Ability to work with characters and graphics on the screen

Yes

Yes

Yes

No

Yes

2

Interactive mode of operation

Yes

Yes

Yes

limited

3

Step-by-step execution of the program

Yes

Yes

4

Ability to write your own blocks

Yes

Yes

Yes

5

Using HTTP protocols for remote control via a browser

Yes

Yes

As an example, we will show how easy it is to create a remote video surveillance system and control your pet's feeder using Snap4Arduino.

Creating a script requires only a few blocks and knowledge of how to use the HTTP protocol to control the script through a browser.

While there are usually no problems with using graphic blocks to create scripts, the issues of using the HTTP protocol require some explanation, as they are not reflected in detail in the Snap4Arduino documentation. However, the developers of this environment very kindly answered all our questions.

Before we begin describing the process of creating a “remote feeding video system,” let’s talk about how the HTTP protocol is used in the Snap4Arduino development environment.

Snap4Arduino and HTTP Protocol

Snap4Arduino comes with a tiny HTTP server built in. This means you can control your Snap4Arduino app from any computer, mobile phone, tablet or any other network device. in the same network. In the following sections, the word IP refers to the IP address of the PC running Snap4Arduino. You can see the IP address of your PC in the dialog box HTTP serverwhich opens when the option is enabled HTTP serverIn our case, the IP address is 192.168.0.105.

The HTTP protocol only works in the Desktop version. To use it, you need to enable the HTTP server option. To do this, select the options “Gear»| HTTP server.

Fig. 2

Fig. 2

Fig. 3

Fig. 3

Table 2. Command line for interaction via HTTP protocol

Task

Description of the command line in the browser

Sending messages from the browser to Snap4Arduino

http://IP:42001/broadcast=+MESSAGE

where IP is the IP address of your PC in the internal network (for example, 192.168.0.105), broadcast is a command for transmitting messages. The “+” symbol before the message means that it is public and can be transmitted over the network. Other messages are considered private and are not accessible from the network.

Updating a variable value in a Snap4Arduino script via a browser

http://IP/vars-update=VARIABLE-NAME=VARIABLE-VALUE

where IP is the IP address of your PC on the internal network (for example, 192.168.0.105),
VARIABLE-NAME – the name of the variable, which must begin with the + symbol (for example, +myVar),
VARIABLE-VALUE — the meaning of the variable.

This line will assign the global variable VARIABLE-NAME the value VARIABLE-VALUE .

Reading a variable value from a Snap4Arduino script in a browser

http://IP :42001/send-var=VARIABLE-NAME

where IP is the IP address of your PC on the internal network (for example, 192.168.0.105),
VARIABLE-NAME – a variable name that must begin with a + or – character to be accessible from the network (e.g. +myVar)

Reading the values ​​of all variables in the browser

http://IP:42001/send-vars

where IP is the IP address of your PC in the internal network (in our case it is 192.168.0.105),

send-vars – command for reading variables

This will display a list of all global variables defined in Snap4Arduino whose names begin with the + or – symbol, along with their current values.

Reading messages sent in the Snap4Arduino script in the browser

http://IP:42001/send-messages

You can read all messages in the browser that have the “+” symbol in front of them. Such messages are considered publicly available; if there is no “+” sign, the message is considered private and functions only within the script. To do this, type the following line in the browser

Snap4Arduino Live Scene Video Streaming

The Snap4Arduino scene with characters can be displayed in the browser in real time. To do this, you need to:

1. Enable the HTTP server. To do this, select the option “Gear» | HTTP–server.

2. Enable the option of transmitting the scene image via HTTP protocol. To do this, select the option ““Gear” | Public Stage.

3. Enter the address in your browser
http://IP:42001/stage
where IP is the IP address of your PC on the internal network (in our case it is 192.168.0.105).

Snap4Arduino scene live view

To take a “photo” of the Snap4Arduino Scene with characters, enter the address in the browser:

http://IP:42001/stageimg

POST: Updating Multiple Snap4Arduino Variables from an HTML Form

You can update multiple global variables in a project at once using an HTML form sent via a POST request.

Using < tagsINPUT> and <TEXTAREA>, you can send all types of input to Snap!, except booleans. Their name attribute must contain the name of the variable you want to update.

Please note that Snap4Arduino expects all requests to be in UTF-8 encoding. To ensure that your request goes through correctly, especially when dealing with special characters, you can always use:

<form action="http://IP:42001" method="post" accept-charset="utf-8">

Create a Remote Video Surveillance and Control System Using Snap4Arduino

Let's say you're leaving home for a few days, and your cat is left home alone. You need to solve the problem of his food. Of course, you can ask your neighbors (if you're on good terms with them) or buy an automatic feeder, but we won't look for simple solutions and will develop our own interactive video feeder for cats.

As a “feeding mechanism” we use ready-made solutions for automatic cat feeders. The video below shows in detail how to make a mechanical structure for feeding the bowl, as well as assemble and program the control unit. The “brain” of the control system is the Arduino Uno/Nano controller, the feed flow into the bowl is provided by a servomotor, and the feed delivery time is provided by a time module. The sketch is loaded onto the Arduino board via a USB cable using the Arduino IDE development environment.

We decided to improve the control of such a feeder, and for this we will use the Snap4Arduino platform and its capabilities for remote interaction with the browser via the HTTP protocol (described above). This will allow:

1. Improve and modernize the proposed scenario for controlling the feeder even for people who are far from programming. Programming in Scratch is already available from the 4th grade of school.

2. Monitor your pet's bowl on video.

3. Remotely pour dry food into the bowl

4. Record the number of times the cat approaches the bowl to assess its appetite).

5. Visually assess whether your pet has lost weight.

Block diagram of an interactive video cat feeder

The figure below shows a block diagram of the connection of the feeder to the Network, indicating the physical devices involved (frame outlines), the programs running on them (inside the frames), and the communication protocols connecting them (dashed lines between the frames). Such a diagram will help you plan the development of the code and the creation of the connection diagram in advance – even before you start implementing the project.

Fig. 4

Fig. 4

What we need

To create the feeder control we will need the blocks and components described in the following table.

Table 3. Elements for assembling and controlling the feeder

Wiring diagram

As a control board, we used the multifunctional SPBot controller (Fig. 5), which already has a photoresistor, buzzer, and a convenient power connector installed. In this case, you will only need to buy a servomotor (for example, SG90 or MG90S). If you do not have this board, you can use the “standard” Arduino Uno (Fig. 6).

Fig. 5

Fig. 5

Fig. 6

Fig. 6

Writing a script

First of all, we will develop an algorithm for remote feeding of the cat.

1. Open a browser on your smartphone (laptop, PC) and send a message that turns on the video camera and displays the video on the application Scene.
http://IP:42001/broadcast=+video_ON

2. We send a message that starts broadcasting the Snap4Arduino scene in a browser window.
http://IP:42001/stage

3. We see a bowl (empty or full) on the smartphone screen, and if we're lucky, the cat itself. And also the number of times the cat approaches the empty bowl.

4. We decide whether to add food to him or not. If yes, we send a message via the browser on our smartphone.
http://IP:42001/broadcast=+feed

In this case, the buzzer sounds, the door opens and the food is poured into the bowl for 3 seconds. Then the door closes.

5. You see how a happy cat runs up to the bowl and gratefully eats the food.

6. To stop the broadcast, you should send a message
http://IP:42001/broadcast=+video_OFF

7. A photoresistor, which is installed near the bowl, records the number of times the cat approaches it.

Fig. 7

Fig. 7

The scenario consists of 4 groups of blocks, the interaction between which is carried out using the mechanism of broadcasting messages (broadcast) in Snap4Arduino (Fig. 8).

Fig. 8

Fig. 8

There are two ways to send messages to Snap4Arduino:

– using the “send” block

– in the browser command line using the HTTP protocol (see section 1.2)

To debug the script, it is convenient to add the blocks shown in Figure 9 below. With these blocks, you will simulate sending a message through the browser.

Fig. 9

Fig. 9

The project can be downloaded from the link https://disk.yandex.ru/d/1bqV1KF-ol3VGA.

Turn on the video camera (#1)

To turn on the video camera and transmit video images to the stage, you need to send the message “+video_ON”.

To do this, press the < key.1> or enter the address http://IP:42001/stage in the browser command line
where IP is the IP address of your PC in the internal network (in our case it is 192.168.0.105) — see p.1.2.

An image from a video camera will appear on the Stage.

Fig. 10

Fig. 10

Turn off the video camera (#2)

To turn off the camera, send the message “+video_OFF”.

Fig. 11

Fig. 11

Pour food onto a saucer (#3)

In order for the food to start flowing from the container into the saucer, you need to open the valve using the servomotor.

Send the message “feed”. You will hear three short signals emitted by the sound module, then the servomotor will open the shutter, the food will pour out into the saucer, and after 3 seconds the servomotor will close the shutter. Determine the time of food pouring experimentally.

After the valve closes, a “+feed” message will be sent out, which “starts up” the 4th group of blocks.

Fig. 12

Fig. 12

Defining the “hungry cat coefficient” (#4)

The fourth block of the script is designed to determine how often your pet approaches the bowl. It starts functioning when it receives the message “N_feed”.

To record the fact of the cat's approach to the bowl, we use a light sensor. If the light changes sharply (L1-L2>100), then the variable L3, which stores the number of approaches, will increase by 1. With each new filling of food into the saucer, the variable L3 is reset.

Fig. 13

Fig. 13

Connecting via the Internet

We have created a script that allows you to connect to a video camera and control the feeder within your home (or office Wi-Fi) network. This project is interesting, but has more educational than practical value, since the cat and you must be within the range of your Wi-Fi network.

In order to implement remote control via the Internet from anywhere in the world, it will be necessary to provide additional conditions:

  • your Wi-Fi network must have Internet access;

  • the external IP address of the router for accessing the Internet must be static (this parameter is controlled by the provider);

  • on the router through which you access the Internet, you need to perform an operation called “port forwarding”;

As an example, we will show the steps for setting up a tp-link router with an external IP address of 176.197.XXX.XX and an internal IP address of 192.168.0.1:

In the browser address bar, enter the internal address of the router: 192.168.0.1, go to the authorization window and enter the administrator login and password (by default admin/admin);

Fig. 14

Fig. 14

On the menu Screen redirect select the item Virtual servers and click on the button in the right part of the window Add:

Fig. 15

Fig. 15

1. In the opened window for entering parameters, enter the parameters of the new virtual server:

  • Service port (chosen at random, but more than 8000).

  • IP address (in our example, 192.168.0.105).

  • Inland port — 42001.

  • Protocol — TCP.

  • State — included.

Fig. 16

Fig. 16

Click the button Save.

The created server will appear in the list of virtual servers.

Fig. 17

Fig. 17

Now you can access your HTTP server from any Internet network, but instead of the internal HTTP server address, you should enter the static address of your PC.

For example, when transmitting a message +MESSAGE instead of a line

http://192.168.0.105:42001/broadcast=+MESSAGE

the line must be specified

http://176.197.XXX.XX:42001/broadcast=+MESSAGE

where 176.197.XXX.XX is your static address.

Internet control

We can control the feeder by entering the lines in the browser address bar, described in paragraph 1.3.4.

http://IP:42001/broadcast=+video_ON

turn on the video camera and transmit the image to the Snap4Arduino Scene

http://IP:42001/stage

broadcast video image in browser

http://IP:42001/broadcast=+feed

open the hatch to load feed

http://IP:42001/broadcast=+video_OFF

turn off the video camera

where IP is the IP address of your PC. Either on the internal network (e.g. 192.168.0.105) or on the external network (e.g. 176.197.XXX.XX).

Agree, it is inconvenient to enter long lines for control. Therefore, we wrote code for a simple HTML page, the task of which is to open links.

Of course, it is possible to make a more beautiful and functional application, but our goal was to simplify it as much as possible so that it would be clear how everything works.

<!DOCTYPE html>
<html lang="ru">
 <head>
  <meta charset="utf-8">
  <title>Feeder</title>
 </head>
 
 <body>
 
<a href="http://192.168.0.109:42001/stage" target="iframe_a">Показать сцену</a>&nbsp;|&nbsp; 
<a href="http://192.168.0.109:42001/broadcast=+feed" target="iframe_b">Добавить корм в миску</a><br><br>
<a href="http://192.168.0.109:42001/broadcast=+video_ON" target="iframe_b">Включить видео</a>&nbsp;|&nbsp; 
<a href="http://192.168.0.109:42001/broadcast=+video_OFF" target="iframe_b">Выключить видео</a><br> 
<br><hr>
<iframe src="demo_iframe.htm" name="iframe_a"width="900" height="700"></iframe><br>
<iframe src="demo_iframe.htm" name="iframe_b"</iframe>
 
 </body>
</html>

The results of the application are shown in the figure.

Fig. 18

Fig. 18

Conclusion

In conclusion, I would like to note:

1. Using a visual programming environment, you can create fairly complex electronic devices even without having programming skills in high-level languages.

2. Visual programming environments, despite the general approach to developing scripts using Scratch blocks, have distinctive features that may be useful to you.

3. You can easily add new sensors and devices to our project, giving the feeder new functionality.

4. To perform experiments with the Arduino Uno/nano board, you do not need to use these boards. There are other ATMega328-based controllers on the market, to which you can upload Arduino Uno sketches. The advantage of such controllers is the presence of integrated sensors, motor drivers, etc.

It would be interesting to know the developers' opinion about the labor costs in the case of implementing a similar project using the Arduino IDE development environment (Platform I/O, etc.).

To read

1. Snap Reference Manual 8.0 (https://snap.berkeley.edu/snap/help/SnapManual.pdf)

2. Golikov D. V. Scratch and Arduino. 18 game projects for young microcontroller programmers, 2nd edition.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *