In this new instalment of the Arduino and Protocoder programming course for makers, we will carry on with the project from the previous entry, where we learnt how to read data from a temperature and humidity sensor and display it in Protocoder.

In this lesson, we will add two new characteristics: we will modify the interval in which Arduino sends data to Protocoder and set an alarm to notify us if the temperature exceeds a specified limit.

Arduino code

In relation to the Arduino code from the previous entry, the contents of the loop responsible for taking and sending readings via Bluetooth to Protocoder have been moved to a new function. A new function has also been created which checks if there is data and modifies the time Arduino takes to send new data to Protocoder. This last function has been developed to support the receival and easy decoding of other types of data, just by adding a few lines of code.

The code that was in loop in the previous entry has now been included in the getTH function:

The h, t and sampleTime variables in this function are global, meaning we can access and modify their values from any point in the program. The aux variable is only required inside getTH, so it is initialised locally within the function. The checkIncomming function receives and processes data. It waits for two integers from Protocoder, we will use the first one as a flag, meaning a control variable that “decides” what to do with the received data, and the second one will be the data we are going to modify. As an example, to modify the value of the sampleTime variable, from Protocoder, we will first send flag, in this case which has a value of 1. Next, separated by a comma, the number we want to assign to the variable, for example 2000, and finally, the line feed character “\n”. The data packet will look like this: 1,2000\n. The “\n” character is used to determine when it has finished receiving the message, to ensure it has been completely received.

To add new functions into checkIncomming, simply add a new case to the switch block and program the delivery from Protocoder using a new flag. In the next lesson, a new function will be added to this one, but in the meantime, you can try adding new ones yourselves.

Protocoder code

In the Protocoder code, the text that displays the temperature and humidity data has been centred and its size changed. Two text boxes and a button to send data via Bluetooth to Arduino have been added, as well as a toggle button to enable an alarm that activates if the specified temperature is exceeded. bq_Aquaris_E4_7Firstly, we modify the size of the text that displays the temperature and humidity data in order to centre it on the screen:

It´s time to give our application some new functionalities. To start with, we are going to add the possibility to modify the speed that data is sent from Arduino to Protocoder. We will also create an alarm that will go off if the temperature reading in Arduino is greater than the temperature we enter on screen. If the alarm goes off, in addition to displaying a message on the screen, the device will also vibrate and produce a sound. To set the speed that data is sent, we will use the checkIncomming function that we created in Arduino. This function waits to receive two comma-separated integers, followed by the “\n” symbol, which closes the data packet. We configure the send function so that the data is formatted as explained earlier:

When data is to be sent, simply call the function and include the flag and value arguments, the function itself will format the data. To change the interval, we will include a text box, or input, in which the data to be sent can be written. The text box has been configured so that the time is entered in seconds and converted internally to milliseconds, the unit of time used in Arduino. We use the following command to include a text box.

Two variables will be used to modify the time that Arduino waits to send data, the timeToSend variable, which will store the time to send, and flag, which will be set to 1 so that it corresponds to the case in which the time is changed, found in the switch block of the Arduino checkIncomming function. boton5

We also include a button to send the data when pressed:

To create the temperature alarm, we will use another text box and a toggle button, a switch which takes one value or another depending on whether it is pressed or not. Additionally, we use the tempAlarm variable to store the temperature we want as the alarm activation limit, and isAlarmSet, which will take the value of the toggle button, that can either be true or false. To create the text box and the variables, we will use the following commands:

The syntax for generating a toggle button is:

The toggle button is created using the following commands: boton6

Within the toggle button, we assign the value, whether true or false, to the isAlarmSet variable. If it is false then we set the notification variable to false. We will use this variable later to prevent the notification from being repeated more than once, in other words, the toggle button must be disabled for the sound and vibration notification to be resent. Within the onNewData function, located in the btnConnect button, we will include the code which checks if the temperature reading is greater than the specified alarm temperature. This is done only if the toggle button is enabled. For this, we use the following commands:

Firstly, we use an if condition to check if the isAlarmSet variable is true, which mean if the toggle button is enabled, and if temperature is greater than tempAlarm. If both conditions are true a notification will be displayed with the command:

The syntax of the notification is:

Following on, if the notification variable is false, meaning that a notification has not occurred or the toggle button has been disabled, then the device vibrates for 1 second and a notification sound is produced using the following commands:

Finally, the value of the notification variable is set to true, indicating that a notification has occurred.