Testing your hardware connections

Now that the connections are done, we are going to test everything before we start sending energy consumption data to the cloud and building the interface to control the relay. We are going to test the different modules as if the project was already in operation. For the entire duration of the tests, we are going to connect the project to the power socket in the wall and to the lamp that we want to control. This way, we will ensure that all the hardware connections are correct before moving further.

The relay, for example, will be controlled via Wi-Fi using the Arduino Yún REST API, just as it will be in the final version of the project. Basically, we will just send a command from your web browser to directly set the value of the pin to which the relay is connected. Later in the project, we will make this call via a graphical user interface instead of actually typing the command in a browser.

For the current sensor, we are going to simply read the value measured on the analog pin A0 using the analog-to-digital converter of the Yún, convert it to a usable current, and then calculate the value of the effective current and the effective power as we already know the value of the effective voltage (110V or 230V depending on where you live).

Let's first have a look at the Arduino code. It starts by importing the right libraries, as shown in the following code. We need the Bridge library so that we can use the functions from the onboard Linux machine of the Yún, and the YunServer and YunClient libraries so that we can receive external commands using the REST API. REST APIs are usually only used by web developers, but Arduino actually proposes a sketch that implements such an API for the Arduino Yún. This sketch is directly accessible in the library that comes with the Arduino Yún, and in this project, I used a modified version of this reference sketch.

#include <Bridge.h>
#include <YunServer.h>
#include <YunClient.h>

To use the REST API of the Yún, we need to create a YunServer instance, as shown in the following line of code. This server will run continuously and wait for incoming commands.

YunServer server;

We also need to define the pins that our sensors are connected to, as shown in the following lines of code:

#define CURRENT_SENSOR A0
#define RELAY_PIN 8

One important part of the sketch is to declare the value of the effective voltage, which will be used later to calculate the effective power of the device, as shown in the following line of code. This value depends on where you are located (for example, 230 for Europe, and 110 for the USA):

float effective_voltage = 230;

In the setup() part, we need to start the bridge between the Arduino microcontroller and the Linux machine as follows:

Bridge.begin();

We also need to start the web server as follows:

server.listenOnLocalhost();
server.begin();

Then, the last and most important part of the setup() function is to calibrate the sensor in order to determine which value is returned when the current is null. This is done by the following line of code:

zero_sensor = getSensorValue();

Let's dive into this function. We could simply get one measurement from the current sensor but that would be a bad idea. Indeed, the value that you get from the sensor varies slightly over time, around an average that we actually want to measure. This is typical behavior when using analog sensors that have important sensitivities such as the one we are using here. This is why the function basically samples and averages the signal over several measurements with the following lines of code:

for (int i = 0; i < nb_measurements; i++) {
  sensorValue = analogRead(CURRENT_SENSOR);
  avgSensor = avgSensor + float(sensorValue);
}  
avgSensor = avgSensor/float(nb_measurements);

After these measurements, we return the average as follows:

return avgSensor;

This way, we are sure to get a stable value of the sensor reading every time. This value is then used throughout the whole sketch as a reference value for the current sensor readings. For example, if the measured value is equal to this reference, we will know that the current in the lamp is null. The actual sensor reading during the operation of the project uses the same function, so we always get a stable measurement.

Now, comes the loop() part of the sketch. It actually consists of two parts: in the first part, we will receive incoming connections on the Yún web server that we started earlier, and in the second part, we will print out the measurements that come from the current sensor.

For the web server part, we can listen for connections as follows:

YunClient client = server.accept();

If a client is detected, we process the request with the following code:

if (client) {
  // Process request
  process(client);

  // Close connection and free resources.
  client.stop();
}

I won't detail the Process function as it is the same as in the Bridge example for the Arduino Yún that we used earlier (this is available as an example in the Yún Bridge library). To know more about the Yún REST API, you can visit the official Arduino documentation on the Arduino website at http://arduino.cc/en/Guide/ArduinoYun.

Now, we will write the part of the sketch that is responsible for the current measurements. It starts when you get a stable measurement, just as we did earlier for the null current as follows:

float sensor_value = getSensorValue();

I won't get into the details of this function as it is the same as which we used to get the value for the null current. We can now do some calculations on this measured value. First, we need to convert it to a usable current value as follows:

amplitude_current=(float)(sensor_value-zero_sensor)/1024*5/185*1000000;

This is the amplitude of the current, which is a sinusoidal current. This formula can be found in the datasheet of the sensor as well as on the ITead Studio website. Because we know this information about the current, to get the effective current, we simply need to divide it by the square root of two as follows:

effective_value=amplitude_current/1.414;

To get the effective power, we then need to transform this current in amperes by dividing the value by 1000 and multiplying it with the effective voltage. I also added an absolute value operator so that the power is always positive, even when you connect the current sensor to measure negative currents, as follows:

abs(effective_value*effective_voltage/1000);

The sketch ends by printing all these values on the Serial monitor and repeats itself every 50 milliseconds. The complete sketch for this part is available on the GitHub repository of the book at https://github.com/openhomeautomation/geeky-projects-yun/tree/master/chapter2/sensors_test.

Now you can upload the sketch to the Arduino board. Remember that at this point, the Arduino Yún board should be powered by either your computer or a USB power adapter, the lamp should be plugged to the project in the female power cord, and the project itself should be plugged into the wall socket.

The relay is quite easy to test; you just need to go to your web browser and type in the right command. The REST API of the Yún works by typing the name of your Arduino Yún board followed by .local (in my case, I named it myarduinoyun). Then, if it is followed by arduino/, you can directly use commands to change the value of the Arduino pins. For example, to change the value of the relay pin to 1, you need to add digital/8/1, as shown in the following screenshot:

Testing your hardware connections

The preceding command means that you are calling the command digitalWrite(8,HIGH) using the REST API. You should instantly hear the relay switch and see the light turn on. Try again by adding a 0 after the command instead of a 1; the relay should switch again and turn the light off. Don't worry, as later in the project, we'll build a nice graphical interface so that you don't have to write this command every time.

Now we are going to check the measurements coming from the current sensor. Make sure that the lamp is off, reset the Arduino microcontroller to be sure that the sketch starts from the beginning again, and then open the Serial monitor. To do this, the Arduino Yún board has to be connected to your computer via the USB cable. The first thing you should see is the measurement for a null current as follows:

Sensor value: 493.05

Then, the sketch continuously displays the value of the sensor reading, current amplitude, effective current, and effective power. Even if the current is null, remember that we average the sensor readings over several measurements, so there can be minor fluctuations in the value, as shown in the following code:

Sensor value: 492.87
Current amplitude (in mA): 
4.5
Current effective value (in mA)
3.2
Effective power (in W): 
0.7

If you then turn the lamp on using the REST call in your browser, you should instantly see a change in the current and power readings as follows:

Sensor value: 486.52
Current amplitude (in mA): 
-163.1
Current effective value (in mA)
-115.4
Effective power (in W): 
26.5

If you can see these values and your relay is responding to the REST calls in your browser, it means that your hardware is working correctly and you can proceed to the next step. If it doesn't work, the first step is to check the different connections of the current sensor and relay module. Also check that you have selected the correct Serial speed in the Serial monitor so that it matches the speed defined in the sketch.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.184.126