Introduction: IOT BeeHive - Beehappy Project

About: Hi, im currently an electrical engineer student and im trying my best to do some great project here.

For many beekeeper, distance between their home and their beehive is way too big. To make sure the beehive is working properly they have to physically see. With this project, beekeeper can now look on a website and see honey quantity and many other information. For example, you can also add an anti-theft mechanism and lot more.

Pour les francais, le compte rendu du projet est egalement en ligne a l'adresse suivante : https://docs.google.com/document/d/1c5zfOx-Dk0f9ZKs6T7R2ZxKnkmTd7tI0CWUsfRBZTYQ/edit?usp=sharing

Supplies

All SMD component :

  • Atmega3208 or Atmega808 (The code use 92 % of the 808 Versions + you can only Tar 2 channel if using 808)
  • ADS1241E (24 bits CAN with 4 channel multiplexed)
  • Rak3172 (LoraWan emitter / receiver module)
  • Ams1117 3.3V ( LDO 5V to 3.3V)
  • K7803m-1000r3 ( Buck converter up to 20V to 5V)
  • Some resistors 0605
  • Some capacitor 0605

Through Hole component :

  • 32.768 Khz crystal
  • 2.459Mhz crystal
  • Jack power plug
  • Some Male and female pin header
  • A transistor to power off the weight scale

Other component :

  • Weight sensor

Step 1: Power Supply

In order to power the board , we used a small buck converter called k7805m-1000r3. This buck converter can go up to 1A. It can step down from more than 12V to 5V.

All components work by using a 3.3V power supply, so we have to convert a 5V to 3.3V.

We could use an other buck, but a buck as too many variation ! As we want to power a 24bits ADC, a small variation can make the system less accurate.

Obviously , we choose to use a LDO that can convert a 5V to 3.3V .

In order to stabilize the power supply, we also added some decoupling capacitor next to this components.



Step 2: ADS1241

This part is only for understanding purpose. You can skip if you only want to build the system.

The Ads1241 is used to amplified analog signal, then convert it into digital values by using his 24bits ADC, then it can send this data using SPI bus. It have up to 4 differential input for the 1421 version and 2 differential input for the 1420 version.

This is probably the hardest part because this component isn't easy to understand. To begin, we tried to send a simple command to the ads1241 using an Arduino . Then we tried to read a register by sending him the command Read Data = 0x10 followed by the register value. For example, we send the command readReg followed by 0x0A. The reply should be 59 or 89 in decimal.

If the previous step is working, you can now send the ReadData command. By default it should send the difference between pins Ain0 and Ain1. You can change the input by modifying the MUX register value. It can be done by sending the command WREG = 0x50 followed by the input selection.

Step 3: SPI

The SPI protocol is made to control many peripheral using only 3 common wires and 1 wire for each peripheral . A peripheral is called a slave, and the controller is called master. There is two wire used to send and receive data (MISO and MOSI). One means Master Output Slave Input and the other means Master Input Slave Output. Then, an other wire is used to generate a clock signal. This wire is named SCLK and only control by the master. Last but not least, we have to select which slave are we speaking to. To do it there is an other wire named SS or Chip Select . Just send a low signal on this wire to select the slave.

In this system, when we send a command to the ADC, the clock is active. But, when the ADC is trying to reply the clock isn't active. We have to send fake data to receive data.

An other problem can occur when the distance between the master and the slave is too long or when the bus is working on mid frequency(1Mhz). To prevent this problem, you can add a 22ohm resistor on the line. You can also add a capacitor between the data line and GND. It can be useful but you will have to do some math to find the correct values depending on the frequency your bus is working.

You should also add a pull down/ pull up resistor on your chip select line.

Step 4: Rak3172 & TTN

Now , let's take a look to the transmission process ! 

I chose a RAK as Lora emitter because it's quite simple to use and well documented first , but also because it has the Lora WAN stack . This component is based on a stm32 microcontroller and has multiple feature.

I said Lora or Lora Wan , but what is the difference ? The answer is easy to understand : Lora is the technology and Lora wan is the network. Some Lora module are only build to make pear to pear communication (P2P) , whereas other have the ability to connect to gateway and Lora wan network .

Then , what is TTN ?

TTN also called "The things network" is a public network made by numerous users around the globe.


The rak as it's own software to save some parameter. This software is called RAK serial port tool . In order to configure it , wire the rak by using a USB to Uart adapter. Then plug the module into your computer and launch the Rak software.

Click on the right com port number and select Baud Rate : 9600 , now just write into the text box (next to the send button) "ATE" . If everything work correctly , the receiving box should show a perfect "ok" . Many command can be used on the rak , and you can find them all just there .

Be careful, in reality you will have to add the \r\n string after your command to be understood by your rak. But the software do it by itself.

By default, the Lora module is working using class A, this mean that the rack can only receive data when he also send data. It is also using OTAA protocol to make connection to Lora network.

Okay, to connect to the thing network you will need some information. You can find them by using :

AT+NWKSKEY=? 
AT+APPSKEY=?
AT+APPEUI=?
AT+DEVEUI=?

Now just go on The things network consol and follow this procedure :

  1. If you don't have an account just create one / log in
  2. Click on "Go to application"
  3. Click on "Add application"
  4. Write application id field (it could be whatever you want)
  5. Then click on "create an app"
  6. Click on "add end device"
  7. Go on manually
  8. Choose the right Frequency plan (868 MHz for Europe)
  9. Select Mac V1.0.2
  10. Select RevB
  11. Now , write every information starting by DEVEUI


Okay, well now, you will have to initialize the connection to TTN . To do so just write into your serial software :

AT+JOIN=1:0:10:8  

If everything goes write you should be able to see +EVT:JOINED.

Sometimes , you will have the +EVT:JOIN FAILED message. That mean there is no Lora connection where you are .

Now, i will show you how to change settings through the Lora network !

First you will have to go into your end device and find this messaging page. Then click on downlink and choose the port number 9. Okay now you just have to enter your command. This command is an hexadecimal command. The first two number are used to choose which settings it will have to change. Then the other are the value to applied to this setting .

For example, here i choose to set the delay between two measurement at 165 min.

Step 5: PCB Design

This PCB was design using KICAD. The PCB could look a bit complexes but is not ! There is a SPI bus between the µC and the ADS1241, and an UART bus ( RX/TX ) between the RAK3172 and the µC. There is also a led used for debug purpose, connect to the µC and a DHT22 socket.

There is also some testpoint used to debug, but the second version of the board didn't have this feature.

You can find all versions on my GitHub page!

I tried to make it simple, but if you want more details, just let me know in the comment.

Step 6: Programming the Software

This step took us some days , but we were able to make it work. We choose to use Arduino Ide because everybody already know this software and because it's quite easy to use.

First, we got 3 files :

  1. Ruches or beehive in English is the main branch of the program. It contain all the setup and the void function.
  2. ads1241 is a simple storage used to know exactly the link between register number and name.
  3. fontion or function is were all the function are stored. It should be organized by their functionality.

As we use a small microcontroller, we tried to not use library. All the software should be comment and clear but if you have a question, ask me ;D



Step 7: Uploading the Software

Now it's time to put the software inside our controller !

The Atmega3208 use an UPDI protocol, this mean Unified Program and Debug Interface. This data could be a little odd first, but it is in really simple to use . First, there is only 3 wire, one is a bidirectional Data wire (we only use it in one way today) and two power wire.

In order to use this interface, we first build a simple Arduino UPDI programmer using a capacitor and a resistor. Simply plug a 4.7K resistor between D6 of the Arduino and the Data input of the controller. You will also need a capacitor between the reset and the GND of the Arduino. Now, your Arduino will have to be the switch between JTAG programming method and UPDI method. To do it simply upload in the Arduino the Jtag2UPDI file. You can find it on GitHub.

Now you will need to install all the Arduino compilation files. To do it just follow this steps :

  1. Open Arduino IDE.
  2. Open the File > Preferences menu item.
  3. Enter the following URL in Additional Boards Manager URLs
https://mcudude.github.io/MegaCoreX/package_MCUdude_MegaCoreX_index.json
  1. Open the Tools > Board > Boards Manager... menu item.
  2. Scroll down until you see the MegaCoreX entry and click on it.
  3. Click install

Congratulation you just installed MegaCoreX, now you can build Arduino code for Atmega3208 and many others.

You can now choose the Atmega3208 into your board section. You should have the following settings:

  • Clock = 16Mhz
  • BOD = 2.6V
  • EEPROM = EEPROM retained
  • Pinout = 32pins standard
  • Reset = Reset
  • Bootloader = no bootloader

Now just plug the UPDI to UPDI pin on the board, and the 2 wire correctly. As the microcontroller have 2 power supply (1 from the jack and an other one when programming), you will have to plug a wire on the middle part of "Alim" and the other end of this wire on prog for programming or Run for jack power supply.

If everything is correctly done, you can just press the upload button and it will work !!!

If you want to program your controller, you should consider to build a USB UPDI programmer.

Step 8: Payload Formatter TTN

This step is used to format your payload on the TTN Server. To do it, Just follow this steps :

  • Go into your application and then your end devices
  • Click on payload formatter and choose JavaScript.
  • Just Copy/Paste the next point into the input box
function decodeUplink(input) {
  var data = {};
  data.poid = (input.bytes[2]) + (input.bytes[1] *256) + (input.bytes[0] *65536);
  data.balance = input.bytes[3];
  var warnings = [];
  if (data.balance > 6){
    warnings.push("Le nombre de balance n'est pas correcte");
  }
  if (data.poid > 5000000){
    warnings.push("Le poid semble trop important");
  }
  return {
    data: data,
    warnings: warnings
  };
}
  • Don't forget to save and this step is done.

Step 9: Web Interface & DB

This step is the most configurable steps! We choose to do a basic website that import data from a Database and show it on a graph. But you can create your own website if you want.

First, we need to create the Database, to do it we used phpmyadmin and a MariaDB install, but we will come back on this point later. Your database should be organize with 5 table :

  1. ruche1 : id is an integer / masse is a float and date is a time stamp.
  2. ruche 2 to ruche 4 is the same as ruche1
  3. modif : this table is use to send data to the beehive

If you want to create your own website, make sure to name correctly all 5 !

As the website part ca, be found everywhere, I will not explain to much this part, but you still can find all the files on my GitHub.

Step 10: From TTN to Database

Now we have to send data from the TTN server to our database. Before we start this step, you will need a fully installed apache server with php and MariaDB. you will also need a node-red server installed.

To make a link between these server we used a software called NODE-RED. It is a software use to transfer data flow from one point to an other. It is quite simple to use because there is no programming skills needed, but only block assembly. If you don't want to create your own flow chart (a schematic of every block used) you can download mine. First, you will have to download two libraries called MQTT and database. This two library are used to make the connection to the database and to the TTN server easily.

First we created the link to The TTN server using a MQTT protocol. This work by having a sender and a subscriber to a sender. When the sender send a files it will be automatically send to all subscribers. To activate the MQTT protocol you need to go into your application and integration and then MQTT. Just copy all the information and paste it into the Node RED MQTT block. Now if you just upload the flow you should see a green light under the mqtt block. It mean the connection between ttn and Node red is done. You can find some information about MQTT and TTN here.

Now we have to make some data separation because the data of the first channel should stay on the first database. To do it I used a sorter. Then i created an injection node using JavaScript and send it into the database. You can find more information here.

Step 11: Calibration Testing & Conclusion

We need to do a calibration because the board and all component didn't send us the weight, but only a value corresponding to the weight. So we calibrate it by putting some weight from 1 KG to 200Kg. We have to do it 3 times and we have to go up to 200Kg and then to go down to 1Kg because the sensor can react differently when the weight is going down. When everything is done, you should have a line (not perfect). Just put it into the flow of node-red and boom you now have the weight value.

After some test, we find out the system was working greatly, but it was maybe too power consuming. For the moment, the power supply is given by a batterie pack and a large solar panel. But in the future, we started thinking about saving power and making a little board solar powered.

Electronics Contest

Participated in the
Electronics Contest