Introduction: NexArdu: Illumination Smart Control

Picture of NexArdu: Illumination Smart Control

A sketch to control home illumination in a smart manner via 433.92MHz (aka 433MHz) wireless X10-like devices, e.g. Nexa.

Background

When it comes to decorative illumination, it has been somehow tiring to me that every second or third week I had to readjust the timers that switch the lights on because of the shifting of the solar hour with respect to the CET.
At the same time, some nights we go to bed earlier than other. Because of this, sometimes the lights switch off either "too late" or "too early". The above challenged me to think: I want the decorative illumination to switch on always at the same level of ambient light and then switch off at certain time depending on whether we are awake or not.

Objective

This instructable exploits the possibilities of wireless controlled devices like System Nexa operating on the 433.92MHz frequency. Herein we are to feature:

  1. Automatized illumination control
  2. Web control

Web control. Internal vs External Web server

The Internal Server exploits the possibility of the Arduino Ethernet shield to provide a web server. The web server will attend web client calls to check and interact with the Arduino. This is a straight forward solution with limited functionality; the possibilities of enhancing the web server code are limited by the memory of the Arduino.
The External Server requires the setup up of an external PHP web server. This setup is more complicated and not supported by this tutorial however, the PHP code/page to check and steer the Arduino is provided with basic functionality. The possibilities of enhancing the web server are, in this case, limited by the external web server.

Bill of materials

To take fully advantage of the possibilities this sketch gives, you need:

  1. An Arduino Uno (tested on R3)
  2. An Arduino Ethernet shield
  3. A Nexa set or similar operating at 433.92MHz
  4. A PIR (Passive InfraRed) sensor operating at 433.92MHz
  5. A 10KOhms resistor
  6. An LDR
  7. A RTC DS3231 (external server version only)
  8. A 433.92MHz transmitter: XY-FST
  9. A 433.92MHz receiver: MX-JS-05V

The minimum recommended is:

  1. An Arduino Uno (tested on R3)
  2. A Nexa set or similar operating at 433.92MHz
  3. A 10KOhms resistor
  4. An LDR
  5. A 433.92MHz transmitter: XY-FST

(The omission of the Ethernet shield requires modifications of the sketch not provided within this instructable)

The Nexa Logic. A brief description

The Nexa receiver learns the remote control ID and button ID. In other words, every remote has its sender number and each pair of on/off buttons has its button ID. The receiver has to learn those codes. Some Nexa documents state that a receiver can be paired with upto six remotes. The Nexa parameters:

  • SenderID: ID of the remote control
  • ButtonID: button-pair number (on/off). It starts with number 0
  • Group: yes/no (aka "All off/on" buttons)
  • Command: on/off

Instructable Steps. Note

The different Steps described herein are to offer two different flavors on how to achieve the objective. Feel free to choose the one at your convenience. Here is the index:

Step #1: The circuit

Step #2: Nexardu with Internal Web Server (featuring NTP)

Step #3: Nexardu with External Server

Step #4: Valuable Information

Step 1: The Circuit...

Picture of The Circuit...

Wire the diverse components as shown in the picture.

Arduino pin#8 to Data pin on RX (receiver) module
Arduino pin#2 to Data pin on RX (receiver) module
Arduino pin#7 to Data pin on TX (sender) module
Arduino pin A0 to LDR

RTC configuration. Only needed on the External Server configuration.
Arduino pin A4 to SDA pin on RTC module
Arduino pin A5 to SCL pin on RTC module

Step 2: Nexardu With Internal Web Server (featuring NTP)

The Libraries

This code makes use of a lot of libraries. Most of them can be found thru the "Library Manager" of the Arduino IDE. Shouldn't you find a listed library, please google.

Wire.h
SPI.h - Required by Ethernet shield
NexaCtrl.h - Nexa device controller
Ethernet.h - To enable and feature the Ethernet shield
RCSwitch.h - Required for PIR
Time.h - Required for RTC
TimeAlarms.h - Time alarm management
EthernetUdp.h - Required for NTP client

The Sketch

The code below exploits the possibility of using the Arduino UNO board not only as the mean to control Nexa devices but it also features an Internal Web server. A remark to add is that the RTC (Real Time Clock) module gets automatically adjusted via NTP (Network Time Protocol).

Before you upload the code to the Arduino, you may need to configure the following:

  • SenderId: you need to sniff the SenderId first, see below
  • PIR_id: you need to sniff the SenderId first, see below
  • LAN IP Address: set an IP of your LAN to your Ethernet Arduino shield. Default value: 192.168.1.99
  • NTP Server: Not strictly necessary but it could be good to google for NTP servers in your nearby. Default value: 79.136.86.176
  • The code is adjusted for CET time zone. Adjust this value -if needed, to your time zone in order to display the correct time (NTP)

Sniffing the Nexa codes

For this you need to wire -at least, the RX component to the Arduino as shown in the circuit.

Find below the Nexa_OK_3_RX.ino sketch that, at the time of writing it, is compatible with Nexa devices NEYCT-705 and PET-910.

The steps to follow are:

  1. Pair the Nexa receiver with the remote control.
  2. Load Nexa_OK_3_RX.ino onto the Arduino and open the "Serial Monitor".
  3. Press the remote control button that controls the Nexa receiver.
  4. Take note of the "RemoteID" and "ButtonID".
  5. Set these numbers under SenderID and ButtonID on the variable declaration of the previous sketch.

To read the Id of the PIR, just use this same sketch (Nexa_OK_3_RX.ino) and read the value on the "Serial Monitor" when the PIR detects motion.

Step 3: Nexardu With External Server

The Libraries

This code makes use of a lot of libraries. Most of the can be found thru the "Library Manager" of the Arduino IDE. Should you do not find a listed library, please google.

Wire.h
RTClib.h - this is the library from https://github.com/MrAlvin/RTClib
SPI.h - Required by Ethernet shield
NexaCtrl.h - Nexa device controller
Ethernet.h - To enable and feature the Ethernet shield
RCSwitch.h - Required for PIR
Time.h - Required for RTC
TimeAlarms.h - Time alarm management
aREST.h - for RESTful API services exploited by external server
air/wdt.h - Watchdog timer handling

The Sketch

The sketch below features another flavor of the same thing, this time empowering the possibilities that an external web server can give. As already mentioned in the introduction, The External Server requires the setup up of an external PHP web server. This setup is more complicated and not supported by this tutorial however, the PHP code/page to check and steer the Arduino is provided with basic functionality.

Before you upload the code to the Arduino, you may need to configure the following:

  • SenderId: you need to sniff the SenderId first, see Sniffing the Nexa codes on preceding step
  • PIR_id: you need to sniff the SenderId first, see Sniffing the Nexa codes on preceding step
  • LAN IP Address: set an IP of your LAN to your Ethernet Arduino shield. Default value: 192.168.1.99

For the Nexa code sniffing procedure, please refer to Step #1.

Complementary file

Upload the attached nexardu4.txt file to your external PHP server and rename it to nexardu4.php

RTC time set

To set the time/date on the RTC I use sketch SetTime that comes together library DS1307RTC.

Step 4: Valuable Information

Picture of Valuable Information

Good to know behavior

  1. When Arduino is under "Light Automatic Control", it can go thru four different states in relation to the ambient illumination and the time of the day:
    1. Wakefully: Arduino waits for the night to come.
    2. Active: The night has come and Arduino has switched the lights ON.
    3. Somnolent: The lights are ON but the time to turn them off is coming. It starts at "time_to_turn_off - PIR_time" that is, if time_to_turn_off is set to 22:30 and PIR_time set to 20 minutes, then the Arduino will enter into the somnolent state at 22:10.
    4. Dormant: The night passes by, Arduino has switched the lights OFF and Arduino waits for the dawn to become wakefully.
  2. Arduino is always listening to the signals sent by remote controls. This features the possibility of showing the state of the lights (on/off) on the web when remote control is used.
  3. While Arduino is wakefully it tries to get the lights OFF all the time therefore, ON signals sent by a remonte control to switch the lights on might be captured by Arduino. Should this happen, the Arduino will try to turn the lights off again.
  4. While Arduino is active it tries to get the lights ON all the time therefore, OFF signals sent by a remote control to switch the lights off might be captured by Arduino. Should this happen, the Arduino will try to turn the lights on again.
  5. In the somnolent state the lights can be turned on/off with a remote control. The Arduino will not counteract.
  6. In the somnolent state the PIR countdown will start reseting from "time_to_turn_off - PIR_time" and so the time_to_turn_off getting extended by 20 minutes every time the PIR detects motion. A "PIR Signal Detected!" message will be shown on the web browser when this happens.
  7. While Arduino is dormant lights can be turned on and off via the remote control. The Arduino will not counteract.
  8. A reseting or power cycle of the Arduino will bring it to active mode. This means that if the Arduino has been reseted after the time_turn_off then Arduino will switch the lights on. To avoid this the Arduino needs to be brought into manual mode (tick off "Light Automatic Control") and wait until the morning to get it back to "Light Automatic Control".
  9. As aforementioned, Arduino waits for the dawn to get active again. Because of this, the system can get fooled by directing a strong enough light towards the light sensor that is to surpass the "minimum luminosity" threshold. Should this happen, then Arduino is to change to active state.
  10. The Tolerance value is of high importance in order to avoid the system flapping on and off around the threshold value Minimum Luminosity. Led lights, due to their flickering and their high responsiveness, can be a source of flapping behavior. Increase the tolerance value if you experience this problem. I use value 7.

Good to know about the code

  1. As you can notice, the code is very large and makes use of a considerable amount of libraries. This compromises the amount of free memory necessary for the heap. I have noticed unstable behavior in the past having the system getting halted, specially after web calls. Therefore, the biggest challenge I have had has been to limit its size and the usage of diverse variables in order to make the system stable.
  2. The code that exploits the internal server -used by me at home, has been running now since February 2016 problem free.
  3. I have put considerable efforts on enriching the code with explanations. Take advantage of this to play with diverse parameters like number of Nexa code sendings per burst, NTP sync time, etc.
  4. The code does not feature daylight saving. This needs to be adjusted via the web browser when it applies.

Some points to consider

  1. Add the antenas to the TX and RX radio frequency (RF) modules. It will save you time complaining about two main points: resilience and range of the RF signal. I use a 50Ohms wire 17.28cm (6.80in) long.
  2. This intructable may work with other home automation systems too like Proove, for instance. One of the many conditions to fulfill is to have them operating on the 433.92MHz frequency.
  3. A big headache with Arduino is to deal with libraries that may get updated over the time and suddenly not being back compatible with your "old" sketch; the same problem may raise when upgrading your Arduino IDE. Beware that this could be our case here -yes, my problem too.
  4. Multiple concurrent web clients with different light modes creates a "blinking" state.

Screenshot

In the picture carousel above, you find a screenshot of the web page displayed when you call the Arduino thru your web browser. Given the default IP configuration of the code, the URL would be http://192.168.1.99

One aspect that might be subject of improvement is the positioning of the "submit" button since it takes effect on all input boxes and not only on the "Light Automatic Control" as one might think. In other words, if you would like to change any of the possible values, you always need to press the "submit" button.

Detailed/Advanced documentation

I have attached the following files so they can help you to understand the whole solution, specially for troubleshooting and improvement.

Arduino_NexaControl_IS.pdf provides documentation on the Internal Server solution.

Arduino_NexaControl_ES.pdf provides documentation on the External Server solution.

External references

Nexa System (Swedish)

Comments

Swansong (author)2016-09-26

Thanks for sharing :)

About This Instructable

1,554views

12favorites

License:

More by Tamadite:ArduPhotographerNexArdu: Illumination Smart Control
Add instructable to: