FISH&GO! A WiFi Rover controlled by Blynk APP

By on July 5, 2018
Pin It

[bctt tweet=”Based on the Fishino UNO board, a simple and funny project to join practicality and fun together controlled from the “Blynk” app” username=”OpenElectronics”]

 

Based on the Fishino UNO board, a simple and funny project to join practicality and fun together. We are going to build a robot on wheels controlled from the “Blynk” app and we are going to take advantage of the color sensor to program for games: Freestyle, ColorColor, Run and CrazyTaxi.

Thanks to development boards like Arduino, today, if you have some familiarity with electronics and programming, you can turn once complex ideas into reality. Even the programming of applications (more commonly called apps) for a smartphone is nowadays made easier by specific programs that guide into their creation. The project proposed in these pages supports these statements, because it is about creating a robot on wheel piloted by a smartphone through Wi-Fi and an app which implementation is made easy by the availability of some free applications, thanks to which we will be able to create a catching graphical interface without having to write a single line of code for Android or iOS! We didn’t, however, stop at wirelessly commanding a small robot, but we wanted to create some electronic games, which have the robot as a protagonist, to execute on your smartphone. Thanks to the Fishino UNO board (with integrated Wi-Fi module) acting as the brain of our robot on wheels, development, and test of applications have been remarkably simplified.

 

The games we have set to create are described below.

  • Freestyle: allows to pilot your robot machine using the joypad; it’s a common driving mode for a smartphone.
  • ColorColor: it’s a game in which we can select the color to detect, which can be picked from red, blue and green by default; once you chose the color, you will have to detect it on the track on the ground and tap the “GO!” Button to start the analysis from the color sensor on the robot. If it corresponds to the selected color, you win.
  • Run: we can challenge another machine (or use just one and compare times) in a run. We start by selecting the number of checkpoints and then the reference color; at each passage on a zone of that color, the inter-time will be displayed. Once the run is over, all you have to do is compare the two times to see who is the winner.
  • CrazyTaxi: we have from 10 to 60 seconds to reach an established checkpoint based on the color. The number of a checkpoint can range from 3 to 10 and we can choose just one color for all the game, or randomly generate another one at each checkpoint. Time can be incremented by 10 seconds at each checkpoint found, or randomly. Who manages to find all the colors first, is the winner.

 

With that said, let’s see how we have made everything, starting by analyzing the hardware (the robot on wheels) and later taking care of the smartphone user interface.

 

 

The system

In order to create our robot car, we’re not gonna use any specific electronic circuit, but we entrusted preassembled modules piloted by the Fishino UNO board which, as you might know, is an enhanced version of Arduino UNO, adding Wi-Fi, an RTC, and a microSD reader. Nothing stops us from using an Arduino UNO, provided you will have to add a Wi-Fi shield and add the sketch.

The system is powered by eight 1.5 V AA batteries: four are used for the Fishino UNO board and the breakout board for detecting colors, and just as many for the motors. This solution will allow us to have a bit less than half an hour of autonomy (which is not a lot).

We can, alternatively, use rechargeable AA batteries, or a Lipo battery with at least 2 Ah, which you can couple to the Torpedo2 board, a DC/DC converter with lithium battery charger stage for a single cell.

As for the connections, we are going to use some digital pins on the board in order to manage the motor direction and two PWM pins to regulate their speed.

The color sensor will be connected to four of the five available analog pins, which can be used at digital I/Os.

the figure shows the robot’s circuited diagram.

 

 

Modules employed

The hardware is composed of two modules on breakout board, the first one takes its name from the integrated circuit at its heart, the popular L298 by ST, capable of managing two direct current brush motors, or a bipolar stepper motor, with a maximum current output of two amps per channel. The piloting uses a double H-bridge. Maximum current and output are more than enough to power our motors, which, if powered at 6 V (maximum voltage allowed), absorb around 4070 mA each in standby.

 

 

The module accepts voltages ranging from 5 V to 30 V as output and between 4.5 V and 5.5 V for the control logics. Moreover, we can take 5 V from the regulator (it’s an LM7805) which is integrated in the board, using the dedicated pin only if we powered module using a voltage lower than 12 V and we leave the dedicated jumper in place. The color sensor is a TCS230 model, the same one used for making the Christmas RGB lamp published in issue 191, which you can read for the technical info. The TCS230 sensor is available on breakout board.

 

 

 

Practical making

Given the simplicity of the diagram, we chose to avoid a printed circuit to connect modules and motors, and we opted to wire the Fishino UNO board with the two modules using male – female jumpers. Despite the high number of cables used, the result will be very organized and visually clean.

Let’s start from the mechanics, which requires a chassis and two motoreducers with front wheels, plus a pivoting wheel on the backside.

The figure shows the components used in the mechanics section, each one labeled with the initials we will measure in the next paragraphs.

 

 

The mechanics section can be put together by installing the motor (2A) through two mounts (1A and 1B) that must be inserted in the chassis in the slots 3A and 3B, using a V4 screw to pierce through the bottom external support first, then the motoreducer and then the internal mount. You can tighten the screw with a nut and then repeat the operation for the top hole.

Then, you’re going to repeat the same passages for the other motor and lock the wheels in the dedicated protrusions. Finally, you’re going to install, using 8 V1 screws, four per side, the V2 spacers in sections 4A, 4B, 4C and 4D per to install the wheel (5) that will keep our robot horizontally stable. The result is shown in the figure. Now, we insert the on/off switch in the dedicated central space (6) and solder the black cable of the battery box to the central pin and another cable to the other pin which we’re then going to connect to the motor module.

 

 

We mount the battery box on the chassis using two V3 screws in holes 7A and 7B and we tighten them using two nuts. Same goes for the other battery box, inserting a V3 screw in the hole 7C with its nut.

 

 

The electronic components must be placed on the second chassis; in particular, the Fishino UNO board must be placed in the points 8A and 8B using two spacers with their nuts. The motor module will be kept in place by 2 V5 screws going through holes 9A and 9B with their nuts. Now, we are going to place the second on/off switch into the second chassis and, just like before, we are going to solder the black cable of the other battery box on the central pin and another cable on the other pin.

In order to join the two chassis together, we’re going to use V4 screws with a couple nuts for each. We pass the screws through the holes 10A, 10B, 10C and 10D of the bottom chassis and we tighten them in place using 4 nuts; then, we insert the four screws in the same holes, into the second chassis and we make everything stable using four other nuts.

 

 

Finally, we are going to place the color sensor board on the front side, passing the cable through slots 11 of the two chassis; since there are no holes, if you want to make it more stable you can block it using a plastic “U” glued to the bottom chassis and to the board itself using silicone.

 

 

In the wiring scheme, you can see the connection you will have to make between the two electronic boards to complete the robot. The final result is shown in figures.

 

 

Blynk

Let’s now move on to the software section and start to explain how to create the software control and handling.

Let’s start from Blynk, which is a free application that allows controlling prototyping boards like Raspberry Pi, ESP8266 and of course our Fishino UNO in many different ways (ethernet, Wi-Fi, Bluetooth, USB, GSM, BLE). The potential of this app is much bigger though: in fact, we can use it to also create a bona fide graphic interface by dragging various control inside the available area.

So, let’s take a look at the configuration, once we download and open the application we will find a login screen where we can authenticate using a Blynk account or we can just use Facebook;

 

 

this way, we will use the servers made available by the platform, but this will add a small latency between sending and receiving a command. Since we need a very quick response in this project, we chose to create a local server using Blynk; on the official page you just have to click on “docs” in the upper right corner in Luke four “Blynk server” on the left side, while, lower in that section, we will find the button that allow us to download the server. We will then open a link to github from which we can download the latest version, which is currently 0.24.1.

 

 

Let’s click on “server-0.24.1.jar” and start the download; while we wait, we can create a folder on our desktop where we can then save the server. Inside it, we will create another folder named “Data”.

Once the download is over and we have created the folders, we can start our server by opening Windows’ command prompt as admin. Now, we must first check we have version 8 of Java installed in our computer; you can do that just by typing the command “java -version. If you don’t have virtual 8 installed, you will get a notification; you will then have to download it, otherwise you won’t be able to continue. Let’s now move to the directory where we have downloaded the server using the command “cd DRIVELETTER:\directoryServer then press enter.

 

 

To finish the creation of the server, let’s type the command “java -jar server-0.24.1.jar -dataFolder /Data”, which is then confirmed by the message “Blynk Server successfully started. All server output is stored in folder ‘C:\Users\CarloPalumbo\FishGo\.\logs’ file. Of course, in case of server update, you will have to write the correct name file; besides, you will have to replace the name of the folder, if you didn’t call it Data.

 

 

Let’s go back to the “docs” page of Blynk website and download the necessary libraries for Arduino IDE from the right side menu “Blynk Library”. As we did earlier, let’s download and save the .zip file and add it to Arduino’s libraries; in order to do that, you just have to open the IDE and select the archive you just downloaded.

 

 

Now, we can go back to our smartphone and click on “Create New Account”; then we choose an email address and a password, finally we click on the icon that looks like a traffic light, to open the drop-down menu “Server Settings”, let’s move on “Custom” and insert the private IP address of the computer where the server is launched in the field “Host address”, while we are going to use value 8443 in the field “Port. This allows to communicate with our server, however, we are forced to use the personal computer on which we have created the “Data” folder.

 

 

If the aim of a project is portability, we recommend using Blynk’s server. In both cases, we will see the window to create our first app. Let’s click on “New Project”, then we give our name to our project and select Arduino UNO from the list. Finally, let’s select Wi-Fi as connection type, then we choose a theme and click on “Create”.

 

 

Now we have reached the screen where we can proceed to create our app, all we have to do is click in a random point to open the drop-down menu of the widgets and insert them in our screen.

 

 

We have to make a little note regarding a number of widgets we can use: we have 1000 points available (called “energy” in the app) and each widget we are going to add will use some of them. If we need to use more “energy” we will have to pay in order to get it, so the use is free for apps which do not exceed 1000 points.

Let’s conclude by saying that if you click on the bolt icon (top right) the settings menu will appear.

 

 

Anyway, if you want to get all the information on the widget, configurations and news, all you have to do is visit the webpage http://www.docs.blynk.cc/.

Very well, not that all our tools are ready, we can import the widget configuration through QR code: let’s go back to starting manual and in the top left next to the button to add a project, we can find the button to scan a QR code.

 

 

Let’s click on it and point our camera towards the QR code, as shown in the figure;

 

 

in a few seconds, we will have a bespoke application for our robot car!

 

 

The firmware

Now that we are done preparing the application on the smartphone, let’s take a look at the most interesting part of the project: the software, which can be freely downloaded from our magazine’s website and from which we are going to extract some sections (the whole code would have required too many pages…) in List 1, List 2 and List 3. To be specific, in List 1 you will find the section of code regarding color detection, List 2 shows the Freestyle mode and finally, List 3 reports the code for managing the motors. Taking the firmware file that you can download from our website as reference, the first line of code into firmware is used to activate Blynk’s debug, which is very useful in the starting phase in order to understand if there are connection problems between the app and the devices, but it is recommended to command on it later during the development of the application because there is the risk to slow down the information flow. Right below, we can find the libraries and variables necessary for the games. A word of mention for the instruction “char auth[] = “INSERIRE AUTH TOKEN”;” where we are going to insert the code we find in the settings menu on the app: let’s go to the section “AUTH TOKENS”, then click on “Copy All” and paste the text in our smartphone’s notes so that we can copy it into sketch. We can also click on “E-mail All” and receive it at the address inserted when signing up. As it always happens for Fishino, we input SSID and password for our work in order to connect. Then, we have the variables that allow us to control the motors, the color sensor, the menu functioning and the game mechanics as we previously explained. In the setup function, the most peculiar setting is Blynk.begin(auth, ssid, pass, IPAddress(***, ***, ***, ***)); where we are going to initialize a connection to our local server. We then have to insert the private IP of our computer in brackets, only if we are using a local server, otherwise, the IPAddress instruction is not needed.

List1

void DetectRed () {
// ACTIVATE THE FILTER FOR RED
digitalWrite (s2, LOW);
digitalWrite (s3, LOW);
// store data in the Global varibale
data = pulseIn (sensorData, LOW);
// DECOMMENT TO ACTIVATE THE DEBUG OF THE SENSOR
Serial.print ("Red PulseIN =");
Serial.print (date);
Serial.print (";");
data = map (date, 1356, 345, 0, 255);
// DECOMMENT TO ACTIVATE THE DEBUG OF THE SENSOR
Serial.print ("red DataMap =");
Serial.print (date);
Serial.println ("");
}
void RilevaVerde () {
// WE ACTIVATE THE GREEN FILTER
digitalWrite (s2, HIGH);
digitalWrite (s3, HIGH);
data = pulseIn (sensorData, LOW);
Serial.print ("Green PulseIN =");
Serial.print (date);
Serial.print (";");
data = map (date, 2350, 450, 0, 255);
Serial.print ("green DataMap =");
Serial.print (date);
Serial.println ("");
}
void RilevaBlu () {
// WE ACTIVATE THE BLUE FILTER
digitalWrite (s2, LOW);
digitalWrite (s3, HIGH);
data = pulseIn (sensorData, LOW);
Serial.print ("Blue PulseIN =");
Serial.print (date);
Serial.print (";");
data = map (date, 1990, 410, 0, 255);
Serial.print ("blue dataMap =");
Serial.print (date);
Serial.println ("");
}

List2

// FREE MODALITY
void Free () {
free = true;
exit = true;
X = 128;
Y = 128;
Blynk.virtualWrite (V3, 0);
Blynk.virtualWrite (V4, 0);
Blynk.virtualWrite (V5, 0);
Blynk.virtualWrite (V6, 0);
do {
Blynk.run ();
Movement();
} while (free);
}

 

Now, let’s see how to interface the app with Fishino: in the loop function, all we have to do is have the instruction Blynk.run(). In order to manage the various widgets, we just have to follow the instructions in the examples you can find on the website. In this article, where only going to analyze those cues, for the sake of simplicity, that is:

  • Button;
  • Labeled Value;
  • Joystick;
  • Led.

Let’s start with the Button widget, which allows us to browse the manuals and select the games. Besides, Blynk provides us with 31 virtual pin marked with a V (V1, V2, … V31) for sending data from the app to work. In order to send the status of a Button, for instance, all you have to do is recall the function BLYNK_WRITE(V1), declare and initialize the verbal v1= param.asInt();. Now, anytime we press the button, the app will send a 1 and will then send a 0 upon release. In our code, we will take only the pressure in into consideration, by filtering through a if cycle the value 1 alone.

list3

// MOTOR MOVEMENT MANAGEMENT
void Movement () {
//come on
if (Y == 255 && (X> = 68 && X <= 188)) {
digitalWrite (MotorEA, HIGH);
digitalWrite (MotorA1, HIGH);
digitalWrite (MotorA2, LOW);
digitalWrite (MotorEB, HIGH);
digitalWrite (MotorB1, HIGH);
digitalWrite (MotorB2, LOW);
}
//back
else if (Y == 0 && (X> = 68 && X <= 186)) {
digitalWrite (MotorEA, HIGH);
digitalWrite (MotorA1, LOW);
digitalWrite (MotorA2, HIGH);
digitalWrite (MotorEB, HIGH);
digitalWrite (MotorB1, LOW);
digitalWrite (MotorB2, HIGH);
}
// av right
else if ((X> 128 && X <= 255) && (Y> 128 && Y <255)) {
digitalWrite (MotorEA, LOW);
digitalWrite (MotorA1, HIGH);
digitalWrite (MotorA2, LOW);
digitalWrite (MotorEB, HIGH);
digitalWrite (MotorB1, HIGH);
digitalWrite (MotorB2, LOW);
}
// left
else if ((X> = 0 && X <128) && (Y> 128 && Y <255)) {
digitalWrite (MotorEA, HIGH);
digitalWrite (MotorA1, HIGH);
digitalWrite (MotorA2, LOW);
digitalWrite (MotorEB, LOW);
digitalWrite (MotorB1, HIGH);
digitalWrite (MotorB2, LOW);
}
// on the right
else if ((X> 186 && X <= 255) && (Y> 1 && Y <128)) {
digitalWrite (MotorEA, LOW);
digitalWrite (MotorA1, HIGH);
digitalWrite (MotorA2, LOW);
digitalWrite (MotorEB, HIGH);
digitalWrite (MotorB1, LOW);
digitalWrite (MotorB2, HIGH);
}
// on the left
else if ((X> = 0 && X <= 71) && (Y> 1 && Y <128)) {
digitalWrite (MotorEA, HIGH);
digitalWrite (MotorA1, LOW);
digitalWrite (MotorA2, HIGH);
digitalWrite (MotorEB, LOW);
digitalWrite (MotorB1, LOW);
digitalWrite (MotorB2, HIGH);
}
else if (Y == 128 && X == 128) {//
digitalWrite (MotorEA, LOW);
digitalWrite (MotorEB, LOW);
}
}

 

 

It is also possible to set the text of various buttons by using the instruction Blynk.setProperty(Vpin, “onLabel” or “offLabel”, “Testo”).

As for Labeled Value you just have to recall the command Blynk.virtualWrite(Vpin, “Testo”) to send out the text that will be displayed in the dedicated label on the smartphone.

For managing the Joystick widget, on the other hand, we can do it using two physical pins of the board with the option SPLIT or a virtual pin with MERGE. We are going with the second option and let’s make sure that the visible symbols in figure are disabled, leaving the remainder unaltered. In the sketch, we have to use BLYNK_WRITE(Vpin) and parables X = param[0].asInt(); e Y = param[1].asInt(); to which we are going to assign the related values of the two axis of the joystick.

The last widget we have to configure is Led: this must be declared using the object WidgetLED nomeled(Vpin). Then, in order to turn it on and off we will use, respectively, commands nomeled.on(); and nomeled.off(); just like the color, on the other hand, we are going to edit the properties of the object using the command Blynk.setProperty(Vpin, “color”, “#codice esadecimale del colore”);.

 

 

Finally, to set the level of brightness we can use command nomeled.setValue(value); value is an integer from 0 to 255, minimum and maximum brightness respectively.

We also have to be careful not to use the function delay() or to make too many reading or writing requests from the app; in fact, this will cause a data sync error, with subsequent app disconnection.

To bypass the problem, Blynk recommends to use the library SimpleTimer, which allows to manage delays or repeat functions at fixed time intervals without risking disconnection from Blynk. To use it, you just have to download it from “http://playground.arduino.cc/Code/SimpleTimer” and include library SimpleTimer.h, declare an object SimpleTimer and, in the interested code section, add the line nometimer.run();.

In order to use the timer we can take advantage of various functions; below, we take a look at the most commonly used ones:

  • int setInterval(long d, timer_callback f):

it recalls function “f” necessarily declared as void() each “d” milliseconds;

  • int setTimeout(long d, timer_callback f):

it recalls function “f” necessarily declared as void() after “d” milliseconds; once the function has been executed, the timer will be automatically deleted;

  • int setTimer(long d, timer_callback f, int n):

it recalls function “f” necessarily declared as void() each “d” milliseconds, “n” times.

 

Once the function has been executed “n” times, the timer will be automatically deleted. Of course, there are many other ways to interact with the object SimpleTimer but, for more in-depth information, we suggest you review the related documentation at the previous link. Finally, as you might notice, the first function does not automatically delete the timer. Therefore, we will have to assign a unique ID to each one of them, when using multiple timers. In order to do that, we can use the line of code:

int id = timer.setInterval(long d, timer_callback f)

this way, by using the functions nometimer.disable(id) or nometimer.delete(id), we can deactivate or delete, respectively, the related timer ID.

 

Programming the games

Before an in-depth analysis of all the modes, let’s take a look at the functions which are used in every games and the commands associated with the various widgets. The first is surely the labelBtn which sets the labels for the buttons and the texts of the label at each first start and anytime we exit a game. Besides, with the right modifications, we can use it to create a part of the interface in some selection menus. The function Movimento() will allow us, based on the values of X and Y assigned by the Virtual Pin 1 on the app, to manage the direction of the motors and steering. We will have six possible movements, as is shown in the figure,  RilevaColore(), uses the color sensor to detect if the caller is right, depending on the request, and it sees it inside the variable data which will be remapped to make the measurement easier to read.

 

 

Now, let’s move on to the buttons and the LEDs, all associated to a virtual pin. Virtual pins 4 and 5 are used to browse between the various modes and to choose the settings in the games where required. Virtual pin 3 is used for confirmation of selection of the parameters previously described, besides being used to exit the various games. The last virtual pin associated to a button is number 6, which, in the games ColorColor and Crazytaxi allows, once pressed, to detect the color, while the LED associated to virtual pin 7 will be used in the same modes to signal the color to be found. Now, let’s analyze some programming specifics for several games: in the Run mode, after selecting the number of checkpoint and the color identifying it, the LED will light on and the timer will start. This is managed by the function Time which, through the instruction ms = millis() – startconteggio; counts how many milliseconds have elapsed from when the mode starts. Then, the value milliseconds is converted into other time units and the string displayed in the label is built.

Note that the function Time is called through a timer after half a second; this because, during the passage on the caller, the data value could be several times higher than the fixed limit. This calls for a frequent recalling of the instruction Blynk.virtualWrite(V2, tempo); which, as we mentioned before, must be avoided so that the app doesn’t disconnect from the server due to the excessive number of requests. So, we will use debounce which ensures the execution of the Time function just once for each passage.

The game CrazyTaxi is, on the other hand, the more complicated. Before being able to play, we will have to choose the number of checkpoints to find, the color and the countdown.

Moreover, the last two parameters can be randomly or arbitrarily selected. In case we go with a random color, will have to find one color out of three and, if we do it, we will be assigned another random one, otherwise, we will have to keep looking for the same one.

In case of random time, each checkpoint will generate another one, as random as the previous one, otherwise, 10 seconds will be added to the time previously selected. Time is handled by a timer that subtracts 1 second from the countdown value each second. Contrary to the previous one, which was used just once, this timer is activated each second, so we have to delete it using the method deleteTimer(id). In fact, as you may notice, the timer has been assigned to a ID variable and it is deleted at the moment you exit the game or the game itself is over.

 

Calibration of the color sensor

In order to calibrate the color sensor, all we have to do is connect our car to the computer by using the USB connection of Fishino, then we open the serial monitor with the preset settings on the virtual COM port assigned to the USB in use, then launch a server on the PC and the ColorColor game on the app. In fact, we’re going to take advantage of this mode to read the values detected by the sensor and then map them from 0 to 255. In the list, finally, we are going to remove the comments from the lines related the serial communication in the functions RilevaRosso(), RilevaVerde(), RilevaBlu(). Once the serial monitor is opened and the game is launched, we are going to lift our robot on wheels up and click on “GO!” Several times in the app: on the serial monitor, we will see various lines like PulseIN colorescelto = value; dataMap colorescelto = value.

At the moment, we are only interested in the value recorded by PulseIN; where are then going to make an approximate average and note the result down. Then, we put our car on the ground and place the color we want to detect under the sensor. Then, we click on “GO!” One more time and take note of the result. These two numbers will be, respectively, the minimum and maximum and we are going to insert them in the sketch line:

 

data = map(data, minimum, maximum, 0, 255);

 

We are going to do the same thing for the other two colors. After that, our sensor would be calibrated to recognize red, green and blue, our basic colors.

 

Conclusions

The choice of these three colors is not a coincidence, in fact, they correspond to the sensor’s filters, therefore they are easier to detect; anyways, nothing stops you from adding other colors by programming a dedicated function that will detect a color once the game starts, and then will use it again as a sample during the game.

The possible development of this project are many and limited only by your imagination.

 

From openstore

Base Robot Chassis + Wheels + Motors + Battery Holder

TCS230 Color Recognition Sensor module

Motor Driver 2A Dual H-Bridge L298

FishinoUNO

 

About Carlo Palumbo

Electronics and informatics are my biggest passions and through the development of projects in both areas during my graduation I was able to improve skills required to become expert in both. Curiosity was essential to increase my willing for learning more every day and that same curiosity is what motivates me to keep developing my knowledge. I have also attended the iOS Apple Developer Academy in Naples and won the WWDC18 contest. LinkedIN: https://www.linkedin.com/in/carlo-palumbo-992358111/ YouTube: https://www.youtube.com/channel/UCO-HWem97Jj0kjwYH-1vIXQ Facebook: https://www.facebook.com/patana93 GitHub: https://github.com/patana93

Leave a Reply

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