
IoT開発、設備・DIYのブログ!
#define M5STACK_MPU6886 | |
#include <M5Stack.h> | |
#include <Wire.h> | |
#include "Adafruit_Sensor.h" | |
#include <Adafruit_BMP280.h> | |
#include "SHT3X.h" | |
#include <WiFi.h> | |
#include <PubSubClient.h> | |
#include <ArduinoJson.h> | |
char *ssid = "*******";// Wi-FiのSSID | |
char *password = "*******";// Wi-Fiのパスワード | |
const char *endpoint = "*******";// MQTTの接続先のIP | |
const int port = 1883;// MQTTのポート ※サーバー側で解放してください | |
char *deviceID = "M5Stack";// デバイスID デバイスIDは機器ごとにユニークにします | |
char *pubTopic = "sanmple";// メッセージを知らせるトピック | |
char *subTopic = "sample";// メッセージを待つトピック | |
SHT3X sht30; | |
Adafruit_BMP280 bme; | |
float tmp = 0.0; | |
float hum = 0.0; | |
float pressure = 0.0; | |
float pres = 0.0; | |
WiFiClient httpsClient; | |
PubSubClient mqttClient(httpsClient); | |
void setup_serial(){ | |
Serial.begin(115200); | |
while (!Serial) continue; | |
} | |
void setup_wifi(){ | |
Serial.println("Connecting to "); | |
Serial.print(ssid); | |
WiFi.disconnect( true, true ); | |
delay(500); | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(100); | |
M5.Lcd.print("."); | |
} | |
Serial.println("\nWiFi Connected."); | |
M5.Lcd.setCursor(10, 40); | |
M5.Lcd.setTextSize(2); | |
M5.Lcd.println("WiFi Connected."); | |
M5.Lcd.print("IP address: "); | |
M5.Lcd.println(WiFi.localIP()); | |
} | |
void setup() { | |
M5.begin(); | |
Wire.begin(); | |
M5.Power.begin(); | |
M5.IMU.Init(); | |
setup_serial(); | |
setup_wifi(); | |
mqttClient.setServer(endpoint, port); | |
connectMQTT(); | |
String f32 = "genshin-regular-32pt"; | |
M5.Lcd.loadFont(f32, SD); | |
M5.Lcd.setBrightness(10); | |
M5.Lcd.setTextSize(3); | |
Serial.println(F("ENV Unit(SHT30 and BMP280) test...")); | |
while (!bme.begin(0x76)){ | |
Serial.println("Could not find a valid BMP280 sensor, check wiring!"); | |
// M5.Lcd.println("Could not find a valid BMP280 sensor, check wiring!"); | |
} | |
M5.Lcd.clear(BLACK); | |
//M5.Lcd.println("ENV Unit test..."); | |
} | |
void connectMQTT(){ | |
while (!mqttClient.connected()) { | |
if (mqttClient.connect(deviceID)) { | |
Serial.println("Connected."); | |
int qos = 0; | |
mqttClient.subscribe(subTopic, qos); | |
Serial.println("Subscribed."); | |
} else { | |
Serial.print("Failed. Error state="); | |
Serial.print(mqttClient.state()); | |
// Wait 5 seconds before retrying | |
delay(5000); | |
} | |
} | |
} | |
void mqttLoop(){ | |
if (!mqttClient.connected()) { | |
connectMQTT(); | |
} | |
mqttClient.loop(); | |
} | |
void loop() { M5.Lcd.clear(BLACK); | |
pressure = bme.readPressure(); | |
if(sht30.get()==0){ | |
tmp = sht30.cTemp; | |
hum = sht30.humidity; | |
} | |
pres = pressure*0.01; | |
Serial.printf("Temperatura: %2.2f*C Humedad: %0.2f%% Pressure: %0.2fPa\r\n", tmp, hum, pres,0); | |
M5.Lcd.setTextColor(WHITE, BLACK); | |
M5.Lcd.setCursor(0, 120); | |
M5.Lcd.setTextSize(7); | |
M5.Lcd.printf("%2.1f %2.0f %2.0f", tmp, hum, pres); | |
M5.Lcd.setTextSize(3); | |
M5.Lcd.setCursor(0, 80); | |
M5.Lcd.printf("温度"); | |
M5.Lcd.setCursor(100, 80); | |
M5.Lcd.printf("湿度"); | |
M5.Lcd.setCursor(200, 80); | |
M5.Lcd.printf("気圧"); | |
M5.Lcd.setCursor(0, 150); | |
M5.Lcd.printf("[゜C]"); | |
M5.Lcd.setCursor(100, 150); | |
M5.Lcd.printf("[%]"); | |
M5.Lcd.setCursor(200, 150); | |
M5.Lcd.printf("[hPa]"); | |
char pubMessage[128]; | |
mqttLoop(); | |
sprintf(pubMessage, "温度:%.1f,湿度:%2f,気圧:%4f", tmp, hum, pres); | |
//sprintf(pubMessage, "{\"temperature\":\"%.1f\",\"humidity\":\"%2f\",\"barometricpressure\": \"%4f\"}", tmp, hum, pres); | |
mqttClient.publish(pubTopic, pubMessage); | |
Serial.println("Published."); | |
delay(10000); | |
} |