---
# System prepended metadata

title: How to create a temperature senor using Helium and Datacake

---

# How to create a temperature senor using Helium and Datacake

**Eric Wikberg (ew223hi)**

This will be a short tutorial on how to create a temperature sensor that uses Helium to transport data and Datacake to visualize it. The entire tutorial should not take more than a couple of hours to complete.  


# Objective
At first I meant to create a temperature and GPS sensor for my car so that I could locate where I had parked it and see the temperature inside it. Due to lack of time and the fact that I haven't gotten a hold of a good GPS sensor I made the decision to only go for the temperature sensor and do the GPS-locater after the course. 

The purpose of the temperature sensor is for me to know if I need to go out earlier to the car and cool it down or heat it up before I put my child in it. 

It will be a fun thing to do but I dont think it will bring me any greater insights, more than the things I learn from doing it. :)

# Material
For this project i used the follwoing material: 

* PySense 
* FiPy
* MCP9700
* Breadboard
* Antenna
* Jumper wires
* USB-cable

*The Pysens enables me to for example connect the usb-cable.
FiPy is a microcontroler that i use to run micropython. 
MCP9700 is the temperature sensor.
The breadboard is used to easily connect and change senors.
The antenna enables LoRa conectivity. 
The jumperwires is used to connect different compnents on the breadbord. 
I use the USB-cable to power the device at the moment and also to push code to the FiPy.* 


The reason for using these are the fact that I got them in the FiPy and sensors bundle that I bought for this course. It cost me 1499kr. https://www.electrokit.com/produkt/lnu-1dt305-tillampad-iot-fipy-and-sensors-bundle/. I have no prior knowledge of Pycom devices so it was a natural choice just to go for the recommended bundle. 

# Computer setup
First of all you need to install Node.js to make the IDE work. You do this by following the instructions on this page: https://nodejs.org/en/ choose the "Current" version. 

Next you need to install your IDE of choice. I chooce Atom just becouse it was recommende in this course and it have worked really well for me. You do this by installing Atom here: https://atom.io

Next up you need to isntall the plugin "Pymakr" on Atom. This makes it posible to upload you code to the FiPy. Open Atom and the package manager from: File >> Settings >> Install. Search for Pymakr and press install. 

Now we have to update the firmware on the FiPy. 
First download the firmware software from: [here](https://software.pycom.io/findupgrade?product=pycom-firmware-updater&type=all&platform=win32&redirect=true).
Next install it and follow the instructions.

![](https://i.imgur.com/o1fh2nA.png)
![](https://i.imgur.com/z1XUSFY.png)
(Also mark the "CONFIG partition" and "NVS partition")

After that you should be done with the installation and you will be able to use Atom and the FiPy. 

To upload code to you device you only need to is 1. choose the right project, 2. select the right COM and 3. push the upload button. 
![](https://i.imgur.com/50bpLBQ.png)

# Putting everything together
The temperature sensor (MCP9700) is powered by the 3v pin and then returns to the ground pin. From the sensor there is a connection to pin 13 on the FiPy to get the temperature reading. 
![](https://i.imgur.com/Vxl65l5.jpg)




# Platform
I use the free version of Datacake at the moment as my platform. I just picked one that I thought was easy to use and I like Datacake now that I have used it. It gives me the ability to send the data that I want and present it in a nice way. 
I might use Pybytes later mainly because it is able to remotely update the FiPy so I dont have to bring it back in if I use it in my car. 

# The code
I am not a strong coder so I copied the code from https://hackmd.io/@lnu-iot/B1GUJqJq9 which sends temperature data but also humidity data. I have tried to remove the humidity parts but im pretty sure there is more stuff that could be removed in the code. But it works at least. 

Connecting to Helium:
```python=
from network import LoRa
import time
import binascii

lora = LoRa(mode=LoRa.LORAWAN, region=LoRa.EU868)

dev_eui = binascii.unhexlify('0000000000000000')
app_eui = binascii.unhexlify('0000000000000000')
app_key = binascii.unhexlify('00000000000000000000000000000000')

lora.join(activation=LoRa.OTAA, auth=(dev_eui, app_eui, app_key), timeout=0)

# wait until the module has joined the network
while not lora.has_joined():
    time.sleep(2.5)
    print('Not joined yet...')

print('Network joined!')

# Your old code from main.py should be here

```

Collecting temperature from pin 13, converting it and sending it to Datacake:
```python=
# Data is sent to Pybytes. Needs to flashed with Pybyte firmware
import time
import machine
#from machine import Pin
import socket

s = socket.socket(socket.AF_LORA, socket.SOCK_RAW)
s.setsockopt(socket.SOL_LORA, socket.SO_DR, 5)
s.setblocking(False)

adc = machine.ADC()
apin = adc.channel(pin='P13')

while True:
  millivolts = apin.voltage()
  temp = (millivolts - 500.0) / 10.0
  temp_to_send = int(temp*1000000) 
  bytes_to_send = temp_to_send.to_bytes(4, 'big') # convert to bytes in big endian order
  s.send(bytes_to_send) # send
  time.sleep(300)
```

Payload decoder:
```python=
function Decoder(payload, port) {
    // we extract the bytes and save them into an integer
    var payload_integer = 0 
    for (var i = 0; i < payload.length; i++) {
        payload_integer = (payload_integer << 8) | payload[i];
    }
    
    // we convert the integer to a string to more easily split it up
    payload_string = payload_integer.toString()
    
    // we split the values up and store them in a list
    values = [payload_string.slice(0,2) 
              ,payload_string.slice(2,4)
              ,payload_string.slice(4,6)
              ,payload_string.slice(6)]
    
    // we put them together and convert to a float
    temp = parseFloat(values[0]+'.'+values[1]) 
    
    // Finally return the values to be stored in the data fields
    return [
        {
            field: "TEMPERATURE",
            value: temp
        }
    ];
}
```

# Transmitting the data / connectivity
I started of by using WIFI just to get a hang of things but then decided to use LoRa. The main reason for this decision was that I wanted to use the project in my car to messure the temperature when it is parked. I park my car some distance away from where I live so i cant use the WiFi-connection but I live in a bigger city so we have a good coverage with Helium. That is why i choose LoRa and Helium. 

At the moment I send data every minute to see if it works, but I will probably put it around 30 minutes when I am done and then evaluate the time later on to se if I can prolong it or if I need to shorten it. 

Helium has an integration with Datacake and i think they use a webhook to forward the data.


# Presenting the data
I am using the built in dashboard on Datacake to present the temperature. Nothing special. 
![](https://i.imgur.com/lZzgzM9.jpg)

The data is saved every minute at the moment but I will increase the time in the future to save datacost and power on the device. 




# Finalizing the design

![](https://i.imgur.com/6T9J07c.jpg)

The project was really fun to do and I wish that I had more time during the course to do more. I will try to include a GPS and also add a battery in the future. 

Over all I think it went pretty smooth. I was very lost in the beginning and it was pretty intimidating when I first got the hardware but there is a lot of information on the internet and in the Discord and the workshops really helped me. I have learned a lot and had a good time in doing so. 