Audio meets IoT (DIY)

I am running RPis and (HAT) DACs for years by now. One thing's been bothering me all the time. 

The powering scheme. The handling of it to be exact. I've still been pulling plugs all the time for that very purpose. Pulling plugs in a lab is one thing, doing that in a living room is simply not acceptable on the long run. How do you tell your wife or kids how to turn on that DIY monster-stereo !?!? Push that plug, next this one and finally that one. Simply not possible.

I finally thought to get some Wifi controlled mains plugs in place. Neither Dect, infrared nor bluetooth controlled devices made it on my wishlist.

Let see what IoT (Internet of Things) projects are offering...




OK. You'll find all kind of Wifi controlled IoT plugs over at Amazon for around 10$/€ each nowadays. 

E.g. I own and use some of these:




The small boards you see below the power plug host the very same microcomputer (MCU) - an ESP8266 - as being used in that powerplug and usually cost around 5€/$. 

Such devices are the basis for numerous IoT DIY projects out there. The main issue I had/have with these IoT devices and that has been a showstopper for years is:

Their stock software. (or better firmware) 

First, many of these devices force you to open up whatever account in the cloud, 
connect to whatever cloud site, transfer whatever data. 
Then, what about security !?!? Hmmh. No idea. Greyzone.
Finally, maintenance !?!? A ten bucks device, maintenance !?!? You gotta be kidding.

From my perspective IoT - done that way - without me. I'm out.


OK. OK. Calm down. There are solutions.

I stepped over a nice project. It's called Tasmota
Tasmota offers an "image" similar to the RPi OS images, which many of you know by now,  which gets written (flashed) directly to the board. It actually won't require any programming skills to get going.

The Tasmota opensource project is well established and widely used. It's well maintained.
And there's a huge community supporting the efforts. These tiny boards are actually not a competition to the RPi. These boards serve a slightly different purpose.

The Tasmota opensource firmware project has been so successful because many of these cheap IoT and quiet useful devices have the same MCU chip family inside.

We'll take a closer look @ The ESP8266 MCU from chinese manufacturer Espressif. 

The model ESP-12E is the one we're looking at. It comes with 4 MiB flash memory. 
Yep. I know. There's a newer ESP32 MCU out there. IMO that's overkill. 
It's IMO not needed for controlling a couple of relays. 
Beside that, and that's IMO a key subject, the ESP8266 is very well (community) documented and supported.

You can easily flash (program) these ESP8266s with Tasmota firmware via serial interface  (We'll get to that later on). With Tasmota on the device you'll get yourself into the driving seat. Tasmota is well maintained, no need for online accounts and the security risks are IMO seriously lowered

You can usually flash these mains plugs if there's a ESP8266 inside. And many of these plugs come with ESP8266 inside. The one shown above also has that chip inside. On the Tasmota pages you'll find device reference lists. These are not 100% up2date though. Google will be your best friend again. Just google the brand and device name and add "Tasmota" to the search criteria.
Flashing these mains plugs requires a bit of effort. You usually need to open the case. Then you have to solder 5 wires (VCC/GND/RX/TX/IO-0) to the board to be able to set up a serial connection to your PC. That serial link you connect to the PC through a USB to serial adapter... 

OK. Let's stop here. That's NOT what I'm going to talk about in this article. 




What I'm going to show is how to get something like below going:






What I'll describe is the setup of a so called NodeMCU style ESP8266 proto board. You'll find numerous versions of these boards over at Amazon.
These proto boards come with all basics onboard. The ESP8266 MCU,  a USB interface (for initial programming), a USB to serial converter (UART) , Wifi incl. antenna, regulators. 
You just have to connect the boards to a PC and start the programming.

Later on you attach e.g. a relay board as shown above. Done.

And all that runs from a single 5V supply. 

Sounds easy!! 

It actually isn't. ;)

Otherwise I wouldn't write all this. It took me quite a while to figure it all out. Most info on the web is pretty fragmented. 
However. Once you know how it's done, e.g. by following this guideline, you'd get things done rather easy! 

Obviously first you need to get yourself one of these proto boards -- if you really intend 
to get into all this.


*************************************

Disclaimer:

Don't blame me if something goes wrong! As usual, no guarantees from my side. 
All you do, you do at your own risk. And I also can't guarantee that all these boards
out there work the same way as described in this article. 
With this blog post I'll give directions. Nothing more and nothing less. Don't expect any further support from my side.

And be very careful while working with high voltages or batteries! You better know what you're doing.

****************************************


Let's continue. Sourcing first.

Search ESP8266 over at Amazon. Numerous NodeMCU proto boards will pop up.

Minimum feature set:
  • ESP-12E
  • USB interface onboard
  • NodeMCU style
  • CP2102 serial converter

I'd go for the one with the most favorable comments. Most boards look very similar. And that's what they are. Very similar.
There are quality issues reported on this or that board. Usually soldering issues and poorly soldered GPIO headers.
I prefer the boards without presoldered GPIO header. If I need the headers I can solder them myself. Usually I just solder wires straight to the board.

I also ordered a breadboard, jumper cables (male/male and female/male) and a dual-relay board. Actually I bought 3 relay boards and 3 ESPs. That really brings the average cost down. 

Advise: Before powering up the board - inspect the board thoroughly. Check  for soldering issues and obvious production related quality issues and dirt.


Tasmota Initial flashing 


The ESP board needs some digital food first. I will now explain how to get these little guys programmed via RPi running Raspbian. 

That's pretty much the first task. To run the initial flashing process the ESP8266 has just to be connected and powered via its USB port. You simply attach a uUSB cable. 
And that'd already be it from the initial HW setup perspective.








Why RPi for flashing? RPi because most of you will have an RPi around.  Of course
any PC could do the same job. But the process wouldn't be the same.

Actually. Why not using the RPi as IoT device?

Yep. A very vaild idea. Using the GPIOs of a RPi would pretty much cover the ESP8266 spectrum. 

However. A RPi is more complex, more expensive, more sensitive, requires a SD-card, draws more power, asf, asf...
Such an ESP board simply fits that special IoT purpose much better.


The next question will be what OS to use for the flashing task!?!?
Raspbian. That's what I propose.
Most of my blog articles refer to piCorePlayer (pCP) as RPi audio OS of choice.

Why Raspbian now? Raspbian because it's IMO simply the preferred generic OS of choice for a RPi. It's SW is 100% supported by the RPi foundation. They use Raspbian as base. The SW spectrum is much wider compared to pCP. It can be easily installed, is much easier to handle and there's a lot of documentation about it.
You don't necessarily need to install a new/fresh Raspbian though. You could also use e.g. Moode Audio, Volumio or DietPi for the job. These are all based on Raspbian. 
As usual, there are numerous other ways of getting this done. I've chosen this one.

I think the actual flash process can be executed this way in a pretty straight forward manner, even by people with close to no Linux skills who happen to own and run a RPi. 


Step-By-Step Tasmota flash instruction 


This chapter describes the initial flash procedure. Only the "initial" Tasmota flashing needs to be done via RPi btw. Further updates can be done through the Tasmota firmware via the internal Tasmota web interface later on. 

As prerequisite, you'd need to have ssh enabled on your Raspbian installation.   


If you - at this point - don't know what I'm talking about or you are afraid of entering this or that command from a commandline (you don't have to) better skip this little project.



OK. Let's get it done.

#######################################################
###
### Power up and boot the RPi WITHOUT ESP8266 attached
###
#######################################################

### ssh login to Raspbian 

### become superuser
sudo su

### update packages
apt-get update && apt-get -y upgrade

## check Debian python version ( some other systems might run incompatible python 3.x)
python --version

Python 2.7.16

### install python package manager - pip
apt-get install python-pip

### install ESP programming tool
pip install esptool



### download latest firmware binaries from Tasmota to the RPI /tmp directory
### you'll find the files over at: 
### https://github.com/arendst/Tasmota/releases
### choose the minimal version and your country specific version
###
### you could also simply copy/paste below 3 commands
### The first line identifies the most current Tasmota version. 
### The next line downloads the minimal image
### and the final command downloads the english standard version

VERSION=$(curl -s https://github.com/arendst/Tasmota/releases | grep "Version" | head -1 | awk '{print $2}')

wget https://github.com/arendst/Tasmota/releases/download/v$VERSION/tasmota-minimal.bin -P /tmp

wget https://github.com/arendst/Tasmota/releases/download/v$VERSION/tasmota.bin -P /tmp


### at the time of writing 
### Tasmota version 7.2.0 was the latest version
### update 12/30/2019 : Tasmota 8.1.0 has been released. All below still applies. 

### check if the download went OK.

ls /tmp/*bin

/tmp/tasmota.bin  /tmp/tasmota-minimal.bin

### you see two files?  Great. We're on a good way!


### clear dmesg buffer
dmesg -C

########################################################################
###
### NOW attach the ESP8266 board to a RPi USB port 
###
########################################################################
### we check if the ESP has been recognized
dmesg


[   97.461874] usb 1-1.4: new full-speed USB device number 4 using xhci_hcd
[   97.568300] usb 1-1.4: New USB device found, idVendor=1a86, idProduct=7523, bcdDevice= 2.54
[   97.568316] usb 1-1.4: New USB device strings: Mfr=0, Product=2, SerialNumber=0
[   97.568328] usb 1-1.4: Product: USB2.0-Serial
[   97.577059] ch341 1-1.4:1.0: ch341-uart converter detected
[   97.581746] usb 1-1.4: ch341-uart converter now attached to ttyUSB0


### Looks similar to your? That's great. 
### Note: 
### There are other models that come with the supposedly 
### better USB to UART controller CP2102 the above listed ch341 uart will also do

### What just happpend? The ESP device is recognized. 
### The onbard UART converter got attached to ttyUSB0 - the serial interface device.
### ttyUSB0 is the virtual device on your RPi that's gonna be used for flashing. 

### If ttyUSB0 or similar ttyXXX is not shown - the UART somehow gets not recognized
### it could be a hardware issue - or a cable/connection issue
### or some config issue
### see e.g. https://www.raspberrypi.org/forums/viewtopic.php?t=160400

########################################################################
###
### YOU NEED TO STOP AT THIS POINT IF ttyUSB0 is not shown.
###
########################################################################

otherwise continue....

################################################
###
### now the environment is set
### next we can start programming the ESP
###
################################################

You're still logged in via ssh and you still have root user permissions at this point!

### 1st a ESP communication test
esptool.py --port /dev/ttyUSB0 read_mac

esptool.py v2.8
Serial port /dev/ttyUSB0
Connecting....
Detecting chip type... ESP8266
Chip is ESP8266EX
Features: WiFi
Crystal is 26MHz
MAC: 84:f3:eb:e3:8e:92
Uploading stub...
Running stub...
Stub running...
MAC: 84:f3:eb:e3:8e:92
Hard resetting via RTS pin...

### above output looks similar to yours?? Great. The ESP talks to us. Go ahead.

### make sure that we start with a clean device - we erase the flash storage first
esptool.py --port /dev/ttyUSB0 erase_flash

esptool.py v2.8
Serial port /dev/ttyUSB0
Connecting....
Detecting chip type... ESP8266
Chip is ESP8266EX
Features: WiFi
Crystal is 26MHz
MAC: 84:f3:eb:e3:8e:92
Uploading stub...
Running stub...
Stub running...
Erasing flash (this may take a while)...
Chip erase completed successfully in 9.6s
Hard resetting via RTS pin...

### above output similar to yours??  Great. Now we've got a clean base.  Go ahead.


### Finally: flash Tasmota firmware

### flash minimal image first as precaution 
### (a memory issue might occur if flashing the actual large image first)
### the process will take a while. The tool shows a % progress counter.

### flash minimal image (below command goes in one line!)
esptool.py --port /dev/ttyUSB0 -b 115200 write_flash --flash_freq 26m --flash_size 1MB -fm dout 0x0 /tmp/tasmota-minimal.bin


esptool.py v2.8
Serial port /dev/ttyUSB0
Connecting....
Detecting chip type... ESP8266
Chip is ESP8266EX
Features: WiFi
Crystal is 26MHz
MAC: 84:f3:eb:e3:8e:92
Uploading stub...
Running stub...
Stub running...
Configuring flash size...
Flash params set to 0x0321
Compressed 382304 bytes to 269164...
Wrote 382304 bytes (269164 compressed) at 0x00000000 in 23.8 seconds (effective 128.5 kbit/s)...
Hash of data verified.

Leaving...
Hard resetting via RTS pin...


### above output looks similar to yours?? Nice. 
### You just flashed your first Tasmota image successfully. We're not done yet.


### flash standard image (below command goes in one line!)
### replace "tasmota.bin" with your chosen country specific image name
esptool.py --port /dev/ttyUSB0 -b 115200 write_flash --flash_freq 26m --flash_size 1MB -fm dout 0x0 /tmp/tasmota.bin


esptool.py v2.8
Serial port /dev/ttyUSB0
Connecting....
Detecting chip type... ESP8266
Chip is ESP8266EX
Features: WiFi
Crystal is 26MHz
MAC: 84:f3:eb:e3:8e:92
Uploading stub...
Running stub...
Stub running...
Configuring flash size...
Flash params set to 0x0321
Compressed 581776 bytes to 400933...
Wrote 581776 bytes (400933 compressed) at 0x00000000 in 35.5 seconds (effective 131.2 kbit/s)...
Hash of data verified.

Leaving...
Hard resetting via RTS pin...


### above output similar to yours?? Wow. You got your Tasmota firmware finally loaded!


### Now power down the RPi (job done) and disconnect the ESP8266 from the RPi 

### You just flashed your first ESP8266. Congrats!





Next comes: 

####################################################################

The Tasmota network setup

### to execute this step you'll need your home Wifi network ID (ssid) and Wifi password.
### go, get it now! 
### Keep in mind. Only the 2,4GHz Wifi networks are supported! Do not mix up the
### credentials.


### attach the ESP8266 to any 5V uUSB power source
### we need to power up the ESP8266 now

### Tasmota needs to know where to connect to. 
### For the very initial Wifi setup, Tasmota -- and that it'll do only once !!! -- boots as 
### Wifi AP (accesspoint). 
### Tasmota then offers an initial network configuration web-interface.
### that one allows you to enter your own home Wifi network credentials

### how to access that initial Tasmota AP ??  Check: Is your ESP powered up?
### you could use your mobile phone to connect to the initial Tasmota AP / Wifi network
### lookup available Wifi networks on your phone 
### you'll find a new network called ""Tasmota-xxxx"" (xxxx = 4-digit number).  
### connect to it

### ignore a potential "no internet access" message of your phone once connected
### internet is not needed 

### now open your favorite browser
### enter:  "192.168.4.1"
### the initial Tasmota network config screen shows up 
### enter your wifi home network ssid
### enter your wifi password 
### Hint: make password inputs visible by clicking that empty box
### Why?: Entering wrong data at this point, will make the Wifi access to your network fail, 
### and that will require you to redo the whole flash procedure!!!

### "save" your inputs!

### after pushing "save" the ESP restarts and connects to your home Wifi network
### you can reconnect your phone to your home Wifi network now

### find out the new IP address that's been assigned to your ESP board (e.g. via router,
### nmap, ???)
### while you are on the router, reserve the IP address for your ESP. You
### don't want to get that IP address randomly changed once in while!
### fixed IPs are very important - especially in an IoT environment!

### again, if the ESP won't show up on your Wifi network, something went wrong with your 
### Wifi credentials and you'd have to redo the flashing procedure.

### NOTE: Only 2.4GHz Wifi networks will work!


### Remember or write down that new IP address. You'll need it quite often later on.

I hope everything went fine up 2 here.


The Tasmota Setup

### Now we need to configure Tasmota and the ESP.

### In this example I'll show you how to setup a 2-relay board. The one you've seen
### on the image above.

### enter the new ESP8266 IP network address into any browser in your home network
### to get access to the Tasmota web server.

### once connected, you'll initially see this:





### we need to change this.

### Enter the Configuration menu





### Enter Configure Module






### Select "Generic (0)" from the pulldown and "Save"

### Now the ESP will restart
### Once that is done go back to the module menu






### As you can see the layout changed.
### And you also see that I already changed the D1 and D2 outputs 
### to Relay with inverted (i) logic.
### It needed to be inverted logic to make the relays work properly! (on my relay board!)
### "Save" . The ESP will restart again.

### Leading to this overview menu:






### Now you have two switches matching the dual-relay setup we've been configuring.
### You could now connect a relay board with e.g. IN1 to D1 and IN2 to D2.

### Since Tasmota 8.1.0 you can assign names to above "Toggle1/Toggle2"
### Memorizing all the ToggleX is simply impossible.
### Got to "Console" menu  
### e.g. I use 2 relays two turn on/off 5V DC rails for my RPi and my DAC. 
### You'd smply enter e.g. "WebButton1 DAC" + "return" and
### than "WebButton2 RPI" + "return" . 
And that'd be it.

### Next we gonna change the name of your ESP
### We do that in the "Configuration/Configuration Others" menu






### you can disable MQTT for now and enter "Friendly Names". 
### save and the ESP will restart again.

### Wow. What a journey.
### We're almost done.

### Now we need to configure the default "PowerOnState" of the relays.
### At ESP8266 PowerOn the relays shall remain OFF.

### This config can be done only via console (Tasmota Main Menu: Console):





### You enter "PowerOnState OFF" in the command field and press return.
### Make sure the RESULT printout confirms your action.

### ADVISE:

### Never attach a relay board to the ESP 5V pin and use ESP attached USB power.
### This will fry your ESP! (Why I know that??? Don't ask.  ;)  )

### As usual. Everything you do, you do at your own risk. If you work 
### with high voltages or batteries be very careful!

### Run a few test hours on your setup to see if everything runs stable.
### There are boards that might not be of highest quality!


If you have questions or improvement proposals, we can discuss it over at DIY-Audio.
For further an more in-depth info I refer you to the Tasmota pages.


Here's my ESP8266 setup hooked up to the two DC rails of the Allo Shanti power supply, powering my RPi 4 and Khadas Toneboard:






One major point is still missing. 


Tasmota Control

There are several ways of controlling your new setup

  • Web Browser - you just enter the IP address of your ESP8266
  • Android - "Tasmota" app
  • Android - "http shortcuts" app
  • iOS - Apple "shortcut"  app
  • command line - curl
  • and more
Of course you can also integrate above into your home automation environment. Tasmota even offers MQTT. But this discussion I consider out of scope.

Basically the ESP8266 gets controlled via web or http request commands. 
The Tasmota command list and descriptions you'll find over here.

Beside controlling your ESP via web-browser, you'll find an Android app called "Tasmota".  It's very basic. But it's working.

On iOS  you can define direct interaction with the ESP by using the Apple Shortcut app.
Apple Shortcut can handle so called http requests. If I find time I might describe how that'd be done. It's rather simple. You can add just the http request, very similar as shown down in the Annex.


Very powerful is using the commandline utility "curl". I'm showing some examples down in the Annex. 

Tasmota also offers a "Timer" feature for automatic switching. I use it for different purposes, such as coffee machine, asf. ;)


Tasmota Maintenance

Tasmota is a work-in-progress project. Updates pop up regularly. Keep an eye on that!
You can load these via Tasmota WEB-GUI.

Advise1:
I prefer do download the .bin files on my PC. I  do not use the Tasmota Update-Over-Webserver function.

Advise2: 
Before you run the update via web-GUI, make a backup of your Tasmota config.
In case something gets overwritten you can simply reload it.

Advise3:
I e.g. do have the issue on my Gosund power-plugs that I first have to load the minimal image before I can upload the normal image. Keep that in mind if an upload/update fails!

Advise4:
Make sure the update over Wifi runs over a stable Wifi connection. You better avoid that the Wifi connection breaks during the upgrade. 
You might want to get your Tasmota device closer to the router/AP before running the update.



IoT Power Consumption (blog update: 12Feb2020)


While working on the subject I found that one subject wasn't particularly well discussed out there. 

That subject - meanwhile - became one of the main issues I'm having with IoT. 

All these switches consume quite some power themselves. Some more, some less.

The MCU consumes power and the relays consume power.

The MCU can consume quite some power if there are no power saving features offered 
or if offered if these are not enabled. Even after enabling these features there'll still be idle power consumption of course. Then there are different levels of power-saving for these MCUs.

Especially if in ON state the relays involved let the power consumption skyrocketing.
Active relays can draw quite some current. Well engineered relay-boards should go into a low power state after being switched on. The "hold" current would be much lower than the the very short "switch" current. So far I havn't seen m(any) of these relay boards running low-power modes.
That's also an issue I have with above shown relays. These won't offer a low-power mode.

To be honest - I really underestimated the impact of the whole subject.

Some basic rules I put up for myself to begin with.

  1. Make smart choices where to put these switches. You save most power by not having pretty useless devices idling around.
  2. When choosing a relay or IoT switch the focus should be put on idle and active power consumption. 
  3. Keep the ON state periods as short as possible. Otherwise you'll burn, or better, waste a lot of energy. 
  4. Use the power-saving features of Tasmota - otherwise the ESP also burns a hell lot of power.


Keep in mind. The more IoT stuff u use the worse it gets.

If I find time I'll run some measurements. I might also reconsider my choice of devices if I can find better/alternative solutions.



Final words

There'd be a lot more to talk about. All this IoT stuff is a huge playground. 

I do hope that most people get a nice headstart or at least an idea about what it takes to enter the IoT world without using devices that you can't control yourself.


Enjoy.


#########################################################################
#########################################################################



ANNEX1

curl control examples


To control your ESP environment from a linux commandline or script ""curl"" can be used.
That's IMO a very powerful way of handling the IoT environment, without the need for home automation servers.
Below I show some examples how curl can be used in this context:

IP=192.168.x.xxx

##status relay d1
curl -s http://$IP/cm?cmnd=Power1

##on relay d1
curl -s http://$IP/cm?cmnd=Power1%20On

##off d1
curl -s http://$IP/cm?cmnd=Power1%20Off

##on d2
curl -s http://$IP/cm?cmnd=Power2%20On

##off d2
curl -s http://$IP/cm?cmnd=Power2%20Off

## Using the "backlog" command allows to chain up commands

## turn d1 on and 10 seconds later turn d2 on 
curl -s http://$IP/cm?cmnd=Backlog%20Power1%20On%3BDelay%20100%3BPower2%20On

## turn d2 off and 3 seconds later turn d1 off.
curl -s http://$IP/cm?cmnd=Backlog%20Power2%20Off%3BDelay%2030%3BPower1%20Off


Note: delay/s to be given in x seconds * 10

## I can use this "chain-and-delay"  method to e.g. turn on my DAC first, followed by the RPi and finally the amp
## by using a single button/shortcut. 
## turning these devices OFF, would go the other way around. for this there'd be a second button.

Note: "%20" are spaces and "%3B" are semicolons in above examples. It has to be done that way!









No comments:

Post a Comment