# Development of a Wireless System to Control a Trombe Wall for Poultry Brooding
## 2 Materials and Methods
### Prototype
#### 2.1.2 Treating, Sending and Receiving Data  
The DHT21 library referenced in [37] converts the collected temperature values into float, which is then converted into bytes, multiplying by 100 and dividing by 256: the integer value of the result is the \"msb\" (most significant byte), and the rest is the \"lsb\" (less significant byte). This means that each measure from each sensor requires two bytes to be sent in each packet. In order to address and forward the information, the send function is used in the previously opened socket that binds to The Things Network server that delivers each of the two byte measurements from the deployed sensors. As seen in the following code, before sending these bytes, the socket should be in blocking mode: meaning that the program does not proceed until the requested call is completed. Once the operation has ended, the socket should be set to a nonblocking mode. ```
imports socket s.bind(1) s.setblocking(True) s.send(bytes([HUMcam_msb, HUMcam_lsb, TMPcam_msb, TMPcam_lsb, HUMi_msb, HUMi_lsb, TMPI_msb, TMPi_lsb ])) s.setblocking(False)
```  
receive()  
In TTN, a decoder is written in Javascript, which receives the corresponding bytes where the information was carried and puts them in their original structure, in order to obtain the original values read by the sensors. The ThingSpeak integration is active from the moment when a value is given to a variable where the read numbers by the sensors are allocated to fields (in this case, field 3 to the temperature and field 4 to the humidity). The TTN immediately sends it to the application and plots it in the corresponding field. As can be seen in the following portion of the code, the bytes sent from the microcontroller are organized in the server side and are transformed into the original values. Once this step is completed, the values are assigned to fields, which constitute the information that is displayed on ThingSpeak side. var decoded={}; if(port===1){ decoded.HUMcam=(((bytes[0]<<8|bytes[1])/100).toFixed(2)); if(bytes[2]&0x80){ decoded.TMPcam=(((bytes[2]<<8|bytes[3])-65536)/100).toFixed(2)); }else{ decoded.TMPcam=(((bytes[2]<<8|bytes[3])/100).toFixed(2)); } return{ field3:decoded.TMPcam, field4:decoded.HUMcam }; } ```
The same process is repeated to the received sensor values inside the poultry brooding (DHTi). As previously defined, actions are taken according to the temperature differential between the air gap and the interior of the poultry house (Tcam--Ti). With this, a new field is assigned (field 1) to the temperature difference, where the React is acting and so an action in ThingHTTP is triggered. In one of the cases, it is defined that if the temperature difference is below 15 \({}^{\circ}\)C, XPS boards are opened, and the heat flow is also allowed by air convection through the massive wall air vents (Figure 5). As can be seen in Figure 5, every time the temperature conditions are reached, the ThingHTTP action is unleashed, by making a POST in the downlink URL provided by The Things Network in each uplink. The body must contain the application ID and the port where the downlink is performed. If it is intended to receive an ACK from the microcontroller, the \"confirmed\" field should be specified and set to \"true\". This information reaches the microcontroller after going through TTN, as presented in Figure 6, where the sent byte is \"01\", which should be filled in base64 codificat  
Figure 5: Implemented React to open the XPS boards. Right after the information is sent by the server in downlink format, the information is received when an uplink is performed from the microcontroller via LoRa as it can seen in the following portion of code. The received function previously called in LoPy4 checks which port was the information sent from, decodes the message using the hexlify() method that converts binary data to hexadecimal, and gives an instruction based on the received content. In this case, if the information received is 01, it opens the XPS boards, and if it is 00, it closes them.