In this post I´m covering three components: RF plug control, detecting IR from the TV remote and measuring the node´s environment. This last one I intend to reutilize it in most of the other nodes. The point is that there is lots to cover before the kit arrives, and from what we are reading, this can happen really quick
As suggested by DAB last time, I will add a snapshot with the features I´m discussing in each post. I hope this will make it easier to follow the project development!
This is a snapshot of all of the features from the PiIoT - DomPi: Intro. At the end of the post, you can find the nodes´ dashboard with some cells coming into green!
Lights Control
This feature leverages the RF plugs from the DIO Chacon producer that I have at home. As explained in the previous post, they operate at 433Mhz, which can be easily replicated by an Arduino or Raspberry Pi. I have decided to create a small class (DIO_lib) to implement the required code. In this way, each of the three plugs will be managed independently by a DIO_lib object.
Below is the h file for the Arduino class. DIO_lib.h - note that the class wrapper is "home" made by myself, meaning... it may not be the best C code ever... but it works
/* Library based on different codes found in the Internet as per shared in the Element14 Design Challenge posts. This library has an object, DIO_lib, that helps control my RF plugs from the DIO Chacon brand. RF header of DIO: - 26 bits header. It incluyes “00” and the specific code for each plug set, as per below - "0" a bit of “0” - On/Off a bit to state on or off - 4 bits identifying the plug to actuate on DIO id for the RF set: 14137952 Plug 1: "0000" added at the end Plug 2: "0001" Plug 3: "0010" */ #ifndef DIO_lib_h #define DIO_lib_h #include <Arduino.h> #define DIO_CMD_OFF 0 //Turn off #define DIO_CMD_ON 1 //Turn on #define ENCHUFE1 1 //Id for the plug in the library #define ENCHUFE2 2 #define ENCHUFE3 3 #define CABECERA_DIO 14137952 class DIO_lib { public: DIO_lib(int enchufe, int pin); void setPIN(int pin); //Select pin where the RF 433Mhz module is connected void sendCmd(int cmd); //send on or off void On(); void Off(); private: int _pin; int _enchufe; void itob(unsigned long integer, int length); void transmit(int blnOn); void sendPair(boolea unsigned long power2(int power); }; #endif
In the attachments section you can find this file and also the .cpp Additionally, I have added an examples folder. If you add the library to your Arduino IDE in the regular way, you can use the examples and the library as usual. Hope it helps!
Lights Control via the TV remote
As commented in the previous post, I will use the TV remote to turn on and off the lights connected to the RF plugs. Using the Arduino IR library with its dump example and the IR receiver, I have identified the IR codes of the buttons I want to detect. Mainly the red, yellow, green and blue ones.
At the beginning I implemented a code that would detect the red button, this button acted as an activator for the Arduino. It made the Arduino to wait up to 4 secs until a yellow-green-blue button was detected. The yellow would activate the plug 1, the green activates the plug 2, etc. This worked well in the "alpha" test, meaning myself. But with the Beta tester (my wife), I realized that it was not the easiest way: you need to move your finger! Finally I´m implementing another approach. If you click twice the red button, it turns on and off the plug 3, first double click it turns it on, the next one off, the third one on, and so on. The green button turns on/off the plug 2 and the yellow button acts on the plug 1.
If you are thinking that there is no "double" click in a TV remote, you are right. I´m allowing up to 4 secs to get the second click. My guess is that by allowing "only" 4 seconds, it is a good trade-off: it is enough time for the user to click it twice without rush and stress. And also should not interfere with the TV usage. Meaning, I seldom use those buttons in my TV. When I use them is to move between menus, and this usually takes more than 4 secs (loading the page, me reading what I need and moving to the next page should take longer). If by using this I see that it does interfere, I will just move it down to 2 secs.
I have already some code that makes these actions, let me share it with you hopefully in the next post with most of the features for the living room node.
Environment Conditions
This feature obtains the temperature, humidity and luminosity of the room. The idea will be to reuse the same code for all of the nodes that require this feature. From the sensors perspective, I will be using the DHT11 for the humidity and the DS18B20 for the temperature (see reasons for these in the previous post). The output of both of them is digital and require some decoding, therefore, I´m leveraging two libraries: DHT.h and DallasTemperature.h together with the OneWire.h. For the luminosity sensor i will take the GL5528, its output is analog and I am connecting it to the A3 analog pin of the Arduino Nano.
Problems encountered: In theory, you should be able to connect both the DHT11 and the DS18B20 to the same Arduino input pin, but for some reason that I have not looked deeply into, I am having issues sharing the same pin. Since I don´t foresee problems with running out of digital pins, I have decided to keep both sensors in different pins.
The connections for the DHT11 andDS18B20 sensors are very much the same: a resistor from +5V to the relevant pin and then it comes the sensor pin data. For the photoresistor, the second leg of it is connected to ground.
The libraries for the DHT11 and the Dallas sensor are quite intuitive and besides some fine tune, they are straightforward when looking at the examples. One call out though, for the Dallas sensor there are two modes of invoking the class. It seems that if you provide the device id when creating the object, the Arduino and the sensor will work faster - probably because it avoids the discovery phase each time it is called (TBC). In principle, I don't expect issues by spending some more milisecs in each thermometer reading, however, by setting up ones per node does not take much time and... well, I decided to invoke the object with the device address - to get its address, I just leveraged the tester example.
Getting the luminosity is as well quite straightforward. I just need to read the value of the analog pin A3 when required. The only trick, is that I want to map the luminosity into a 0-100 scale before the value is sent to the Command Center. This mapping requires some fine tuning and Arduino IDE offers two functions that will help me, the constrain() and the map(). The only point missing is that I´d need to take some measurements along the day and night to understand what is the range of the readings, before I can properly constrain them and map them to the 0-100 scale I want to. Therefore, I need to spend some efforts still in this fine tune.
Let me share with you the code of these functions in the next post, hopefully the living room node will be almost finished.
Nodes´ Dashboard
If anything needs further details or you have any suggestion, please do let me know!Write to you in the next post.
Top Comments