Skip to content

Instantly share code, notes, and snippets.

@TakehikoShimojima
Last active April 1, 2021 02:07
Show Gist options
  • Save TakehikoShimojima/7a3317a4f1542c5b3dce0414e5fcc4bf to your computer and use it in GitHub Desktop.
Save TakehikoShimojima/7a3317a4f1542c5b3dce0414e5fcc4bf to your computer and use it in GitHub Desktop.
#include <M5StickC.h>
#include <Wire.h>
#include "porthub.h"
#include "Ambient.h"
PortHub porthub;
uint8_t HUB_ADDR[6]={HUB1_ADDR,HUB2_ADDR,HUB3_ADDR,HUB4_ADDR,HUB5_ADDR,HUB6_ADDR};
WiFiClient client;
Ambient am;
const char* ssid = "ssid";
const char* password = "password";
unsigned int channelId = 100; // AmbientのチャネルID
const char* writeKey = "writeKey"; // ライトキー
void setup() {
M5.begin();
porthub.begin();
M5.Lcd.setTextSize(2);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
M5.Lcd.print(".");
}
M5.Lcd.print("WiFi connected\r\nIP address: ");
M5.Lcd.println(WiFi.localIP());
am.begin(channelId, writeKey, &client);
}
int oldstat[3] = {0, 0, 0};
int newstat[3] = {0, 0, 0};
void loop() {
int val[3];
int thresh[3] = {300, 180, 250}; // 緑・橙・赤の点滅判定しきい値
M5.Lcd.fillScreen(BLACK);
bool changed = false; // 前回の測定から変化したかのフラグ
for(int i = 0; i < 3; i++) {
val[i] = porthub.hub_a_read_value(HUB_ADDR[i]); // 光センサの値を取得
newstat[i] = (val[i] < thresh[i]) ? 1 : 0; // 値がしきい値以下なら点灯
if (oldstat[i] != newstat[i]) { // 前回と違っていたらフラグを立てる
changed = true;
}
M5.Lcd.setCursor(10, 10 + 50 * i);
M5.Lcd.print(val[i]);
M5.Lcd.setCursor(10, 30 + 50 * i);
M5.Lcd.print(newstat[i]);
}
Serial.printf("%d, %d, %d\r\n", val[0], val[1], val[2]);
if (changed) { // 前回の測定と違っていたら
for (int i = 0; i < 3; i++) {
if (oldstat[i] != newstat[i]) { // 違っていたら
am.set(i + 1, newstat[i]); // 値をsetする
}
oldstat[i] = newstat[i];
}
am.send(); // Ambientに送信
}
delay(5000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment