Ambientでは、端末ごとの固有のキー(デバイスキー)をチャネルに対応付け、端末からデバイスキーを送って、対応するチャネルIDとライトキーを取得する機能があります。
デバイスキーとしては端末の通信デバイスの物理アドレス(MACアドレス)など端末ごとに固有のデーターを使います。 Ambientのgetchannel()関数を使って、複数台の端末でも同じプログラムで端末ごとに別のチャネルにデーターを送信できるようになる機能を使用します。 Ambientでは4ヶ月間使用しないとチャンネルを削除されてしまいます。 チャンネルキーとライトキーの指定でスケッチを書いている場合は再度チャンネルを作り、それに合わせてスケッチ書き込みをしなければなりません。 デバイスキーとユーザーキーの指定であれば、チャンネル削除された場合Ambient側の再設定だけで済みます。 スケッチはAmbient再設定後でもそのまま使用出来ますので、改めて書き込みする必要がありません。
ESPr® Developerのセットアップ
こちらはESPr® Branch 32(Groveコネクタ付き)を使って、 温度センサーモジュールDS18B20の値をスマホに表示 設定温度を超えたらLINEに通知するのが目的です。 後述するスケッチをこちらの投稿のものにすることで、デバイスキーでのアクセスが可能となります。
ESPr® Developer32
include "Ambient.h"
#include <OneWire.h>
#include <WiFi.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 26 //温度センサーの黄色の線をESP32-WROOM-32の26番ピンに接続
#define uS_TO_S_FACTOR 1000000 //マイクロ秒から秒への変換係数
#define TIME_TO_SLEEP 300 //ESP32がスリープする期間を定義 (秒)
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature DS18B20(&oneWire);
// ESPr個体ごとのID設定
const char* kotaiId = "1";
// ESP32が接続するWi-Fiアクセスポイントの設定
const char* ssid = "SSID"; //無線ルーターのSSID
const char* password = "PASS"; //無線ルーターのパスワード
// Ambientのユーザーキー設定
const char* userKey = "ユーザーキー";
char devKey[20];
unsigned int channelId;
char writeKey[20];
//IFTTTの設定
const char* host = "maker.ifttt.com";
const char* secretkey = "シークレットキー";
WiFiClient client;
Ambient am; // Ambientオブジェクトを作る
void setup() {
int lpcnt = 0;
Serial.begin(115200); //デバッグ用にシリアルを開く
delay(1000); //シリアルモニターを開くまでの時間
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
Serial.println("Setup ESP32 to sleep for every " + String(TIME_TO_SLEEP) +
" Seconds");
//ディープスリープ状態の時すべてのRTC周辺機器をオフに設定
esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_OFF);
esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_SLOW_MEM, ESP_PD_OPTION_OFF);
esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_FAST_MEM, ESP_PD_OPTION_OFF);
esp_sleep_pd_config(ESP_PD_DOMAIN_MAX, ESP_PD_OPTION_OFF);
uint8_t mac[6];
WiFi.macAddress(mac); // Wi-FiのMACアドレスを取得する
sprintf(devKey, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
Serial.println(devKey);
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password); //アクセスポイントに接続する
while (WiFi.status() != WL_CONNECTED) { //接続完了したかどうかを判断
delay(500); //完了していなければ0.5秒間待つ
lpcnt +=1 ;
if (lpcnt>10) { ESP.restart(); }
Serial.print("."); //接続が確立するまで、・・・を表示
}
//ここに来たら、接続完了している。
Serial.println(""); //改行して繋がったことをシリアルで伝える。
Serial.println("WiFi connected");
Serial.println("IP address: "); //WiFiの状態を表示
Serial.println(WiFi.localIP());
if (am.getchannel(userKey, devKey, channelId, writeKey, sizeof(writeKey), &client) == false) {
Serial.printf("Cannot get channelId. Please set DeviceKey (%s) to Ambient.\r\n", devKey);
while (true) {
delay(0);
}
}
Serial.printf("channelId: %d, writeKey: %s\r\n", channelId, writeKey);
am.begin(channelId, writeKey, &client); // チャネルIDとライトキーを指定してAmbientの初期化
DS18B20.begin();
}
void loop() {
float celsius;
//float celsius2;
DS18B20.requestTemperatures(); // 温度取得要求
celsius = DS18B20.getTempCByIndex(0); // 温度センサーから摂氏気温を取得
//celsius2 = DS18B20.getTempCByIndex(1); //2本目の温度センサーから摂氏気温を取得
Serial.println("");
Serial.println("TEMP:" + String(celsius));
//Serial.println("TEMP:" + String(celsius2));
delay(10);
const char* event ="";
if (celsius >= 30 || celsius == -127) { //温度が30℃以上または-127℃なら
event = "high_temp";
}
Serial.print("connecting to ");
Serial.println(host);
// Use WiFiClient class to create TCP connections
WiFiClient client;
const int httpPort = 80;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
// We now create a URI for the request
String url = "/trigger/";
url += event;
url += "/with/key/";
url += secretkey;
url += "?value1=";
url += String(celsius);
url += "&value2=";
url += String(kotaiId);
Serial.print("Requesting URL: ");
Serial.println(url);
// This will send the request to the server
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
delay(1000);
// Read all the lines of the reply from server and print them to Serial
while(client.available()){
String line = client.readStringUntil('\r');
Serial.print(line);
}
Serial.println();
Serial.println("closing connection");
// センサー値をAmbientに送信する
am.set(1, celsius);
//am.set(2, celsius2);
am.send();
Serial.println("Configured all RTC Peripherals to be powered down in sleep");
Serial.println("Going to sleep now");
Serial.flush();
WiFi.disconnect(true);
esp_deep_sleep_start(); //ディープスリープスタート
Serial.println("This will never be printed");
}ESPr® Developer
こちらは参考まで。
#include <ESP8266WiFi.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include "Ambient.h"
#define ONE_WIRE_BUS 14 //温度センサーをESP-WROOM-02の14番ピンに接続
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature DS18B20(&oneWire);
// ESPr個体ごとのID設定
const char* kotaiId = "1";
// ESP8266が接続するWi-Fiアクセスポイントの設定
const char* ssid = "SSID";
const char* password = "pass";
// Ambientのユーザーキー設定
const char* userKey = "ユーザーキー";
char devKey[20];
unsigned int channelId;
char writeKey[20];
//IFTTTの設定
const char* host = "maker.ifttt.com";
const char* secretkey = "シークレットキー";
WiFiClient client;
Ambient am; // Ambientオブジェクトを作る
void setup() {
Serial.begin(115200); //デバッグ用にシリアルを開く
delay(10);
uint8_t mac[6];
WiFi.macAddress(mac); // Wi-FiのMACアドレスを取得する
sprintf(devKey, "%02X:%02X:%02X:%02X:%02X:%02X", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
Serial.println(devKey);
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid,password);
while (WiFi.status() != WL_CONNECTED) { //接続が確立するまで、・・・を表示
delay(500);
Serial.print(".");
}
//ここに来たら、接続完了している。
Serial.println(""); //改行して繋がったことをシリアルで伝える。
Serial.println("WiFi connected");
Serial.println("IP address: "); //WiFiの状態を表示
Serial.println(WiFi.localIP());
if (am.getchannel(userKey, devKey, channelId, writeKey, sizeof(writeKey), &client) == false) {
Serial.printf("Cannot get channelId. Please set DeviceKey (%s) to Ambient.\r\n", devKey);
while (true) {
delay(0);
}
}
Serial.printf("channelId: %d, writeKey: %s\r\n", channelId, writeKey);
am.begin(channelId, writeKey, &client);
DS18B20.begin();
}
int value = 0;
void loop() {
float celsius = 0;
//float celsius2 = 0;
DS18B20.requestTemperatures();
celsius = DS18B20.getTempCByIndex(0);
//celsius2 = DS18B20.getTempCByIndex(1);
Serial.println("");
Serial.println("TEMP:" + String(celsius));
//Serial.println("TEMP:" + String(celsius2));
++value;
delay(10);
char* event = "";
if (celsius >= 32 || celsius == -127) {
event = "イベント名";
}
Serial.print("connecting to ");
Serial.println(host);
// Use WiFiClient class to create TCP connections
WiFiClient client;
const int httpPort = 80;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
// We now create a URI for the request
String url = "/trigger/";
url += event;
url += "/with/key/";
url += secretkey;
url += "?value1=";
url += String(celsius);
url += "&value2=";
url += String(kotaiId);
Serial.print("Requesting URL: ");
Serial.println(url);
// This will send the request to the server
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
delay(1000);
// Read all the lines of the reply from server and print them to Serial
while(client.available()){
String line = client.readStringUntil('\r');
Serial.print(line);
}
Serial.println();
Serial.println("closing connection");
// センサー値をAmbientに送信する
am.set(1, celsius);
//am.set(2, celsius2);
am.send();
delay(2000);
ESP.deepSleep(300 * 1000 * 1000, RF_DEFAULT); //復帰までのタイマー時間設定
delay(1000);
}コメント
この記事へのトラックバックはありません。




この記事へのコメントはありません。