This is a guide to help you get your Sigfox device connected through the Sigfox cloud and sending sensor measurements into Tinamous so you can see your sensor data just like the screenshot below for my Sigfox System Monitor.
In this guide we'll be using an Arduino MKR Fox 1200 as our Sigfox device. The example is from another project I'm working on so keep an eye out for that on Hackster.io soon!
Deploy your code to the ArduinoYou can use the Arduino cloud IDE or the local IDE to develop and deploy your application (note that you will need to install the driver for the MKR FOX 1200 locally so you need the local IDE anyway to install the board anyway).
I tried both IDE's but ended up preferring to use the local IDE editor on Windows as it was more reliable at programming the Arduino, so the instructions are for that.
From the Arduino IDE select the Boards Manager and find the "Arduino SAMD Boards" (version 1.6.16 at the time of writing) and install that.
After installation you can plug your Arduino into the USB port and the appropriate device driver should install. Be sure to select the board under Tools -> Board in the IDE.
You are also going to need to install the Sigfox library in the IDE as well, select the library manager and add:
- Arduino SigFox for MKRFox1200 (Version 1.0.2 at the time of writing)
You might also like to install the Arduino Low Power library as one of the great features of Sigfox is low power usage, so it's likely you will be using it on a power limited system and putting the Arduino to sleep will help.
The actual code deployed to the Arduino is attached to this project and yours will no doubt be different so I won't discuss that, however the important part is the data structure we use and should be based on your requirements. Here's the data structure used for this project:
typedef struct __attribute__ ((packed)) sigfox_message {
uint8_t status; // status::uint:8 -> Split to 8 bits
uint8_t version; // version::uint:8
int16_t temperature; // temperature::int:16:little-endian
int16_t current; // current::int:16:little-endian
uint16_t voltage; // voltage::uint:16:little-endian
int16_t currentOffCharge; // currentOffCharge::int:16:little-endian
uint16_t voltageOffCharge; // voltageOffCharge::uint:16:little-endian
} SigfoxMessage;
This struct gets send over the air byte by byte in the order it's defined in the struct. For example if we have the following:
SigfoxMessage message;
message.status = 0xFF;
message.version = 0x00;
message.temperature = 0xC01D;
message.current = 0x1000;
message.voltage = 0x1234;
message.currentOffCharge = 0x0015;
message.voltageOffCharge = 0x1234;
The packet sent to Sigfox would look like:
0xFF 0x00 0x1D 0xC0 0x00 0x10 0x34 0x12 0x15 0x00 0x34 0x12
Notice how 0xC01D for temperature is converted to the bytes 0x1D 0xC0 in little endian order.
Our application needs to do one other thing, that's to tell us the ID and PAC of the Sigfox radio module. Once we have these we can disable that bit of the code. You'll need to open the Arduino serial monitor to capture them.
At this point we can build and deploy our code and get it running on our Arduino and sending messages to the Sigfox cloud, however we have no access to those messages so they just end up going nowhere.
Activate your device at SigfoxIn order to get our messages we need to tell Sigfox that the device belongs to us, we do that by activating the device with the device ID and PAC at Sigfox.
Head on over to https://backend.sigfox.com/activate
Select your device (Arduino in this case).
Select your country (UK for me).
(Tip: Don't close the cookie warning as it may cause the page to chage to the login page).
Enter the DeviceID and PAC code that is being output to the serial monitor by the Arduino.
You can also register for a Sigfox account in the process if you do not already have one.
At this point we should be able to see our device in the Sigfox Device list page, notice that the Average Rssi and SNR are populated, if your device is not sending these will be greyed out. Pro-tip: don't forget to connect the aerial, it makes a big difference to your data actually making to the receiver!
Once the data arrives at the Sigfox cloud we can opt to parse it back to a data structure. We can also sent the 12 bytes straight on but then it just needs to be parsed later on so we use the Sigfox cloud to do the work.
Sigfox provides a language to help you parse the data into something a little more meaningful. We can split our 12 bytes into booleans, bytes, 16 or 32 bit signed or unsigned integers, floats, and chars. Note: You don't have to use all 12 bytes if you don't need to.
We can also set the endianness for those integers and floats which is important as the Arduino uses Little Endian and Sigfox defaults to Big Endian.
Sadly their appears to be no way to factorise our values back to original values, so if you converted a float to an int by multiplication. e.g. 12.2 (float) * 10 -> 122 (byte or int) you still have the converted value at this stage (see later).
The format is: field_name:byte_index:type:size_or_bit
We can leave byte_index empty and rely on the sequence to represent the bytes index.
Below is the Sigfox Custom payload config used to parse our Arduino structure. This should be pasted into the callback set-up (see later) as one long line.
status::uint:8 version::uint:8 temperature::int:16:little-endian current::int:16:little-endian voltage::uint:16:little-endian currentOffCharge::int:16:little-endian voltageOffCharge::uint:16:little-endian
Lets split the line up to make it more readable
status::uint:8
version::uint:8
temperature::int:16:little-endian
current::int:16:little-endian
voltage::uint:16:little-endian
currentOffCharge::int:16:little-endian
voltageOffCharge::uint:16:little-endian
You can see here how the types, sizes and endianness are set and aligns with our data structure.
The status byte is actually going to be 8 boolean flags to represent the state of relays and outputs so it would be more appropriate as below, but for now we'll keep it as a byte.
batteryLow::bool:7
chargeRelaySet::bool:6
charging::bool:5
spare::bool:4
outputRelaySet::bool:3
output1Enabled::bool:2
output2Enabled::bool:1
output3Enabled::bool:0
Registering at TimamousIf you don't already have a Tinamous account you can register quickly and easily at https://tinamous.com/Registration and create a basic account for free. When you create an account you get your own little organisational area of Tinamous with it's own subdomain (i.e. MyTinamousAccount.Tinamous.com) so you can create accounts for project teams, or work and home without sharing data.
Create the Sigfox Bot at TinamousOnce you've registered and logged in (you did use a nice secure password now didn't you...) we can start by adding the Sigfox Bot to enable integration with Sigfox.
From the menu select "Bots" to take you to the Bots page, then click the "[Add Sigfox Bot]" option on the top right of the page.
A dialog box will appear and ask you for information about the bot.
Most of this can be left as default, however if you wish to let the bot access your Sigfox cloud account you will need to enter the API Login and password details. Letting the Sigfox Bot access the API allows it to get additional information on your devices which can be reported back to you through Tinamous and add your devices with appropriate device types (and tags) automatically.
To use the API you will need to create an Api access setting in the Sigfox cloud. Find your GROUP and then select API ACCESS from the left menu, then click the [New] button top right.
I opted to give the Sigfox Bot full access, you may wish to do otherwise.
Copy and paste the Login and Password values to the Add Sigfox Bot dialog box.
Then click the green [Add] button on the dialog box. Once the bot has been created (this should be nearly instantaneous) the dialog box will display the settings you need to use to configure the custom callback.
Do not close the dialog box yet as some of the information you need (particularly the authorization header) is not available after you close the dialog box.
Create the callback at the Sigfox backendWe use a custom callback from Sigfox to push our messages to Tinamous, this needs to be configured with the values shown on your Add Sigfox Bot dialog box.
Head back to the Sigfox backend https://backend.sigfox.com
Select your device type, and click CALLBACKS from the left menu.
Click [New] in the top right of the page.
Select Custom callback
Set the Type to DATA and UPLINK with URL as the Channel.
Enter your custom payload config based on your data structure.
Enter the Url pattern, HTTP Method and Authorization headers as detailed in the Sigfox Bot dialog box (not that the Url will be based on your Tinamous account name and Authorization is tied to the Sigfox Bot instance so don't copy these from anywhere else!).
Ensure content type is set correctly otherwise Tinamous won't get any data.
Copy and paste the body from the Sigfox Bot into the Callback Body section, adding in your own custom variables into the Fields section. The json field name (e.g. "status") automatically becomes a field on your device when the measurement is published.
Be sure Device is set correctly in the Body as this is important to assign the message to the device, other information is optional.
Notice by pushing "Lat" and "Lng" with your data, Tinamous is able to show you the approximate device location and movement on a map. This needs the new Sigfox geolocation service which may not be available for devices other than the Arduino.
Click the [OK] button on the callback editor ensuring their are no validation errors. We should now be good to get our data from the Arduino into Tinamous.
Monitoring your device and data in TinamousOnce you have created the callback you should start to see data being published for your devices. If you enabled Sigfox API access for the Bot your device will already be listed in the Tinamous device list, however if you did not then the device will be created for you once it pushes data through to Tinamous.
Device fields (your sensor measurements) are created automatically based on the fields you defined in the callback.
Find your device on the Tinamous devices page (Hint: Filter by "Added in the last hour" or "Added Today" to make it easier to find your device if you have lots.
You can Click on [Edit] to edit the device details, you might like to give it a better display name (device name is fixed), set the not reporting after period, change the tags and edit the fields.
I've set my device to be flagged as "Not Reporting" after 1 hour of no measurements being sent, I can then set-up a notification to warn me of any devices not reporting and see this information on my Tinamous dashboard.
I've also set units and tagged certain fields, as well as hiding other ones. Tagging fields makes it easy to generate dashboard charts for a field based on it's tag rather than it needing to have a specific name, across a range of devices.
Be sure to click the green [Save] button when you've finished editing the device.
You can now click the device link to go to the device page and view your data.
The graph and last measurements are updated in real-time, in-fact you can expect to see your measurements appear even before the debug LED has gone off on the Arduino!
Adjusting fields for converted valuesSigfox is limited to 12 bytes of data per message, in order to pack as much into those 12 bytes as possible we may need convert our sensor readings.
For example, when we read the voltage we use a float, however that float will need 4 or 8 bytes and we really don't care about anything past the second decimal place, in this example I simply multiply the voltage by 100 and cast it to a 2 byte unsigned integer (uint16_t) (12.23V becomes 1223), if we know the battery voltage is never going above 25.5V and we don't care about the second decimal then we can safely use a single byte. This method keeps the conversion nice and simple and is easy to figure out in our head. To get the voltage back we simply divide by 100 (or multiply by 0.01)
voltage = 0.01 x value
We could use a narrower range (e.g. maybe 10-15V is the working range) so we only need to represent 5V in the byte which would allow us to go down to 0.02V per bit. In order to get our voltage back from the byte we apply a factor:
voltage = 0.02 x byte_value + 10
You can also divide the original value by a factor if it is large and you don't mind dropping some precision.
We can restore our actual sensor values by using the calibration feature of a field in Tinamous to apply the y=mx + c (y=0.01 * x + 0) equation.
Notice that I've also set a working range for the voltage between 11 and 15 volts. I can use this to set-up a notification if the voltage goes below (or above) this range to send me an email or sms message.
Getting Tinamous NotificationsOn the Tinamous Notifications page click [Add Notification]
Our voltage field has a lower working range set to 11 Volts, so I'll set-up a notification based on a field value being below the working range to warn me the battery is going flat. I could also select a "Device is not reporting" if I wished to know if the device stops sending data (maybe due to a flat battery), or other notifications.
I've left field tag and name empty as this includes all fields and selected only "Below Working Range" for this notification.
Next I'm filtering the devices for just those tagged with "Sigfox", I could select the specific device but if I add more then I would need to amend this notification.
I decided I wanted to be notified once per day for each device so I've set the re-trigger options for this. Tinamous defaults to sending the notification on every matching instance, this would cause us to get a notification every 15 minutes when the battery starts to go flat which would get annoying very fast.
I opted for email and sms notifications.
Finally I gave it a half decent name and created the notification...
That's our notification created. Now when the battery being monitored by my Sigfox device goes low I'll get a message or two.
DashboardI also added a solid gauge and 7-segment display to a dashboard so I can see what my Sigfox device is up to.
Try and avoid using pin 6 on the Arduino as it is connected to the on-board LED which is used by the Sigfox module when debug is enabled to make it flash. I've ended up with a noisy relay when transmitting Sigfox data!
More InfoSigfox Makers Page: http://makers.sigfox.com/
Tinamous Sigfox Bot help page https://tinamous.com/Help/Bots/SigfoxBot
About Tinamous notifications: https://tinamous.com/Help/Notifications
Comments