ESP8266/ESP32自体でWebサーバを持つのではなく、外部にWebサーバを持つ環境を考えていたのですが、「Ambientでいいんじゃない?」という気がして、ESP8266とAmbientを試してみました。環境はいつもの如く、LinutMint19 + Arduino環境です。Ambientのユーザー登録は別途してあるものとします。
1.Ambientライブラリのインストール
Zipで持ってきてインストールする方法もあるのですが、Linuxの場合はgitで持ってくるほうが簡単です。持ってきてからArduino IDEを起動します。
~$ cd Arduino/libraries/ ~/Arduino/libraries$ git clone https://github.com/AmbientDataInc/Ambient_ESP8266_lib Cloning into 'Ambient_ESP8266_lib'... remote: Enumerating objects: 3, done. remote: Counting objects: 100% (3/3), done. remote: Compressing objects: 100% (3/3), done. remote: Total 101 (delta 0), reused 1 (delta 0), pack-reused 98 Receiving objects: 100% (101/101), 44.32 KiB | 242.00 KiB/s, done. Resolving deltas: 100% (37/37), done. ~/Arduino/libraries$
2.ソースコードの作成
ソースコードは smartconfig が入っているものをベースにしました。
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include "Ambient.h"
/*****************************************************************************/
/* NTP Client */
#include <time.h>
#define JST 3600*9
/*****************************************************************************/
/* Ambient */
unsigned int channelId = xxxxx;
const char* writeKey = "xxxxxxxxxxxxxxxx";
WiFiClient client;
Ambient ambient;
/*****************************************************************************/
/* Timer interrupt */
#define MS2CLK(ms) (ms * 80000L)
uint32_t nxTim;
uint32_t numTim = 0;
bool exec = false;
// 割り込みハンドラ
void timer0_ISR (void) {
nxTim += MS2CLK(100); // 100msec
timer0_write( nxTim );
// 割り込み処理
numTim++;
if( numTim % 10 == 0 ){
exec = true; // 1秒に1回処理するため
}
}
/*************************** Sketch Code ************************************/
void setup() {
uint8_t cnt = 0;
// set for STA mode
WiFi.mode(WIFI_STA);
// put your setup code here, to run once:
Serial.begin(115200);
Serial.flush();
Serial.println("\r\n");
// sensor input
pinMode(14,INPUT);
// LED
pinMode(12,OUTPUT);
pinMode(13,OUTPUT);
//configure pin0
pinMode(0, INPUT_PULLUP);
// deplay for 2 sec for smartConfig
Serial.println("2 sec before clear SmartConfig");
delay(2000);
// read pullup
bool isSmartConfig = digitalRead(0);
if (isSmartConfig == false) {
// bink for clear config
blinkClearConfig();
Serial.println("clear config");
// reset default config
WiFi.disconnect();
}
// if wifi cannot connect start smartconfig
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
if(cnt++ >= 15){
WiFi.beginSmartConfig();
while(1){
delay(500);
if(WiFi.smartConfigDone()){
Serial.println("SmartConfig Success");
blinkSmartConfig();
break;
}
}
}
}
Serial.println("");
WiFi.printDiag(Serial);
// Print the IP address
Serial.println(WiFi.localIP());
// Ambientへ接続
ambient.begin(channelId, writeKey, &client);
// タイマ割り込みの設定
noInterrupts();
timer0_isr_init();
timer0_attachInterrupt(timer0_ISR);
nxTim = ESP.getCycleCount() + MS2CLK(100); // 100msec
timer0_write( nxTim );
exec = false;
interrupts();
// time set with NTP
configTime( JST, 0, "ntp.nict.jp", "ntp.jst.mfeed.ad.jp");
}
void blinkSmartConfig() {
digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
delay(50); // wait for a second
digitalWrite(13, LOW); // turn the LED off by making the voltage LOW
delay(50);
}
void blinkClearConfig() {
int i=0;
while(i<=3) {
digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
delay(100); // wait for a second
digitalWrite(13, LOW); // turn the LED off by making the voltage LOW
delay(100);
i++;
}
}
int detcnt=0;
uint16_t SW1=0;
void loop() {
time_t t;
struct tm *tm;
char msg[64]="";
bool isSW1 = digitalRead(0);
// チャタリング除去
if(isSW1 == false){
SW1 = (SW1 << 1) | 0x0001;
} else {
SW1 = (SW1 << 1);
}
// SW1を押した回数を数える
if(SW1 == 0x0001){
detcnt++;
Serial.print(".");
}
if(exec){ // 1秒間に1回処理
digitalWrite(12, 1-digitalRead(12)); // Blink the LED
/*******/
/* NTP */
t = time(NULL);
tm = localtime(&t);
sprintf(msg,"%04d/%02d/%02d %02d:%02d:%02d %ld",
tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday,
tm->tm_hour, tm->tm_min, tm->tm_sec,
(long)t);
int s = tm->tm_sec;
//
if( s==0 ){ // 1分間に一回、毎分0秒にAmbientにデータ送信
ambient.set(1,detcnt);
ambient.send();
Serial.println("");
Serial.print(msg);
Serial.print(" - ");
Serial.print(detcnt);
Serial.print(" : ");
detcnt=0;
}
exec = false;
}
delay(5);
}
タクトスイッチを押した回数を1分間に1回送信するものですが、非常に簡単にグラフ表示できました。簡単でいいかもです。








