Skip to content

Instantly share code, notes, and snippets.

@TakehikoShimojima
Created February 8, 2021 04:17
Show Gist options
  • Save TakehikoShimojima/153e03e12d1f522cfbb5bf82902ff856 to your computer and use it in GitHub Desktop.
Save TakehikoShimojima/153e03e12d1f522cfbb5bf82902ff856 to your computer and use it in GitHub Desktop.
#include <M5Stack.h>
#include <Wire.h>
#include "Adafruit_Sensor.h"
#include <Adafruit_BMP280.h>
#include "SHT3X.h"
#define rxPin 16
#define txPin 17
SHT3X sht30;
Adafruit_BMP280 bme;
#define TIME_TO_SLEEP 30 // データ送信周期
void setup() {
M5.begin();
M5.Lcd.setTextSize(2);
Wire.begin(); // I2Cを初期化する
while (!bme.begin(0x76)){
M5.Lcd.println("Could not find a valid BMP280 sensor, check wiring!");
}
Serial2.begin(9600, SERIAL_8N1, rxPin, txPin); // Sigfoxモジュールと通信するシリアルを初期設定する
M5.Lcd.setCursor(20, 80);
M5.Lcd.print("press A btn to start");
while (!M5.BtnA.wasReleased()) {
M5.update();
}
}
String convertFloatToHex(float val) { // float型データを16進数に変換する
union {
uint32_t B32;
float Float;
} floatb32;
floatb32.Float = val;
return String(floatb32.B32, HEX);
}
void loop() {
float tmp, hum, pressure;
if (Serial2.available()) {
M5.Lcd.println(Serial2.readString());
}
pressure = bme.readPressure() / 100; // 気圧をhPaで取得する
if (sht30.get()!=0) {
return;
}
tmp = sht30.cTemp; // 温度を取得する
hum = sht30.humidity; // 湿度を取得する
M5.Lcd.fillScreen(BLACK);
M5.Lcd.setCursor(20, 40);
M5.Lcd.printf("temp: %4.1f'C\r\n", tmp);
M5.Lcd.setCursor(20, 80);
M5.Lcd.printf("humid:%4.1f%%\r\n", hum);
M5.Lcd.setCursor(20, 120);
M5.Lcd.printf("press:%4.0fhPa\r\n", pressure);
// 送信メッセージを作る
String msg = "AT$SF=" + convertFloatToHex(tmp) + convertFloatToHex(hum) + convertFloatToHex(pressure);
M5.Lcd.setCursor(20, 160);
M5.Lcd.print(msg); // 送信メッセージをLCDに表示する
Serial2.println(msg); // メッセージをSigfoxで送信する
delay(TIME_TO_SLEEP * 1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment