Friday, October 9, 2020

Magic Wand

 For an index to all my stories click this text

Halloween is coming up and I wanted to build something to amaze friends and kids. So I decided to build a magic wand. But what should it do. I decided to make a magic wand that would set a lamp on or off.

Lots of lamps in my house are connected to my home automation system. This system is called Domoticz and is Open Source. You can install it on a Raspberry Pi like I did and that keeps the overall cost down. Check it out !!! You can find it here: https://www.domoticz.com/

The previous story showed how an ESP8266 could send a command to Domoticz to put a lamp on or off. I am going to use that to build my magic wand. Please re-read that story because some parts of this story lean on it. You can re-read that story here:
http://lucstechblog.blogspot.com/2020/01/sending-data-from-esp-to-domoticz.html

In that story I used a Wemos Mini D1 to send commands to Domotcz. However a Wemos Mini D1 is to large to build into a magic wand. So I am going to use an ESP-01 this time. And as usually it will be programmed in Arduino language.

The ESP01 has just a few IO ports. One of the ports has to be unconnected when starting up otherwise the ESP-01 will start in programming mode in stead of normal mode. So we have to find a clever way to use the scarce IO ports.

The next challenge was how to operate the magic wand.

The way I build it is this.
There is an on-off switch on the magic wand that is used to save battery life. The ESP-01 needs some time to connect to my router. And to be able to know wether the connection has succeeded I put a led on top of the magic wand. The led will light up when the connection is established.

The next step is to operate the magic wand. I used a tilt switch for that. It is called the SW520D. The way it operates is that there is a small ball inside the sensor. When the sensor is in upright position the ball shortens two contacts in the bottom. When the sensor is tilted the connection is broken. There are a few future projects coming up with this sensor.



The overal design is as follows.

The on-off switch puts the power on.
The magic wand is held upright. The tilt switch is reverse mounted so the contacts are broken as long as the magic wand is kept upright.  The ESP01 starts making a connection to the router. When the connection is established the led will go ON. Then we can lower the magic wand to a less then horzontal position and the SW520D sensor will make contact. This triggers the ESP-01 to send the command to Domoticz to set the lamp on. When the magic wand rizes again the connection in the SW520D is broken agian and then the ESP-01 will send a command to Domoticz to put the lamp off again.

I used GPIO-0 and GPIO-2 on the ESP for the sensor and the led. The problem is that it is best to have both IO pins not connected at booting, otherwise the ESP-01 will boot in programming mode. Therefore the SW520D is reverse mounted so there will be no contacts made at boot time. The led is also reversed connected. This means that the VCC pin is connected to VCC and the GND is connected to the GPIO-2.

Breadboard setup.



The GND and VCC pins are connected to the batteries. Do not forget to connect VCC to the CH-EN (enable pin) otherwise the ESP-01 will not start up.

GPIO-2 is connected with an 220Ohm delimiting resistor to the GND pin of the led. The VCC pin from the led is connected to VCC.
GPIO-0 is connected to the SW520D and the remaining connection on the sensor is connected to GND.

The power supply.

Well a magic wand can not be connected to a mains outlet. That certainly would look stupid. So it has to be battery operated. Testeing learned that I needed 3 AAA batteries to get good results. So I drew a custom battery pack in Tinkercad. It had to be small to keep the overall width of the magic wand narrow.





I started with a casing for 1 battery. Mulitplied it and made one long casing from this.

You can download my design from Tinkercad if you like. The STL file has to be sliced. I sliced it at 0.3mm which is accurate enough. It does not have to be pretty as it is going to be mounted inside the magic wand. here is the link.
https://www.tinkercad.com/things/7lT5xm1sNRq-3-aaa-bat

The contacts were made from paperclips. I did not print the holes for the contacts but just drilled them.

There are a few steps to go. First is the program.

Magic Wand program

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>

// WiFi settings
#define wifi_ssid "YOUR-ROUTERS-NAME"
#define wifi_password "ROUTERS PASSWORD"

// HTTP Domoticz settings
const char* host = "DOMOTICZ IP ADRESS";
const int   port = 8080;

int led = 2;  //GPIO1 TX
int sensor = 0; //GPIO2

HTTPClient http;

void setup() 
{
  pinMode(led, OUTPUT); 
  pinMode(sensor, INPUT_PULLUP);
  digitalWrite(led, HIGH);  // Led HIGH so starts off
  setup_wifi(); 
}


void switchonoff(){
      // Here is the Json format that Domoticz expects
      // /json.htm?type=command&param=switchlight&idx=99&switchcmd=On
      String url = "/json.htm?type=command&param=switchlight&idx=";
        url += String(3);
       if (digitalRead(sensor) == HIGH)
        {
        url += "&switchcmd=Off"; 
        }
        else   
        {
          url += "&switchcmd=On";
        }
      sendToDomoticz(url);
  }  


void loop() 
{
  switchonoff();
}

//Connect to wifi
void setup_wifi() {
  delay(10);
  WiFi.begin(wifi_ssid, wifi_password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
  }
  digitalWrite(led, LOW);
}

void sendToDomoticz(String url){
  http.begin(host,port,url);
  int httpCode = http.GET();
    if (httpCode) {
      if (httpCode == 200) {
        String payload = http.getString();
      }
    }
  http.end();
}

 

The code has a few pitfalls which I wil highlight.

// WiFi settings
#define wifi_ssid "YOUR-ROUTERS-NAME"
#define wifi_password "ROUTERS PASSWORD"

// HTTP Domoticz settings
const char* host = "DOMOTICZ IP ADRESS";
const int   port = 8080;


Fist thing is not to forget to put your own credentials here.

  pinMode(led, OUTPUT);
  pinMode(sensor, INPUT_PULLUP);
  digitalWrite(led, HIGH);  // Led HIGH so starts off


The pin for the LED is set as OUTPUT which is obvious but the pin is initially set HIGH. As the led is connected reverse the led will be off at the beginning.

void switchonoff(){
      // Here is the Json format that Domoticz expects
      // /json.htm?type=command&param=switchlight&idx=99&switchcmd=On
      String url = "/json.htm?type=command&param=switchlight&idx=";
        url += String(3);
       if (digitalRead(sensor) == HIGH)
        {
        url += "&switchcmd=Off";
        }
        else   
        {
          url += "&switchcmd=On";
        }
      sendToDomoticz(url);
  }


Here the string is created that will be send to Domoticz. The IDX which is the ID of the switch is dependend on your own system. In my case it is 3. The program tests wether the SW520 sensor is open or closed and puts the lamp accordingly on or off.

Detailed information on how to find the IP adress and the ID's of your switches can be found in the first part on sending commands to Domoticz which can be found here:
http://lucstechblog.blogspot.com/2020/01/sending-data-from-esp-to-domoticz.html

Stripboard

Well a breadboard is no good to hide your electronics in a magic wand. So I put it all on a small piece of stripboard.




I put headers on the stripboard to plug the ESP01 in. Do not forget to cut the connections between the headers so you do not short circuit the ESP's pins. As you can see I also cut some connections on the top of the breadboard otherwise the pins of the resistor would short circuit.

When I put the setup into the magic wand I did that upside down. The batteries are on the bottom the electronics on the top. That way the SW520D is upside down and does not make contact when the magic wand is held upright, just like we want.

Make sure you make the wires between the batterie holder and the electronics long enough to take the electronics out of your magic wand to replace the batteries.

The Magic wand

Luckily I found a carton tube at my job which was narrow but wide enough to put the electronics in. 


I printed a rounded top in which I put the led and used a push button as a power switch. As soon as I put the magic wand in my hand the pressure of my hand pushes the button and that powers the electronics. If I lay it down the pushbutton is released and the power is cut off.

Have fun !!!
Till Next time

Luc Volders