Quantcast
Channel: lucstechblog
Viewing all 318 articles
Browse latest View live

Alarm !!

$
0
0
For an index of all my stories click this text

This story tells about an alarm for paintings, But do not be set off by that. You can use the same alarm for doors that open, drawers and cookie jars.

As you might know by now, my girlfriend paints and often has expositions.



Due to the new and utterly stupid privacy laws in the Netherlands I had to blur out the faces from this photo.


At this moment she has an exposition in a gallery that shares its place with the local library. Nothing wrong with that, because it attracts more people. There is however a small problem and that is security. The building has security, the paintings are insured. However there is always a chance that something happens as the gallery is not manned all the time while the library is.

So I decided to design an alarm system for the paintings.

The idea

I wanted to be able to send a notification to my phone and that of some other people when a painting is agitated or moved. To do this I used several techniques and parts which have been covered in previous stories. It all comes together here.

First the movement detection. I am going to use the SW18010P vibration sensor for that. You can find the details about this sensor in this story:http://lucstechblog.blogspot.com/2018/05/vibration-detection.html

Next I am using an ESP8266 and I am going to program it in ESP-Basic. Find an introduction about ESP-Basic here: http://lucstechblog.blogspot.nl/2017/03/back-to-basic-basic-language-on-esp8266.html

The ESP is not going to do much. It just sits there and waits till agitation or movement is detected. I can not use a wall mounted power outlet. So I have to feed it with batteries. To use as less power as is possible I am going to use Deepsleep. Find details about Deepsleep here: https://lucstechblog.blogspot.com/2018/07/esp-power-reduction-withn-deepsleep.html

Next step is to send the information to mine and others phones. As Mit's App inventor has no push notification detection until now I am going to use IFTTT for this. You can find detailed information about IFTTT in this story (which has a link to the other stories about IFTTT): https://lucstechblog.blogspot.nl/2017/09/ifttt-4-send-notification-to-my-phone.html

And then there is a housing needed. The frame of the paintings is less then 2 cm thick. So I needed to design a housing that would fit into the frame and yet holds batteries and the perfboard.

Last but not least. The location has free internet that can be used by everyone. That is just what I need for this alarm to work.

The hardware.

I am going for a minimalistic design here. There has nothing to be done except detecting a movement and sending a message. So an ESP-01 will do the trick.

From the Deepsleep article (read it by clicking here
https://lucstechblog.blogspot.com/2018/07/esp-power-reduction-withn-deepsleep.html you will know that we can put the ESP8266 into Deepsleep and it can be woken by connecting RST (reset) to ground. We are going to use the second schematic from that article here. However we are going to modify it a bit as we do not need the led we used in the test-setup.




As you can see I attached the SW18010P directly to ground and the RESET pin of the ESP. To make sure the Reset pin only gets connected to Ground when needed I attached a pull-up resistor 10K to the VCC and the Reset pin. So reset will be high all the time until vibration is detected and that attaches it to Ground which wakens the ESP.




And here you can see the Stripboard version. you are looking at the soldering side. The components are in real life at the back-side.

The ESP is at the top and the antenna is at the top.




This is how it looks in real life.

What I did in this prototype is to solder the SW18010P thight against the stripboard. In my next version the SW18010P will be floating above the stripboard. I leave the leads longer and therefore there will be more space between the stripbaord and the sensor which makes it more sensitive.

Power


As the ESP01 is in Deepsleep it will consume very little power and therefore I can use batteries. 3 AA-type batteries will supply the power. Just like they did with the rain-sensor (read that story by clicking here).



The battery case I am using is just 1.6 mm high which neatly fits into the painting frame.
 

The stripboard has even less height and it's length just fits within the width of the battery case. The only thing we have to do is to expand the battery case and adapt it so the stripboard fits within.





The ESP-Basic Program

Programming the ESP in ESP-Basic is done with an on-screen editor in your web-browser which makes programming and editing very comfortable. Again: read the basics (pun intended) about ESP-Basic programming here: http://lucstechblog.blogspot.nl/2017/03/back-to-basic-basic-language-on-esp8266.html

The program is very straightforward:



cls
wprint"I am awake"
wprintwget("maker.ifttt.com/trigger/alarm/with/key/PUT-YOUR-KEY-HERE")
wprint"<br>"
button"go to sleep",[sleep]
wprint"<br>"
button"stop",[stop]
wait

[sleep]
cls
wprint"sleeping"
sleep0
wait

[stop]
end


No fancy webpage. Just a button to start the sleep function and a button to stop the program.

What happens when you power the ESP01 up for the first time is that it sends a notification through IFTTT. This makes sure that everything works as intended and you have an internet connection. Now you can mount the ESP in the frame.




Then press the "go to sleep" button

The ESP enters Deepsleep mode for an undefined time (sleep 0). When the painting vibrates the sensor will connect Ground to the Reset pin of the ESP01 and the ESP will awake and start the default.bas program.



Just make sure in the SETTINGS page that default.bas wil automatically run when the ESP is booted. So the cycle just repeats.

IFTTT

I have written several stories about IFTTT which I suggest you read them all. Here are the links:

https://lucstechblog.blogspot.nl/2017/04/ifttt-if-this-then-that.html
https://lucstechblog.blogspot.nl/2017/05/ifttt-part-2-maker-channel.html
https://lucstechblog.blogspot.nl/2017/05/ifttt-part-3-basic-alert-over-ifttt.html
https://lucstechblog.blogspot.nl/2017/09/ifttt-4-send-notification-to-my-phone.html



Start with making a webhook (the IF section) and link it to a notification (the THEN section).

The notification



Here you can see what happens on my Android Phone when vibration is detected.


If you want a copy of the STL files of the casing for 3D printing or adjusting for your own needs just send me an email.


Till next time
Have fun !!

Luc Volders

Simon

$
0
0
For an index off all my stories click this text

Simon is an electronic memory game invented by Ralph H. Baer and Howard J. Morrison and was develloped and sold by Studio 54 in 1978. It took till 1980 before it was available in Europe. The original Simon is as far as I know not sold anymore but there are many derivates available.

Simon as a game is still very popular.

How does it work.




The layout (mostly in a circle) shows 4 colours. A random color lights up and you press the button with the same color. Next step is that the same color lights up followed by another random color. You press the buttons in the same sequence of the colors shown etc etc etc. The first steps are easy to remember but it gets more and more difficult as the sequence grows. It is very addictive.

You can find a version for windows here: http://www.memozor.com/other-free-memory-games-online/simon-games/simon-game
And you can find many variations for Android here: https://play.google.com/store/apps/collection/search_results_cluster_apps?clp=ggEHCgVzaW1vbg%3D%3D:S:ANO1ljIRByI

I have seen several versions of Simon on the internet so it is not like this has never done before. But I wanted to make my own version: hardware and software. And most of all it is a fun project for a rainy afternoon. Considering the total cost it could be a nice present for under the Christmas tree. And indeed you still have time to build one for Christmas.

Let's buid a simon game.

Most versions I saw on the internet are build with an Arduino Uno and use 4 pushbuttons and 4 leds. Therefore they need 8 I/O pins.
I am building my version with 4 pushbuttons and a string of neopixels. Therefore I only need 5 I/O pins.

Using my multiple buttons on I/O pins trick I could even reduce the I/O pins to 3 for the pushbuttons and 1 for the Neopixels. I will publish that trick in an upcoming story on this weblog. So keep on coming back.

For a detailed introduction on Neopixels read this story:
http://lucstechblog.blogspot.com/2015/10/neopixels-ws2812-intro.html

Hardware




The first version I build used the ESP8266 as a microcontroller programmed in Arduino.




The breadboard layout is simple. Just 4 pushbuttons connected to 4 I/O pins (D3,D5,D6 and D7) and a string of 4 neopixels connected to D8.
As you can see I used a separate power supply (USB wall socket) for the neopixels and for the ESP. In the end it proved that this was not necessary as the Wemos D1 supplies enough current through the 5Volt output (pin 5V) to drive the 4 neopixels.




Therefore this complete setup can be powered from a powerbank as the photo shows.

Software.

The software is in Arduino code and therefore can easily be ported between Arduino, Attiny85 and ESP8266 or ESP32 models. Choose whatever suits you best. The version presented here is the ESP8266 (Wemos D1 Mini) version. So adjust the pin numbers for your preferred microcontroller.


/* Simon Says by Luc Volders
* ESP8266 version
* 1 = green 0,255,0
* 2 = red 255,0,0
* 3 = blue 0,0,255
* 4 = yellow 255,255,0
*/


#include <Adafruit_NeoPixel.h>
#define PIXEL_PIN D8
#define PIXEL_COUNT 7

inti=0;
intj=0;
intk=0;
intfound=0;
intmustbe=0;
intnum=0;
intplaynum=0;
intstartprog=1;
intturns=0;
intturnarray[100];

Adafruit_NeoPixelstrip=Adafruit_NeoPixel(PIXEL_COUNT,PIXEL_PIN,NEO_GRB+NEO_KHZ800);

voidsetup()
{
randomSeed(analogRead(0));
delay(10);
pinMode(D3,INPUT_PULLUP);// geen
pinMode(D5,INPUT_PULLUP);// red
pinMode(D6,INPUT_PULLUP);// blue
pinMode(D7,INPUT_PULLUP);// yellow
strip.begin();
strip.show();
}

voidloop()
{
start();
}

voidstart()
{
if(digitalRead(D3)==LOWordigitalRead(D5)==LOWordigitalRead(D6)==LOWordigitalRead(D7)==LOW)
{
strip.setPixelColor(0,0,255,0);
strip.setPixelColor(1,0,255,0);
strip.setPixelColor(2,0,255,0);
strip.setPixelColor(3,0,255,0);
strip.show();
delay(500);
startprog=0;
}
if(startprog==1)
{
initprog();
}
if(startprog==0)
{
startsequence();
}
}

voidinitprog()
{
for(i=0;i<4;i++)
{
strip.setPixelColor(0,0,0,0);
strip.setPixelColor(1,0,0,0);
strip.setPixelColor(2,0,0,0);
strip.setPixelColor(3,0,0,0);
strip.setPixelColor(4,0,0,0);
strip.setPixelColor(5,0,0,0);
strip.setPixelColor(6,0,0,0);
strip.setPixelColor(i,0,255,0);
strip.show();
delay(100);
}
}

voidkeydetect()
{
if(startprog==1)
{
strip.setPixelColor(0,0,0,0);
strip.setPixelColor(1,0,0,0);
strip.setPixelColor(2,0,0,0);
strip.setPixelColor(3,0,0,0);
strip.setPixelColor(4,0,0,0);
strip.setPixelColor(5,0,0,0);
strip.setPixelColor(6,0,0,0);
strip.show();
delay(100);
strip.setPixelColor(0,0,0,255);
strip.setPixelColor(1,0,0,255);
strip.setPixelColor(2,0,0,255);
strip.setPixelColor(3,0,0,255);
strip.show();
delay(100);
strip.setPixelColor(0,0,0,0);
strip.setPixelColor(1,0,0,0);
strip.setPixelColor(2,0,0,0);
strip.setPixelColor(3,0,0,0);
}
startprog=0;
startsequence();
}


voidtestsequence()
{
for(k=1;k<=turns;k++)
{
found=0;
do
{
delay(10);
}
while(digitalRead(D3)==HIGH&&digitalRead(D5)==HIGH&&digitalRead(D6)==HIGH&&digitalRead(D7)==HIGH);
mustbe=turnarray[k];

// =========================================================================================
// test for right button
// =========================================================================================

if(mustbe==1)
{
if(digitalRead(D3)==LOW)
{
strip.setPixelColor(0,0,255,0);
strip.setPixelColor(1,0,0,0);
strip.setPixelColor(2,0,0,0);
strip.setPixelColor(3,0,0,0);
strip.setPixelColor(4,0,0,0);
strip.setPixelColor(5,0,0,0);
strip.setPixelColor(6,0,0,0);
strip.show();
delay(500);
found=1;
}
}

if(mustbe==2)
{
// button 2 ==> 101
if(digitalRead(D5)==LOW)
{
strip.setPixelColor(0,0,0,0);
strip.setPixelColor(1,255,0,0);
strip.setPixelColor(2,0,0,0);
strip.setPixelColor(3,0,0,0);
strip.setPixelColor(4,0,0,0);
strip.setPixelColor(5,0,0,0);
strip.setPixelColor(6,0,0,0);
strip.show();
delay(500);
found=1;
}
}

if(mustbe==3)
{
// button 3 ==> 110
if(digitalRead(D6)==LOW)
{
strip.setPixelColor(0,0,0,0);
strip.setPixelColor(1,0,0,0);
strip.setPixelColor(2,0,0,255);
strip.setPixelColor(3,0,0,0);
strip.setPixelColor(4,0,0,0);
strip.setPixelColor(5,0,0,0);
strip.setPixelColor(6,0,0,0);
strip.show();
delay(500);
found=1;
}
}

if(mustbe==4)
{
// button 4 ==> 100
if(digitalRead(D7)==LOW)//
{
strip.setPixelColor(0,0,0,0);
strip.setPixelColor(1,0,0,0);
strip.setPixelColor(2,0,0,0);
strip.setPixelColor(3,255,255,0);
strip.setPixelColor(4,0,0,0);
strip.setPixelColor(5,0,0,0);
strip.setPixelColor(6,0,0,0);
strip.show();
delay(500);
found=1;
}
}

if(found==0)// FAULT FAULT FAULT

{
for(j=1;j<10;j++)
{
strip.setPixelColor(0,255,0,0);
strip.setPixelColor(1,255,0,0);
strip.setPixelColor(2,255,0,0);
strip.setPixelColor(3,255,0,0);
strip.setPixelColor(4,0,0,0);
strip.setPixelColor(5,0,0,0);
strip.setPixelColor(6,0,0,0);
strip.show();
delay(120);
strip.setPixelColor(0,0,0,0);
strip.setPixelColor(1,0,0,0);
strip.setPixelColor(2,0,0,0);
strip.setPixelColor(3,0,0,0);
strip.setPixelColor(4,0,0,0);
strip.setPixelColor(5,0,0,0);
strip.setPixelColor(6,0,0,0);
strip.show();
delay(120);

}
i=0;
j=0;
k=0;
found=0;
mustbe=0;
num=0;
playnum=0;
startprog=1;
turns=0;
delay(1000);
start();
}



// ============================================================================
// end test right button
// ============================================================================

strip.setPixelColor(0,0,0,0);
strip.setPixelColor(1,0,0,0);
strip.setPixelColor(2,0,0,0);
strip.setPixelColor(3,0,0,0);
strip.setPixelColor(4,0,0,0);
strip.setPixelColor(5,0,0,0);
strip.setPixelColor(6,0,0,0);
strip.show();
delay(500);
}// end for i to turns
}// end void testsequence

voidplaysequence()
{
for(i=1;i<=turns;i++)
{
playnum=turnarray[i];
if(playnum==1)
{
strip.setPixelColor(0,0,255,0);
strip.setPixelColor(1,0,0,0);
strip.setPixelColor(2,0,0,0);
strip.setPixelColor(3,0,0,0);
strip.show();
}
if(playnum==2)
{
strip.setPixelColor(0,0,0,0);
strip.setPixelColor(1,255,0,0);
strip.setPixelColor(2,0,0,0);
strip.setPixelColor(3,0,0,0);
strip.show();
}
if(playnum==3)
{
strip.setPixelColor(0,0,0,0);
strip.setPixelColor(1,0,0,0);
strip.setPixelColor(2,0,0,255);
strip.setPixelColor(3,0,0,0);
strip.show();
}
if(playnum==4)
{
strip.setPixelColor(0,0,0,0);
strip.setPixelColor(1,0,0,0);
strip.setPixelColor(2,0,0,0);
strip.setPixelColor(3,255,255,0);
strip.show();
}
delay(1000);
strip.setPixelColor(0,0,0,0);
strip.setPixelColor(1,0,0,0);
strip.setPixelColor(2,0,0,0);
strip.setPixelColor(3,0,0,0);
strip.show();
delay(500);
}
do
{
delay(10);
}
while(digitalRead(D3)==HIGH&&digitalRead(D5)==HIGH&&digitalRead(D6)==HIGH&&digitalRead(D7)==HIGH);
testsequence();
}


voidstartsequence()
{
/*
* 1 = green 0,255,0
* 2 = red 255,0,0
* 3 = blue 0,0,255
* 4 = yellow 255,255,0
*/
turns=turns+1;
num=random(1,5);
turnarray[turns]=num;
startprog=0;
strip.setPixelColor(0,0,0,0);
strip.setPixelColor(1,0,0,0);
strip.setPixelColor(2,0,0,0);
strip.setPixelColor(3,0,0,0);
strip.show();
delay(500);
playsequence();
}


Now before you start nagging: yes I know this software can very much be optimised. However in this form everyone might be able to read and understand what is happening so anyone can alter it to its own needs. Besides that, neither speed or memory space are a problem here.


Lets look at some parts of the software.

#include <Adafruit_NeoPixel.h>
#define PIXEL_PIN    D8
#define PIXEL_COUNT 4

As you can see you need the Adafruit Neopixel library. With the new Arduino IDE's it is a piece of cake to install new libraries so that should not give you any problems.

The setup starts with:
  randomSeed(analogRead(0));

Please make sure that this is indeed the first line in your program. The next lines will set the IO pins as input with a pull-up resistor. If you do not define the randomseed first you will not get a random figure.

In the start() part we wait till a key is pressed. Until that happens the Neopixels wil give a seqeunce of green pixels.
When a button is pressed all pixels will briefly be green and then one pixel will get the first random color choosen in the startsequence() routine.

When the first color has been choosen by startsequence() the program moves to playsequence() where the colors will be shown. The first time there will be just 1 color. The second time the array turnarray[] will have 2 values etc etc etc.

When the color sequence has been shown the program jumps to the testsequence() routine. There your keypresses will be chequed against the values in the array. If you pressed the correct buttons the variable turns is incremented and the program jumps to startsequence() again to pick the next random color.

This keeps repeating till you make an error.

If you make an error the testsequence() routine will notice. The routine will clear all variables and the program will start again.

Next step.

Attiny85

The ESP is a bit of overkill. No it is a lot of overkill !!!

Nowadays everything seems to be reeling around the ESP8266 and ESP32 but for small projects like this we seem to forget the Arduino and it's little broter the Attiny85. And the Attiny85 is well equipped for this project. So let's exchange the ESP8266 for an Attiny85. This microcontroller has 5 digital I/O pins and works at 8Mhz which is sufficient. 




As you can see the breadboard setup is not much different. However there is one extra feature which gives this setup an extra feature: it works on batteries !! 



So for the end result we can choose wether we want to power it using a power bank or plain AA or even AAA batteries. I was able to play Simon in this setup for several hours on just AAA batteries.
 

The software alterations are also minimal. In the program just replace the IO pin references to the Attiny references:

D3  ==> 0
D5  ==> 1
D6  ==> 2
D7  ==> 3
D8  ==> 4

Thats all !!!

How to play Simon.

A) the program starts by sequencing all neopixels in green till you press a button
B) If a button is pressed the Neopixels will all briefly light up green then dim
C) A random color is choosen
D) The program waits till you press the right button
E) When the right color button is chosen the program picks another random
color and will display the first and second after eachtother
F) The program then waits till you press in sequence the right color buttons
G) If the right sequence has been pushed the program loops back to step E and another color is added etc etc etc
H) if you pressed the wrong color button all Neopixels will flash red for several times and the program restarts.

Next stop: Stripboard layout and casing

Till then
Have fun

Luc Volders

Simon part 2

$
0
0
For an index to all my stories click this text.

This story shows how I finished my version of the Simon Game which I described in the story you can re-read here: https://lucstechblog.blogspot.com/2018/12/simon.html

Now the game setup was finished, meaning hardware and software worked without any bugs, I started thinking about a casing for the game. I did not want to leave it as a bread-board prototype as my girlfriend liked the game and wanted to play it more often.

First step was to put all the hardware on stripboard. Well that was easy as the schematics are not complicated. Fritzing was used to document all the parts.You can find Fritzing here: http://fritzing.org/home/
I used two pieces of stripboard.




The first was used to put the buttons on.
Make sure you cut the strips below the buttos so you will have independend contacts and no short-circuit.

The second piece of stripboard was used to put the Attiny85 on as well as the resistor and capacitor for the neopixels.




Here also make sure you cut the strips below the Attiny 85 to prevent short-circuit.
Also be aware that you are looking at the top of the stripboard, being the component side. The actual strips are on the bottom side.

The neopixels were just soldered as a string with some wires between them like I showed in the Neopixel intro which you can re-read here: http://lucstechblog.blogspot.com/2015/10/neopixels-ws2812-intro.html

When the hardware was finished I could start drawing my casing. I am using Tinkercad for this . Tinkercad is outstanding for simple designs. And as I am no industrial designer my designs generally are simple. You can find Tinkercad here: https://www.tinkercad.com/#/


 

I started with a casing for the batteries. I power my Simon game from 3 AA batteries as they will last a long time. The Attiny85 does not consume much and the Neopixels are never lit all at the same time. The box is printed and the contacts are made from bended paperclips.




Next a simple box as casing that would fit the batteries and the board with the Attiny85.



In real world it looks like the picture above.



And last the lid for the box. I added an on-off switch in the power line. I just had to do some minor filing before all fitted.



And here is the end-result.

The STL files (to print your own casing) can be found on github with this link:

https://github.com/Lucvolders/Simon

Simon version 2 ???

By popular demand (meaning pressure by girlfriend) I am thinking about building version 2. This will have a speaker that will play a tone when a color is shown (or the button is pressed) and a display (read Oled .96 inch) for keeping the score. But please do not hold your breath while waiting for it.

Besides that it will be more expensive to build due to the screen and the use of a bigger microcontroller as we will need more I/O pins.



This was the last post for this year. I whish you all a happy christmass and hope to welcome you all again in good health in the new year.

Till next time
and don't waste all your time playing ;)

Luc Volders

ESP - ESP Communication with WGET and MSGBRACNH

$
0
0
For a complete index of all my stories click this text

The general purpose of this story is a way to send data directly between ESP-8266's. So without the need of sending the data to a computer and having an other ESP retrieving it. No just direct communication between ESP's
There are many ways of communication between wifi devices. UDP is one way. And here I present you with another way.

The general line of this story may sound familiar because I have used this method before for communicating between an Android Phone/Tablet and an ESP8266

Send and Receive data with WGET and MSGBRANCH


Despite the fact that many people loathe it I am going to use my favorite ESP8266 devellopment environment for this tutorial: ESP-Basic.

ESP-Basic is very easy to use, interactive and has loads of drivers build in. Therefore it is very easy and fast to build IOT project's with it. It also is stable as I have made a thermometer with the softawre in ESP-Basic that is running non-stop from July 2017 without any flaws.

For an introduction and tutorial on ESP-Basic click here.
http://lucstechblog.blogspot.nl/2017/03/back-to-basic-basic-language-on-esp8266.html

Sending data

To send data we will use the wget command. The program is actually very simple.


' =================================  
' msg send/receive with msgbrach
' and wget
' sending routine
' Luc Volders
' http://lucstechblog.blogspot.nl/
' =================================
cls
counter = 0
button "send it" , [sendit]
wait  
 
[sendit]   
counter = counter + 1
tosend = "192.168.1.80/msg?data=there-you-are-"
tosend = tosend & str(counter)
wget(tosend)
wait


Make sure you replace the IP number with the number your receiving ESP has. You can find the IP numbers of your ESP in your router.

I have used a variable with the name counter that is incremented every time the button is pressed. The variable is then transformed into a string as we can only send strings. The sentence that is actually send is:

there-you-are-X

With X being the number of times you clicked the button.

You might wonder why the "-"'s are placed between words. This is because this way of sending data does not send anything after a space. So I replaced all spaces with "-".

The important part is:

tosend = "192.168.1.80/msg?data=there-you-are-"

The receiving program will look for the phrase "data=" and knows that what follows this phrase is the data you are looking for. You can replace "data=" with anything you like but remember to alter it in the same phrase in the receiving program.

Receiving data

The receiving part is run on a second ESP. Obvious as we want to communicate between two or more ESP's.

' =================================  
' msg send/receive with msgbrach
' and wget
' receive routine
' Luc Volders
' http://lucstechblog.blogspot.nl/
' =================================
cls
receiveddata = ""
memclear
msgbranch [getit]
textbox receiveddata
wait 
 
 [getit]  
receiveddata = msgget("data")
receiveddata = replace(receiveddata , "-" , "")
wait


It can't get any simpler as this.
The program waits till it gets some data and then jump to the [getit] routine.

receiveddata = replace(receiveddata , "-" , "")

This is the only tricky part in the whole program. This line makes sure that the "-" are transferred back into spaces. And then the vriable is placed in the textbox on the screen.

That's all folks.

Check, check !!!

Just like sending/receiving data with UDP there is a problem here. You can never be sure wether the data you are sending is actually received by the other ESP. We need a check for that.

' =================================  
' msg send/receive with msgbrach
' and wget
' sending routine with feedback
' Luc Volders
' http://lucstechblog.blogspot.nl/
' =================================
cls
counter = 0
returntext = ""
msgbranch [check]
button "send it" , [sendit]
wprint "<br><br>"
wprint "returned text "
textbox returntext
wait 
 
 [sendit]   
counter = counter + 1
tosend = "192.168.1.80/msg?data=there-you-are-"
tosend = tosend & "00"& str(counter)
a = wget(tosend)
wait 
 
 [check]  
returntext = msgget("back")
returntext = right(returntext,3)
wait


This is the sending program which has some extra routines to check wether the sended data is received well at the other side.

The program has the [sendit] button and accompanying routine for sending the data which is basically the same as in the previous send program.

The program also has a message-branch routine called [check]. What it does is basically wait till it receives some information from the other ESP8266. Whatever is received back is placed in the textbox on the screen. In this example I just take the last 3 received characters which will be the number from the variable counter but received back from the other ESP.

' ====================================  
' msg send/receive with msgbrach
' and wget
' receive routine with sending feedack
' Luc Volders
' http://lucstechblog.blogspot.nl/
' ====================================

cls
receiveddata = ""

memclear
msgbranch [getit]
textbox receiveddata
wait

[getit]
receiveddata = msgget("data")
receiveddata = replace(receiveddata , "-" , "")

'send confirmation
tosend = "192.168.1.75/msg?back="
reccounter = right(receiveddata, 3)
tosend = tosend & reccounter
a=wget(tosend)

wait


The program for the receiving ESP is the same as the previous receive program except for some lines in the [getit] routine.

After we received the data and the "-"are replaced for spaces we are going to send back what we just received. Not everything though. We send the last 3 characters, which is the received number, back to the ESP that originally send it.
Make sure you replace the IP number with the number from the other ESP.

The sequence

When you click the button on ESP 1 the text "there-you-are-001" is send to ESP 2.

ESP 2 will filter the "-" out and sets the received text "there you are 001" in the textbox on the screen.

ESP 2 will then take the last 3 charactes of the received text "001" and sends them back to ESP 1

ESP 1 will receive the number and put it in the textbox on it's screen.

When you click the button on ESP 1 again the counter is incremented with 1 and the sequence repeats

So to actually check if the sended information is indeed received well you only have to compare the value of the counter with the value that is received back.


And this is how that looks on your screen. As you can see I opened two browser windows each pointing to the right IP adress so I have both the sending and receiving screen open and can check if everythings goes as expected.

Expansion

This is a simple example which can easily be expanded to send all kinds of sensor data like temperature, color codes, switch settings etc etc etc. Use your imagination.

Android, IFTTT and Google Home

Often we not only want to send data between ESP's but also between your Android Phone or Tablet and an ESP8266. Well like said in the intro of this story we have done that before.

Just look at these stories:

http://lucstechblog.blogspot.nl/2018/02/esp-voice-control-with-esp-basic.html

http://lucstechblog.blogspot.nl/2017/05/google-home-and-esp8266.html

In the second story we have Google Home triggering IFTTT and IFTTT will send a similar message to the receiving ESP. Adapt it to your own needs and you will have a plethora of possibillities.

Till next time
Have fun

Luc Volders

Fritzing projects duplicated easy

$
0
0
For a complete index of all my stories press this text

Sometimes you use a tool for many years and suddenly you become aware of a feature that you never knew. Well that happened just to me.

If you are a faithfull reader of this blog you will have seen numerous schematics, breadboard layouts and stripboard layouts and they were all made with Fritzing. Fritzing is one of those valuable free tools that you really should check out. It is available from the Fritzing webpage where you can also find all kinds of projects: http://fritzing.org/home/

And after years of using Fritzing I just came after a feature I have never seen before and it is worth having a look.




Above you can see a breadboard layout made with Fritzing. It is from my weblog entry about controlling a led strip over wifi. You can find that entry here:
https://lucstechblog.blogspot.com/2018/04/rgb-strip-control-over-wifi-part-2.html

Now suppose you want to rebuild this layout yourself. This would give you no trouble bcause it is just a simple layout. For the sake of explanation let's do it.

What you would do is look at the screen and then place the components on the breadboard. Right ???

Yes but there is an easier way.
Load the design in Fritzing, chose print and just print it out.





After printing cut the picture with scissors and place it on the breadboard and just place the components where the picture displays them. The print Fritzing produces is real size !!!

For a simple layout like the one above this is overkill but look at the next layout.



Here things get a bit more complicated and then this will help you a lot !!!


As you can see the prints are really real size and it is easy to put the wires where they need to go. Just push them through the paper into the breadboard.

A simple trick that makes tinkering a lot easier.

However it gets better.


Look at this stripboard setup. It is from my article about controlling a led strip over wifi using an ESP-01.


If you want to reproduce it. Just print the stripboard layout from fritzing. Put it on a stripboard and place the components where the print shows them.


Just remember that with the stripboard version you need to place the print on the side where the components must be placed, not on the side where you solder the components !!!

Easy and efficient. Takes away a lot of work !!!

Till next time
Have fun.

Luc Volders

Remote controlled car with Wifi

$
0
0
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|<bodystyle="background-color:powderblue;">|
wprint|<H1><spanstyle="color:

red;"
>|
wprint"Motor Control<br>"
wprint"By Luc Volders"
wprint"</H1>"
wprint"Motor speed"
slidermotone,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

Simple IFTTT webhook test

$
0
0
 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

Image Recognition for free !!!

$
0
0
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.phpand 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

UDP Communication part 1

$
0
0
For a complete index of all my stories click this text

There are times / circumstances that you want to have two (or more) ESP8266's communicating with eachother. For example you put an alarm on a door or window attached to an ESP8266. When the door or window is opened it will send a message.

Another example is an ESP8266 attached to a Dallas DS18B20 thermometer that sends an alarm-message when a critical temperature is reached in a server room.
Or you just want a simple alarm when someone in the kitchen puts his hand in the cookie jar. Attach a vibration sensor to an ESP8266 and have it send a message.

That message can be send into the cloud to IFTTT, Thingspeak, IOTtweet or Dweet or whatever. For you to see the alarm you will need to have access to a PC or at least a smart phone. Now suppose you do not have that always at hand and you want just a led to blink or a buzzer to alarm you. Wouldn't it just be nice to have the alarm-esp send directly a message to another ESP that blinks a led or sounds a buzzer when it receives the alarm message.

And actually you can. You can have two or more ESP-8266's communicate with eachother without the need of a server or cloud-based service. The communication method is called UDP.

UDP communication

UDP stands for User Datagram Protocol. It is one of the basic communication protocols of the internet. UDP is designed for fast communication between two devices. The reason why it is not always used is that there is no guarantee that the receving device actually has received the message. Another protocol called TCP actually waits till the package is received. And when it is not it will re-send the package until it is acknowledged that the packet is received. UDP just sends the package and that's it.

The good part is that we can actually write or own safety protocol to make sure the packages have been send and received. I'll show you how to do that later on in the story.

The first hardware setup

I'll start easy with 2 ESP8266's. I use the NodeMCU version as that has a connection for USB programming and power supply. You can use the Wemos version to and you could even use the ESP-01.



The first ESP is just powered and nothing further. The seond ESP has a led attached to D7

The first program

My apologies for the diehard c-coders and Arduino afficionado's I am going to do this in my favoritie rapid devellopment environment: ESP-Basic. If you are not familiar with ESP-Basic I urge you to read this introduction story which will help you setup your ESP and help you start programming in no-time:
http://lucstechblog.blogspot.nl/2017/03/back-to-basic-basic-language-on-esp8266.html

Let us start with sending an UDP message.

The first thing to do in ESP-Basic is to tell the interpreter on which wifi-port the UDP communication is going to take place. Port 80 is normally used for HTML communication. UDP is generally done on port 5001. However feel free to use another port.

The second thing is to actually send the message. However you need to know to which IP adress you are going to send the message.



So open your router and look for the two ESP's. As you can see in my Zyxell router they are found with the names ESP_XXXXX. Clicking on these icons reveals their IP'adresses.

So now we have the information compleet we have the basic information to send UDP messages. In ESP-BASIC we just need two lines to send a message:

udpbegin 5100

udpwrite "192.168.1.78", 5001, "on"


This will send the message "on" over port 5001 to IP-adress 192.18.1.78.
You will need to replace the IP adress with your own.

Receiving the message on the other ESP-8266 is a bit more complicated.

First we need to start udp communication by defining a port over which the messages are send. We can use the same port or another one. The used port is for this particular ESP. I used the same portnumber:

udpbegin 5001

Now we need to tell the ESP what to do when a message is received. We need to do that by making a udpbranch which is basically a jump to a subroutine. You can name that routine anything you like. I called it udp.received

udpbranch [udp.received]

Now we need to build the actual routine that acts when a message is received:

[udp.received]
rec = udpread()

if rec = "on" then
  io(po,d7,1)
endif

if rec = "off" then
  io(po,d7,0)
endif

return


This is straightforward.
We declare a variable called rec (abbrevated from received) and put whatever is received over udp in it.

Next we test if the message received was "on" or "off" and accordingly set IO port d7 on or off, resulting in the led getting on or off.


'UDPsenddatademo
'writtenbyLucVolders
'http://lucstechblog.blogspot.nl/

wprint|<h1style="text-align:center;">LucVolders</br>UDP-Sender</h1>|
wprint"<br/><br/>"

udpbegin5100

button"on",[ledon]
wprint"<br>"
button"off",[ledoff]
wprint"<br>"
wprint"<br>"

wait

[ledon]
udpwrite"192.168.1.78",5001,"on"
wait

[ledoff]
udpwrite"192.168.1.78",5001,"off"
wait

So here is the complete listing for the Sending ESP


'udpreceiverdemo
'writtenbyLucVolders
'http://lucstechblog.blogspot.nl/

wprint|<h1style="text-align:center;">LucVolders</br>UDP-Receiver</h1>|
wprint"<br/><br/>"

udpbegin5001
udpbranch[udp.received]
wait

[udp.received]
rec=udpread()

ifrec="on"then
io(po,d7,1)
endif

ifrec="off"then
io(po,d7,0)
endif

return


And here the complete listing for the receiving ESP


This is how it will look on your screen when you open two browser windows each pointing to the IP number of one of the ESP's.


And this is how it looks in real life. On the right side is the sending NodeMCU and left the receiving one.

Just push the "on" button on the screen and on the receiving ESP the led will go on. Push the "off" button and on the receiving ESP the led will go off.
It's that easy to have a complete communication between two ESP8266's.

Experiment with this a bit before we go on to the next step.

Checking communication

As you have seen in the previous example the UDP message is send and we can only hope that it is received on the other side. So let's build a check into our receiving program.

wprint "who send information "
textbox remote
wprint "<br>"
wprint "what was received"
textbox whatreceived

To check what is received we can put a textboxes on our screen that will be filled with the information. I put 2 textboxes on the screen. The first displays what IP adress the information was received from, and the second displays what information was received.

Further there is a build-in option that just resends the received information back to the original sender. So in the receiver program we just have to put the following lines:

[udp.received]
rec = udpread()
remote = udpremote()
whatreceived = rec

if rec = "on" then
  io(po,d7,1)
endif

if rec = "off" then
  io(po,d7,0)
endif

udpreply rec

return

The variable rec is filled with the information that is received. The variable remote gets the IP adres from the sender. And the variable whatreceived is copied from the variable rec so the textbox gets filled.

The critical line here is:

udpreply rec

This simple line sends the information received back to the original sender !!!

Now we only have to alter the UDP-send program in such a way that it receives the information back as a check that everything went ok.

Basically this is the same as the lines in the receiver software. So in the UDP-send program we start with a test wether information was received over UDP (the reply from the receiver) :

udpbranch [udp.received]

That information will be displayed in a textbox:

wprint "information returned "
textbox repl

And we need to make the routine where the information which is send back is put into a variable:

[udp.received]
repl = udpread()
return

And that completes our UDP-send program with a feedback that checks if the send information is really received and processed.

'UDPsenddatademo
'writtenbyLucVolders
'http://lucstechblog.blogspot.nl/

wprint|<h1style="text-align:center;">LucVolders</br>UDP-Sender</h1>|
wprint"<br/><br/>"

udpbegin5100
udpbranch[udp.received]

button"on",[ledon]
wprint"<br>"
button"off",[ledoff]
wprint"<br>"
wprint"<br>"
wprint"information returned "
textboxrepl

wait

[ledon]
udpwrite"192.168.1.78",5001,"on"
wait

[ledoff]
udpwrite"192.168.1.78",5001,"off"
wait

[udp.received]
repl=udpread()
return

So above is the complete UDP-send program


'udpreceiverdemo
'writtenbyLucVolders
'http://lucstechblog.blogspot.nl/

wprint|<h1style="text-align:center;">LucVolders</br>UDP-Receiver</h1>|
wprint"<br/><br/>"

wprint"who send information "
textboxremote
wprint"<br>"
wprint"what was received"
textboxwhatreceived

udpbegin5001
udpbranch[udp.received]
wait

[udp.received]
rec=udpread()
remote = udpremote()
whatreceived=rec

ifrec="on"then
io(po,d7,1)
endif

ifrec="off"then
io(po,d7,0)
endif

udpreplyrec

return

And here is the complete UDP-receive program.



And this is what it looks on your screen.

Just open a separate browser window for each ESP and you can see what is happening.

There you have it.
A basic setup for you to play with and enough information to send data between two ESP's without the need for a cloud service or dedicated server.

Next time things get a bit more complicated as I am going to use 3 ESP's. One as a controller and two to switch lamps locally and remote.

Till next time
have fun

Luc Volders

UDP Communication Part II

$
0
0
For a complete index of all my stories click this text

This is the second installment on UDP communication with the ESP8266

In the first story you can read how to send information between two ESP's without using a cloud service or a dedicated local server (like MQTTT). You can read that story here: https://lucstechblog.blogspot.com/2019/03/udp-communication-part-1.html

In this story I am going to show you how to communicate between multiple ESP's. I am setting up 2 ESP's with a switch and 2 led's. And one ESP as the master controller with 4 leds and 2 switches.

The goal is that when you push a button on the master ESP a led will go on at one of the 'slave' ESP's. That ESP will send feedback which will set the control led on the master on. Each of the two 'slave' ESP's has a button. When that one is pressed the other led on the slave will go on and the other control led will go on at the master. This way you can locally see on the slave which led is ON and you can see on the master which leds on all the slaves are ON.

The hardware


The hardware setup is easy. A nodeMCU with two buttons and 4 led's as the master and two NodeMCU's each with 2 leds and a button as the slaves. The two slaves have identical hardware setups.




And this is how it looks in real life.

The software.





'UDPsenddatademo
'writtenbyLucVolders
'http://lucstechblog.blogspot.nl/

wprint|<h1style="text-align:center;">LucVolders</br>UDP-Sender</h1>|
wprint"<br/><br/>"

io(po,d5,0)
io(po,d6,0)
io(po,d7,0)
io(po,d8,0)

press=0
press2=0

interruptd3,[butpress]
interruptd4,[butpress2]

udpbegin5001
udpbranch[udp.received]

button"on1",[ledon1]
wprint"<br>"
button"off1",[ledoff1]
wprint"<br>"
button"on2",[ledon2]
wprint"<br>"
button"off2",[ledoff2]
wprint"<br>"
wprint"<br>"
wprint"information returned "
textboxrepl

wait

[ledon1]
udpwrite"192.168.1.78",5001,"on 1"
wait

[ledoff1]
udpwrite"192.168.1.78",5001,"off 1"
wait

[ledon2]
udpwrite"192.168.1.77",5001,"on 2"
wait

[ledoff2]
udpwrite"192.168.1.77",5001,"off 2"
wait

[udp.received]
repl=udpread()

ifrepl="on 1"then
io(po,d5,1)
endif
ifrepl="off 1"then
io(po,d5,0)
endif

ifrepl="on 1a"then
io(po,d6,1)
endif
ifrepl="off 1a"then
io(po,d6,0)
endif

ifrepl="on 2"then
io(po,d7,1)
endif
ifrepl="off 2"then
io(po,d7,0)
endif

ifrepl="on 2a"then
io(po,d8,1)
endif
ifrepl="off 2a"then
io(po,d8,0)
endif

return

[butpress]
press=1-press
ifpress=1then
udpwrite"192.168.1.78",5001,"on 1"
else
udpwrite"192.168.1.78",5001,"off 1"
endif
delay200
interruptd3,[butpress]
wait

[butpress2]
press2=1-press2
ifpress2=1then
udpwrite"192.168.1.77",5001,"on 2"
else
udpwrite"192.168.1.77",5001,"off 2"
endif
delay200
interruptd4,[butpress2]
wait


First let's have a look at the sending software for the master ESP.

io(po,d5,0)
io(po,d6,0)
io(po,d7,0)
io(po,d8,0)

I start with setting the led's off by writing 0 to the io ports.

interrupt d3, [butpress]
interrupt d4, [butpress2]

Next 2 interrupts are attached to the two buttons. The buttons are connected to D3 and D4 and I attached a 10k pull up resistor to them to make sure the level is HIGH when the button is not pressed.

udpbegin 5001
udpbranch [udp.received]

UDP is started on port 5001 and a branch is created that checks wether any information is coming from one of the 'slave' ESP's.

The last part of the setup draws 4 buttons on the screen and a textbox.

When one of the buttons on the screen is pressed an ON or OFF command is send to one of the 'slave' ESP's in the [ledon] and [ledoff] routines.

When one of the real buttons is pressed the interrupt is triggered.

[butpress]
press = 1 - press
if press = 1 then
  udpwrite "192.168.1.75", 5001, "on 1"
else
  udpwrite "192.168.1.75", 5001, "off 1"
endif
delay 200
interrupt d3,[butpress]
wait

If the interrupt is triggered like in the above snippet the variable press is inverted. When it is 1 it will become 0 and visa versa. that is what the line press = 1 - press does. Simply said the line makes the button a toggle button.



Make sure you replace the IP numbers with the IP numbers of your slave unnits.

The udpwrite line sends the information to the selected 'slave' and a small delay is initiated to make sure we wait till the information has been send. And then the interrupt is re-initiated.

When the information has been send the receiving ESP will send a confirmation back. That is received by the [udp.receive] routine. In this routine the incoming information is checked and as a visible feedback a led is set ON or OFF as confirmation.



'udpreceiverdemo
'writtenbyLucVolders
'http://lucstechblog.blogspot.nl/

io(po,d6,0)
io(po,d7,0)
press=0

wprint|<h1style="text-align:center;">LucVolders</br>UDP-Receiver</h1>|
wprint"<br/><br/>"

wprint"who send information "
textboxremote
wprint"<br>"
wprint"what was received"
textboxwhatreceived

interruptd3,[butpress]

udpbegin5001
udpbranch[udp.received]
wait

[udp.received]
rec=udpread()
remote = udpremote()
whatreceived=rec

ifrec="on 1"then
io(po,d7,1)
endif

ifrec="off 1"then
io(po,d7,0)
endif

udpreplyrec

return

[butpress]
press=1-press
ifpress=1then
io(po,d6,1)
udpwrite"192.168.1.79",5001,"on 1a"
else
io(po,d6,0)
udpwrite"192.168.1.79",5001,"off 1a"
endif
delay200
interruptd3,[butpress]
wait


The software for the 'slave' ESP's is almost identical although there is a bit less hardware: just 2 leds and one button.


[butpress]
press = 1 - press
if press = 1 then
  io(po,d6,1)
  udpwrite "192.168.1.77", 5001, "on 2a"
else
  io(po,d6,0)
  udpwrite "192.168.1.77", 5001, "off 2a"
endif
delay 200
interrupt d3,[butpress]
wait

The button press routine is the same one as the one on the 'master' ESP

The button works as a toggle button and sets the led on the breadboard on or off and sends the ON or OFF information to the 'master' ESP. The delay makes sure that nothing happens until the info is send and the interrupt is re-initiated.


And also note here that you have to replace the IP number with the IP number of your master ESP.

[udp.received]
rec = udpread()
remote = udpremote()
whatreceived = rec

if rec = "on 2" then
  io(po,d7,1)
endif

if rec = "off 2" then
  io(po,d7,0)
endif

udpreply rec

return

The [udp.received] routine listens for information coming from the 'master' ESP and accordingly sets the other led ON or OFF.


Summarising:

On the master ESP we have two buttons that send information to the slave ESP's which activates a led on the choosen slave. That is confirmed by a control led on the master. When the on the slave itself the button is pressed a signal is send to the master that sets the other led for that slave ON or OFF.

On the slave ESP we have two leds. One is controlled by the local button and when that is pressed a signal is send to the master which there triggers a control led. The second led on the slave is controlled by the master.

This way we have 2 led's on each slave. One is controlled locally and the other is controlled by the master ESP.

Ideas and sidenotes.

UDP communication is easy and you can have one master sending commands to lot's of slaves. A simple home automation controller can be build with this. That is practical But how about devellopping a game with this. Or replace your easter eggs with ESP's. Or build an intruder alarm with several slaves each with a PIR or radar module that can locate the intruder. A remote controlled car in which a master with some buttons sends speed and directions to a slave that controls the motor is another possibillity. Use your imagination.

The UDP communucation works with strings. So do not forget to covert your sensor values to strings when sending them over UDP.

I bet you can build a project with this which I did not think off.

Till next time
Have fun

Luc Volders

UDP Communication part III ESP to Raspberry

$
0
0
For an index of all my stories click this text

This is the third installment about UDP Communication between wifi enabled devices.

In the first two stories I showed you how to send data using UDP between two ESP's. Re-read that story here:http://lucstechblog.blogspot.com/2019/03/udp-communication-part-1.html
The second story showed you how to send data between multiple ESP's. Re-read that story here: http://lucstechblog.blogspot.com/2019/03/udp-communication-part-ii.html
Both stories showed you also a way to check wether the data was really received. And in both stories the communication was direct between the ESP's without a server or a cloud service for the communication.

It is easy to imagine that you need something more powerfull to process all data that is send between ESP's. Suppose that you want to build a home automation system with a central computer that processes all data. An ESP would probably not be sufficient to control all processes.

In comes the Raspberry Pi.






You could even use the humble Pi Zero for this purpose. The Pi Zero itself is dead-cheap (around 5 euro / 5 USD) and a complete setup with an SD card, powersupply and USB-Ethernet adapter will set you back below 25 euro/USD with some carefull sourcing. This means that for about 25 euro/dollar you can have a full blown Linux computer that controlls your automation processes.

If you want more processing power the Raspberry Pi 3 or 3B offers that for around 35 Euro / USD and offers the advantage that you do not need an USB ethernet adapter due to its build in Ethernet.

Besides that the Pi is easily programmable with Python and loads of drivers and documentation are readily available.

ESP-hardware

For the testing purpose you can use the setup from the previous stories. I am just using a plain ESP-8266 (NodeMCU) with no attached hardware. The test software puts two buttons on a web-page and a slider. That is all.

You can modify the software easily to send all kinds of data like button readings, temperature, motion etc etc etc.

Finding IP adresses.

To get this to work you will need to know the IP adresses for the Raspberry and the ESP8266. The Raspberry's IP adress can be looked up in the Raspberry itself as I'll show you later on in this story.

The IP adress of the ESP8266 is a bit trickier to find. You will have to get into your router to find it.

In my router there is a page that looks like this:
 

In the lower section you can see all the wired devices. And hey there is the Raspberry. So you can also use this to find the Raspberry's IP adress.
In the upper section you can see two devices. The first is my ESP's thermometer and the second is the ESP we are looking for.


The ESP side of the software.

I am using almost the same software as I used in the first and second story. I just added a slider. The program is written in ESP-Basic my favorite rapid devellopment environment.



' UDP send data with feedback demo  
' written by Luc Volders
' http://lucstechblog.blogspot.nl/

wprint |<h1 style="text-align:center;">Luc Volders</br>UDP-Sender</h1>|
wprint "<br/><br/>"
olddimmer = 0
udpbegin 5001

button "on", [on]
wprint "<br>"
button "off", [off]
wprint "<br>"
wprint "<br>"
slider dimmer ,0,255
wprint "<br>"
wprint "<br>"
textbox rec
timer 1000, [sendslider]

udpbranch [udp.received]

wait

[on]
udpwrite "192.168.1.78", 5001, "on"
wait

[off]
udpwrite "192.168.1.78", 5001, "off"
wait

[udp.received]
rec = udpread()
return

[sendslider]
if dimmer <> olddimmer then
udpwrite "192.168.1.78", 5001, "slider "& "00"& str(dimmer)
olddimmer = dimmer
endif
wait



Let's have a brief look at the program.

The program starts with the declaration of the variables and the building of the webpage with 2 buttons and a slider.

timer 1000, [sendslider]

udpbranch [udp.received]

These two statements are important in this section. The first (timer) jumps to a routine that makes sure that every second (timer 1000) there is a test wether the slider has changed.
The second statement makes sure that a routine is called that updates the textbox on the screen which is a check wether the UDP command is actually received by the Raspberry.

[on]
udpwrite "192.168.1.78", 5001, "on"
wait

This simply sends the "on" command to the Raspberry. You will need to alter the IP adress in the IP adress of the Raspberry.
Almost the same routine is made for the "off" command.

[sendslider]
if dimmer <> olddimmer then
udpwrite "192.168.1.78", 5001, "slider "& "00"& str(dimmer)
olddimmer = dimmer
endif
wait

This routine sends the slider value to the Raspberry. Again exchange the IP adress for the adress of your own Raspberry.

if dimmer <> olddimmer then
olddimmer = dimmer

This is a clever mechanism in this routine that checks wether the value of the dimmer has altered. When it has been altered the new value is send to the Raspberry. There is no need to send the value if it is not altered.

This is just a framework. Adjust it to your own needs for sending the values you want to send to your Raspberry.


And this is how it looks on your screen. 

As you can see the textbox shows the information (slider 00104) that has been send to the Raspberry and which the Raspberry has send back as a check.


The Raspberry side of the software.

First point your mouse at the top-right side off the Raspberry screen. There is the Panel item for Ehernet.




Click on it and it will reveal the Raspberry's IP adress. Like described earlier you can also find the IP adress in your router. You can fill that in, in the ESP-Basic program described above.

On the following web-page you can find all documentation for UDP communication with Python.
https://wiki.python.org/moin/UdpCommunication#Receiving



I am still using the Geany editorinstead of the Thonny editor. Thonny is preferred by the Raspberry Organisation so I should switch. However Geany still accepts Python 2 code which Thonny refuses.

 

importsocket

UDP_IP="192.168.1.78"# Pi's own IP-Adres
UDP_RETURN="192.168.1.79"# IP adres from sender
UDP_PORT=5001

sock=socket.socket(socket.AF_INET,# Internet
socket.SOCK_DGRAM)# UDP
sock.bind((UDP_IP,UDP_PORT))

whileTrue:
data,addr=sock.recvfrom(1024)# buffer size is 1024 bytes
print"received message:",data
sock.sendto(data,(UDP_RETURN,UDP_PORT))

I just copied the UDP receive program from that page and made some alterations.

import socket

This line speaks for itself. It just imports the UDP library.

UDP_IP = "192.168.1.78" # Pi's own IP-Adres
UDP_RETURN = "192.168.1.79" # IP adres from sender
UDP_PORT = 5001

UDP_IP is the Raspberry's own IP adress which you just found.
UDP_RETURN is the IP adress of the ESP8266. You can find that in your router as described before.

while True:
    data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
    print "received message:", data
    sock.sendto(data, (UDP_RETURN, UDP_PORT))

This is where all the magic happens.

while True:

This simple line makes sure that the program keeps running indefinitely

data, addr = sock.recvfrom(1024)

This line sets the variable data to the information that is received over UDP.

sock.sendto(data, (UDP_RETURN, UDP_PORT))

And this line sends the confirmation back to the ESP so we know for sure that the data has been received.

The program is written in Python 2. If you want to use Python 3 you will have to change this line:

    print "received message:", data

Into this:

    print ("received message:", data)



Here you can see the output. I have changed the slider a few times and pushed the ON and OFF buttons on the screen. The information is received by the Raspberry and on the ESP webpage you can check that the information indeed has been send back as a confirmation.


Expansion

Like I said this is just a framework. To make this usefull you would need to expand the ESPBasic program as well as the Raspberry's Python program.

On the ESP side there should be some action when the command is not received back, or mutilated. You could send it again. In any case you should take action.
You could also send an identification with the data so the Raspberry woud know from which ESP the data is coming.

On the Raspberry side we should make a nice graphical page with all the received information.

sock.sendto(data, (UDP_RETURN, UDP_PORT))

This line can easily be altered to send all kinds of information to multiple ESP's. Just alter the data and the UDP_RETURN in the right ESP's IP adress and that's it.

Well that sums it up. It was easier to get the two platforms to talk to eachother as I had expected. My setup worked immediately.

Rests only one thing: Getting my Android Phone to talk over UDP to the ESP and to the Raspberry.

For now a good setup to get things starting.

Till next time
Have fun

Luc Volders

UDP Communication Part IV Android to ESP

$
0
0
For an index of all my stories click this text

This is the last installment about UDP communication between Wifi Devices.

In the first story I showed you how to send data from one ESP to another. You can re-read that story here: https://lucstechblog.blogspot.com/2019/03/udp-communication-part-1.html

The second story showed how you could send information from one ESP8266 to many ESP8266's. Re-read that story here: https://lucstechblog.blogspot.com/2019/03/udp-communication-part-ii.html

The third story was a tutorial on how to send data from an ESP8266 to a Raspberry Pi and the other way round. You can read that part here: https://lucstechblog.blogspot.com/2019/03/udp-communication-part-iii-esp-to.html

In this last story I am going to show you how to send data using the UDP protocol from your Android Phone/Tablet to an ESP8266 or from an ESP8266 to your Phone or Tablet.

This completes the series and with all this information you will be able to send and recive data from almost any device in your home to any other device. That's the basic setup for any home automation system.

APP Inventor.

For writing APP's for my Android phone and tablet I use MIT's App Inventor. App inventor is based on Blockly just like the Arduino programming Environment Ardublock, and Microsofts Makecode for the Micro:bit. And maybe you recognise the method from Scratch on the Raspberry. All are based on the same block building programming environment which originated at Google.
It started as an easy tool for children to learn programming but App Inventor has grown to be a full programming environment which makes it possible to build professional APP's that can even be sold on Google's Play store.

As almost everybody has a mobile phone nowadays and certainly the readers of this blog must have one I urge you to have a look at App inventor and start building your own APP's.

UDP communication in App Inventor

App Inventor has a lot of features for building your own App's however UDP communication isn't one of them. Fortunately a lot off programmers are building extensions for App Inventor. You can compare extensions with libraries for the Arduino or ESP.

A Software Engineer with the name Taifun has written some excellent extensions and has a webpage that lists not only his extensions but all together a few hundred available extensions. Although I must say some (minority fortunately) are not free.

One of the extensions is an UDP extension. You can find it on the extensions page from Taifun https://puravidaapps.com/extensions.php which links to the actual page where the extension can be downloaded and that is this page: https://community.thunkable.com/t/free-udp-client-extension/5831

Using extensions in App Inventor

There are some excellent tutorials on using extensions in App Inventor on the web. So I will be brief about this.

Start with downloading the UDP extension to your computer from this web-page: https://community.thunkable.com/t/free-udp-client-extension/5831

Now start App inventor and start a new project.



On the left side of the designer screen at the bottom you can see the extension entry. Click on that and you can upload the extension into App Inventor. That's all. You are now ready to use it.

ESP program to receive UDP packages

For testing sending UDP data from your Android Phone we first need something that can receive UDP packages. In the previous stories I showed you how to send data between ESP8266's using UDP communication. I wrote some ESP-Basic programs for that. I am using a small ESP-Basic program here again so we can send data from our phone to an ESP8266


For those of you who have no idea what ESP-Basic is about just look at this tutorial:
http://lucstechblog.blogspot.nl/2017/03/back-to-basic-basic-language-on-esp8266.html



' UDP receive  
' written by Luc Volders
' http://lucstechblog.blogspot.nl/

wprint |<h1 style="text-align:center;">Luc Volders</br>UDP-Receive</h1>|
wprint "<br/><br/>"

udpbegin 5001
textbox received
udpbranch [getudp]

wait

[getudp]
received = udpread()
return


Nothing special. The program puts a textbox on the webpage in which the received data will be displayed. Next the program just waits till a UDP package is received. That's all.

App Inventor program to send data with UDP

Visit App Inventor, log in and create a new project. Start with importing the UDP extension like mentioned above. Now drag the extension to your design screen and a non-visible component will be inserted in your APP just like the screen below shows.



Put a textlabel on the screen and set the width to full. Give it a large font, I used 14 and set the text color to red.
Put a button below the label.

Now switch to the blocks section.



As you can see the program is not very complicated.

Start with initialising a variable called var1

Everytime the button is clicked the variable is incremented with 1 and the UDP extension is activated. The IP adress should be altered in the IP adress of your ESP8266 to which you are sending.
The port is 5001 but you can use another port.
The message is a concatinated string consisting of the word "test " and the value of the variable var1. The message is then also put into the label so we can see on the screen of the phone what is actually send.



After you start the App with the live function of App Inventor and start the ESP-Basic program you can see the text in the textfield of the ESP8266 change every time you press the send button on your phone.

I leave it for now to your creativity to alter the commands you are going to send to the ESP. You can use it to put led's on or off, or switch relays on or off, or send sensor or location data from your phone to an ESP8266.

Next step: receive UDP data on your phone.

ESP program to send UDP packages

Now we know how to send data with UDP from the Phone to your ESP it is time to look at how we can receive data with UDP on your phone.

To achieve that we need something that can send data using UDP. Again we are going to use our faithfull ESP8266 for this using a simple ESP-Basic program just like we used in the previous stories in this series.

' UDP send  
' written by Luc Volders
' http://lucstechblog.blogspot.nl/

wprint |<h1 style="text-align:center;">Luc Volders</br>UDP-Send</h1>|
wprint "<br/><br/>"

count = 0

udpbegin 5001
button "send" , [sendudp]

wait

[sendudp]
count = count + 1
countst = str(count)
mess = "test numero "& countst
udpwrite "192.168.1.70", 5001, mess
print mess
wait

Again a very simple program.
A button is put on the webpage and every time you press that button the variable 'count' is incremented. Then a message is created consisting in the text "test numero " and the variable countst which is the string version of 'count' transformed by this statement: countst = str(count). This is necessarry because UDP can only send strings.

udpwrite "192.168.1.70", 5001, mess

This line sends the message using port 5001 to the selected IP adress. Make sure you exchange the IP adress for the IP adress of your phone.



This is how it looks on your computer screen when you have pressed the button a few times.

App inventor program to receive data over UDP

Start a new project in App-Inventor and design the screen as follows:



Not much to it.

We put the UDP extension in the screen and then it appears as an invisible component in the APP. and there is just a label on the screen which uses a large font.

The last component is again an invisible component: clock



The program starts with initialising a global variable with the name var1

The next part is a test wether some information is received over UDP.
If that is the case the label's text is changed into the received information, and the received information is cleared.

Then there is a last block testing the clock.
The clocks timer is automatically set to 1 second. So every second this block is triggered. The variable var1 is incremented with 1 and a message consisting of the word 'test'and the value of the variable is send over UDP to the ESP.

Looking at the ESP's program you can see that there is no routine for receiving anything.
So why should you send something to the ESP.

Actually this is a flaw (bug) in the UDP extension.
For one reason or another you need to trigger UDP communication by sending something to the ESP. Mind you this is a bug in the UDP extension for Android because as you might recall (or re-read) such an action is not needed in ESP to ESP communication.



And this is what can bee seen on your phone's or tablet's screen after you pressed the button on the ESP several times.

Last step is to combine the send and receive part.

ESP program to send and receive UDP packages

We already covered this in the orevious stories when we had two (or more) ESP8266's communicating with eachother. So re-read these stories here XXXXXXXXXXXXXXXXXXX and here XXXXXXXXXXXXXX

' UDP send and receive  
' written by Luc Volders
' http://lucstechblog.blogspot.nl/

wprint |<h1 style="text-align:center;">Luc Volders</br>UDP-Send</h1>|
wprint "<br/><br/>"

count = 0

udpbegin 5001
udpbranch [udpreceive]

button "send" , [sendudp]
textbox receive
wait

[sendudp]
count = count + 1
countst = str(count)
mess = "test numero "& countst
udpwrite "192.168.1.67", 5001, mess
print mess
wait

[udpreceive]
receive = udpread()
if receive = "Just Luc" then
wprint "."
else
print receive
endif
return

Lets have a brief look at the program.

udpbegin 5001
udpbranch [udpreceive]

This sets the port on the ESP8266 to 5001 and jumps to the routine "udpreceive" when some data is received.

button "send" , [sendudp]
textbox receive

When the button is pushed the program jumps to the UDP "sendudp" routine. And the textbox is just there to display anything that is received by the "udpreceive" routine.

The "sendudp" is the same routine we used in the send-only program.

The "udpreceive" routine examins what is being received from your Android Phone or Tablet. If it receives the phrase "Just Luc" it prints a dot "." on the screen. Else it puts the information that is received in the variable receive which will put it in the textbox.

Why the test for "Just Luc" (you can and will alter this for something significant for yourself) you might ask.
Remember the clock function in the Android program that sends every second a UDP package. Well we just test what it sends and if that is "Just Luc"it is irellevant so it will just print a ".". Alter this to your own needs in your project.

Android Program to send and receive UDP packages.

The Design part is simple.



Just a Label which will display the received text. Give it a large font, and maybe a fancy color.
And a button that will send dome data when pushed.

The block section is also not too complicated. it is just a mix of the previous send and receive programs.



First part is when a UDP package is received. The program will put whatever is received into the Text Label.

The clock part will send a UDP package everytime it is activated (every second) and sends the phrase "Just Luc" which will be filtered out in the ESPBasic program as described above.

When you click the button the global variable var1 will be incremented in value and the text "Luc sends text number " with the global var1 will be send to the ESP.

Adapt the clock and the button program parts so they use the IP number of your ESP8266 instead the number I filled in.

That's all folks.

Real life sending and receiving.



This is what the Android program will look on your phone's or tablet's screen.
As you can see it has received two times a data package from the ESP8266



This is what the ESP8266's screen will show you.

Everytime "Just Luc" is received a dot is printed next to the line where the last information was printed.

When you push the send button the text "test numero x" is printed and everytime the ESP receives real data the line "Luc sends test number X" is printed.

Summarising

I do not expect you to use this to send simple text messages from one device to another. The described programs can easily be adapted to send sensor data like temperature, buttons, movement, color codes for leds, slider values for motors etc. etc. etc.

Next to that we have a complete communications suite now for sending data between ESP8266's, Raspberry Pi's and Android Phone's or Tablets.

I bet you can come up with something usefull for this. Do not hesitate to mail me about your projects.

Till next time.
Have fun

Luc Volders

Always knowing where you are

$
0
0
For an index to all my stories click this line

I'll let you in to a little scret. I am notorious known for getting lost.
Drop me somewhere in a city, walk a few meters and turn some corners and I will never find my way back. Visit some place with me and come back a few weeks later: I really can not find where it was.

So luckily for me there is navigation. And Google Maps.
And just when you think verything is fine and the world a sunny place I realise I have forgotten my navigation and disaster strikes.

What I have learnt to do is to orient upfront. Before I go somewhere I look at the place on Google maps. And then I got this fantastic idea to never get lost again.



Suppose you want to go to the National Monument at the Dam in Amsterdam.
First open your webbrowser and type the name of the place you want to visit.



A list is shown with things Google found but look at the top off the screen. There you can find maps. Click on that.



 You'll be presented a map of the environment of the place you have searched for.



And exactly in the middle you'll find a red pin right at the spot you are looking for.

This gave me this tremendous idea.

Would this work the other way round ????

If you can see the red pin, you know where you are !!!

So here is the solution.



Copy the picture of the pin. Enlarge it in a drawing program and print it. Glue it to a piece of carton and punch a small hole in it so you can hang it on a wire.



Or even better. 3D print it. I made a version with a suspension eye. I have several of them and never leave the door without one. There is one permanent in my car, and one hanging on my bicycle.

Hang it on your coat or shirt, depending on the outside temperature, and you never can get lost again cause you are were the pin is.

For convenience I will put one on my backpack when I am participating in the Walk of the World in july so the people walking behind me know where they are.



To print some for yourself  you can find the STL file on my github account:

https://github.com/Lucvolders/Googlepin

Till next time
Make fun of yourself

Luc Volders

P.s. Remember what day it is today.

Unlimited outputs for your microcontroller

$
0
0
For an index to all my stories click this text

Sometimes I have these strange / stupid / twisted ideas let me explain.

While thinking about a project I realised I would need a controller with many outputs. Let's first look at the options.

- An ESP-01 has 2 I/O ports and with a trick you can use 4.
- A NodeMCU has 11 I/O ports.
- An Attiny 85 has 5 I/O ports.
- An Arduino Uno has 19 ports.
- An Arduino Mega has 53 ports

The Arduino Mega would have been an option however it lacks Wifi.
I could use a NodeMCU with several 74HC595 shift registers which each would give me 8 ports. For 50 output ports I would need 7 of these which would make programming a complicated matter.

I then had a twisted Idea.

Look at the Neopixels. I can attach a whole bunch of Neopixels to any of the above mentioned controllers and set each led to any RGB color individually. If you want to know more about these small wonders read this story:
http://lucstechblog.blogspot.nl/2015/10/neopixels-ws2812-intro.html

Neopixels contain 3 leds and a controller chip in one casing. That controller chip is the WS2811.

The WS2811 can be daisy-chained almost endlessly and each of these chips has 3 output ports. Well, well. If these chips could be bought without the led's, that would make an almost endless supply of output ports for any controller. And they would be easy programmable. 


And best of all: Not only would they be able to set a led on or off, they would be able to use PWM to dim the leds, or control the speed of a motor, or control multiple ledstrips !!!

Tell me. Isn't that a strange / stupid / twisted idea ???

The WS2811 chips

Let me start with some general information.

A search on the website of my favorite Chinese supplier immediately showed that the WS2811 chips indeed can be bought without leds. So I immediately ordered a few. I got 10 pieces for about 3 euro but they are even cheaper now. For economics sake this boils down to (in my case) 10 cents for every extra output on your Microcontroller.





Mine where shipped as a strip of 5 pieces. They were not interconnected so there is soldering involved.




Each individual PCB on which the WS2811's are soldered is 15 x 15 mm.




 This picture clearly shows you the size of the PCB's compared to an ESP01.

As the WS2811's are put on individual PCB's you could break the strips up and put them apart if you needed more adressable outputs over a larger distance. Adafruit advises a maximum distance of about 1 meter between each WS2812 neopixel. However I read on a forum that someone had good results with WS2812's which were 6 meter apart.
All depends on the quality of the soldering, the used wires and the power supply. So testing is required  !!!!

Please be aware that the WS2811's have a common Anode.
Normally you will attach a led to a signal and to the GND. This is the other way round. The led is attached to an I/O port which must provide the GND and the other side is attached to the +5V. Don't forget the delimiting resistor.
We will see more of this later on.

You should wire the WS2811's the same as you would wire Neopixels. Attach a large capacitor (1000 microfahrad at 6.3 volts or higher) across the GND and + of the power supply. And connect a 300-500 ohm resistor between the pin of your microcontroller and the first Neopixel in line.

Soldering

As you can see the individual PCB's have a GND and 5V connection on either side so the can easily be daisy-chained. On one side is the DI (data-in) connection and on the other side the DO (data-out) connection. The DI of the first in line will be connected to your Micro Controller and the DO can be connected to the next DI. The same as you would do with Neopixels.

At the center of the PCB are 4 connections. R G B and +. Normally the LED's will be soldered here. That would be RGB leds with a common Anode (+).

I started with wiring the connections to the first PCB. I sacrified some Dupont wires for this experiment. This made it easy to connect the WS2811's to a breadboard.


Next I interconnected 2 PCB's by soldering small pieces of wire between them.


And the last step, was to solder wires to the RGB connections. For clarity I used red, green and blue wires.

First test

Just be patient and keep reading this web-log. Because in the next part I am going to show you how to use the WS2811's with an Arduino and an Attiny85 programmed in C++ and even with a BBC Microbit programmed both in Python and in Java. Best part Is that I am going to show you how to attach and control 2 ledstrips at the same time !!!

However for all my rapid testing and fast devellopment I use ESPBasic.  As all programming an testing is done in a web-browser it is the fastest way to test anything on an ESP. Read a tutorial on ESP-Basic here: http://lucstechblog.blogspot.nl/2017/03/back-to-basic-basic-language-on-esp8266.html

First look at the breadboard setup


And this is how it looks in real life. As you can see I have soldered wires to 2 of the WS2811's and attached leds with a delimiting resistor.

Further you can see there are two USB cables. The first one (the pink one) powers the Wemos D1 (NodeMCU) and the second one (the green one) powers the WS2811's. You can not feed all of this from your Wemos D1 or NodeMCU module. Thats why I attached two separate power supplies, both an ordinary 5Volt phone charger from the dollar shop.

Attach a multimeter across the blue connection of the first WS2811 and GND.

And use the following program:


neo(0,0,0,0)

wprint|<h1style="text-align:center;">LucVolders</br>WS2811Relay|
wprint"<br/><br/>"

button"<h3>Relay on</h3>",[relon]
button"<h3>Relay off</h3>",[reloff]

wait

[relon]
neo(0,0,0,255)
wait

[reloff]
neo(0,0,0,0)
wait

That's all


That small program looks like this on the screen of your computer / phone /tablet

Pressing the Relay-on button will give you 0 Volts and pressing the Relay off button will give you something like 0.9 volt. That brings us nowhere.

Now attach one end of the multimeter to +5V in stead of the GND and you will measure -5.22 volts when you press Relay-on and 0 volts when you press relay off. And that's what we need.

Like stated above the WS2811 have a common anode so the GND comes from the R,G,B side.

Attach a led with a 220 ohm resistor, and to make it go on and off, attach the Anode side of the led to +5 volt and the cathode side to the B wire of the first WS2811. Just press the button on the web-page to switch the led on and off.

As a side-note: Guess you know now why I use ESP-Basic so often as my favorite devellopment system. In just a few lines of code I have made a web-page with a button and control some WS-2811's

Next step is attaching a Relay.
This is easier. Just attach the power lines and attach the control line to the Blue line of the first WS2811.
Pressing the Relay on or Relay Off button will switch the realay accordingly on or off.
Modern relay modules have a common connection in the middle and a connection that closes when a GND signal is supplied and one that closes when a + signal is supplied.




So in this case the upper connection and the middle connection are closed when the button is not pressed and the value send to the WS2811 is 0. If a 1 (5volt) is send the middle connector is connected to the lower connector.

Something more complex.

I had 6 leds attached to the output lines of 2 WS2811's.

 
In order to test this setup I wrote a program in LUA.

 
The program builds a webpage and puts 12 buttons on it. Each led has 2 buttons: one for ON and one for OFF.

wifi.setmode(wifi.STATION)
wifi.sta.config("XXXXXXXX","YYYYYYY")
ws2812.writergb(3,string.char(0,255,0,0,255,0,0,255,0))
print(wifi.sta.getip())
led1=0
led2=0
led3=0
led4=0
led5=0
led6=0
tmr.delay(2000000)
ws2812.writergb(3,string.char(0,255,0,0,255,0,0,255,0))
srv=net.createServer(net.TCP)
srv:listen(80,function(conn)
conn:on("receive",function(client,request)
localbuf="";
local_,_,method,path,vars=string.find(request,"([A-Z]+) (.+)?(.+) HTTP");
if(method==nil)then
_,_,method,path=string.find(request,"([A-Z]+) (.+) HTTP");
end
local_GET={}
if(vars~=nil)then
fork,vinstring.gmatch(vars,"(%w+)=(%w+)&*")do
_GET[k]=v
end
end
buf=buf.."<h1>WS2811 Web Server</h1>";
buf=buf.."<h1>Made By Luc Volders</h1>";
buf=buf.."<p>Switch 1 <a href=\"?pin=ON1\"><button>ON</button></a>&nbsp;<a href=\"?pin=OFF1\"><button>OFF</button></a></p>";
buf=buf.."<p>Switch 2 <a href=\"?pin=ON2\"><button>ON</button></a>&nbsp;<a href=\"?pin=OFF2\"><button>OFF</button></a></p>";
buf=buf.."<p>Switch 3 <a href=\"?pin=ON3\"><button>ON</button></a>&nbsp;<a href=\"?pin=OFF3\"><button>OFF</button></a></p>";
buf=buf.."<p>Switch 4 <a href=\"?pin=ON4\"><button>ON</button></a>&nbsp;<a href=\"?pin=OFF4\"><button>OFF</button></a></p>";
buf=buf.."<p>Switch 5 <a href=\"?pin=ON5\"><button>ON</button></a>&nbsp;<a href=\"?pin=OFF5\"><button>OFF</button></a></p>";
buf=buf.."<p>Switch 6 <a href=\"?pin=ON6\"><button>ON</button></a>&nbsp;<a href=\"?pin=OFF6\"><button>OFF</button></a></p>";
local_on,_off="",""
if(_GET.pin=="ON1")then
led1=255
ws2812.writergb(3,string.char(led1,led2,led3,led4,led5,led6))
elseif(_GET.pin=="OFF1")then
led1=0
elseif(_GET.pin=="ON2")then
led2=255
elseif(_GET.pin=="OFF2")then
led2=0
elseif(_GET.pin=="ON3")then
led3=255
elseif(_GET.pin=="OFF3")then
led3=0
elseif(_GET.pin=="ON4")then
led4=255
elseif(_GET.pin=="OFF4")then
led4=0
elseif(_GET.pin=="ON5")then
led5=255
elseif(_GET.pin=="OFF5")then
led5=0
elseif(_GET.pin=="ON6")then
led6=255
elseif(_GET.pin=="OFF6")then
led6=0
end
ws2812.writergb(3,string.char(led1,led2,led3,led4,led5,led6))
client:send(buf);
client:close();
collectgarbage();
end)
end)

As you can see there is a lot more code involved to build a webpage with some buttons on in LUA.

A little explanation.

wifi.sta.config("XXXXXXXX","YYYYYYY")

Fill in your own routers credentials.

Next a network connection is established.

buf = buf.."<p>Switch 1 <a href=\"?pin=ON1\"><button>ON</button></a>&nbsp;<a href=\"?pin=OFF1\"><button>OFF</button></a></p>";

These lines put the name "Switch x"on the webpage and put two buttons after the name being ON and OFF and they will get connected to the variables ON1 and OFF1

        if(_GET.pin == "ON1")then
              led1 = 255
              ws2812.writergb(3, string.char(led1,led2,led3,led4,led5,led6))
        elseif(_GET.pin == "OFF1")then
              led1 = 0

In these lines is tested wether the button is pressed and the accompanying led is set ON of OFF by setting the variable led1 to 255 or 0. This is done for all 6 leds.

        ws2812.writergb(3, string.char(led1,led2,led3,led4,led5,led6))

The WS2811 DATA IN line is connected to D3 on the NodeMCU and the led's are set according the value that is in the varaibles led1 through led6


As you can see in the picture I replaced a led with a relay for testing and it worked just fine.

Even more complex

The next step was to write a program that produced a webpage with sliders which would set the intensity of the leds. I did that in Basic as I have (until now) no clue on how to put sliders easily on a webpage with Lua or C++ code.


The first program was very simple and had no fancy layout.

neo.setup(d8)
timer100,[change]

rgbval1=0
rgbval2=0
rgbval3=0
rgbval4=0
rgbval5=0
rgbval6=0

sliderrgbval1,0,255
wprint"<br>"
wprint"<br>"

sliderrgbval2,0,255
wprint"<br>"
wprint"<br>"

sliderrgbval3,0,255
wprint"<br>"
wprint"<br>"

sliderrgbval4,0,255
wprint"<br>"
wprint"<br>"

sliderrgbval5,0,255
wprint"<br>"
wprint"<br>"

sliderrgbval6,0,255
wprint"<br>"
wprint"<br>"

wait

[change]

neo(0,rgbval1,rgbval2,rgbval3)
neo(1,rgbval4,rgbval5,rgbval6)

wait

The code was equally simple.

De first WS28-11's Data-In line is connected to D8 on the NodeMCU. Then a timer is initiated that every 100 ms calls the routine [change]. In this routine the value of the sliders is send to the WS2811's which alter the intensity of the leds accordingly.


I wanted it to look a bit more fancy and that resulted in the above picture. Besides having a better looking web-page I decided to alter the last slider in an on-off button and like before I attached a relay in stead of the last led.


wprint"<!DOCTYPE html>"
wprint"<html>"
wprint"<head>"
wprint"<style>"
wprint"h1 {text-align:center;}"
wprint"</style>"
wprint"</head>"
wprint"<body>"
wprint|<bodystyle="background-color:Burlywood ;">|
wprint|<H1><spanstyle="color: Dodgerblue;">|
wprint"WS2811 Control"
wprint"<br>"
wprint"</H1>"

neo.setup(d8)
timer100,[change]

ws0102=0
ws0102=0
ws0103=0
ws0201=0
ws0202=0
ws0203=0

wprint"Control 1"
sliderws0101,0,255
wprint"<br>"
wprint"<br>"

wprint"Control 2"
sliderws0102,0,255
wprint"<br>"
wprint"<br>"

wprint"Control 3"
sliderws0103,0,255
wprint"<br>"
wprint"<br>"

wprint"Control 4"
sliderws0201,0,255
wprint"<br>"
wprint"<br>"

wprint"Control 5"
sliderws0202,0,255
wprint"<br>"
wprint"<br>"

'wprint "Control 6"
'slider ws0203, 0, 255
'wprint "<br>"
'wprint "<br>"

button"Lamp 01 ON",[lamp01on]
a="background-color:Gainsboro;"
a=a&"border: 3px solid black;"
a=a&"font-size: 22px;"
a=a&"font-weight: bold;"
a=a&"color: fuchsia ;"
a=a&"border-radius: 12px;"
cssidhtmlid(),a

button"Lamp 01 OFF",[lamp01off]
a="background-color:Gainsboro;"
a=a&"border: 3px solid black;"
a=a&"font-size: 22px;"
a=a&"font-weight: bold;"
a=a&"color: fuchsia ;"
a=a&"border-radius: 12px;"
cssidhtmlid(),a

wait

[change]

neo(0,ws0102,ws0101,ws0103)
neo(1,ws0202,ws0201,ws0203)

wait

[lamp01on]
ws0203=255
neo(1,ws0202,ws0201,ws0203)
wait

[lamp01off]
ws0203=0
neo(1,ws0202,ws0201,ws0203)
wait

Again the code speaks for itself and you can see how easy it is to build something fantastic in just a few lines with Basic.

This gives you enough to play with for a short time. Story number 2 coming up soon and like I said before that will show you how to use this with an Arduino (attiny85) and even a BBC Microbit. And I'll show you how to control multiple RGB led strips simultaneous using this method.

So till next time.
Have fun !!!

Luc Volders

Unlimited outputs for your microcontroller part II

$
0
0
For an index to all my stories click this line

In the previous part I showed you how to use WS2811 chips to create an almost unlimited number of PWM controlled outputs for an ESP8266. You can find that story here: http://lucstechblog.blogspot.com/2019/04/unlimited-outputs-for-your_12.html

The WS2811's are the chips that are build into the so called Neopixels. More detailed basic information about Neopixels can be found here:http://lucstechblog.blogspot.nl/2015/10/neopixels-ws2812-intro.html

So a Neopixel is basically a WS2811 and 3 leds build as a single chip. I am using just the WS2811's. As these can control 3 leds we should be able to use them as 3 outputs for a microcontroller. By daisy chaining them (just like we do with Neopixels) we can create an infinite number of outputs for any microcontroller. I did this succesfully with an ESP using LUA and Basic in Part 1 of this series.

Now let's see if we can use this technique also with an Arduino or an Attiny85.


Oh and by the way, with a little modification (mainly altering the pin number) you can use this also for the ESP8266 and ESP32 in Arduino language.

As both the Arduino and the Attiny85 have no Wifi capability it is more difficult to control them from the outside world. Control has to be taken care off with potentio meters or sensors. In the examples I am going to show here there is no outside control. The programs will control everything. For demo purposes this is sufficient and I am sure you find ways to incorporate this in your own projects.

Breadboard setup




As you can see the setup is almost the same as in the previous story. I attached the Arduino's ground and +5Volt to the ground and power rails of the rest of the circuit to power the complete setup. The Arduino's pin 6 is used to control the WS2811's through a 470 OHM resistor.
It is the same setup you would use to attach a bunch of Neopixels to your Arduino. As long as you use just a few WS2811's and leds youn can power the setup from your Arduino.

When you are going to attach more WS2811's use the seperate USB power supply for the WS2811's like discussed in part 1 in this series.


The Arduino program

The program is written in C++ the standard programming environment for the Arduino. Lets look at the code:



// WS2811 Test by Luc Volders
// This program sets multiple leds on and off in a sequence

#include <Adafruit_NeoPixel.h>
#include <avr/power.h>

// Which pin on the Arduino is connected to the NeoPixels?
#define PIN 6

// How many NeoPixels are attached to the Arduino?
#define NUMPIXELS 2

// When we setup the NeoPixel library, we tell it how many pixels, and which pin to use to send signals.
Adafruit_NeoPixelpixels=Adafruit_NeoPixel(NUMPIXELS,PIN,NEO_GRB+NEO_KHZ800);

intdelayval=500;// delay for half a second

voidsetup()
{
pixels.begin();// This initializes the NeoPixel library.
}

voidloop()
{
pixels.setPixelColor(0,pixels.Color(00,0255,255));
pixels.setPixelColor(1,pixels.Color(00,0255,255));
pixels.show();
delay(delayval);
pixels.setPixelColor(0,pixels.Color(0255,0,0));
pixels.setPixelColor(1,pixels.Color(0255,0,0));
pixels.show();
delay(delayval);
}


The first thing the code does is to import Adafruit's Neopixel library. If you do not already have installed this library you will find a description on how to do that here:
https://learn.adafruit.com/adafruit-neopixel-uberguide/arduino-library-installation

Next step is that we tell the program that the WS2811's are attached to pin 6. I/O port no 6 on the Arduino is a PWM port and that is what we need to control the WS2811's.

Then we define the number of WS2811's which is 2 in this case. These 2 WS2811's will give us 6 output ports.

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

This line does the actual setup for the WS2811's.
NUMPIXELS is the amount of WS2811's, PIN is on which IO port they are attached, NEO_GRB tells the library that it must use GRB instead of RGB and NEO_KHZ800 tells it the frequency with wthat the WS2811's should be controlled.

Let's look at the main loop:

pixels.setPixelColor(0, pixels.Color(00,0255,255));

The first zero is the number of the WS2811 in the line. The last 3 numbers are the PWM values we are going to send to the individual outputs of that WS2811. So in this line we set the first led OFF and the second and third led ON.

After the delay we turn that around:

pixels.setPixelColor(0, pixels.Color(0255,0,0));

This sets the first led ON and the other two OFF.

Altering the value 255 in any figure between 0 and 255 will DIM the led in stead of setting it to full brightness.

And here are the real life results:





So this works flawlessly. With 2 WS2811's I have created 6 extra outputs for the Arduino.

On to the Attiny85

We can use this technique also with an Attiny85. The Attiny85 normally just has 5 output pins. It would give our projects an enormous advantage if we could expand this to many more PWM able outputs. Well here we go.

Breadboard setup:



As you can see the breadboard setup is again almost identical to the previous setups.
In this case you need to use the USB external power supply.

As usual the WS2811's are attached through a 470 Ohm resistor to I/O port 1 of the Attiny85.


And this is what it looks like in real life.

The program


// Attiny85 code for controlling 6 output with ws2811
// Code written by Luc Volders

#include <Adafruit_NeoPixel.h>

// Which pin on the Attiny85 is connected to the NeoPixels?
#define PIN 1
// How many NeoPixels are attached to the Arduino?
#define NUMPIXELS 2

// Setup the NeoPixel library with how many pixels, and which pin to use to send signals.
// Note that for older NeoPixel strips you might need to change the third parameter
Adafruit_NeoPixelpixels=Adafruit_NeoPixel(NUMPIXELS,PIN,NEO_GRB+NEO_KHZ800);

inttoets1=0;

voidsetup()
{
pixels.begin();// This initializes the NeoPixel library.
for(inti=0;i<NUMPIXELS;i++)
pixels.setPixelColor(i,0,0,0);
pixels.show();
}

voidloop()
{
for(inti=0;i<255;i++)
{
pixels.setPixelColor(0,255-i,i,i);
pixels.setPixelColor(1,255-i,i,i);
pixels.show();
delay(10);
}
}


I just altered the Arduino program a bit. The program is going to fade the leds in and out. This is done in the main loop.

The first leds are going to fade from bright to OFF and the other leds are fading from OFF to full bright.


 

And here is the result. Six extra (PWM) outputs for an Attiny85 !!!!

The next part is going to show you how we can use this technique for expanding the BBC's Micro:Bit output ports.

Till next time.
Have fun

Luc Volders

Unlimited output ports for your Micro:bit

$
0
0
For an index of all my stories click this text

WS2811 as output for micro controllers part III

In the first part I showed you how to use WS2811 chips to create an almost unlimited number of PWM controlled outputs for an ESP8266.

The WS2811's are the chips that are build into the so called Neopixels. More detailed basic information about Neopixels can be found here: http://lucstechblog.blogspot.nl/2015/10/neopixels-ws2812-intro.html

So a Neopixel is basically a WS2811 and 3 leds build as a single chip. I am using just the WS2811's. As these can control 3 leds we should be able to use them as 3 outputs for a microcontroller. By daisy chaining them (just like we do with Neopixels) we can create an infinite number of outputs for any microcontroller. I did this succesfully with an ESP using LUA and Basic in Part 1 of this series. Read that story here http://lucstechblog.blogspot.com/2019/04/unlimited-outputs-for-your_12.html

Part II described how to use this technique with an Arduino and an Attiny85. Read that story here: http://lucstechblog.blogspot.com/2019/04/unlimited-outputs-for-your.html

And in this part I am going to show you how it is done with a Micro:bit.
For this I presume you have basic electronics knowledge because we are going to use the same hardware setup as we did with the Attiny85.

Hardware

As said above use the same hardware setup as used with the Attiny85.




In this setup replace the Attiny85 with the Micro:bit.




Power the Micro:bit independently from the WS2811's. The WS2811's work best at 5 volts and the Micro:bit works at 3 volts. So attach the GND from the Micro:bit to the Ground rail on the breadboard and attach the data line to P0. Forget about the 5Volt power line and supply the Micro:bit with it's own power supply.


Fortunately the WS2811's (just like the Neopixels) accept data at 3 volt level.




Here is the real-world setup.

Software.

The software is written in blocks.
I presume you are familiar with the Micro:bit programming environment; You can find the editor here: https://makecode.microbit.org/

Here is the full code:









The first block is always run when the Micro:bit is started. It displays a welcome message and attaches the neo driver to P0 and tells it that we are going to use 2 leds (WS-2811's)

The second block is just a counter that counts from 1 to 6 everytime the A button is pressed.

The third block is where the magic happens.
If you press the B button the block checks what number the counter was set at with button A in the previous block.
If the counter is set from 0 to 2 the first WS2811 is adressed. When the counter has a value from 3 to 5 the second WS2811 is adressed. The actual value determines which output then is activated.

So what happens is that you choose a number by repeatedly pressing the A button and activate the choosen output by pressing B.

That's all folks.



For clarity I show you here the underlying Javascript code.

Python

Next to programming in Blocks there is Python which has a loads more possibillities but is intended for the more experienced programmers.



The Python code almost speaks for itself.
First the libraries are imported. Then the WS-2811's are initialised on P0 (pin0)
Next step is a loop that sets the values of the output ports at a random PWM value so all led's will independently be dimmed at different values.

That's it.

I bet you can start all kinds of projects with this technique. This is not only good for attaching numerous leds to your Micro:bit. Attach transistors to each output and you can control multiple motors or ledstrips. As always: use your imagination and have fun.

Till next time

Luc Volders

Dweet: A volatile IOT cloud service

$
0
0
For an index to all my stories click this line

In a previous story which you can read here I wrote about Thingspeak. Thingspeak is a service where you send (sensor) data which is then displayed on a webpage. The advantage of this is that you can check your data from anywhere in the world. This way you do not have to open a port on your router to get to your data which may present some vulnerability.

Dweet is a similar service only with a twist.



The name Dweet is derived from Twitter where you tweet messages. Dweet is Data tWEETing. You can send as many data sets to dweet as you like but only the last five are stored for 24 hour. Next to that Dweet does not offer a website where you can check your data. Data has to be retrieved by an API call and is send back in JSON format. You even do not have to make an account on the Dweet service. You just Dweet your data and that's that.

Sounds complicated ??? Well it is not. Read on.

So why should you use Dweet.

First you do not have to make an account or anything. You can use the service without logging in or any other means of identifying yourself. So Dweeting is pretty anonymous. Next you can send as many Dweets as you like. A Dweet can contain up to 2000 characters. So thats an awfull lot of sensor data you can send. Besides that you can have multiple microcontrollers each sending Dweets under a different name. All those Dweets can be retrieved by one or more different microcontrollers even by multiple users.

I presume this all sounds hazy. So let's start with some examples that make everything clear for you.

Let's start !

Suppose you have a sensor reading that you want to share. For simplicity's sake let's say it is a temperature value of 25.

Dweet is meant to be used for the IOT (Internet Of Things). So to distinguish your Dweet amongst the thousands of other Dweets send every minute, think up for an unique name for your Thing. Keep it simple so you can remember it easily. For example. My name is Luc I, I want to Dweet the temperature and it is 2018. Lets call my Thing: Lucstemp2018

Now we have a name (Lucstemp2018) and an example sensorvalue (25). Let's Dweet that.

The Dweet.io home pages tells us to dweet you simply have to call an URL like:

https://dweet.io/dweet/for/my-thing-name?hello=world

In our example that would be:

https://dweet.io/dweet/for/Lucstemp2018?temp=25

Just put that in your Browser and you will get the following message back:


 
As you can see the Dweet has succeeded, the name of our Thing is Lucstemp2018 and the data what we are looking for is called temp and the value is 25.

Let's see if we can retrieve the information. For retrieving we use again an API call. The home page tells us again the general format which is:

https://dweet.io/get/latest/dweet/for/my-thing-name

That's easy to implement for our example. The Api Call should be:

https://dweet.io/get/latest/dweet/for/Lucstemp2018

Again put this in your browser and you will get the following result:



So we succeeded retrieving the data. The Thing name is the same and the data is 25.

ERROR !!!!



If something in the name is wrong or you are trying to retrieve data that was send more as 24 hours ago you will get an error message looking like the above picture.

Sending and retrieving multiple data.

Suppose you want to send multiple data. For example not only the temperature but also a notification if a door is open, a window is open and if a light switch is on or off.

Well that is easy. You just add multiple data by combining them with the & sign.

Lets try with the temperature and the indication of a light switch. We send a 1 if the switch is on and a 0 if the switch is off.
You could send the words ON and OFF however I will combine Dweet in the future with another service called Freeboard and for that purpose it is easy to represent on with 1 and off with 0.

Here is the Dweet, put it again in your browser:

https://dweet.io/dweet/for/Lucstemp2018?temp=18&switch=1



And here is the confirmation that we succeeded. As you can see two variables have been saved being temp and switch.

Lets see if we can retrieve that. The command for retrieving is the same one we used before:

https://dweet.io/get/latest/dweet/for/Lucstemp2018



There it is: we have retrieved the data.

Remember that I told you that a Dweet can contain up to 2000 characters. Therefore you can not only send sensor data but also complete texts. An example could be:

https://dweet.io/dweet/for/Mymessage?text=This is an example of what can be send with Dweet


And bingo, this worked. Without any hassle we created a new Thing which is called Mymessage and the variable is called text. This does not erease or interfere with the previous thing which still exists (as long as there have not waited 24 hours)

To retrieve the text you use the next call:

https://dweet.io/get/latest/dweet/for/Mymessage



And this is the result.



In case you are wondering...........
YES this story was written on 17 November 2017 at almost 1 o'clock in the night.

The purpose of all this.

So what's the purpose of this. What can we do with it.

Suppose you are at work and want your loved ones at home to know that you are on your way home. This can be done by having an ESP8266 at your work sending a Dweet and at home an ESP can be polling the latest Dweets so they know when you are coming home. As you might have seen in the previous examples Dweet automatically adds the date and time to a Dweet. So your loved ones even know when you have left work.

Let's look at another example.
Attach at holliday cottage a door or drawer sensor to an ESP and let it Dweet when the door or drawer is opened. An ESP at home can then check for Dweets and see if the door/drawer were opened and blink a light or give an audible signal or even send a message to your Phone.

"Hoho" is what I can here you think. The examples show how to Dweet from your browser. But your talking about Dweeting with an ESP.  So let's do that.

Dweeting from a program.

We can do this the hard way (the C++ way) or the easy way. Well I am lazy so let's take the easy way. Let's use my favorite devellopment tool: ESPBasic.

I am using ESPBasic here as it is (in my humble opinion) THE most easy developping environment for the ESP series. You can read a detailed instruction on how to install ESPBasic and take the first steps here: http://lucstechblog.blogspot.nl/2017/03/back-to-basic-basic-language-on-esp8266.html





'=================================================
'DweetforLuc'sweblog
'Firstsendafaketemperatureandswitch
'=================================================

tosend$="dweet.io/dweet/for/Lucstemp2018?temp=18&switch=1"
printwget(tosend$,80)
delay2000

'=========================================
'nowletsseeifwecanretrievetheDweet
'=========================================

request$=wget("dweet.io/get/latest/dweet/for/Lucstemp2018",80)
printrequest$

You can see there isn't much to it.

The sending part.

tosend$ = "dweet.io/dweet/for/Lucstemp2018?temp=18&switch=1"

First we make a string containing the api call.

print wget(tosend$,80)

Next print wget sends it onto the internet.

The retrieving part.

request$ = wget("dweet.io/get/latest/dweet/for/Lucstemp2018",80)
print request$

Just as simple as the sending part.

Suppose you want to distill the temperature out of the answer that is returned.

reqfound$ = JSON(request$, "temp")
print reqfound$

Takes care of it all.
The JSON parser distills the value of the temp variable for you.

Here is the complete code that first sends a Dweet, then waits 2 seconds and then retrieves it after which the required data is distilled:



'=================================================
'DweetforLuc'sweblog
'Firstsendafaketemperatureandswitch
'=================================================
wprint"<br>"
wprint"Sending the Dweet<br>"wprint"=================<br>"tosend$="dweet.io/dweet/for/Lucstemp2018?temp=18&switch=1"wprintwget(tosend$,80)
wprint"<br><br>"
delay2000'========================================='nowletsseeifwecanretrievetheDweet'=========================================wprint"Receiving the Dweet<br>"wprint"===================<br>"request$=wget("dweet.io/get/latest/dweet/for/Lucstemp2018",80) wprintrequest$
wprint"<br><br>"
'========================================='nowletsseeifwecanretrievetheDweet'=========================================wprint"The temperature is<br>"wprint"==================<br>"reqfound$=JSON(request$,"temp")

This is how it looks in the output window:




There's more

Just go to Dweet's home page and you will find a few more API calls which you can use. There is also a paid service that makes it possible to save the Dweets for a month. You can find the homepage here: https://dweet.io/

Ok guys and girls.
Time for you to do some experiments.
Play with this and wait for the next installment.

Have fun

Luc Volders

MP3 Player - stand alone

$
0
0
For an index to all my stories click this line

I know they are getting a bit outdated but there are still some uses for them: MP3 players. And sometime ago when I was surfing the net (who uses that expression nowadays.....) I stumbled upon a great MP3 module.



The module is sold and documented on the internet by several names. The first name is MP3-TF-16P and the second name is the DFPlayer Mini. Both seem to be exactly the same module.

So what is it.

It is an MP3 chip with an SD card reader on one module. The module is breadboard friendly and can be powered by any voltage between 3.2 to 5 Volts. So suitable for Arduino and ESP8266 projects. Next to that it has an onboard 3 Watt amplifier so you can direct attach a speaker. There is also a headphone / amplifier output with which you can attach a headphoe or attach it to your home stereo set.

The module supports MP3 and WMV decoding and sampling rates up to 48khz. The SD card supports FAT16, FAT32 up to 32GB with a maximum of 100 folders with each 1000 songs. The speaker and headphone connectors are attached to a 24 bit DAC with a dynamic range up to 90dB

Besides all this the module can be controlled by digital I/O ports, a resistor cascade on analog ports and by a serial interface.

Last but not least there seems to be an USB interface build in. I have found however no information or documentation on that up to now.

All in all pretty amazing specifications for a module that (at this moment) can be bought for about 1 dollar / euro from our favorite chinese suppliers !!!




Controlling the MP3-TF-16P

Like I mentioned above there are 3 methods to control this module.

- Using switches on the digital I/O pins
- Using switches with a resistor cascade on 2 analog inputs.
- Sending commands over the serial interface

As there are digital I/O pins the module can be controlled by an Arduino or an ESP8266. This can also be done by sending commands over the serial interface. There is an Arduino library available for that.

The resistor cascade needs some explanation.

Basically a range of resistors is connected to Ground. The remaining pin is connected to a switch which is connected to one of the analog inputs. Pressing a switch will therefore send an electric signal which value is dependend on the resistor value. This will trigger a specific command.
 


I think the schematic explains all.

You can find more information in the manual for this module which can be found here:

https://www.dfrobot.com/wiki/index.php/DFPlayer_Mini_SKU:DFR0299

A minimalist MP3 Player

I started with building a minimalists MP3 Player.

The only things you will be needing to build this are:

- battery holder for 3 AA batteries
- a breadboard
- some breadboard cables
- 2 switches

For the battery holder I used my own version which you can find here:
http://lucstechblog.blogspot.com/2016/04/3d-printed-battery-holder-i-was.html

In the final version I attached a switch to the VCC power line. That made it possible to switch between battery power and an USB power bank.



The switches are attached to GND and the I/O pins.
The DAC-L and DAC-R outputs arae attached to a headphone or to an amplified speaker or to an input on your home stereo set.





The above picture shows you my real-world test setup. I have used this with simple head-phones, an USB powered speaker and attached it to my stereo set.

I was impressed by the overall sound quality. It was much better as I expected from such a cheap module.

Flaws.

well yes there are a few problems / flaws.

Watch the cabling.
By using leads that are too long there is noise being picked up. This will disappear at higher volumes but is annoying at low volumes. Strange is that it did not occur all the times. Whyen skipping through the songs on the SD card the noise seemed to disappear. However best practice is to keep the lines short to avoid the noice alltogether.

No continuity
The MP3 player stops playing after each song. You will have to press the next or previous button for playing another song.

Solutions

For the noise the solution is simple: keep the leads as short as possible.

For the continuity the solution is obvious: use an Arduino or ESP to control the module. That's something that is coming up in another story.

Till then,
have fun

Luc

At last.....but only dutch for now

$
0
0
For an index to all my stories click this line

Just a short entry today because today was the day I was eagerly waiting for.



By mail I received the proof of my book. So I spend the day checking if everything
was well. And boy I loved it. The print was crisp and clear. 
368 Pages information on the ESP32. From installing the software on your PC to building your own webserver to control lamps, motors etc and remotely read your sensors.
All written in the Arduino language (C++) with breadboard schematics made in Fritzing.

For now only in Dutch. I am working on the English version.

It will be available in bookshops and websites, on demand, throughout the Netherlands in the next weeks, when I finish checking and give the green light for publishing.

So if you want to buy one, send me an email : lvolders@gmail.com
As soon as it is available I will give you notice. Just state wether you want to buy the Dutch or English version. The english version will be available through a yet to be chosen publisher.

Nederlands

Vandaag is de dag waar ik al een tijdje naar uitkeek !!!
Ik ontving per post de drukproef van mijn boek: ESP32 uitgelegd.
Ik ben nu aan het controleren of ik er nog ernstige fouten in tegenkom en daarna kan het gepubliceerd worden. Dat zal nog een paar dagen tot maximaal 2 weken duren.

Het boek heeft 368 pagina's met informatie over de ESP32 van het installeren tot het maken van een webserver waarmee je motoren, lampen etc kunt bedienen of je sensoren uitlezen. Alles in C++ de bekende Arduino taal. Het is een werkboek voor beginners met veel uitleg, een of meer oefeningen aan het einde van elk hoofdstuk en ideeen voor projecten die je met de informatie uit het boek kunt bouwen.

Als je het wil kopen, stuur dan een mail naar: lvolders@gmail.com
Zo vlug als het echt beschikbaar is krijg je dan een berichtje van mij. 
In Nederland zal het boek te koop zijn bij boekhandels, webshops als bol.com en via de webshop van de drukker. Als elektronica winkels of fablabs geïnteresseerd zijn kunnen ze ook contact met mij opnemen.

Luc Volders

Ping Ping Anybody Home ???

$
0
0
For a complete index of all my stories click this line

A nice feature of the Philips Hue lamps is called Geofencing. This just means that you can instruct the Hue bridge to automatically switch your lights on when you are approiaching your house. So for a time I wondered how they do that. And in fact it is very easy.

It is a technique that can be used for many different purposes. Not only to test if you are around but this also makes it possible to test wether your wireless apparatus are actually functioning.

It's called ping.

Ping

Ping is a network function that sends a command to an IP adress. If the device with that IP adress is connected to the router you will get a response. If the device is off (not connected) then you will get an error back.

It is even possible to ping web-adresses. That way you can see if a certain server on the web is on-line.
If you for example want to test wether this web-log is still on-line you could use something like the next command:
ping("http://lucstechblog.blogspot.nl/")

Ping Ping Anybody Home ?

So how is that going to tell me wether anybody is home or nearby. Well simple. Most people I know always carry their mobile phone around. So do I. And most of the time wifi is turned on. So if I give my phone, and for that my girlfriends phone etc, a fixed IP adress in my router I can Ping that IP adress et voila.

Give your home mates a fixed IP adress.

Start with opening the IP adress of your router in your browser. In my case my router has IP-adress 192.168.1.1 Your's may vary so look it up in the manual.



Enter the ip-adress in your browser and if you have a Zyxell router like I do you will be presented the above screen. Different routers give different results, but it should be easy to find the page that shows all connected devices.

As you can see I have given all my networked devices meaningfull names. So let's look at the details of my phone.



First you can see my phones IP-adress and as you can see I checked Static DHCP. This makes sure that everytime my phone connects to the router it will get the same IP adress.
Do this for all the phones and wireless apparatus in your home.

Ping ??

Ping is a wifi function. In some languages it is build in (yes in Basic) and in some languages it is not. What I wanted is to have my ESP Ping my devices and depending on the outcome switchs led's (relay's) on or off.

Standard C++ (Arduino) for the ESP has all kinds of Wifi functions build in but no Ping.

Fortunately Daniele Calanardi has build a library that we can use. You can download that library from the following link: https://github.com/dancol90/ESP8266Ping

Unpack the library and place it in your \Arduino\libraries folder and start the Arduino IDE.

Circuit



The hardware is simple just an ESP8266 (I used a NodeMCU) and 4 leds connected through 220 Ohm delimiting resistors. The led's are attached to D0, D1, D2 and D4.

The Arduino source



/*
* Anybody Home ???
* This program tests IP numbers with PING to see if they are
* active. If so the Led will be on if the IP number is not
* active the Led will be off
* This way we can check activity and person presence
* Written by Luc Volders
*/

#include

<ESP8266WiFi.h>

#include

<ESP8266Ping.h>


constchar*ssid="XXXXXXXXX";
constchar*password="YYYYYYYYY";

constIPAddressthermometer(192,168,1,65);
constIPAddressdomoticz(192,168,1,66);
constIPAddressLucsphone(192,168,1,70);
constIPAddresselsphone(192,168,1,72);

voidsetup()
{
pinMode(D0,OUTPUT);
digitalWrite(D0,LOW);
pinMode(D1,OUTPUT);
digitalWrite(D1,LOW);
pinMode(D2,OUTPUT);
digitalWrite(D2,LOW);
pinMode(D4,OUTPUT);
digitalWrite(D4,LOW);
delay(10);

// Start by connecting to our WiFi network

WiFi.begin(ssid,password);

while(WiFi.status()!=WL_CONNECTED)


{
delay(100);
}
}

voidloop()
{
if(Ping.ping(thermometer))
{
digitalWrite(D0,HIGH);
}
else
{
digitalWrite(D0,LOW);
}
if(Ping.ping(domoticz))
{
digitalWrite(D1,HIGH);
}
else
{
digitalWrite(D1,LOW);
}
if(Ping.ping(Lucsphone))
{
digitalWrite(D2,HIGH);
}
else
{
digitalWrite(D2,LOW);
}
if(Ping.ping(elsphone))
{
digitalWrite(D2,HIGH);
}
else
{
digitalWrite(D4,LOW);
}
delay(1000);
}



Let's look closer to the program.

#include <ESP8266WiFi.h>
#include <ESP8266Ping.h>

to activate the wifi functions of the ESP we need to include the Wifi library and for Ping we need to include the ESP8266Ping.h library.

const char* ssid     = "XXXXXXXXX";
const char* password = "YYYYYYYYY";

Fill in your router's credentials so you will connect to your network automatically.

const IPAddress thermometer(192, 168, 1, 65);
const IPAddress domoticz(192, 168, 1, 66);
const IPAddress android(192, 168, 1, 70);
const IPAddress elsphone(192, 168, 1, 75);

Look in your router for the IP adresses of your Wifi Devices and replace the ones I have used.

The rest of the program is straighforward.

  if(Ping.ping(Lucsphone))    
    {                         
      digitalWrite(D2,HIGH) ; 
    }                         
  else                        
    {                         
      digitalWrite(D2,LOW);   
    }                         


In the loop all devices are pinged. In this part the IP number of my phone is pinged. If it is present Ping will respond with a 1. If I am not at home Ping will respond with a 0 and the led will be set on or off accordingly.

This can be optimized :

digitalWrite(D2,Ping.ping(Lucsphone));

However I wanted the code to be easy to follow for less experienced C++ programmers.

Basic program

Naturally (I know some loathe this) I wrote the same program in ESP-Basic which is my favorite rapid software devellopment platform. So I hereby give you the simple code that does the same as the C++ program:



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

timer1000,

[
testping]

wait

[testping]

io(po,D0,ping("192.168.1.65"))'thermometer

io(po,D1,ping("192.168.1.66"))'domoticz

io(po,D2,ping("192.168.1.70"))'luc'sphone

io(po,D4,ping("192.168.1.72"))'elsphone

wait


Let us look at some details.

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

These lines set all the leds in the initial state which is off.

timer 1000, [testping]

This line initiates a timer that every second (1000 miliseconds) jumps to the routine testping.

io(po,D2,ping("192.168.1.70")) 'luc's phone

This line actually tests whether my phone is detected by the router so if I am in. Depending on the
feedback it sets the led on or off. Replace the IP numbers with your own.

That's all folks.

Just for usability I modified the program so it has a webpage on which the information is displayed as wellas leds giving the information. So if you are around the ESP the leds will show what's happening and if you are not in its neighbourhood you can check the webpage.


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

timer1000,[testping]

wprint|<bodystyle="background-color:powderblue;">|
wprint|<H1><spanstyle="color: red;">|
wprint"Anybody Home ??"
wprint"</H1>"
wprint"<h3>Thermometer "

textboxtherm
cssidhtmlid(),"background-color:powderblue;border-style: hidden;font-size: 16px;display:block;width:80px"

wprint"<br>"
wprint"Domoticz "
textboxdomo
cssidhtmlid(),"background-color:powderblue;border-style: hidden;font-size: 16px;display:block;width:80px"

wprint"<br>"
wprint"Luc's phone "
textboxlucphone
cssidhtmlid(),"background-color:powderblue;border-style: hidden;font-size: 16px;display:block;width:80px"

wprint"<br>"
wprint"Els phone "
textboxelsphone
cssidhtmlid(),"background-color:powderblue;border-style: hidden;font-size: 16px;display:block;width:80px"

wprint"<br>"
button"QUIT",[progend]
wait

[testping]
therm=ping("192.168.1.65")
iftherm=1then
io(po,D0,1)
therm="available"
else
io(po,D0,0)
therm="off line"
endif


domo=ping("192.168.1.66")
ifdomo=1then
io(po,D1,1)
domo="available"
else
io(po,D1,0)
domo="Domoticz off line"
endif

lucphone=ping("192.168.1.70")
iflucphone=1then
io(po,D2,1)
lucphone="at home"
else
io(po,D2,0)
lucphone="he's not in"
endif

elsphone=ping("192.168.1.72")
ifelsphone=1then
io(po,D4,1)
elsphone="available"
else
io(po,D4,0)
elsphone="she's out"
endif

wait

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

The result.





Modifications.

Use your imagination.

You can send the information to cloud-based IOT servers like thingspeak (look for a tutorial here:
https://lucstechblog.blogspot.nl/2017/11/thingspeak.html )

Replace the leds with relays so the lamps will go on if you approach your house.

Send the information to your home-automation system (like Domoticz which I am using) so
it can alter your thermostat settings or switch on your lights etc.

Till next time.
 

The next few weeks I am off on a long-earned holliday. There is however enough material on this web-log to get you going and start experimenting. So till July and remember:

Have fun !!

Luc Volders
Viewing all 318 articles
Browse latest View live