#include <M5Stack.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include "time.h"
const char* ssid = "自宅のSSID";
const char* password = "WIFIのパスワード";
String makerKey = "IFTTTから持ってきたキー"; // Maker Webhooks
const char* server = "maker.ifttt.com"; // Server URL
WiFiClient client;
int Pin_a = 2;//1
int Pin_b = 5;//2
int Pin_c = 18;//3
int Pin_d = 19;//4
int Pin_e = 21;//5
int Pin_f = 35;//6
int Pin_g = 36;//7
int Pin_h = 25;//8
int Pin_i = 26;//9
int scan_step = 100;
//get time
const char* ntpServer = "ntp.nict.jp";
const long gmtOffset_sec = 3600 * 9;
const int daylightOffset_sec = 0;
bool checkWifiConnected() {
// attempt to connect to Wifi network:
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
// wait 1 second for re-trying
delay(1000);
}
Serial.print("Connected to ");
Serial.println(ssid);
return true;
}
void send(String value1, String makerEvent){//, String value3) {
while (!checkWifiConnected()) {
Serial.print("Attempting to connect to WiFi");
WiFi.begin(ssid, password);
}
Serial.println("\nStarting connection to server...");
if (!client.connect(server, 80)) {
Serial.println("Connection failed!");
} else {
Serial.println("Connected to server!");
// Make a HTTP request:
String url = "/trigger/" + makerEvent + "/with/key/" + makerKey;
url += "?value1=" + value1;// + "&value2=" + value2 + "&value3=" + value3;
client.println("GET " + url + " HTTP/1.1");
client.print("Host: ");
client.println(server);
client.println("Connection: close");
client.println();
Serial.print("Waiting for response "); //WiFiClientSecure uses a non blocking implementation
int count = 0;
while (!client.available()) {
delay(50); //
Serial.print(".");
}
// if there are incoming bytes available
// from the server, read them and print them:
while (client.available()) {
char c = client.read();
Serial.write(c);
}
// if the server's disconnected, stop the client:
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting from server.");
client.stop();
}
}
}
void lcd_message(String switch_number){
//initialize and get time
M5.Lcd.clear(BLACK);
M5.Lcd.setTextColor(YELLOW);
M5.Lcd.setTextSize(2);
M5.Lcd.setCursor(35, 10);
M5.Lcd.println("sRemo Hardware Switch");
M5.Lcd.setTextColor(GREEN);
M5.Lcd.setTextSize(3);
M5.Lcd.setCursor(80, 120);
M5.Lcd.println(switch_number);
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
struct tm timeinfo;
String day;
getLocalTime(&timeinfo);
char *wday[7] = {"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"};
M5.Lcd.setTextColor(YELLOW);
M5.Lcd.setTextSize(2);
M5.Lcd.setCursor(50, 80);
M5.Lcd.printf("%02d/%02d(%s) %02d:%02d:%02d"
//,timeinfo.tm_year + 1900
,timeinfo.tm_mon
,timeinfo.tm_mday
,wday[timeinfo.tm_wday]
,timeinfo.tm_hour
,timeinfo.tm_min
,timeinfo.tm_sec
);
}
void setup() {
//Initialize serial and wait for port to open:
M5.begin(true, false, true);
M5.Power.begin();
dacWrite(25,0);//speaker off
Serial.begin(115200);
delay(100);
M5.begin(true, false, true);
M5.Power.begin();
M5.Lcd.clear(BLACK);
M5.Lcd.setTextColor(YELLOW);
M5.Lcd.setTextSize(2);
M5.Lcd.setCursor(35, 10);
M5.Lcd.println("sRemo Hardware Switch");
WiFi.begin(ssid, password);
while (!checkWifiConnected()) {
WiFi.begin(ssid, password);
}
pinMode(Pin_a, INPUT);
pinMode(Pin_b, INPUT);
pinMode(Pin_c, INPUT);
pinMode(Pin_d, INPUT);
pinMode(Pin_e, INPUT);
pinMode(Pin_f, INPUT);
pinMode(Pin_g, INPUT);
pinMode(Pin_h, INPUT);
pinMode(Pin_i, INPUT);
delay(scan_step);
}
void loop() {
// put your main code here, to run repeatedly:
int state_1 = digitalRead(Pin_a);//1
int state_2 = digitalRead(Pin_b);//2
int state_3 = digitalRead(Pin_c);//3
int state_4 = digitalRead(Pin_d);//4
int state_5 = digitalRead(Pin_e);//5
int state_6 = digitalRead(Pin_f);//6
int state_7 = digitalRead(Pin_g);//7
int state_8 = digitalRead(Pin_h);//8
int state_9 = digitalRead(Pin_i);//9
//Serial.println(state_8);
if (state_1 != 0){
Serial.println("1");
send("a", "M5pushedA");
lcd_message("Send No. 1");
}else if(state_2 != 0){
Serial.println("2");
send("a", "M5pushedB");
lcd_message("Send No. 2");
}else if(state_3 != 0){
Serial.println("5");
send("a", "M5pushedE");
lcd_message("Send No. 5");
}else if(state_4 != 0){
Serial.println("3");
send("a", "M5pushedC");
lcd_message("Send No. 3");
}else if(state_5 != 0){
Serial.println("6");
send("a", "M5pushedF");
lcd_message("Send No. 6");
}else if(state_6 != 0){
Serial.println("9");
send("a", "M5pushedI");
lcd_message("Send No. 9");
}else if(state_7 != 0){
Serial.println("8");
send("a", "M5pushedH");
lcd_message("Send No. 8");
}else if(state_8 != 0){
Serial.println("4");
send("a", "M5pushedD");
lcd_message("Send No. 4");
}else if(state_9 != 0){
Serial.println("7");
send("a", "M5pushedG");
lcd_message("Send No. 7");
}else{
Serial.println("0");
}
delay(scan_step);
}