Category Archives: HowTo’s

HowTo: SMS Alarms

The problem

Just last week a house burned down to the ground here in my hometown. The family of two adults and three kids just made it out. This reminded me of the importance of alarms. My family as any family I guess have had some incidents over the year. In our case with a four floor building, cellar, office, apartment where we live and the attic it can be hard to, despite alarm equipment, to recognize what is happening in the cellar when your are in the apartment watching a film or even worse, sleeping. We have had our incident over the years as most people do. I use VSCP here of course. This system is intelligent enough to close down functionality  when things get bad. But a few weeks ago I had a bad incident. I have a VSCP node that control the water boiler in the cellar for warm water. This is a very simple setup. A relay, a sensor and a control node. I can thus remotely control sense and control the temperature of the hot water in the house. Sitting in the office I noticed a strange sound. I usually play music loud so it had been going on for some time I guess. I went around the house investigating the sound and when I came down to the cellar I heard that the boiler literally boiled hard. Not a good thing. There is a risk for a steam explosion from this so I got really worried. I turned of the power. Opened valves for hot water in the office and got steam and boiling water coming out. I checked the control unit and it had turned of the relay. Still the temperature was way over the limit. There is an independent security unit inside the boiler that should turn off power when it get over a certain temperature. Apparently this mechanism had not done it’s job. Also the security valve that prevent to hot water from going out in the system also malfunctioned otherwise I should not have got steam hot water out of the office valve. Two faults. The pressure security mechanism on the other side worked. But the boiler never got up to the 10 bar needed to release it. So things were actually not to bad. Bit could gone worse if I had been away for example. I later checked the cause and detected that the relays had gone stuck in the on position.  Something that happen to relays some times when load is heavy or control is a bit jumpy.  I also tested the overheat protection and it worked as it should, probably it would have just needed a bit more time to cut the power. The blender protecting the water line from to hot water was the only faulty component. Still this was a bit scary. I need to be alarmed if this happen again.

GSM Modem

I previously sold professional GPRS modems in the FrogShop from Caimore. Still have some around so I decided to put one to work. You can find may similar on Aliexpress or ebay. and even lower cost modules intended for Arduino or similar as the one below. They all have there pros and cons.

SIM Card

Next thing is to find a SIM card. In Sweden this is not a simple thing if you just want a module that sends a few SMS’s per year. The solutions available are highly overpriced. I know there are other and better options available in other countries. I just select a cash card to get started.

Software

Initially I thought about writing a driver for the VSCP daemon and this may still be an option, we will see. I have for a long time used Smstools a wonderful package for uses like this. With the help of the decision matrix of the VSCP daemon it will be possible to do plenty without a driver. So this is the solution I go for. There is a nice getting started tutorial for smstools here. Thus no need for me to write one again. The only thing i ned to do to get things working is to set the device and the baudrate lines in /etc/smsd.conf file
device = /dev/ttyUSB0
as I use an USB adapter for serial communication. And
baudrate = 115200
as i use that higher speed. Then I just restart the smsd daemon with
sudo service smsd restart
and everything is working.

Sending SMS’s

We are just interested in sending SMS’s here so we stick to that. But receiving SMS’s (we look at that in a later post) is also equally simple. To send a SMS one just copy a formated file into the folder /var/spool/smsd/outgoing. So to send an SMS to +467012345678 one send a file
To: 467012345678
ALARM: The water in the boiler is way to hot!
Write this to a file and copy it to /var/spool/smsd/outgoing and the SMS will be delivered to the set recipient. You can set the header to
To: 467012345678
Flash: yes

ALARM: The water in the boiler is way to hot!
to send a flash SMS (is displayed on the users screen directly but not saved). It can hardly be simpler.

Voicecall

Another possibility is to make a call to a receiver instead. Add Voicecall to the header like this
To: 46730533146
Voicecall: yes
TONE: 5 #,#,#
Here the mentioned precipitant will receive a call and when answered five groups of three “#” DTMF tons will be sounded. Tones that can be used to tell the recipient about the cause of the alarm. I will for instance use the number of “#” sounds as an indication of what floor the alarm happened. Possible with different tones for different alarms.

vscpd and smstools

So now lets put this to work for our VSCP based system. Alarm related events is collected in the class CLASS1.ALARM. The CLASS1.ALARM, Type=2, Alarm occurred is intended for alarm conditions.  So if we let sensors that generate alarms send this event and trigger on it in the decision matrix of the VSCP daemon we can send our SMS and voicecalls from there. The zone and subzone can be used as markers for where the alarm occurred. The originating GUID can also be used to identify this.
<row enable="true" groupid="alarm" >

	<comment>Send alarm.</comment>
	
	<mask priority="0" class="65535" type="65535" guid="00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00" >
	</mask>
	
	<filter priority="0" class="1" type="2" guid="00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00" > 
	</filter>
	
	<action>0x10</action>
	<param >/home/vscp/doalarm.py %zone %subzone event.guid %isodate %isotime "Current boiler temp is %vardecode:[boiler-temp]"</param>
	
	<allowed_from>0000-01-01 00:00:00</allowed_from>
	<allowed_to>9999-12-31 23:59:59</allowed_to>
	<allowed_weekdays>mtwtfss</allowed_weekdays>
	<allowed_time>*-*-* *:*:*</allowed_time>
</row>
So what do we have here? We trigger on any alarm occurred event regardless of priority and from which originating GUID (which unit) it comes from. It would have been possible to do several DM rows for different GUID’s (originating nodes) of course to handle alarms from them different. The time block (form <allowed from> to the end) just say that this can happen anytime. Leaving this block out has the same meaning. And last the important action.  In this case, execute external program (0x10) and in the parameter for the action the actual program to execute is specified with the path to it (/home/vscp/doalarm.py) is given. The rest of the action parameter line is arguments to this external program.
  • %event.zone – Zone for alarm event.
  • %event.subzone – Subzone for alarm event.
  • %event.guid – GUID for alarm event.
  • %isodate – Date on ISO format for alarm.
  • %isotime – Time on ISO format for alarm.
  • %vardecode:[//alarmtext//] – Specific text for alarm.
The description of the decision matrix is here. Zone and subzone is numericals that can be used to specify the place and equipment that generated the alarm. I have floor plan in zone and a room on that floor plan in subzone. The vardecode writes out the content of the given variable if it is present. We will come back to it later. When this row is added the external script will be executed when CLASS1.ALARM, TYPE=2 is generated by a node and received by the VSCP daemon. It is good if the script can do both voice calls and SMS sends and that both can be sent to a list of numbers.

The doalarm.py script

import sys
import uuid

filename_voice = str(uuid.uuid4())
filename_sms = str(uuid.uuid4())

# Dir that holds outgoing SMS messages
OUTDIR="/var/spool/sms/outgoing"

# Voicel recipients (comma separated list, empty for non)
VOICE_RECEIVERS="4673xxxxxxxx,4676yyyyyyyyy"

# Set to true for flash SMS
bflash = False

# SMS recipients (comma eparated list, empty for non )
#SMS_RECEIVERS="4673xxxxxxxx,4676yyyyyyyyy"
SMS_RECEIVERS=""

SMS_TEXT="An alarm condition has occured!"

# -----------------------------------------------------------------------------

voicetone="5 "
for x in range(0, int( sys.argv[1] ) ):
	voicetone = voicetone + "1,"

voiceRcvList = VOICE_RECEIVERS.split(",")

cnt=0
if VOICE_RECEIVERS != "" :
	for receiver in voiceRcvList:
		print( OUTDIR + "/" + filename_voice + str(cnt) )
		with open( OUTDIR + "/" + filename_voice + str(cnt), "w") as text_file:
			text_file.write( "To: {0}\n".format(receiver) )
			text_file.write( "Voicecall: yes\n\n" )
			text_file.write( voicetone )
		cnt = cnt + 1

SMSRcvList = SMS_RECEIVERS.split(",")

if SMS_RECEIVERS != "" :
	for receiver in SMSRcvList:
		print( OUTDIR + "/" + filename_voice + str(cnt) )
		with open( OUTDIR + "/" + filename_voice + str(cnt), "w") as text_file:
			text_file.write( "To: {0}\n".format(receiver) )
			if bflash:
				text_file.write( "Flash: yes\n" )
			text_file.write( "\n\n" )
			text_file.write( SMS_TEXT+"\n" )
			text_file.write( "Zone={0} SubZone={1} Time={2} Date={3}\n".format(sys.argv[1],sys.argv[2],sys.argv[3],sys.argv[4] ) )
			text_file.write( "{0}".format(sys.argv[5]) )        
		cnt = cnt + 1
This is the script. It is written i Python to make things simple to edit and/or change. You can find the script in the VSCP repository. Just edit the variables in the beginning of the script to fit your setup and you are set to go. Note that the voice call will do five groups of a series of “1”-press DTMF where the count is the same as the supplied zone parameter. In my case this is the floor plan so I know instantly where something happened and runa way and fix it. The SMS also give me more info.

The variable

To come back to the variable that is sent as a parameter. Perfect is to save a  measurement value in another part of the DM. Like this
<row enable="true" groupid="temperature" >
    <mask priority="0" class="65535" type="65535" GUID="FF:FF:FF:FF:FF:FF:FF:FF:FF:FF:FF:FF:FF:FF:FF:FF" > </mask>
    <filter priority="0" class="10" type="6" GUID="FF:FF:FF:FF:FF:FF:FF:FF:3C:00:08:01:92:9D:3D:10" > </filter>
    <action>80</action>
    <param>boiler-temp;float;true;0;0x777;%measurement.float</param>
    <comment>Store boiler temperature in variable</comment>
    <allowed_from>0000-01-01 00:00:00</allowed_from>
    <allowed_to>9999-12-31 23:59:59</allowed_to>
    <allowed_weekdays>mtwtfss</allowed_weekdays>
    <allowed_time>*-*-* *:*:*</allowed_time>
</row>

An alternative way to do it

Another way is to do measurement compares, as in this case check the boiler temperature, you can read more about this method here and it is in many ways the same as the one we described above but more specific for boiler temp alarm.

And next?

Now you should be able to add your own alarms to your VSCP setup. Maybe something that will do good work in your remote cottage, alarming  freezing conditions before bad and costly things happens. In my house we will now sleep much better knowing that we will get alarms when things go wrong. I promised to continue with a follow up on how to react on incoming SMS mesages. This makes it possible to remotely control things as well such as turning on the heat in that remote cottage I mentioned above. I will try to write that story up soon. Time is my enemy, or rather lack of it. Cheers /Ake
   

#VSCP interfaces howto

Something that may confuse new users of VSCP is the GUID of interfaces. Looking at the interface above the CAN4VSCP driver have GUID set to

FF:FF:FF:FF:FF:FF:FF:FE:B8:27:EB:0A:00:02:00:00

which means nodes connected to this interface will come in with GUID’s

FF:FF:FF:FF:FF:FF:FF:FE:B8:27:EB:0A:00:02:00:01
FF:FF:FF:FF:FF:FF:FF:FE:B8:27:EB:0A:00:02:00:02
FF:FF:FF:FF:FF:FF:FF:FE:B8:27:EB:0A:00:02:00:03
FF:FF:FF:FF:FF:FF:FF:FE:B8:27:EB:0A:00:02:00:04
....

If we look at the GUID’s if this machine we see that they all start with

FF:FF:FF:FF:FF:FF:FF:FE

As of  the spec we know that this is a GUID constructed from an Ethernet address. In this case only four of the six MAC digits is used. The

00:02

is the interface id as set by the VSCP daemon and the last two digits are the nickname id for a connected node.

The problem that can occur here is that the interface digits can be different at different runs of the VSCP daemon. The number is just set when the interface is set up and from time to time this setup can happen in different order. So sometime

00:02

can be

00:03

or even

00:11

One can’t tell beforehand.

This is a problem if one want to use the GUID to identify a node. Problematic as this is just what we want in most cases. To trigger on an event from a specific node in a decision matrix the GUID is the item to filter on. Just as in this case

Here we store a temperature measurement in a variable if it comes from a node with GUID

FF:FF:FF:FF:FF:FF:FF:FE:B8:27:EB:0A:00:02:00:01

and measurement index = 1

If the interface ordinal  becomes something else like

00:03

we are in trouble here as the action (store measurement value in variable) will not be triggered.

If we look at the current driver for this setup it is set to

 

<!-- The can4vscp driver -->
<driver enable="false" >
    <name>can4vscp</name>
    <config>/dev/ttyUSB1</config>
    <path>/srv/vscp/drivers/level1/vscpl1drv-can4vscp.so</path>
 <guid>00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00</guid>
    <flag>>0</flags>
</driver>

The “problem” is solved by changing the <guid> tag from an all zero value (or not defined) to a valid GUID.  If not defined or all zero the VSCP daemon will assign a GUID as of above. But if a valid GUID is set this value will always be used.

In my case I can use

01:00:00:00:00:00:00:00:00:00:00:00:01:02:00:00

as I have some assigned GUIDs.  The two LSB’s is still used for the nicknames.

After that change the “problem” is no more and I can filter on the new GUID instead which alway will be the same.

 

 

#ESP8266 #VSCP Development Intro – part 1

Serial port

Connect the USB cable to the board.  You will get a new CDC serial port. You can check which one you get with

ls /dev/tty*

Normally you get /dev/ttyUSB0 or /dev/ttyUSB1 or the like. To make them usable for you you should add yourself to the dialout group

addgroup user dialout

where “user” is your username. You can also do this manually in the /etc/group file or

chmod a+rw /dev/ttyUSB0

if you like brute force.

Loading code to the ESP8266

The ESP8266 CPU can be booted in three different ways:

  • Flash Mode: default booting mode. Firmware is read and executed from the flash memory. Pins need to be set to: GPIO15=0, GPIO0=0, GPIO2=1
  • UART Mode: used to program our board (through a Serial-to-Usb adapter). Pins need to be set to:  GPIO15=0, GPIO0=1, GPIO2=1
  • SDIO Mode: loads firmware from an SDIO card? Pins need to be set to:  GPIO15=1, GPIO0=0/1, GPIO2=0/1

The boot process is described here.

Switching from a mode to another requires to reset the module while the pin states listed is set.

With the nodemcu board and esptool flash uploader all this is handle automatically but if you have another board you must handle this yourself.

esp-open-sdk

You find it here.

Fetch it

sudo git clone https://github.com/pfalcon/esp-open-sdk.git

cd esp-open-sdk

Install it

apt-get install make unrar autoconf automake libtool libtool-bin gcc g++ gperf flex bison texinfo gawk ncurses-dev libexpat-dev python sed

git clone --recursive https://github.com/pfalcon/esp-open-sdk

cd esp-open-sdk/

make
Error when building?  See this post
This builds the standalone version of the SDK (Non FreeRTOS)
You should add the bin directory to he path
echo 'PATH=$PATH:~/development/esp8266/esp-open-sdk/xtensa-lx106-elf/bin' >> ~/.profile

echo 'PATH=$PATH:~/development/esp8266/esp-open-sdk/esptool' >> ~/.profile

PATH=$PATH:~/development/esp8266/esp-open-sdk/xtensa-lx106-elf/bin
PATH=$PATH:~/development/esp8266/esp-open-sdk/esptool

I have installed the toolchain in

 ~/development/esp8266/ 

so change paths above for your installation folders.

Update it (when updates are available)

make clean
git pull
git submodule update

Firmware upload tool

You need the esptool.py to upliad firmware to the module, you find it here
Install with
pip install esptool

First code (blinky)

You find it here

cd ~/development/esp8266/esp-open-sdk/examples/blinky

Type

make

to build it. Remember that the paths above must have been set. You may get some warnings. No problem.

For your information. If you build from source code should be loaded like this:

  • bin/0x00000.bin to 0x00000
  • bin/0x10000.bin to 0x10000

That is just what we do here. Upload to your module with

esptool.py --port /dev/ttyUSB1 write_flash 0x00000 blinky-0x00000.bin 0x10000 blinky-0x10000.bin

No need to press any buttons during flashing. It is handled automatically by the USB DTR circuitry.

Now we are ready to do some real work for our VSCP system with the ESP8266.  If you rather prefer Arduino this howto may be the one you should go for instead of this one. Life is much simpler in the Arduino world. But the degrees of freedom is better if you do it all by yourself. The penalty for freedom is more problems of course. As always.

Another intro is here http://www.electrodragon.com/w/ESP8266_Open_SDK
esp8266 wiki is here.

Part 2 will follow.

Howto: Minimum VSCP wifi Level II node (VSCP PID detector)

In this  howto we will build a simple VSCP level II node that with the help of a PIR motion sensor detects motion and sens this as a VSCP event wireless to a VSCP daemon.

We use the famous ESP8266  for this  and work in the even more famous Arduino environment.  The ESP8266 comes in many flavors but a convenient form factor is the NodeMCU board.  You can buy it on Aliexpress or Ebay at a low-cost (USD 2.2).

There is also a baseboard available for the NodeMCU and I use it as it is convenient when working with a new design. It to is available on Aliexpress and Ebay at a low-cost (USD 1.54).

The last item you need is the PIR sensor, they to are available at low-cost. I bought mine from Ebay (USD 0.99).

If you haven’t set up your Arduino IDE for work with the ESP8266 you need to do that now.  Here is an excellent tutorial on how to do this. Make the blink example work before you move on.

I find the Arduino IDE a bit crude to work with so I use Visual Studio Code instead.  An excellent programming editor that works on all the major operation systems. I tend to use it more and more. But there are other options to such as Atom. Anyway instructions on how to get it working for Arduino development is here.  Make sure to set the “output” tag to a valid location in the .vscode/arduino.json file otherwise upload will be VERY slow.

You also need a VSCP daemon/server running for this example. All info about it is here.

To connect the sensor to the NodeMCU we ned to know about he port mapping for the module which is a bit different from for other Arduino devices. It looks like this

static const uint8_t D0   = 16;
static const uint8_t D1   = 5;
static const uint8_t D2   = 4;
static const uint8_t D3   = 0;
static const uint8_t D4   = 2;
static const uint8_t D5   = 14;
static const uint8_t D6   = 12;
static const uint8_t D7   = 13;
static const uint8_t D8   = 15;
static const uint8_t D9   = 3;
static const uint8_t D10  = 1;

D0 has a blue LED connected to it so it is an obvious candidate for status. D1 can be used to connect to the PIR

The PIR device I have outputs a 3V signal even when powered with +5V so it is OK to connect directly to D1. So the three pins on the PIR

1     +5V
2     Output to D1 (High on detect)
3     GND

The LED on D0 light up when a LOW is written to it. This is opposite to the PIR output. We take care of this in software.

We implement a dump VSCP device here. This is a device that does not handle any of the register reads that “normal” VSCP requires and which don’t have a MDF file either. A dumb device has been 14 set in the header.

We send two events. The heartbeat event (CLASS1.INFORMATION, Type=9) every minute. This event is recommended for all nodes as it is used for node discovery and detection. The other event we send is the detect event (CLASS1.INFORMATION, Type=49 detect) when an object is detected.

The test setup looks like this

and you can find the complete code is here

The heartbeat coming into the VSCP daemon (using VSCP works)

and the detect event

With the event in the VSCP daemon it is easy to add a DM row that for instance light up the lamps in a room. You can also use the rest or the websocket interface to do give visual feedback.

We will follow-up this howto with a post where we use the Expressif SDK instead of the Arduino and use a plain ESP8266 board. But also a ost where we implement a full Level II node that have registers and a MDF and show the advantage we get with a node like that over a dumb one.

An alternative wifi lib is documented here.

 

#VSCP update process #howto

In previous versions of VSCP whenever you did an install all configuration files were replaced with the latest version. This is not true anymore.  Now the new version is instead written as a copy with the date of the install appended to it.  So if you after a “make install” or a “dpkg -i vscpd” want the latest configs you have to copy the backup to the actual config file and restart the VSCP server.

Files that is handled in this way is

/etc/vscp/vscpd.conf
/srv/vscp/dm.xml
/srv/vscp/simtempdata.txt
/srv/vscp/variables.xml

Also if you are on unstable code you should remove the databases before you start the updated server . Use

rm /srv/vscp/*.sqlite3

for this.

Also note that the web sample code is not installed in the install/update process no more. The process to get this subsystem installed with be described later.

#VSCP HOWTO: DM Using timers

Timers. One can wonder what they do in the decision matrix? They even have their own events defined in the CLASS2.VSCP class.

First let us define what a VSCP timer is.

A VSCP timer is a free running 32-bit timer with millisecond resolution.  That is they can hold  0xffffffff = 4294967295 milliseconds which mens they will roll over in about 50 days.

There are actions defined to

There is the following  internal events defined related to timers

To create and start a timer

<row enable="true" groupid="timers" >

  <comment>
    Create timer
  </comment>

  <mask priority="0"
        class="0xFFFF"
        type="0xFFFF"
 GUID="00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00" />

  <filter priority="0"
          class="65535"
          type="23"
 GUID="00:01:02:03:04:05:06:07:08:09:0A:0B:0C:0D:0E:0F" />

  <action>0x60</action>
  <param>
    1;10;timer1flag;true
  </param>

</row>

Here a timer with id = 1 is created. The timer has an initial time set to 10 seconds. When this timer elapses it will set the variable //timer1flag// to true. The last argument is the reload flag. Here it is set to true so when the time has elapsed the initial value will be loaded again and the timer will start again.

In the example above we could have added “;4” at the end of the parameter which would have the effect that the reload would stop after four runs. Default is thus forever.

When the timer is started a CLASS2.VSCPD, Type = 25 (0x0019) Timer started event is generated. When the ten seconds has gone and the timer elapses the  CLASS2.VSCPD, Type = 29 (0x001D) Timer Elapsed event is generated.

We can use either one of these two generated events to do any action periodically like this.

<row enable="true" groupid="timers" >

  <comment> 
    Handle timer elapsed
  </comment>

  <mask priority="0"
        class="0xFFFF"
        type="0xFFFF"
 GUID="00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00" />

  <filter priority="0"
          class="65535"
          type="29"
 GUID="00:01:02:03:04:05:06:07:08:09:0A:0B:0C:0D:0E:0F" />

  <action>0x70</action>
  <param>
    /srv/vscp/timefile;1;%isoboth: Timer with  id=%event.data.int32[0] elapsed %lf
  </param>

</row>


<row enable="true" groupid="timers" > 

   <comment> Handle timer elapsed </comment> 
   <mask priority="0" 
         class="0xFFFF" 
         type="0xFFFF" GUID="00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00" /> 
    <filter priority="0" 
            class="65535" 
            type="25" GUID="00:01:02:03:04:05:06:07:08:09:0A:0B:0C:0D:0E:0F" /> 

    <action>0x70</action> 
    <param> 
      /srv/vscp/timefile;1;%isoboth: Timer with id=%event.data.int32[0] elapsed counter=%event.data.uint32[0]ms %lf </param> </row>

Here some info is just written to a file when the timer is started and when it elapses.   But if you want to send an event periodically instead or do other actions this is the way to do it.

You don’t have to give a variable nor a reload flag when you start a timer. If no variable is given (use ;;) it is just ignored. The reload value will be set to false as default  value, that is the timer will run only once. As the last parameter you can set the number of times the timer should reload before it should stop.  The full documentation is here and here.

One useful use of timers is to handle resend of events. The working is like this

Send the event you expect a reply event from another node.

  1. Create/start a timer that have  period equal to resend intervals for the event you want to send. The timer should  have true for autoloading and the number of autoloads set to the number of resend that are allowed.  You can trigger the creation of the timer  by make a DM entry that triggering on one of the reserved events for example,  CLASS1.LOCAL  or CLASS1.LABORATORY so that when you send this local event  the actual event will be sent (see next point)
  2. In the timer started event send  the event you want to send.
  3. When/If the expected reply  is received pause the timer.
  4. Now if the timer the timer stopped event is detected the reply wait timeout has expired so do timeout handling there by sending another local event or CLASS1.ERROR, Type = 32 Time out.

Can be used for much more of course. Just useful and simple.