Official website

Showing posts with label automation. Show all posts
Showing posts with label automation. Show all posts

Tuesday, May 19, 2015

Inexpensive Z-Wave gateway using a Banana Pro and a RaZberry daughter card

Lately Jubito grows its family by contributions of individuals and vendors and I thank them in public each time I make a reference. More details in social networks e.g.: facebook.

Every piece of hardware I received has an interesting aspect that opens a new challenge to the Jubito ecosystem. For example, Z-Wave integration. An interesting technology, but still, a little bit expensive.

While I'm studying the Z-Way JSON API that allows to execute commands on server side using HTTP GET/POST requests, I'd like to share some early developments for those who want to learn the basics and for the adventurous. In this post I will deal with the RaZberry module by Z-Wave.me on a Banana Pro.

Since I currently owned only one device (Fibaro wall plug) I will focus on SwitchBinary Command Class (0x25/37). The Switch Binary Command Class is used to control all actuators with simple binary (on/off) switching functions, primarily electrical switches.

Those who have experience with the module will probably have a complete understanding of what I'm talking about.

But let's take a look at some methods. The command to execute is taken from the URL. All functions are executed in form:
http://YOURIP:8083/<URL>

  • http://localhost:8083/ZWaveAPI/Run/controller.AddNodeToNetwork(1) - Start inclusion [1].
  • http://localhost:8083/ZWaveAPI/Run/controller.AddNodeToNetwork(0) - Stop inclusion.
  • http://localhost:8083/ZWaveAPI/Run/controller.RemoveNodeFromNetwork(1) - Start exclusion [1].
  • http://localhost:8083/ZWaveAPI/Run/controller.RemoveNodeFromNetwork(0) - Stop exclusion.
  • http://localhost:8083/ZWaveAPI/Run/devices[n].SendNoOperation - Refresh device list.
  • http://localhost:8083/ZWaveAPI/Run/devices[n].instances[0].SwitchBinary.Set(value) - Binary values accept 0-99 for switching off and 255 for switch on.
  • http://localhost:8083/ZWaveAPI/Run/devices[n].instances[0].SwitchBinary.data.level.value - Get the state of the device (true->on, false->off).
  • http://localhost:8083/ZWaveAPI/Run/devices[n].instances[0].Meter.data[s].val.value - Measures the amount of electrical energy in kWh that was consumed.
  • http://localhost:8083/ZWaveAPI/Run/devices[n].data.givenName.value='<A new name>' - Rename a device [2].

[1] You can include devices by pressing the 'Inclusion' button. This turns the controller into an inclusion mode that allows including a device. A led on the controller indicates this status. The inclusion of a device is typically confirmed with a triple press of a button of this particular device. However, please refer to the manual of this particular device for details how to include them into a Z-Wave network. The inclusion mode will time out after about 20 seconds or is aborted by pressing the 'Inclusion' button again.
The same applies for the 'Exclusion'.



[2] Rename a device:


Use <lock></lock> tags when you want to bypass parser and pass a URI with blank spaces as one argument.

To create an HTTP GET method as an Instruction Set for alternative use, you can move to Instructions->Add New Launcher and put your getter there. This will allow you to invoke a Z-Wave function into another Instruction Set. You can search in this blog for common actions, but I will come back later with analytical article (*).


You can review the whole object in real time to reveal all the available functions but I won't deepen on the data model -yet [ http://localhost:8083/ZWaveAPI/Data ]:


To have a human readable tree view, copy and paste the JSON object to an editor, such www.jsoneditoronline.org.


List of available devices [ http://localhost:8083/ZWaveAPI/Run/devices ]:


Let's stand here and see some examples on Jubito's side.
Jubito has a native API for HTTP GET method i.e.: judo http get <Request-URI>.

Try any of the above operations from the terminal.
Examples:
  • judo http get http://localhost:8083/ZWaveAPI/Run/devices[n].data.givenName.value - Gets device name.
  • judo http get http://localhost:8083/ZWaveAPI/Run/devices[n].instances[0].commandClass[37].Set(255) - This will switch on the plug. It is equivalent to http://localhost:8083/ZWaveAPI/Run/devices[n].instances[0].SwitchBinary.Set(255) or .Set(0) to switch off.


That was a quick introduction to the Z-Way API. Before you get confused, Jubito comes with a simple interface that implements the basics like, inclusion, exclusion and list of available devices with their actions (on/off), including some additional information.
While is still beta, I won't add a link on the menu button yet, but you can navigate to z-index.html:
http://<host>:<port>/www/z-index.html


Please make sure you have the latest version [0.2.8.299]:


(*) Let's consider this post as the first part of a Z-Wave session. In future articles I will present ways, on how to use a specific function, on a custom Instruction Set, and how these can interact and expand functionality and logic by other mechanisms, including evaluations and schedules.

Tuesday, July 29, 2014

Arduino RFID access control

In this tutorial I will demonstrate an RFID (Radio Frequency Identification) access control system.

What we need:
Once you wire the module, download the sketch and upload it to arduino.



Alternatively use codebender
Open the serial monitor and approach a token or a card to the antenna. You should receive the tag id.


Now we have readings let's handle them. It's in your discretion what to operate with those, but in this example we will make a simple check-in/check-out sequence.

As always, check your arduino configuration and then enable the serial port.


To achieve our goal we need to evaluate user status in order to switch from present to absent and vice versa when a tag is pointed. At Control Panel click Add New Instruction Set. Type rfideval in the name field (handler) or one of your choice and the evaluation that follows as the action...

{ evalBool("%whereami%" == "present"); check_me_out; check_me_in; }


'check_me_in' and 'check_me_out' are Instruction Sets that corresponds to our preferred actions. For example, saluting user using %salute% function, announce temperature even turning on a light. Also the %checkin% and %checkout% functions are mandatory to changing the current status.


To see how responses will look or sounds like, go to terminal tab and type...
  check_me_in; %whereami%
  check_me_out; %whereami%

...which  will execute the commands and after that get the status with %whereami% function.


We can also drive the check-out sequence within an event handler for extra functionality, like a countdown timer that give as time to leave before status is changed. This action can be achieved by pointing the check_me_out Instruction Set to event handler we just describe. The events will look like this...

judo sleep <ms>; %checkout%



The check_me_out Instruction Set now points to check-out-handler.

It would be wise to read about functions and API for extra customization. Also, it would be very handy to know how events work .It will help you to make multiple actions in a single command.

As we may already know, whatever collected by the serial port triggers corresponding events if they exists. This means that if we create a new event handler with name (id) same as RFID tag then it will be triggered when found.


Notice the last id in the serial monitor screenshot. When specific token (with id 6600942812) is acknowledged the event will trigger the evaluator 'rfideval' and change our status accordingly.

Friday, July 11, 2014

Arduino light sensing

In this tutorial we'll create a simple circuit that sense light using a photoresistor.

What we need:
Follow the screenshot to wire the photocell and then upload the sketch to arduino.



Alternatively use codebender
By hitting the 'light' command in the serial monitor we will get a print out of what it interprets as the amount of light in a qualitative manner. Thus is 'Dark', 'Dim', 'Light', 'Bright', 'Very_Bright'. You can change thresholds readings with values that suits your conditions.


OK, now we are ready to take advantage of the above example and make an assistive illumination system. As we already know we can capture serial messages from Jubito and trigger corresponding events. To make it happen we need to create one event handler with the name 'Dark' that will execute the 'plug-A-on' launcher of this example.

To do so, navigate to Control Panel/Instruction Sets/Add New Event Handler and enter the name and the action. When sensor submits 'Dark', the 'plug-A-on' launcher will be executed and turn on a light that is plugged to a wireless power outlet.


Notice: The above example to take effect, we need to let the loop continuous running and print out conditions than waiting for a 'light' command to be send. The modified sketch should be...

void loop(void) {

    // Light measurement
    photocellReading = analogRead(photocellPin);
    
    // We'll have a few threshholds, qualitatively determined
    if (photocellReading < 10) {
      Serial.println("Dark");
    } else if (photocellReading < 200) {
      Serial.println("Dim");
    } else if (photocellReading < 500) {
      Serial.println("Light");
    } else if (photocellReading < 800) {
      Serial.println("Bright");
    } else {
      Serial.println("Very_Bright");
    }
  }
  delay(1000);

}

But still you can use a scheduler and evaluator that applies to original sketch. Follow this article to see how you can do that.

Update
So we come back to see how we can make a running daemon with a scheduler that evaluates the light conditions.
First we need to create a launcher that executes the 'light' command we set to arduino sketch and fetch the state. Then we need a new Instruction Set that invokes it.
From the Instruction Set menu click Add New Launcher and give a name and the judo API call to send the 'light' command to the serial port.


After that create a new Instruction Set that invoke the launcher and make the evaluation with the 'Dark' condition. Type a name of your choise and the evaluation that follows...

{ evalBool("*getlight" == "Dark"); plug-A-on; ; }


The '*getlight' pointer will invoke the 'getlight' launcher and get its returning. Then it will be replaced by the condition data ('Dark', 'Dim', 'Light', etc) and proceed to evaluation. If condition is true the 'plug-A-on' launcher will be triggered, else nothing will happen.
The last thing to do to make it a runnable daemon is to create a new schedule that call the above Instruction Set within an interval.
From Scheduler menu click Add New enter a name, <Repeat> on the period dropdown, interval in milliseconds (the example use 60000ms which is 1 minute) and finally the 'evallight' Instruction Set as the action.


Video demo

Tuesday, July 8, 2014

Arduino IR remote control

After this tutorial we're coming back to make another kind of remote control. This time we will use infrared modules to receive and transmit codes to devices such as TVs, Hi-Fi systems, Projectors, etc. Thus will give as a universal remote control.

What we need:
First wire the modules and then install the library. To do so navigate to Sketch/Import Library/Add Library

Receiver
Transmitter


After installation is complete it will appear in library list.

Upload the sketch using codebender
Alternatively download the sketch and upload it to arduino.


Open the serial monitor point the IR receiver module and press a key of a remote control. It should return back the readings (code type, code value and code length).


To transmit back we have a simple implementation within loop that translates a specific form of data.
i.e. <codeType:><codeValue>&<codeLength>
For example, son:2704&12 which will cause the transmission.


Jubito as a virtual remote control
First make sure you have properly configure arduino serial port and then enable it.


From Instruction Sets menu press Add New Launcher. Enter a name (handler) and type the following command...

judo serial send son:2704&12


Then visualize it. Again on Instruction Sets menu press Add New Instruction Set and fill the mandatory fields (name, action, category, header) and some optional if you desire, e.g. Thumbnail URL.


As you might already know from previous tutorials, we point the 'sonypower' launcher with an asterisk in front of that.
After that final step you'll see a 'Media' category to your dashboard with the action we've just created.


Scan and enjoy your virtual universal remote control.

Friday, July 4, 2014

Arduino motion detection using a PIR sensor

In addition to this post, I'm providing the arduino sketch and the PIR motion sensor (Pyroelectric "Passive" Infrared Sensor) wiring as follows.

What we need:
Wire module to arduino and upload the sketch.



Alternatively use codebender
Open serial monitor and start moving, after that, follow this post and learn how to perform operations with Jubito or this one that concerns security.

Thursday, July 3, 2014

Arduino MQ-2 Smoke/LPG/CO Gas Sensor Module

In this tutorial we will make a gas leakage detecting system with MQ-2 gas sensor which is sensitive to LPG, i-butane, propane, methane, alcohol, Hydrogen and smoke.

What we need:
Wire module to arduino and upload the sketch.



Alternatively use codebender
Open serial monitor and type 'smoke', 'co' or 'lpg'. Each of these commands will return concentrations of smoke, carbon monoxide and liquefied petroleum gas. You can read more details here.

You can define your own commands by changing the lines below...


After that we will use those values to detect for any king of leakage. We will make a new schedule that will run every 60 seconds and perform evaluation.

As usual please make sure you have properly configure arduino serial port and then enable it.


Let's get dirty
First we need to make a new launcher to retrieve smoke values for this particular example. Go to Instruction Sets menu and press Add New Launcher. Enter a name (handler) and type the following command...

judo serial send smoke


Then our notification launcher using judo mail send API command.
Syntax: judo mail send <from> <to> `<subject>` `<message>`

judo mail send xxx@gmail.com yyy@gmail.com `smoke alert` `Smoke detected *getsmoke ppm`


And finally the Instruction Set that handle the evaluation function. Press Add New Instruction Set, type a name and the following command...

{ evalBool(*getsmoke > 0); sendmailonsmoke; ; }

Note: Zero value can be changed with one of your choice you consider dangerous.


The above action will call the first launcher and get a value from the sensor, then will evaluate if it's greater than zero, if the statement is true the second launcher will be triggered and send us an email notification. Read more about evaluation function.

Now we are ready to create a 60 seconds schedule for task to be repeated. Click on scheduler and press Add New. In the popup window enter a name, period <Repeat>, interval in milliseconds <60000> and the 'smokeschedule' instruction set we had create in order to call the evaluation procedure.


This is it, once we save the schedule it will be activated. You can do the same way for gas (co) and perform a test with a lighter.

Another approach is to trigger an event from the sketch like this example.
You can accomplish that by change the lines below...

void loop(void) {
 
  if (MQGetGasPercentage(MQRead(MQ_PIN)/Ro,GAS_SMOKE) > 500)
    Serial.println("HazardousSmokeDetection");



When the measurement exceeds the value we have set, the 'HazardousSmokeDetection' will be printed to serial port and cause Jubito to execute the corresponding event.