An ESP8266 with Static IP, sending DHT11 data via MQTT, going into DeepSleep

Sometimes, all you need is a quick program that needs to send some sensor data from a location that you don’t want or can’t put a power cable to.
Battery power, sending an ESP8266 into deepsleep for most of the time can be the answer.

The program here does just that. For reasons of speed it uses a static IP set up.
Dont forget to connect GPIO16 to the RST pin on your ESP8266.  A bare ESP8266 is your best choice. If you are using an ESP8266-01, you will need to have good eyes to solder a wire between pin 16 and the RST pin

You can download the full program here.

MX1508 vs L9110S vs TB6612 vs L293 Motordriver board

Two popular, cheap motordriver boards that are available on chinese webstores are the MX1508 and L9110S.

MX1508
At Aliexpress and Banggood, the MX1508 is listed as a “Dual Channel L298N DC Motor Driver Board PWM Speed Dual H Bridge Stepper Module”.However it is an MX1508.

This board can drive two motors independently. It is an H-bridge configuration for each motor and so can drive the motors in either direction. It is rated at 1.5A on each motor with a peak of 2.5A.
The module itself is not really breadboard friendly. The pin spacing is 0.1″ but the various connectors are at aberrant spacing.
The MX1508 from Shenzhen Guanghui Electronics Co., Ltd.  has a working voltage of  1.8-5Volt, but can drive motors with an operating voltage of 2-9.6 Volt. The module however, has only 1 Voltage input, that supposedly can take 2-10 Volt. The digital part of the MX1508 however is fed through a 220 ohm resistor with the resulting voltage being capped by what I presume to be a zenerdiode of 5 Volt.  With most hobby motors being either 6 or 12 Volt, 10 Volt may not be the most practical voltage.
When analyzing the PCB, one odd thing becomes clear: Pin 4 (VDD1), the input for the operating voltage of Motor A, seems to be not connected to anything. Yet the module works. Connecting it seems the right way to go though. It is very well possible that it is an unintentional mistake in the PCB.

The MX1508 has no fly back/free-wheeling diodes present, though they are advised by the chip manufacturer. As a result, there might be big spikes on the powerline when you are reversing the direction, which could lead to problems with other devices connected. Connect the motor like in the figure to the right. Controlling a DC motor with an H-bridge such as the MX1508 is in fact quite simple. Yet there is a library available.

The only  (currently) available datasheet is in chinese. It advises to put a 100nF over each motor output, something that is not done on the module PCB.

There is also an MX1208 with a current of 1.3A (2A peak). These are only available as a bare chip, not as a module.

Although the chinese webstores market the MX1508 as a replacement for the L298N motordriver, that is with its 46 Volts and 4 amp total, quite a strech.

The L9110S
So, if 10 Volt motorvoltage is not enough, there is the L9119S from Asic. The available module has in fact two L9110 chips, as they each control only one motor. The module can control 2 motors that according to the datasheet can be 2.5-12V with a maximal continuous current of 800mA , with a peak of 1.5~2 Amp.
The labeling on the input pins can be a bit confusing, but it is like this:

  1. B-IA: Motor B Input A
  2. B-IB: Motor B Input B
  3. GND: ground
  4. VCC: 2.5V-12V DC
  5. A-IA: Motor A Input A
  6. A-IB: Motor B Input B

The L9110 chips each have 2Vcc and Ground pins, but there is no indication that these should get different voltages (like one for the chip and one for the motor) and on the module PCB all grounds are connected and all Vcc’s are connected. There is however a small point of concern in using the module with a non-TTL level like 12 Volt: The inputs are pulled up via 10k resistors to the Vcc. That means that if you use 12 Volt DC for the motor and a 3v3 microprocessor to control the motor, on a LOW level there will be possibly flowing 1.2 mA into the uC pin. That does not need to be a major problem, but it is something that needs to be taken into account. If it is a problem, a simple optocoupler circuit can be put in between. Though motors are easy to control with an H-bridge such as this one, there are several libraries for the L9110S.

TB6612
The TB6612 from Toshiba is another relatively cheap board available at chinese webstores. It can drive 2 DC motors (or one stepper) with 1.2A per channel (3A peak). It runs at 2.7V-5V logic (2.7-5.5V according to some sources). The motor voltage is separate from the 2.7-5.5 Volt logic voltage. Good for motor voltages from 4.5V up to  15V (though the max supply  voltage is rated at 13.5V).   It comes with built in kick-back diodes internally.
The control of the TB6612 is a bit different compared to other motor drivers. Where usually the speed of a motor in an H-bridge is controlled by a PWM signal on one of the 2 control pins, the TB6612 has a seperate PWM pin, next to the 2 control pins. It can be pulled up if not used. It also has a standby pin to rapidly disable both motors. Connect to ground to disable. On the ‘Chinese’ boards, it is usually left floating.
Though DC motors are easy to control with an H-bridge such as this one, there is a library for the TB6612.

Though these modules are clearly no competition for the L298, how do they compare to the L293?

MX1508 L9110 TB6612 L293
Voltage 2-10V 2.5-12V 4.5-15 4.5-36V
Current 1.5A 800mA 1.2A 1A (0.6 for 293D)
PeakCurrent 2.5A 1.5-2A 3A 2A (1.2 for 293D)

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). 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 and 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.

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.

The HTU21D on an ESP8266-01, with MQTT and graphics

Limited as the ESP8266-01 might seem regarding the number of pins, it is still a very capable little device that is sometimes wrongfully underrated.

It is perfect however for projects that demand little I/O, such as reading a single sensor.

I am using the HTU21D temperature and humidity sensor. The ESP01 needs constructing a small board that contains the connectors and a small 1117-33 voltage regulator (soldered on the bottom) so the circuit can be fed with 5 Volt. As I am using an ESP8266-01S -that already has 10k pullups on GPIO0 and GPIO2, I do not have to add resistors for an undisturbed startup and use of I2C. If you are using the ‘old’  ESP8266-01, you have to add 4k7 -10k pullups to GPIO0 and GPIO2. If your HTU21 module already contains pullups, those will function as proper startup resistors as well.

The software (download here) is not too complicated, it first connects to your LAN, but it chooses the strongest of two LANs if available.

It then connects to an MQTT server and sends the sensor readings via MQTT every minute.

I also find it convenient when the software tells me what program it is, so I have added that too.

Finally, as in future I may want to include some time dependent signaling, I included a clock that is being synchronized through the internet.

Should you check your DHCP list, the device identifies as “HTU21”

MQTT Output:

Update
If you like a graphical presentation of your data, I have made another file that will exactly do that. It is based on code by Rui and Sara Santos from (randomnerdtutorials) that I discussed in an earlier  post. It will work with a 512k/32k SPIFFS setting as well as with a 1M/64k setting.
Beware though that many ESP8266-01 (and especially the ESP8266-01S) modules now come with a PUYA memory chip that has been causing some issues when using SPIFFS. The userdata.h file contains 2 defines that need to be set or commented out depending on whether you have or do not have the PUYA chip (2 defines to cover different cores). If you are still on the 2.4.1 core, you will need a patch. That you will have to put here: C:/Users/your.name/AppData/Local/Arduino15/packages/esp8266/hardware/esp8266/2.4.1/cores/esp8266.

Using the ESP8285 M2, with a warning

Next to the previously discussed M3, there is also an ESP8285 M2 (in fact, there is also an M1). Like the M3 it is based on an ESP8285 that has 1Mb on board.
It has 11 I/O pins (including Rx and Tx) as well as an ADC.

An advantage though is that it is available on a development board that has a Vcc and Grnd connection per I/O pin (but read the caveat below), whereas e.g. the Wemos D1 mini only has 1 ground and 3V3 pin.
The board can be programmed directly from the Arduino IDE, but it does not have a circuit present that puts it automatically in flash mode. Therefore it is necessary to push the Flash button, hold it and then press the Reset button and only then release the Flash button.

There is a big caveat with this board though: The row of positive pins opposite the I/O pins, is connected to the INPUT voltage, so even if you feed this board via USB, you get 5Volt on the “V” pins. If you feed your board 3.3 Volt via the 3V3 connector……there is no voltage at all on the ‘V’ pins. Whether this is a design flaw or intentional I do not know.