We use cookies to personalise content and ads, to provide social media features and to analyse our traffic. We also share information about your use of our site with our social media, advertising and analytics partners who may combine it with other information that you've provided to them or that they've collected from your use of their services.

Voice control (Amazon Echo & Alexa)

Why would you try to control you home automation with your voice?  Because its possible:)  With Alexa's price tag falling below USD 100, the time has come to get it connected with the PLC.

 Alexa

I used the latest Amazon Echo (2) to connect it to my PLC-base home automation system via a small Raspberry Pi server. The first step involved installing pimatic (at this moment it is the only system I found that works with the new Echo).  The pimatic platform can itself be used to control home automation system based on RPi.  With some scripting it can be used as an interface to the PLC.  I prepared two pimatic plugins:

- pimatic-wago, which adds new devices to the pimatic platform,

- wago-common, as an universal module for interfacing with a PLC.

What are the necessary steps?

  1. Launch a fresh and clean Raspberry Pi, get a Raspbian drive image from https://www.raspberrypi.org/downloads/ and follow the instructions posted at https://www.raspberrypi.org/documentation/installation/installing-images/README.md
  2. Install nodejs in line with the instructions from https://nodejs.org/en/download/package-manager/
  3. Install the "n" packet, which allows for a change of the active version of nodejs, as pimatic does not run under the latest nodejs.  The needed steps are taken from https://www.abeautifulsite.net/how-to-upgrade-or-downgrade-nodejs-using-npm.  It boils down to running "sudo npm install -g n", and "sudo n 4.8.3" to downgrade nodejs.
  4. Install pimatic according to the instructions from https://pimatic.teamemo.com/Guide/Getting-Started, ignoring, obviously, the steps related to installing nodejs.
  5. Modify the config.json in the pimatic directory:
    • set the admin password in the 'users' section,
    • change the port of the www server in the 'httpServer' section from 80 (which must be free for pimatic-echo) to, for example, 8011,
    • add the plugin pimatic-echo in line with the instructions found at https://www.npmjs.com/package/pimatic-echo, what boils down to adding in the 'plugins' section at the first position:

      {
      "plugin": "echo",
      "port": 80,
      "active": true
      }

    • add my plugin "pimatic-wago" in line with the instructions found at https://www.npmjs.com/package/pimatic-wago, what means adding in the 'plugins' section at the last position:

      {
      "plugin": "wago",
      "addressPLC": "192.XXX.XXX.XXX,
      "visuFile": "visualization_name",
      "active": true
      }

  6. Run pimatic with "sudo node_modules/pimatic/pimatic.js" 

The first time you run pimatic, it downloads all the referenced plugins, what means that it will take a lot of time.  I suggest you read the messages displayed on the screen to check if the plugins are installed in their latest versions.  If pimatic installs an outdated file of my plugin (pimatic-wago), you can do it manually running 'npm install pimatic-wago' in the pimatic-xxx directory.

Once pimatic is ready you can check if it works by opening a web browser, entering the IP of your RPi (the IP can be checked with 'ifconfig') and adding the configured port, for example: 192.168.1.200:8011.

Once you log in to pimatic, go to Devices -> Add device. In the Class field choose WagoSwitch and fill:

  • ID - the name, which we want to see on the screen of the pimatic system
  • tapAddr - the name of the variable, which should be tapped (assign 1 followed by 0) to control the outplut, for example "PLC_PRG.Light1_Visu"
  • stateAddr - the name of the variable controlling the given output, for example ".OUT1"

 pimatic new device

In the "Echo" part, which is needed by Alexa, you need to fill:

  • the checkbox near the "name" tag; and enter the name of the output with "Echo" at the front, for example "EchoDining Room. 
  • the checkbox near the "active" tag and switch the button to 'Yes'

pimatic new device 2

After you click 'Save' you should leave the menu 'Devices' by clicking the 'Back' button. To add the new device to a given scree, click the gearbox in the top-right corner, then "Add new item", and click the line with the new device we have just added.  Here is how an example of a screen can look like:

pimatic screen

 

Now you should check if the new device we have created is discovered by the Echo.  Log in to alexa.amazon.com, click "Smart Home", click "Devices" and "Discover". At the same time on the pimatic site click "Devices", and "Discover devices" at the top-right corner. After 20 seconds Echo should finish searching and under Smart Home -> Devices you should see the discovered elements:

pimatic alexa devices

 

If your device is on the list you are ready to test the voice commands like: "Alexa, turn on/off XXX", meaning, for example: "Alexa, turn on dining room".  If all goes well, Alexa will reply "OK", and the light will turn on :)

* * *

As a reminder - a few words about how to prepare your PLC in CoDeSys for such a form of communication:

The first step is to create a new visualization (for example named "example").  Then add all your elements, which should be available to pimatic (or any other external software):

  • in case you want to control lights, add a rectanlge, which should change its color in line with the state of .OUT1, and which, when clicked, taps the "PLC_PRG.Light1_VIS" variable,
  • in case of sensors, add a text field with "%s" inside, which shows the value of the "PLC_PRG.Sensor1" variable,
  • in case of movement sensors add a rectangle singaling the response of the sensor, which changes color in line with the state of "PLC_PRG.MoveSensor1" variable

pimatic wago Codesys1

Your new visualization should be made available via www (right click on the name of the visualization -> Object Properties -> checkbox near Use as -> Web-Visualization)

pimatic wago Codesys2

The visualization should be zipped/packed: go to Resources tab -> Target Settings -> Visualization tab -> checkbox at "Compression".

pimatic wago Codesys5

Once you program is compiled (go to on-line mode) the new file (example_xml.zip) will be placed in the "plc" directory at your PLC:

pimatic wago Codesys4

This file will be downloaded by the plugin wago-common, then extracted and analyzed.  Each variable (for example: PLC_PRG.Light1_VIS) will be assigned the address in the memory (for example 4,101240,2,1), which is later used for communication with the PLC.