Reading temperature with Arduino and sending it via HC-12

I’ll be using Arduino Mini Pro cheap clone, that you can find anywhere on E-Bay for $2. Mine looks like this, works on 3.3v and is 8MHz. Make sure to have the correct settings in the Arduino IDE in order to program it. I’m using cheap PL2303 to RS232, which requires specific drivers for Mac OS X ( maybe for Windows, too? ).

So, I will be connecting pressure and temperature sensor, named BMP180, to it. I will only read the temperature, as I don’t have usage for pressure at the moment. Mine looks like this:

I will be using the HC-12 module, pre-configured in my previous post.

Connect the BMP180 to the Arduino. BMP180 uses I2C, so the connection is very easy:

  • VIN – Connect to Arduino 3.3V
  • GND – Connect to Arduino GND
  • SCL – Connect to Arduino A5
  • SDA – Connect to Arduino A4

Now you can try it out and see the output. You need to install Sparkfun BMP180 library in your Arduino IDE ( Easiest way is to use the Library Manager ).

This is the simplest sketch to display the temperature (both Celsius, Fahrenheit) in the serial output, from the BMP180 sensor.

You should see in the Serial monitor the following output:

temperature: 25.23 deg C, 77.42 deg F
temperature: 25.10 deg C, 77.19 deg F

Next, let’s connect the HC-12. We will be using Software Serial on the Arduino on pins 2 and 3. Connect the HC-12:

  • VCC – to Arduino 3.3v
  • GND – to Arduino GND
  • TX – to Arduino pin 2 ( RX )
  • RX – to Arduino pin 3 ( TX )

That’s it 🙂 Now you can use this simple sketch to send the data. I’ve used very simple format like this:

1|TEMP|CNTR|SENSORV|

But you can use any format you want, of course. 1 – Sensor ID, TEMP – Type of sensor. I recommend if you connect several sensors to one Arduino, to add one new line for each sensor. In this example, I connect only BMP180 temperature reading ( ignoring pressure value, I don’t need it ). CNTR – that’s incremental reading number, which I just added for counting how many packets has been sent. As I’m sending one packet every minute, this is basically the uptime in minutes :). SENSORV – Sensor value. In my case – temperature readings in Celsius.

Here’s the updated sketch file that sends temperature reading over HC-12 ( that I’ve configured on channel 7 ).

Note that this is very very simple way of doing it. As I don’t have tons of sensors and sender nodes, I don’t care that much about packets overriding themselves ( In theory, if two sensors send at the exact same time, the data will be corrupted ).

What can be improved here, is:

  • Sending some calculated HASH, that can be checked? Even if two sensors send at the same time, this will not be read as garbage, but ignored.
  • Authenticating nodes. Implement some very simple encryption on the sensor side, that later will be decrypted on the reader node. This will also prevent collisions, as the data won’t be properly readable. This will also make the communication secure and in case of sending commands, they won’t be executed, if someone tries to hi-jack and send commands directly.

I actually plan on implementing the encryption in a later post 🙂

In the next post, I will show you how to read the sensor data, then do whatever you want with it ( I will display it on the small OLED screen as an example ).

I'm a passionate software developer with experience in hardware, too. I love making things and the software for them. I also take photos for fun, that's my hobby :)

Leave a reply:

Your email address will not be published.

Site Footer