Modern Magic for Schoolchildren Part 2

We finished the previous article with the fact that we need to give our magic scheme a little “smartness” 🙂 That is, to endow it with intelligence. Everyone knows Volkov's work “The Wizard of the Emerald City”, where the girl Ellie with her dog Totoshka, as well as a lion, an tin woodcutter and a bogeyman went to the great Goodwin.

Do you remember why they went to him?) Ellie just wanted to go home, and the others? That's right! The Tin Woodman needed a heart, the Scarecrow needed a mind, and the lion lacked courage.

So, let's assume that our scheme is this happy trinity.

The entire hardware is the tin woodcutter and it needs a heart, in our case – a connection with the source of great magical energy (socket). The Scarecrow is the software part of the magic project, so it needs intelligence from us – accordingly, in our language, a program. We act as the lion and at the same time Goodwin, because we need courage for all this, but we ourselves act as wizards 🙂

We have a “sketch” of the tin woodcutter ready in the previous publication. So, let's get busy with the mind of our creation, for this we will resort to the help of the Internet, and a little bit to our own experience.

The Arduino Wi-Fi connection program is taken from the forum site. https://www.arduino.cc/reference/en/libraries/wifi/client.connect/

To connect our Arduino to the Wi-Fi network, we use the Wi-Fi shield expansion board.

#include

#include

char ssid[] = “myNetwork”; // network SSID (name)

char pass[] = “myPassword”; // password

int status = WL_IDLE_STATUS;

char servername[]=”google.com”; // remote seoveo

WiFiClient client;

void setup() {

Serial.begin(9600);

Serial.println(“Connecting to WPA network…”);

Serial.print(“SSID: “);

Serial.println(ssid);

status = WiFi.begin(ssid, pass);

if ( status != WL_CONNECTED) {

Serial.println(“Unable to establish wi-fi connection”);

// Do nothing if

while(true);

}

else {

Serial.println(“Connected to wifi”);

Serial.println(“\nStarting connection…”);

// If the connection was successful, we return:

if (client.connect(servername, 80)) {

Serial.println(“Connection established”);

// Perform an HTTP request:

client.println(“GET /search?q=arduino HTTP/1.0”);

client.println();

}

}

}

And we will write the loop function ourselves. How, it's very simple, what do we need? So that our controller constantly checks for the presence of on/off signals for the light bulb from our magic wand – the smartphone 🙂

int nLampPin = 1;

void loop() {

If (Serial.SignalOnOff) {

digitalWrite(nLampPin, HIGH);

} else {

digitalWrite(nLampPin, LOW);

{

}

We are making this up as we go, so please correct me in the comments if anything is wrong – we would be very grateful!

Reflashing our Arduino. Hooray!

So, the outline of the “iron man” scheme, as we have already said, is there. “The mind for the Scarecrow” is prepared, now a little courage and skill – we connect the scheme and connect to the source of “great magic” :))

Sketch of the scheme

Sketch of the scheme

Now it remains to teach our magic wand to turn on the light bulb, for this we will not use complex tools. We will use a convenient mobile IDE – DroidScript with support for the magic language JavaScript. Roughly speaking, to implement the simplest interface we will need a screen with only two buttons – On and Off, or, to simplify, with only one button for both functions.

Control

Control

We will take the program code from the examples in DroidScript itself for the serial interface, and slightly modify it for our case for Wi-Fi:

function OnStart()

{

//Creating graphic objects

lay = app.CreateLayout( “linear”, “VCenter,FillXY” )

//Let's create a button

btn = app.CreateToggle( “ON/OFF”, 0.4 )

btn.SetOnTouch( btn_OnTouch )

lay.AddChild( btn )

app.AddLayout(layout)

//Create a wifi object

wifi = app.CreateWIFISerial()

if( !wifi )

{

app.ShowPopup( “Please connect Arduino and restart it…” )

return;

}

wifi.SetOnReceive( wifi_OnReceive )

app.ShowPopup( “Connection established” )

}

//Run the function when the button is pressed

function btn_OnTouch( isChecked )

{

if( !wifi ) return;

//Send a command to turn on the light bulb to Arduino

if( isChecked ) wifi.Write( “ledh”)

else wifi.Write( “ledl”)

}

//Call the function when receiving data from Arduino

function usb_OnReceive( data )

{

console.log(data)

}

Voila, and how, tell me, is this not real magic that anyone can do?! And this is only the beginning, because gradually delving into physics, mathematics and computer science, you can learn more serious spells. And for this you don’t even need to go to Hogwarts, it’s enough to read literature and turn on your own imagination.

We apologize if something doesn't work, because we were writing the theoretical part of the magical science.

Let's learn modern magic together, you can suggest spells yourself, and we will think together how they can be implemented using modern tools and materials. See you at the next classes :))

Similar Posts

Leave a Reply

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