M5StickC를 도어 센서 (& 개폐 통지)로 설정
하드웨어 조달
M5 StickC (2000엔 전후)
M5 StickC Env Hat (500엔 전후)
개발
개발 환경
Arduino IDE 환경 구축
프로그램
#include <M5StickC.h>
#include <Wire.h>
#include "bmm150.h"
#include "bmm150_defs.h"
#include <WiFi.h>
#include <WiFiClientSecure.h>
const char* wifi_ap_ssid = "XXXXXXXX";
const char* wifi_ap_pass = "XXXXXXXX";
String door_state = "close";
uint16_t door_thr_close = 1600;
uint16_t door_thr_open = 800;
uint16_t door_ignore = 60000;
uint32_t door_lastpost = -door_ignore;
BMM150 bmm = BMM150();
void setup() {
Serial.begin(115200);
Serial.println("start debugging");
Wire.begin(0,26);
M5.begin();
M5.Lcd.setRotation(3);
M5.Lcd.fillScreen(BLACK);
pinMode(M5_BUTTON_HOME, INPUT);
initdev();
M5.Lcd.fillScreen(BLACK);
}
void initdev(){
M5.Lcd.setCursor(0, 0, 2);
if(bmm.initialize() == BMM150_E_ID_NOT_CONFORM){ while(1); }
M5.Lcd.printf("Mag sensor OK.\n");
WiFi.begin(wifi_ap_ssid, wifi_ap_pass);
while (WiFi.status() != WL_CONNECTED){ delay(500); }
IPAddress ip = WiFi.localIP();
M5.Lcd.printf("Wifi: %d.%d.%d.%d\n", ip[0], ip[1], ip[2], ip[3]);
delay(2000);
}
uint8_t setup_flag = 1;
void loop() {
bmm.read_mag_data();
int magval = (int)(sqrt(pow(bmm.raw_mag_data.raw_datax, 2) + pow(bmm.raw_mag_data.raw_datay, 2) + pow(bmm.raw_mag_data.raw_dataz, 2)));
String state = "-----";
if (door_thr_close < magval){ state = "close"; }
if (magval < door_thr_open ){ state = "open"; }
M5.Lcd.setCursor(0, 0, 2);
M5.Lcd.printf("%06d [%s] \n", magval, state);
if ((state != "-----") && ((millis()-door_lastpost) > door_ignore) && (door_state != state)){
door_state = state;
door_lastpost = millis();
String r = https_Web_Get("example.com", "/update?1=door&2=" + state + "&3=" + String(magval));
M5.Lcd.printf("send: %s \n", state);
M5.Lcd.println(r);
}
delay(200);
if(!setup_flag){
setup_flag = 1;
initdev();
}
if(digitalRead(M5_BUTTON_HOME) == LOW){
setup_flag = 0;
while(digitalRead(M5_BUTTON_HOME) == LOW);
}
return;
}
String https_Web_Get(const char* host, String target_page){
String res;
WiFiClientSecure https_client;
if (https_client.connect(host, 443)){
String s = "GET https://" + String(host) + target_page + " HTTP/1.1\r\nHost: " + String(host) + "\r\n";
s += "User-Agent: ESP32\r\nConnection: close\r\n\r\n\0";
https_client.print(s);
https_client.flush();
}
if(https_client){
while(https_client.connected()){
String r = https_client.readStringUntil('\n');
if(0 <= r.indexOf("HTTP")){res = r;}
Serial.println(r);
if (r == "\r") break;
}
while (https_client.available()){ https_client.read(); }
https_client.stop();
}
return res;
}
설치 및 조정
Reference
이 문제에 관하여(M5StickC를 도어 센서 (& 개폐 통지)로 설정), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/uniqode/items/1a4fff1e06c8d9958ef2텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)