IoTシステム実習
大見 嘉弘
IoTデバイス(ESP32)のプログラム
/*
* ESP32Test - Sample Program for Excercise of ESP32 IoT System
* Written by Yoshihiro OHMI <ohmi@rsch.tuis.ac.jp>
*/
#include <WiFi.h>
#include <HTTPClient.h>
const int dev_no = 1; // Device Number (XX of "OhmiXX")
const char SSID[] = "OHMI-IoT";
const char PASSWORD[] = "省略";
const char URI_OUTER[] = "http://省略:3000/";
const char URI_INNER[] = "http://192.168.20.20:3000/";
const char KEY_OUTER[][17] = {
// 省略
};
const char KEY_INNER[][17] = {
"",
"8GEZA7TRG9N1KFP3", // ohmi01
"9JX7HFK0Z94UF2BR", // ohmi02
// 省略
};
const int VOLUME1_PIN = 34;
const int BRIGHTNESS1_PIN = 35;
const int MODE_PIN = 13;
void setup() {
Serial.begin(115200);
while(!Serial);
pinMode(MODE_PIN, INPUT_PULLUP);
WiFi.begin(SSID, PASSWORD);
Serial.print("WiFi connecting");
while(WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(100);
}
Serial.println(" connected");
}
void loop() {
char uri[200];
char value_str[100];
HTTPClient http;
int mode = digitalRead(MODE_PIN);
strcpy(uri, mode == 1 ? URI_INNER : URI_OUTER);
strcat(uri, "update?key=");
strcat(uri, mode == 1 ? KEY_INNER[dev_no] : KEY_OUTER[dev_no]);
int v1 = analogRead(VOLUME1_PIN);
int v2 = analogRead(BRIGHTNESS1_PIN);
int v3 = touchRead(T6);
sprintf(value_str, "&field1=%d&field2=%d&field3=%d", v1, v2, v3);
strcat(uri, value_str);
Serial.printf("uri: %s\n", uri);
http.begin(uri);
int httpCode = http.GET();
Serial.printf("Response: %d", httpCode);
Serial.println();
if (httpCode == HTTP_CODE_OK) {
String body = http.getString();
Serial.print("Response Body: ");
Serial.println(body);
} else if (httpCode <= 0) {
Serial.println(http.errorToString(httpCode));
}
delay(1000);
}