LSC smart sensors, switches and lamps

Dutch thrift store ‘Action’ had a promotion of TuyaSmart compatible IoT devices for quite low prices. The devices were:

  • Smart switches
  • Smart doorsensor
  • Smart movement sensor
  • Smart alarm
  • Smart lamps and led strips
  • Remote control

I found the efficiency (as in lumen per Watt) of the lamps a bit low, with exception of one 800 lumen lamp, and the ceiling lamp was not really my taste so I didnt bother with them. The remote control seemed somehow obscure, as it seemed it could only control one device. The other devices though were quite promising so I bought those.

Though LSC has it’s own app, I already had the TuyaSmart app, so I decided to use that one. The LSC app is a spitting image of the Tuya app, but it is not recognized by e.g. Google Home, whereas Tuya is.

The EU type switches are quite compact, but you may still ha e trouble putting 2 right next to each other in a double outlet. The setup is very easy and if you use the ‘scan’ option in the tuya app, not much can go wrong.

The door sensor and the alarm are also easy to set up. The doorsensor is coincell operated and the two parts can be stuck to the door and door frame. In the Tuya app, actions can be attached to the doorsensor to e.g. switch on a light, or sound an alarm. The sensor sends notifications to your phone as well.

That leaves the motion sensor and somehow that was a different story. Connecting to wifi was simple, but after that, nothing g happened. No amount of movement seemed to be noticed. As another sensor had the same behaviour, I had not much choice but to return them.

Yet…..I kept wondering. Just couldn’t imagine two sensors were not working, especially as no one else seemed to return them while they were selling like hot cakes.

So….I bought another one…..same result. Various reinstalls, even in the LSC app, showed no effect, so I left it, frustrated, deciding to return it after the weekend.

And then….out of the blue, it started to work, and quite well I should add. The sensor does ha e a bit of a refractory period, meaning that after it senses movement and sends a notification, it will not sense new movement for a few minutes. People have reported this to be about 5 min, but in my experience it is more like 3 minutes and occasionally even 1 minute.

TuyaSmart

The app seems to have one slight shortcoming though: When a movement is registered and shown at the bottom, the app only shows  “intruder” for a short time and goes back to its ‘No intruder’ status quickly. But that is only a cosmetic short coming.Commercial software or Flashing?

The Tuya app, working with the software in the devices does what it does and it is quite good software. Although I do not believe that ‘the chinese’ are particularly interested in when I switch my lights on, there are many good reasons to put your own software on an IoT device: Not being dependent on 3rd party servers that might go out of business, availability on LAN rather than only WAN, no deluge of different (possibly shady) apps on your phone.
The devices I had all appeared to have an ESP8266 under the hood and could be opened.

LSC motion sensor

 

Getting I2C regulated 0-10Volt out of an Arduino or ESP8266

The max Output of an Arduino or ESP8266 is roughly 5, respectively 3.3 Volt. The Arduino has the possibility to output a varying voltage on its Analog output pins, whereas the ESP8266 can emulate a varying voltage on a PWM addressed pin, with the help of a low pass filter (basically an RC filter [4k7/10uF]). There are instances where it would be handy/necessary to have a 10 Volt regulated output: Some dimmable lights require a control signal of 0-10Volt as do some commercial Power-motor controllers.
It is not hard to get such a signal from the mentioned microcontrollers, by amplifying the output.

The circuit shows such an amplifier. It is a simple Opamp non-inverting amplifier. When the Op Amp receives an input on the non-inverting output,  it will raise or lower it’s output until the level on the inverting output is matched.
As we are using a voltage divider to feedback the output voltage, we can make the opamp to drive the output higher than the input on the non-inverting pin.
The output voltage is thus defined as:
Vo=Vin x (1+(Rtop/Rbottom)), in which Rtop is R1+the top fraction of  P2 and Rbottom is R2+the bottom fraction of P2.
the amplification thus can vary between 1+(10/20) and 1+(20/10) or: between 1.5 and 3.
Ideally, with the variable resistor in the exact middle, the amplification equals 2 and the Output -when driven by the Arduino- should be 10 Volt, however, the Arduino may not output exactly 5 Volt, in which case the output can be trimmed with the variable resistor.
As the LM358 is not a rail to rail opamp, the Vcc needs to be higher than the required output. As the datasheet is a bit unclear on this, I have chosen 12 Volt, which turns out to work fine.
When using the ESP8266, the maximum output of the circuit will be 3×2.2=9.9Volt. In practice however the output on the ESP8266 pins will probably not reach 3v3, also, making an analog output with PWM and a low passfilter is not ideal.

Enter, the MCP4725
The ‘problems’ mentioned above, can largely be remedied by using  a proper DAC (Digital Analog Converter). The MCP4725 is a cheap, easy to use DAC that is available on break out boards at various chinese webshops.
The MCP4725 is a 12 bit, rail-to-rail DAC, which means that if you feed it with 5 or 3V3, that will come out of it (well…almost). It also has on board EEPROM for storage of setup, but for this build that is not really necessary. There is a library available in the Arduino IDE library manager. The Vcc pin requires an appropriate bypass capacitor of about 0.1 µF (ceramic) to ground. An additional 10 µF capacitor (tantalum) in parallel is also recommended to further attenuate high frequency noise present in application boards.

As the MCP4725 is a 12 bit DAC, the range goes from 0-4095. Writing 4095 to the MCP 4725 to the DAC therefore would give the max output. A program for the MCP4725 thus would look like something like this:

#include <Wire.h>
#include <Adafruit_MCP4725.h>
Adafruit_MCP4725 dac;

void setup(void) {
dac.begin(0x60); // This may vary between 0x60 and 0x65 for your board
}

void loop() {
// 1V Out from MC4725 -> 2V Out from LM358
dac.setVoltage(819, false); // false meaning "do not write to EEPROM"
delay(2000);
// 2V Out from MC4725 -> 4V Out from LM358
dac.setVoltage(1638, false);
delay(2000);
// 2.5V Out from MC4725 -> 5V out from LM358
dac.setVoltage(2043, false);
delay(2000);
// 3V Out from MC4725 -> 6V Out from LM358
dac.setVoltage(2457, false);
delay(2000);
// 4V Out from MC4725 -> 8V Out from LM358
dac.setVoltage(3276, false);
delay(2000);
// 5V Out from MC4725 -> 10V Out from LM358
dac.setVoltage(4095, false);
delay(2000);
}

The max output current of the LM358 is a bit obscure from the datasheet, it is probably around 20-40mA which is sufficient for most control signals. If you need more current (maybe you directly want to drive a motor), consider the LM675 that has a max output of 3 Amps. Be careful though as the LM675 can easily oscillate at amplification <10. Oscillation will lead to output going to (almost) rail even at no input and the chip warming up). Use decoupling capacitors – preferably a ceramic (100nF) and tantalum (10uF) type in parallel- and keep the leads short. A a small capacitor (on the order of 50 pF to 500 pF) across the circuit input, helps preventing the input leads to function as antenna.
A better choice might be the OPA544T, though that only has a 2 amp output, but is more stable at low amplifications. It is also more expensive.

Another way to get more current is with a power transistor, such as a BD137 (1A) or even a good old 2N3055/TIP3055 (15A).  However,the hFE starts to play an important role in this circuit. take for instance the BD137 that has a worst case hFE of 40. When it needs to deliver 1A, it will require  25mA as its base current. That is on the edge of what an LM358 seems to be able to deliver. If we add a BC547 that has a hFE of 110-800, a much smaller current will be required from the 358.
Other possible transistors in this range are 2SD882 (2Amp hFE 60), BD135, BD137, BD139 (1.5 A, hFE 40)
In case of the 2N3055, that has a worst case hFE of 20. If 15A is required, a base current of 750mA is required. Adding a BC547 would bring that down substantially, but it can’t deliver 750mA. A 2N2222 could, but it’s hFE is only around 30 at that current so you would need a BC547 AND a 2N2222  AND a 2N3055


In this light a TIP120 or TIP122  is a better choice as those already are powerdarlingtons with an hFE of about 1000 and a max current of 5A

Connecting an ESP8266 to the outside World

I admit that this is a subject that is really really basic and no doubt explained at many places. Yet, I still see the question posed time after time at various fora and sadly the replies are often not on the ball. Things like “You need REST API”, “Just use Blynk”, “Just use IFTTT” or “Why would you want to do that”, surely will not help the struggling novice on their way.
So, rather than having to repeat my explanation again and again, it is better, I just write an article, explaining it…..for the novices amongst us.

Finding 2 IP addresses
OK, so you have an ESP8266 that reads a sensor and you want to be able to get the reading when you are away from home, using internet. It is quite simple, but you need to know 2 distinct IP addresses.
1- you need to know the IP address that connects your computer to the internet. You can easily find that on websites like “whatismyip.com“. It is also called ‘Public IP’ or ‘WAN IP’
2- you need to know the IP address that your ESP8266 has in your local network. You need to check the DHCP table in your router, but many ESP8266 programs actually print that number as well in the serial port. These numbers almost always looks like “192.168.x.yyy”. You can also use a phone app like ‘port authority’ to find the number for you.

Port forwarding
So what happens when you are on your holiday address in Reykjavik or Kabul and you want to know what your sensor back home says? You open a browser, type in the IP address you found under “1”. well that connects you via the internet to your router back home. However, your router when it gets that call, doesn’t have a clue what to do with it, let alone that it knows it should forward that call to the ESP8266 that is connected to it, because you didn’t tell it what to do with it.

So when you get back home you have to instruct your router to forward the calls to your ESP8266 and that is done via “IP forwarding” or “Port forwarding”.
What makes that a bit complicated is that it is done differently in various routers, so you would have to check your specific router for it. Look for “IP forwarding” or “Port forwarding” and in some routers it is called ‘virtual server’.
When you found that you usually have to fill out 2 things. The port that is being forwarded and the IP address it has to be forwarded to. For the port you choose “80” and for the IP address to forward to, you use the local ip address, you found under point “2”.It is the address that started with “192.168.”
If it doesnt allow you to forward a specific port, but it does allow you to forward a service, choose ‘http’ for that service.
If you really cant find the port forwarding, but You happen to find something that is called “DMZ” choose that to forward to the ip address of your ESP8266 (the address starting with “192.168”) that is less elegant, but it will work.

Setting up a Webserver
So you are back on holiday and you want to check again. You fill out the internet IP number of your home system (the number you found under “1” and yes, this time the call comes in to your router and that one knows exactly what to do,so it forwards the call to your ESP8266. The ESP duly receives the call and……….has no idea what to do with it because chances are you didnt tell it what to so with it.
Your ESP8266 needs a program that presents the data in “Webpage”. That is not hard, there are plenty of programs who do that. These programs usually are called “Webservers”. Plenty of those available. (Look in your Arduino IDE examples or check e.g. randomnerdstutorial.)

It works!!!! for now: MAC and Address reservation
So, you now have programmed your ESP8266 with a webserver program that presents the sensor data on its own little webpage.
In order to test it when you are at home you open your browser and fill out the Local IP number (that you found under “2” starting with 192.168) and yes, you see the webpage. To make sure you also fill out the address you found under “1” (thus the address that your system has on the worldwide interweb) and great it all works. You go back on holiday and check from the other side of the world and great still works. Mission accomplished!!!

…..or is it. Suddenly it doesn’t work anymore. What on earth is wrong????? Well chances are that you have restarted your ESP8266 a few times and suddenly it has received a different IP address in your Local network. Say it once had 192.168.1.103, but now you find out it has 192.168.1.110. Your router however still sends the incoming call to the first IP number because that is what you told it to do. Now it is hardly feasible that you constantly check if the local IP is still the same and if it isnt that you keep changing your forward instructions.
Fortunately, most routers allow you to reserve a specific IP number in your network, for a specific device, and this is how you do it:
When you earlier found the IP number of your ESP8266 (the one starting with 192.168), you probably also found its MAC number. A MAC number (or addres rather) looks something like: “5D:CF:7F:AC:61:66”
Now you need to look in your router that is called “MAC binding” or “Address reservation”. The object here is that you give the MAC number, as well as the IP number (192.168.xx.yyy) that should always be reserved just for the device with that specific MAC stands for “media access control”.

Yes, works again
OK you got that done,so now whenever you go to the internet address of your system back home, the call comes into your router, the router knows what internal address to send it to, that address is always the same, and the device on that address knows what to do: it presents (“serves”) a webpage to the computer half across the world behind which you are sitting. That webpage can present data, but it may just as well receive commands from you, e.g. to turn a lamp on or off. Realuse though that anybody who knows your public IP address can also check that webpage. That might not be so bad if it just presents temperature, but it might give a problem if strangers can switch a lamp off and on in your home. So once you set it all up, you may consider making a password  protected webpage.

Let me compare the situation with making a phonecall to a hotel: you need the hotel’s phonenumber (compare to the internet IP) and you need to know what room someone is in (the Local IP). The person in the room needs to know what to do with your questions (serving a webpage). That all works well as long as the person is in the same room (the address reservation: when Mr MAC comes to the hotel, he always gets room 192.168.1.103).

A final touch: DNS providers
So, everything works now………..but what if there is a rezoning and the hotel’s phonenumber changes???? Yes, you could look up the new number, but it is hardly practical.
But what does that mean in internet terms? Well, most of us get a so called ‘Dynamic IP address’ from our provider. Usually those addresses stay the same for a long time, but when you happen to restart your router you could get a different internet IP address. Sure you could check your internet address regularly, but that is only possible when you are at home. What to do when you are away from home????
The solution is a (free) DNS provider.
DNS providers provide you with an internet name, that you can use rather than an IP number. The essence though is that you get to put a tiny bit of software on your computer, that regularly checks your internet address and if that changes, it tells your DNS provider what the new number is. So you only have to provide a domain name to your browser and the DNS provider then knows the most up to date IP number to connect to.
A popular freeDNS provider for instance was “noip.com”, but there are more.