Last active
May 22, 2018 07:47
-
-
Save TakehikoShimojima/b50a36f7007ee2a8ab6316eb132cdf1e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ArduinoJson.h> | |
#include <ESP8266HTTPClient.h> | |
#include <ESP8266WiFi.h> | |
#include <ESP8266WiFiMulti.h> | |
#include "Ambient.h" | |
const char* ssid = "ssid"; | |
const char* password = "password"; | |
unsigned int channelId = 100; // AmbientのチャネルID | |
const char* writeKey = "writeKey"; // ライトキー | |
// IOピン宣言 | |
#define LEFT_BTN 14 | |
#define RIGHT_BTN 12 | |
#define ACTIVE 16 | |
#define RBOOTED 13 | |
#define LBOOTED 5 | |
#define LEFT_RED_LED 15 | |
#define RIGHT_RED_LED 4 | |
#define LEFT_GREEN_LED 2 | |
#define RIGHT_GREEN_LED 0 | |
bool lBooted; | |
bool rBooted; | |
int batteryLevel; | |
ESP8266WiFiMulti wifiMulti; | |
WiFiClient client; | |
Ambient ambient; | |
// 定形初期化処理(理解するまでは変更しないで) | |
void boot() { | |
Serial.begin(74800); | |
// pin setup | |
pinMode(LBOOTED, INPUT_PULLUP); | |
pinMode(RBOOTED, INPUT_PULLUP); | |
pinMode(LEFT_RED_LED, OUTPUT); | |
pinMode(RIGHT_RED_LED, OUTPUT); | |
pinMode(LEFT_BTN, INPUT); | |
pinMode(RIGHT_BTN, INPUT); | |
// スリープから復帰するのに押されてたボタン状態を取得 | |
lBooted = digitalRead(LBOOTED) == LOW; | |
rBooted = digitalRead(RBOOTED) == LOW; | |
// アクティブモードへ移行 | |
digitalWrite(ACTIVE, LOW); | |
pinMode(ACTIVE, OUTPUT); | |
// ACTIVE==LOWのあとでしか緑LEDを使えない | |
pinMode(LEFT_GREEN_LED, OUTPUT); | |
pinMode(RIGHT_GREEN_LED, OUTPUT); | |
// バッテリーレベル取得(アクティブモードでないとダメ) | |
batteryLevel = analogRead(A0) * 3000 / 1024; // [mV] | |
// 電池が消耗していたり、通電のみだった場合はすぐにスリープする | |
if (batteryLevel < 2300 || !lBooted && !rBooted) { | |
ESP.deepSleep(0); | |
delay(500); | |
} | |
} | |
void setup() { | |
boot(); // 必ずsetup関数の最初に実行 | |
ambient.begin(channelId, writeKey, &client); // チャネルIDとライトキーを指定してAmbientの初期化 | |
if (lBooted) { // 左ボタンが押されていたら | |
digitalWrite(LEFT_GREEN_LED, HIGH); // left green on | |
ambient.set(1, 1); // Ambientのデータ1に1をセット | |
} | |
if (rBooted) { // 右ボタンが押されていたら | |
digitalWrite(RIGHT_GREEN_LED, HIGH); // right green on | |
ambient.set(2, 1); // Ambientのデータ2に1をセット | |
} | |
// Wi-Fi connect | |
WiFi.mode(WIFI_STA); | |
wifiMulti.addAP(ssid, password); | |
while (true) { | |
if (wifiMulti.run() == WL_CONNECTED) { | |
break; | |
} | |
delay(500); | |
} | |
ambient.send(); // Ambientにデータを送信 | |
ESP.deepSleep(0); | |
delay(500); | |
} | |
void loop() {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment