Friday, February 22, 2019

Image Recognition for free !!!

For a complete index of all my stories click this text

There is so much to discover, and so little time.

This should be the headline of this weblog. Nevertheless this is something I discovered recently which I do not want to keep from you.





You may be aware that Google is heavily investing in AI. I already showed you how to use the Google Assistant, both the home build version and the smart-phone version, with your home automation projects in these stories:
https://lucstechblog.blogspot.nl/2017/05/google-home-and-esp8266.html

But now they have started another project which involves image recognition. You can find all details on AIY- projects: https://aiyprojects.withgoogle.com/
As with the Assistant they made this available for the Raspberry so a wide audience can start experimenting with this technology, which is really great.  However the costs for this project rather high because it involves a dedicated image processing board next to a Raspberry.

So what if I tell you that you can start playing around with Image Recognition for virtually free ???

Read on !!

You know I have been coding Android App's with MIT's App Inventor. Well there is a derivate around (actually there are several) for some time. The name is Thunkable: https://thunkable.com/

Thunkable works the same as App Inventor but has some extra features which I am not going to discuss here. They will come up in a later blog-post. However there is one APP I have build with Thunkable which is pretty amazing. It is an APP that does image recognition. I am not telling you that I invented this APP myself. I just copied it from the Thunkable website and made some (very small) adjustements.

So get starting with the original App Inventor and also get an account with Thunkable, and learn to code your own Android APP's.


Making the APP.

The more experienced APP Inventor users  can make this APP also in APP-Inventor. What you do need to do is to download the Artificial Intelligence extension from Pura Vida Extensions: https://puravidaapps.com/extensions.php and install that into APP-Inventor

So setup your Thunkable account on https://thunkable.com/ and download the Thunkable Live APP from the Google Playstore https://play.google.com/store/apps/details?id=com.thunkable.appinventor.aicompanion3&hl=en so you can test the app while building it. This is the same process as MIT's App-Inventor uses.

Start with creating a new APP and build your screen in the designer like the image shows you.





The screen has 5 items on it.
- Image 1    an image component found in the User Interface section
- Label 1    a label component also from the User Interface section
- Button 1   from the User Interface section
- Camera 1   from the Media section
- Image_recognizer1 from the Artifical Intelligence section

The last 2 components are invisible components that work in the background.

As usual I have used a wild color scheme. So make sure you use something to your own liking.

When the screen is designed to your liking switch over to the blocks part and make the following code:



And that is really all.

When the Button is clicked the camera of your phone will open the Camera application and you just take a picture like you normally would. When you think it is OK press the V for confirmation.

The text of the label will change into 'thinking' and the image is send into the cloud for image recognition.
As soon as is is r4eceived back the text of the label will change into what the image recognition software thinks it has found.

Getting the APP on your phone

Run it live or make an APK file (in the export menu) and download it to your computer and transfer it then to your phone. The easiest way to do that is to copy it to Google Drive and download it on your phone from your Google Drive.

When downloaded on your phone open the download folder and click the file. Give the Phone permission to install it and your done.

Running the APP

The App will work on modern phones and also on some pretty old ones. My faithfull Alcatel Pop C7 with Android 4.2.2 has no problems with it. And my new Nokia 5 certainly has no problems with it. So I am guessing it will work on yours to.

Pretty Amazing !!!

Well there is no 100% score with this as image recognition is still in it's infancy. However I had some pretty amazing results which I do not want to keep from you.





That's a miss however close !!



Future expansions.

I have just been playing with this and as a Proof of Concept it really is pretty amazing. But what can we do further with it.

First there is an extension that automates taking pictures. You could use that to trigger taking the picture automatically every few minutes.

Or you could have a sensor sending a signal over bluetooth that would trigger taking the picture. I have covered bluetooth communication between Android and an Arduino in these stories:
http://lucstechblog.blogspot.nl/2016/02/arduino-to-android-using-bluetooth.html
https://lucstechblog.blogspot.nl/2016/03/arduino-to-android-part-ii.html

Next you could test if the picture indeed contains what you expect like a person or a red item or whatever you come up with and have the APP send something back over wifi to an ESP or trigger an IFTTT action.

Use your imagination and have fun.

Till next time

Luc Volders

Friday, February 15, 2019

Simple IFTTT webhook test

 For an index to all my stories click this text

Time flies !!! I just can not believe it is already 2 year ago when I wrote my first weblog entry about IFTTT

For those who are still unaware about what IFTTT is read my 2 introduction stories here:
https://lucstechblog.blogspot.com/2017/04/ifttt-if-this-then-that.html
https://lucstechblog.blogspot.com/2017/05/ifttt-part-2-maker-channel.html

And read about a movement detection system using IFTTT to get an alarm on your phone here:
https://lucstechblog.blogspot.com/2017/05/ifttt-part-3-basic-alert-over-ifttt.html

Last november I brought you another story on how to use IFTTT in a practical way. I used a sensor to trigger an alarm when an object (in my case a painting) was moved:
https://lucstechblog.blogspot.com/2018/11/alarm.html

IFTTT Quick test

During devellopping a new project with IFTTT I needed a quick way to test wether my IFTTT setup worked.
Sending a trigger from an ESP8266 looks like a good idea. But what if it does not work. Is the problem in the ESP-code or is the IFTTT applet not setup in the right way.

So I discovered a neat trick to test the IFTTT applet.

First setup the IFTTT applet like you have seen in the alarm story:
https://lucstechblog.blogspot.com/2018/11/alarm.html


 

The event name we used in IFTTT is alarm.






Now just open your browser and type in:

https://maker.ifttt.com/trigger/alarm/with/key/PUTYOUROWNKEYHERE

press Enter and a notification will be triggered on your phone. This will work in any browser . Meaning it will work in the browser on your computer, phone and tablet.






This is a nice quick way to test IFTTT webhook applets.

Android 9 pitfall:

Android 9 has at the moment of this writing a serious battery problem. To prevent draining the battery of your phone best practice is to put all notifications off. However when notifications for IFTTT are set off you will not get the alarm on your phone. So make sure notifications for IFTTT are turned on.

Till next time

Luc Volders

Friday, February 8, 2019

Remote controlled car with Wifi

For an idex to all my stories click this text 


As a kid I was fascinated by remote controlled cars. I never owned one but several of my friends had one. I did not envy them because just driving around such a car and running after it was not really my idea of fun. It just intrigued me how such a thing worked.

A few years ago when I was building my own 3D printer, just before I really dug into electronics, I started to comprehend how the electronics to control motors and stepper motors work. And now surfing all kinds of technical websites I got interested in remote controlled cars again. How difficult could it be ?? A transmitter, a receiver, a transistor to send a strong current to the motor and you're done. Piece of cake or not ??

Forward is easy

Controlling a motor is one of the fundamental excersises when you start learning Arduino and electronics. An arduino can not provide enough current to control a motor. So you will have to reinforce the signal from the arduino with a transistor. I have done this before on this weblog when I was controlling a ledstrip. The drill is basically the same. So let's look at the most simple setup.



Here you can see that a signal from your micro controller (wether it is an Arduino, ESP or Raspberry) is send through an 100 Ohm resistor into a transistor which then send a large current to a ledstrip. So the only thing we have to do is to exchange the ledstrip for a motor and we're off.

The software only has to put one of the digital outputs to high to get the motor running. Set it to low and the motor stops. So we can go forward and we can stop. How about turns ??

Turning left and right.

Well this is easy to. For strength we will use two motors to drive the car. Turning to the left is easily done by stopping the left motor and running the right one. Turning to the right is just the opposite. Stop the right motor and run the left one.

H-Bridge

So with these simple steps we can go forward, left and right. But we also need to drive backwards. That is more complicated. To get the motor running in the opposite direction you need to switch polarity, and that is easier said as done.

Long time ago a solution was found for this problem and it is called an H-Bridge. Basically at first this was achieved with switches. Just look at the next schematics.


First look at the left side.

The switch on the top left side is open and the switch on the bottom right side is open. So the cirrent can only pass on the top right side through the motor and then down. The motor therefore runs in one direction.

Now look at the right side.

The switch on the top left side is closed and the one on the bottom right side is closed. Just the opposite of the previous state. In this situation the currect has to flow from the right side through the motor to the left side. And that makes the motor run in the other direction.

This works great. However done with switches it can not be operated remotely. Well actually it can !! and it was done in the early days. I still remember remote controlled cars with a wired remote...... But that is not what we are trying to achieve.

Fortunately electronics come to the rescue.



An electronic H-Bridge can be made by pairs of NPN and PNP transistors.
The electronic H-Bridge works the same way as the mechanical one. The switching is done by a small current send to the base of the transistors. This works because the NPN transistors allow current to flow when the base voltage is high, and the PNP transistors allow current to flow when the base voltage is low.

As you can see this can easily be build by yourself. For two motors you will need two of these circuits.

Let's make things easier.

As running motors is very popular dedicated H-Bridge IC's are widely available.
We are going to use the popular L293D.

 
As you can see it is easy to use. The IC is build up symetrically, and can control 2 motors at the same time. On the left side there is a VCC connection and in the middle 2 GND pins. Next to the GND are the motor connections and next to that the input pins which can be attached directly to an Arduino, ESP8266 or a Raspberry Pi.
The right side has the same connections mirrorred. So with this you can attach two motors and control them by 4 pins from your microcontroller.

Set one IO port of the controller HIGH and the other LOW and the motor will run forward. Switch the HIGH and LOW and the motor will run backwards.

 


For completeness I herebye give you the pin layout.

The L293D has some impressive features. It will run motors up to 600Ma and it can drive motors, solenoids and even steppers.

Make sure you opt for the L293D version. This one has clamping diodes that makes extra electronics unnecessary. You can also choose the SN754410 which has the same pinout so can be exchanged 1 on1 in the setup. However the SN754410 can supply a larger current (1A) so can be used for stronger motors.

Speed

Going forward and backward is one thing. But by setting the IO pins to 1 or 0 will just make the motor run at full speed or stop.

What we want is speed control. And that is where PWM comes in.

Luckily all modern micro controllers like the Ardiuino, ESP and even the Raspberry have PWM control. I presume you have used it yourself in your projects to dim a led for example.

For those not familiar with the term. PWM is the digital equivalent of setting the voltage on an IO pin. Normally the pin would be LOW or HIGH. And HIGH will be 5Volt or 3.3Volt depending on your micro controller. By setting the PWM value of an IO pin you can set the output voltage of that pin on any value between LOW and HIGH.

Actually it is a bit more complicated. The PWM function sets the pin alternately HIGH and LOW but in a very fast pace and that makes it look like a certain voltage. That is the theory. In real life for most electronics it looks like a variable voltage and that can regulate the speed of our motors.

Wireless remote control.

Several options come to my mind.
- Attach a bluetooth controller and an LD293D to an arduino and write an app with app-inventor to control it
- Attach 433Mhz transmitter to an arduino and use potmeters to set speed and direction. Use a second arduino with a 433Mhz receiver to control the LD293D. This will give you a larger range.
- Attach a NRF24L01 instead of the 433Mhz version described in the preivious point.
- Attach the LD293D to an ESP8266 and control it from a webpage.

Well to get things up and running fast I choose the easy way out (again). I choose my preferred rapid devellopment tool: ESPBasic. If you have never worked with ESPBasic before I urge you to read my introduction story which you can find here:
https://lucstechblog.blogspot.nl/2017/03/back-to-basic-basic-language-on-esp8266.html

Before we look at the program let us have a look at the hardware setup.




As you can see I attached the L293D motor 1 pins to pin D0 and D1 of the ESP and motor 2 pins to D2 and D4.
For testing purposes (before I mounted everything on a frame) I attached leds to the control pins so I had a visual indication to the direction of the motors. In the finished model I left the leds out. You can leave them in and use them as headlights.



So far for all the theory now is the time to try it out real time. Therefore I used a powerfull powerbank that has 2 USB outputs. The first one supplies max 1 amp and that is attached to the ESP. The second USB output supplies 2 amps and that is connected to the power rail of the breadboard to power the LD293D that directly powers the motors.



The motors and wheels I used are the standard versions that can be bought for a few dollar from your favorite Chinese supplier.


From the local dollar store I bought some cheap caster wheels. The motors are used for the front weels and on the back there is just one caster wheel.


When everything worked as expected I made a square frame from carton. With hot glue I glued the motors to the bottom of the frame attached the wheels and glued at the back the caster wheel.


That's all.
It's not the prettiest of all the remote controlled card but remember this is only a proof of concept.

The program.


io(po,D0,0)
io(po,D1,0)
io(po,D2,0)
io(po,D4,0)
motonedir = 0
motwodir = 0
motone = 500

wprint |<body style="background-color:powderblue;">|
wprint |<H1><span style="color: 

red;">|
wprint "Motor Control<br>"
wprint "By Luc Volders"
wprint "</H1>"
wprint "Motor speed"
slider motone, 300, 1024
wprint "<br><br>"
button "Forward", [forward]
wprint "<br><br>"
button "Stop", [motonestop]
wprint "<br><br>"
button "Backwards", [backward]
wprint "<br><br>"
button "left", [leftturn]
wprint "<br><br>"
button "right", [rightturn]
wprint "<br><br>"
button "QUIT", [progend]
wait

[forward]
sliderval = motone
io(po,D0,0)
io(pwo,D1,sliderval)
io(pwo,D2,sliderval)
io(po,D4,0)
wait

[motonestop]
sliderval = motone
io(po,D0,0)
io(po,D1,0)
io(po,D2,0)
io(po,D4,0)
wait

[backward]
sliderval = motone
io(pwo,D0,sliderval)
io(po,D1,0)
io(po,D2,0)
io(pwo,D4,sliderval)
wait

[leftturn]
sliderval = motone
io(po,D0,0)
io(po,D1,0)
io(pwo,D2,sliderval)
io(po,D4,0)
wait

[rightturn]
sliderval = motone
io(po,D0,0)
io(pwo,D1,sliderval)
io(po,D2,0)
io(po,D4,0)
wait

[progend]
io(po,D0,0)
io(po,D1,0)
io(po,D2,0)
io(po,D4,0)
end


The program is pretty straightforward.

io(po,D0,0)
io(po,D1,0)
io(po,D2,0)
io(po,D4,0)

These are the 4 IO pins that are attached to the L293D. D0 and D1 control the right motor and D2 and D4 control the left motor or vice versa depending on how you mounted them on the frame.

slider motone, 300, 1024

The slider controls the speed of the car.
The bottom and top values are chosen for my design and may vary according the frame weight etc. So adjust these for your own setup.

[forward]
sliderval = motone
io(po,D0,0)
io(pwo,D1,sliderval)
io(pwo,D2,sliderval)
io(po,D4,0)
wait

Here we see the forward movement routine. For motor 1 one of the IO pins (D0) is set to LOW and the other one is set to the PWM value determined by the slider. The same is done for the two IO pins for motor 2.

[backward]
sliderval = motone
io(pwo,D0,sliderval)
io(po,D1,0)
io(po,D2,0)
io(pwo,D4,sliderval)
wait

In the backward routine as you can see the IO pins are mirrored.


And here is how the controll will look on your PC, Phone or Tablet.

Extending the design.

This was for me just a prove of concept. Nothing can prevent you from expanding it. let me give you some clues on what you can do:

- Make a nice frame and enclosure so it looks like a real car
- Make a more ridgid frame so it can be used outdoors
- Add a Ultrasonic Sensor HC-SR04 so you can measure distance to walls etc
- Add a switch that keeps contact to the floor in the front of the car so you will know it is at the top of a stair.
- Add a switch that is pressed when the car runs into a wall so it can reverse or turn or whatever
- Add a ventilator that suck in air and you have a remote controlled vaccuum cleaner
- Add some head and tail lights
- Alter the design to make a boat
- Look at the Voice Command article ( https://lucstechblog.blogspot.nl/2016/01/voice-command.html ) and alter the software so you can give speech commands.

Till next time
Drive carefully and have fun

Luc Volders