#ifdef ESP32
#include <WiFiClientSecure.h>
#include <HTTPClient.h>
#else
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#endif
#include <ArduinoJson.h>
#include <vector>
#include <tuple>
#include "DHT.h"
#include <base64.h>
#define DHTPIN 22 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT11 // DHT 11
DHT dht(DHTPIN, DHTTYPE);
/***************************************/
/*** ここから 環境によって書き換える ***/
/***************************************/
//WiFi設定
const char* ssid = "your-ssid";
const char* password = "your-password";
//FIWARE 利用者キー
String customerKey = "K1njk9Rstf8F_KrbXf88rhGtrLsa";
//FIWARE 利用者秘密鍵
String customerSecret = "Xy_uQGLH0WvSOYVyS4tf4jfQWk0a";
//FIWARE サービス
String FiwareService = "18iot_isco_q25x3wik";
//FIWARE サービスパス
String FiwareServicePath = "/seminar_test001";
//コンテキストタイプ
String ContextType = "Room";
//コンテキストID
String ContextId = "Room-1";
/***************************************/
/*** ここまで 環境によって書き換える ***/
/***************************************/
String oauth(String);
bool updateContext(String , std::vector<std::tuple<String, String, String>>);
String contextEntities(String);
void setup() {
Serial.begin(9600);
delay(2000);
//Wifi 接続
WiFi.begin(ssid, password);
Serial.print("WiFi connecting");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(100);
}
Serial.println(" connected");
dht.begin();
}
void loop() {
//FIWARE API にアクセスするためのアクセストークンを取得
String base64Key = base64::encode(customerKey + ":" + customerSecret);
String accessToken = oauth(base64Key);
Serial.println(accessToken);
//温度と湿度を取得し、FIWARE の updateContext API を利用してデータ登録
float h = dht.readHumidity();
float t = dht.readTemperature();
String temperaturevalue = String(t);
String humidityValue = String(h);
std::vector<std::tuple<String, String, String>> updateContextData;
std::tuple<String, String, String> temperature = std::make_tuple("temperature", "float", temperaturevalue);
std::tuple<String, String, String> humidity = std::make_tuple("humidity", "integer", humidityValue);
updateContextData.push_back(temperature);
updateContextData.push_back(humidity);
if(updateContext(accessToken, updateContextData)){
Serial.println("updateContext Success.");
//登録したデータを FIWARE の contextEntities API を利用して取得
String result = contextEntities(accessToken);
Serial.println(result);
} else {
Serial.println("updateContext Failed.");
}
//60秒間待つ
delay(1000 * 60);
}
// FIWARE 認証 API にアクセスしてアクセストークンを返す関数
String oauth(String base64Key) {
HTTPClient http;
http.begin("https://pf01.okinawa-iotpf.jp/wso2am/oauth2/token");
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
http.addHeader("Authorization", "Basic " + base64Key);
int httpCode = http.POST("grant_type=client_credentials");
DynamicJsonBuffer jsonBuffer;
const char* accessToken = "";
if (httpCode == HTTP_CODE_OK) {
JsonObject& root = jsonBuffer.parseObject(http.getString());
accessToken = root["access_token"];
}
http.end();
return String(accessToken);
}
// FIWARE にデータを登録する関数
bool updateContext(String accessToken, std::vector<std::tuple<String, String, String>> data){
bool result = false;
DynamicJsonBuffer jsonBuffer;
JsonObject& json = jsonBuffer.createObject();
json["updateAction"] = "APPEND";
JsonArray& contextElements = json.createNestedArray("contextElements");
JsonObject& contextElement = jsonBuffer.createObject();
contextElement["type"] = ContextType;
contextElement["isPattern"] = "false";
contextElement["id"] = ContextId;
JsonArray& attributes = contextElement.createNestedArray("attributes");
for(std::tuple<String, String, String> item : data){
JsonObject& attribute = jsonBuffer.createObject();
attribute["name"] = std::get<0>(item);
attribute["type"] = std::get<1>(item);
attribute["value"] = std::get<2>(item);
attributes.add(attribute);
}
contextElements.add(contextElement);
HTTPClient http;
http.begin("https://pf01.okinawa-iotpf.jp/orion/v1.0/updateContext");
http.addHeader("Content-Type", "application/json");
http.addHeader("Accept", "application/json");
http.addHeader("Authorization", "Bearer " + String(accessToken));
http.addHeader("Fiware-Service", FiwareService);
http.addHeader("Fiware-ServicePath", FiwareServicePath);
String content;
json.printTo(content);
int httpCode = http.POST(content);
Serial.println(httpCode);
if (httpCode == HTTP_CODE_OK) {
result = true;
}
http.end();
return result;
}
// FIWARE からデータを取得する関数
String contextEntities(String accessToken){
String result;
HTTPClient http;
http.begin("https://pf01.okinawa-iotpf.jp/orion/v1.0/contextEntities");
http.addHeader("Accept", "application/json");
http.addHeader("Authorization", "Bearer " + String(accessToken));
http.addHeader("Fiware-Service", FiwareService);
http.addHeader("Fiware-ServicePath", FiwareServicePath);
int httpCode = http.GET();
Serial.println(httpCode);
if (httpCode == HTTP_CODE_OK) {
result = http.getString();
}
http.end();
return result;
}
コピーしたコードのSSID(your-ssid), PASSWORD(your-password) を設定します。
//WiFi設定
const char* ssid = "your-ssid";
const char* password = "your-password";
他の受講者と被らないようにしてください。
//コンテキストID
String ContextId = "Room-1";