2016年5月21日土曜日

ESP8266でIoT - Adafruit IO偏

  Adafruit IOでESP8266のWiFiモジュールをIoTデバイスに使用する方法を説明します。

※「ESP8266でIoT - 開発環境のセットアップ」の手順で開発環境をセットアップ済のこととします。

○準備
Adafruit IO+Arduino+MQTTでIoT‐センサー・データの収集」の「③Adafruit IOの準備」の手順でアカウントを作成します。
②「Adafruit MQTT Library」(Adafruit_MQTT_Library-master.zip)をダウンロードします。
③Arudino IDEを起動します。
④[スケッチ]→[ライブラリをインクルード]→[.ZIP形式のライブラリをインクルード]を選択します。
⑤ダウンロードしたZIPファイル「Adafruit_MQTT_Library-master.zip」を選択し、[開く]をクリックします。

○プログラム 

・データの送信
 Adafruit IOにデータを送信するプログラムは、次のように書きます。

#include <ESP8266WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#define WLAN_SSID "...your SSID..."
#define WLAN_PASS "...your password..."
// Adafruit.io Setup
#define AIO_SERVER "io.adafruit.com"
#define AIO_SERVERPORT 1883 // use 8883 for SSL
#define AIO_USERNAME "...your AIO username (see https://accounts.adafruit.com)..."
#define AIO_KEY "...your AIO key..."
// Create an ESP8266 WiFiClient class to connect to the MQTT server.
WiFiClient client;
// or... use WiFiFlientSecure for SSL
//WiFiClientSecure client;
// Store the MQTT server, username, and password in flash memory.
// This is required for using the Adafruit MQTT library.
const char MQTT_SERVER[] PROGMEM = AIO_SERVER;
const char MQTT_USERNAME[] PROGMEM = AIO_USERNAME;
const char MQTT_PASSWORD[] PROGMEM = AIO_KEY;
// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
Adafruit_MQTT_Client mqtt(&client, MQTT_SERVER, AIO_SERVERPORT, MQTT_USERNAME, MQTT_PASSWORD);
// Setup a feed called 'Welcome Feed' for publishing.
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
const char PHOTOCELL_FEED[] PROGMEM = AIO_USERNAME "/feeds/Welcome Feed";
Adafruit_MQTT_Publish Analog = Adafruit_MQTT_Publish(&mqtt, PHOTOCELL_FEED);
void MQTT_connect();
void setup() {
Serial.begin(115200);
delay(10);
// Connect to WiFi access point.
Serial.println(); Serial.println();
Serial.print("Connecting to ");
Serial.println(WLAN_SSID);
WiFi.begin(WLAN_SSID, WLAN_PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
// connect to adafruit io
MQTT_connect();
}
void loop() {
// ping adafruit io a few times to make sure we remain connected
if(! mqtt.ping(3)) {
// reconnect to adafruit io
if(! mqtt.connected())
MQTT_connect();
}
// Grab the current state of the analog input
float analog_data = analogRead(A0);
// Publish data
if (!Analog.publish(analog_data))
Serial.println(F("Failed to publish analog data"));
else
Serial.println(F("Analog data published!"));
// Repeat every 10 seconds
delay(10000);
}
// Function to connect and reconnect as necessary to the MQTT server.
// Should be called in the loop function and it will take care if connecting.
void MQTT_connect() {
int8_t ret;
// Stop if already connected.
if (mqtt.connected()) {
return;
}
Serial.print("Connecting to MQTT... ");
uint8_t retries = 3;
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
Serial.println(mqtt.connectErrorString(ret));
Serial.println("Retrying MQTT connection in 5 seconds...");
mqtt.disconnect();
delay(5000); // wait 5 seconds
retries--;
if (retries == 0) {
// basically die and wait for WDT to reset me
while (1);
}
}
Serial.println("MQTT Connected!");
}

・データの取得
 Adafruit IOからデータを取得するプログラムは、次のように書きます。
#include <ESP8266WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
// LED pin
const int led_pin = 5;
#define WLAN_SSID "...your SSID..."
#define WLAN_PASS "...your password..."
// Adafruit.io Setup
#define AIO_SERVER "io.adafruit.com"
#define AIO_SERVERPORT 1883 // use 8883 for SSL
#define AIO_USERNAME "...your AIO username (see https://accounts.adafruit.com)..."
#define AIO_KEY "...your AIO key..."
// Create an ESP8266 WiFiClient class to connect to the MQTT server.
WiFiClient client;
// or... use WiFiFlientSecure for SSL
//WiFiClientSecure client;
// Store the MQTT server, username, and password in flash memory.
// This is required for using the Adafruit MQTT library.
const char MQTT_SERVER[] PROGMEM = AIO_SERVER;
const char MQTT_USERNAME[] PROGMEM = AIO_USERNAME;
const char MQTT_PASSWORD[] PROGMEM = AIO_KEY;
// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
Adafruit_MQTT_Client mqtt(&client, MQTT_SERVER, AIO_SERVERPORT, MQTT_USERNAME, MQTT_PASSWORD);
// Setup a feed called 'LED' for subscribing to changes.
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
const char PHOTOCELL_FEED[] PROGMEM = AIO_USERNAME "/feeds/LED";
Adafruit_MQTT_Subscribe led = Adafruit_MQTT_Subscribe(&mqtt, PHOTOCELL_FEED);
void MQTT_connect();
void setup() {
// Set lamp pin to output
pinMode(led_pin, OUTPUT);
Serial.begin(115200);
delay(10);
// Connect to WiFi access point.
Serial.println(); Serial.println();
Serial.print("Connecting to ");
Serial.println(WLAN_SSID);
WiFi.begin(WLAN_SSID, WLAN_PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
// listen for events on the led feed
mqtt.subscribe(&led);
// connect to adafruit io
MQTT_connect();
}
void loop() {
Adafruit_MQTT_Subscribe *subscription;
// ping adafruit io a few times to make sure we remain connected
if(! mqtt.ping(3)) {
// reconnect to adafruit io
if(! mqtt.connected())
MQTT_connect();
}
// this is our 'wait for incoming subscription packets' busy subloop
while (subscription = mqtt.readSubscription(1000)) {
// we only care about the led events
if (subscription == &led) {
// convert mqtt ascii payload to int
char *value = (char *)led.lastread;
Serial.print(F("Received: "));
Serial.println(value);
// Apply message to led
String message = String(value);
message.trim();
if (message == "ON") {digitalWrite(led_pin, HIGH);}
if (message == "OFF") {digitalWrite(led_pin, LOW);}
}
}
}
// Function to connect and reconnect as necessary to the MQTT server.
// Should be called in the loop function and it will take care if connecting.
void MQTT_connect() {
int8_t ret;
// Stop if already connected.
if (mqtt.connected()) {
return;
}
Serial.print("Connecting to MQTT... ");
uint8_t retries = 3;
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
Serial.println(mqtt.connectErrorString(ret));
Serial.println("Retrying MQTT connection in 5 seconds...");
mqtt.disconnect();
delay(5000); // wait 5 seconds
retries--;
if (retries == 0) {
// basically die and wait for WDT to reset me
while (1);
}
}
Serial.println("MQTT Connected!");
}

 LEDを制御するためのフィード、ブロックの方法については、「Adafruit IO+Arduino+MQTTでIoT‐デバイスの制御」の「③Adafruit の準備」を参照してください。
 LEDは、GPIO5に接続します。
 


0 件のコメント:

コメントを投稿