Ardiunoにsigfoxシールドを付けて無線通信を行う

Ardiunoにはシールドと呼ばれるボードを追加することで多様なことができます。
インターネット上で紹介記事はたくさんあり、このシールドを使った無線通信による環境情報(温度、湿度、照度)のデータをとります。

1.Sigfoxとは?

部品構成は、↓で書いたブログを使います。

フランスSigfox社が開発したIoTを用途とした通信規格です。
データ量は、スマートフォンなどの3G・4Gに対して送れる情報量はすくないものの、安価・省電力で運用できるため、組込用機器に適しています。
温度、湿度、電圧値、電流値などの数値データを遠隔地へ送るロガーとして適しています。
最近話題のIoT農業などでは温度・照度・湿度などの気象情報がないと、分析ができないので必須です。

技術的な詳細はこちらのページを参照ください。
Sigfoxモジュール | LPWA製品 | 村田製作所 (murata.com)
Sigfoxとは|IoTネットワーク「Sigfox」|KCCS

2022年1月のニュースで経営難が報道されています。
省電力で通信できるデバイスは、状態監視には適しており経営をなんとか維持してほしい気持ちがあります。

日経クロステックの記事より
IoT通信のSigfoxが経営難で再建手続き KCCSは日本での運営継続 | 日経クロステック(xTECH) (nikkei.com)

2.機器の構成

2.1 部品構成

2.2 ソースコード

このデータは、30秒おきに温度、湿度、照度を通信する仕組みです。
流れは、①初期化、②温度・湿度・照度の取得(緑マーカー部)、③Sigfoxサーバへ取得した情報を送信します(赤マーカー部)。
その後、30秒おきに②と③を繰り返します。

//  Send sample SIGFOX messages with UnaBiz UnaShield V2S Arduino Shield.
//  This sketch includes diagnostics functions in the UnaShield.
//  For a simpler sample sketch, see examples/send-light-level.
#include "SIGFOX.h"
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include "SparkFun_VEML6030_Ambient_Light_Sensor.h"

#define SEALEVELPRESSURE_HPA (1013.25)

Adafruit_BME280 bme; // I2C

//  IMPORTANT: Check these settings with UnaBiz to use the SIGFOX library correctly.
static const String device = "g88pi";  //  Set this to your device name if you're using UnaBiz Emulator.
static const bool useEmulator = false;  //  Set to true if using UnaBiz Emulator.
static const bool echo = true;  //  Set to true if the SIGFOX library should display the executed commands.
static const Country country = COUNTRY_TW;  //  Set this to your country to configure the SIGFOX transmission frequencies.
static UnaShieldV2S transceiver(country, useEmulator, device, echo);  //  Uncomment this for UnaBiz UnaShield V2S / V2S2 Dev Kit
// static UnaShieldV1 transceiver(country, useEmulator, device, echo);  //  Uncomment this for UnaBiz UnaShield V1 Dev Kit

#include "SparkFun_VEML6030_Ambient_Light_Sensor.h"
#define AL_ADDR 0x48
SparkFun_Ambient_Light light(AL_ADDR);
float gain = .125;
int time = 100;
long luxVal = 0; 



//************************************
static const unsigned int INTERVAL =10000;//メッセージ送信間隔(30秒)
//************************************

void setup() {  //  Will be called only once.
  //transiverの起動
  if (!transceiver.begin()) stop(F("Unable to init SIGFOX module, may be missing"));  //  Will never return.
 
  //  Send a raw 12-byte message payload to SIGFOX.  In the loop() function we will use the Message class, which sends structured messages.
  transceiver.sendMessage("0102030405060708090a0b0c");

    //BME280センサ起動
  if (!bme.begin(0x76)) stop("Bosch BME280 sensor missing");  //  Will never return.
  
   //光センサ起動 詳細はVEML6030の設定を参照
  bool flg = light.begin();
  light.setGain(gain);
  light.setIntegTime(time);
  float gainVal = light.readGain();
  int timeVal = light.readIntegTime();
  
  //5秒間停止    
  delay(50000);

}

void loop() {  //  Will be called repeatedly.
  //  Send message counter, temperature and voltage as a structured SIGFOX message, up to 10 times.
  static int counter = 0, successCount = 0, failCount = 0;  //  Count messages sent and failed.
  
 //getting data from BME280
  float temperature = bme.readTemperature();
  float humidity = bme.readHumidity();
  float pressure = bme.readPressure() / 100.0F; 
  float altitude = bme.readAltitude(SEALEVELPRESSURE_HPA);

  //getting data from umashield
  float voltage;
  transceiver.getVoltage(voltage);

  luxVal = light.readLight();
  int lux = luxVal;



  //INTERVAL間隔でのデータ送信 
  bool suceess = sendSigfoxMessage(humidity,temperature,pressure); 
 
  delay(INTERVAL);
}

// 温度、湿度、気圧をsigfox構造メッセージで送る
bool sendSigfoxMessage(float humidity, float temperature, float pressure) 
{
  Message msg(transceiver);  //  Will contain the structured sensor data.
  msg.addField("hum", humidity);  //  4 bytes for the counter.
  msg.addField("tmp", temperature);  //  4 bytes for the temperature.
  msg.addField("prs", pressure);  //  4 bytes for the voltage.
  bool success =msg.send();
  return success;
}

// counter、電圧、altitudeをsigfox構造メッセージで送る
bool sendSigfoxMessage2(float counter, float voltage, int lux) 
{
  Message msg(transceiver);  //  Will contain the structured sensor data.
  msg.addField("cnt", counter);  //  4 bytes for the counter.
  msg.addField("vol", voltage);  //  4 bytes for the temperature.
  msg.addField("lux", lux);  //  4 bytes for the voltage.
  bool success =msg.send();
  return success;
}

3.結果を見る

データの確認は、SORACOMでSigfox for Arduinoを購入したので、SORACOMのサービスを使いました。
今回は、お手軽にできるHarvestを使いました。
1)アクセス
こちらのページからログオンページへアクセスします。
https://console.soracom.io/#/login?return_to=%2F

2)データの確認
ログオンするとデータが見られます。やってみるとすぐにできます。
ダウンロードからcsv形式での時系列データ取得も可能です。

今回、乾電池4個を接続して通信をしましたが、1日しか持たず長期間運用できませんでした。
課題は省電力化です。

4.まとめ

省電力化は、乾電池2個で数ケ月の運用ができるなど記事もインターネット上で見ますので、できたらブログにアップしたいと思います。