In the latest entry of the Arduino and Protocoder programming course for makers, we will be learning how to read data from a temperature and humidity sensor and display it in Protocoder.

Arduino code

In Arduino code, we will take a measurement with a sensor based in the HTS221 chip and send the data via Bluetooth. In our case, we will connect the sensor’s SCL output to pin A5 and the SDA to pin A4 of the ZUM board. First of all, we will include the libraries and create an object to access the sensor data:

To store the sensor data we will use two variables, h and t, which will contain the humidity and temperature respectively, and the variable sampleTime, which will stop execution for the time period set in milliseconds. We will use an auxiliary variable, aux, which will store the parsed data to be sent, and the boolean variable debug, which will determine if the temperature and humidity data is to be printed unparsed via the serial connection.

The setup function initialises the serial connection and the Wire and hts221 libraries. It also checks the connection has been correctly established.

The loop function accesses and stores data from the sensors. The said values are parsed so they can be sent via Bluetooth.

To send this float-type data, it is converted to integer variables, then both values are concatenated. For example, the values h = 44.38 and t = 26.80 are obtained in one cycle of the loop function. To send them, firstly we multiply both variables by 100 thus shifting the fractional part to the integer, giving us 4438 and 2680; and secondly, the variables are cast to integers with the commands.

Once the cast is done, the next step is to increase the weight of one of the two measurements, resulting in an 8-digit number in which both the humidity and temperature data is stored. In this case, it has been decided the humidity measurement is to have the greater weight within the delivery. We multiply this measurement by 10,000 and, keeping with the example, a humidity value of 44380000 is obtained. We then add the temperature value to this, resulting in a parsed value of 44382680, which is ready to be sent. Once it is received in Protocoder, the process is reversed, separating the values and making them workable:

Protocoder code

We start our Protocoder code by including our program header and two buttons to connect and disconnect Bluetooth. To find out more about how this code works, you can check out this entry. bq_Aquaris_E4_5


The only changes made with respect to previous course lessons is the inclusion of the onNewData function, and within this, the parseIncomming() function. The former is responsible for receiving data from Arduino and the latter for “translating” the said data to numbers that can be used in Protocoder. Once we have “decoded” the humidity and temperature data, we change the text of the variables textH and textT, which are created afterward, and we concatenate “%HR” and “ºC” respectively to the variables so that the values are properly displayed. The onNewData function receives the data sent from the ZUM board and it is stored in the data variable, which we can only use within the onNewData function. To reverse the parsing done on the humidity and temperature values in the Arduino code, we use the parseIncomming function, which takes the data variable. We also create the humidity and temperature variables, that are set to 0, and debugging, that is set to false to avoid data being displayed on the console. If we want to check what data is being used within the function, the value of debugging should be set to true. The code for the parseIncomming function is:

The parseInt and toFixed functions are used in parseIncomming (click on the names to learn more about these functions). The parseInt function eliminates the fractional part of the number, while toFixed forces the number to have two decimal places. A combination of mathematical operations, together with these functions, are used to “decode” the humidity and temperature values received via Bluetooth. Lastly, we create two text variables that will display the humidity and temperature values. The size is modified and the colour is changed, in this case, to black. HTML code is used to modify the colour.