Skip to content

Instantly share code, notes, and snippets.

@n0bisuke
Created Jul 7, 2018
Embed
What would you like to do?
#include <Keypad.h>
#include <NefryDisplay.h>
#include <WiFiClientSecure.h>
WiFiClientSecure client;
String StrPerEncord(const char* c_str);
String escapeParameter(String param);
void hipchatPost(String message);
void iftttClovaPost(String message);
const int HTTP_PORT = 443;
const byte n_rows = 4;
const byte n_cols = 4;
char keys[n_rows][n_cols] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte colPins[n_rows] = {D3, D2, D1, D0};
byte rowPins[n_cols] = {D7, D6, D5, D4};
Keypad myKeypad = Keypad( makeKeymap(keys), rowPins, colPins, n_rows, n_cols);
String storeMessage = "";
//3行目だけにPrint
void nfDisplay3Print(String mes){
NefryDisplay.print("");
NefryDisplay.print("");
NefryDisplay.print(mes);
}
void setup() {
Serial.begin(115200);
NefryDisplay.setTitle("KINTAI");
nfDisplay3Print("Please Input...");
}
void loop() {
char myKey = myKeypad.getKey();
String member = "";
if (myKey != NULL){
Serial.print("Key pressed: ");
Serial.println(myKey);
if(String(myKey) == "1"){
member += "n0bisuke";
storeMessage = member;
}else if(String(myKey) == "2"){
member += "chantoku";
storeMessage = member;
}else if(String(myKey) == "3"){
member += "kiki";
storeMessage = member;
}else if(String(myKey) == "4"){
member += "takudon";
storeMessage = member;
}else if(String(myKey) == "5"){
member += "sasakitomohiro";
storeMessage = member;
}else if(String(myKey) == "6"){
member += "uko";
storeMessage = member;
}
nfDisplay3Print(String(myKey) + ":" + member);
if(String(myKey) == "A"){
nfDisplay3Print(String(myKey) + ": [Office IN] post to HipChat...");
storeMessage += "さんが出勤しました。";
hipchatPost(storeMessage);
iftttClovaPost(storeMessage);
storeMessage = ""; //リセット
nfDisplay3Print(String(myKey) + ": POST DONE");
}else if(String(myKey) == "B"){
nfDisplay3Print(String(myKey) + ": [Office OUT] post to HipChat...");
storeMessage += "さんが退勤しました。";
hipchatPost(storeMessage);
iftttClovaPost(storeMessage);
storeMessage = ""; //リセット
nfDisplay3Print(String(myKey) + ": POST DONE");
}
}
}
void iftttClovaPost(String message){
const char* HOST = "maker.ifttt.com";
String url = "/trigger/clova_things/with/key/xxxxxxxxx";
url += "?value1=";
url += StrPerEncord(escapeParameter(message).c_str());
Serial.print("connecting to ");
Serial.println(HOST);
if (!client.connect(HOST, HTTP_PORT)) {
Serial.println("connection failed");
return;
}
client.println("POST " + url + " HTTP/1.1");
client.println("Content-Type: application/json");
client.println("Content-Type: application/x-www-form-urlencoded");
client.println("Host: " + String(HOST));
client.println("Connection: close");
client.println();
unsigned long timeout = millis();
while (client.available() == 0) {
if (millis() - timeout > 5000) {
Serial.println(">>> Client Timeout !");
client.stop();
return;
}
}
// Read all the lines of the reply from server and print them to Serial
while(client.available()) {
String line = client.readStringUntil('\r');
Serial.print(line);
}
Serial.println();
Serial.println("closing connection");
}
void hipchatPost(String message){
const char* HOST = "xxxx.hipchat.com";
String url = "/v2/room/4651875/notification?auth_token=xxxxxxxxx";
url += "&color=green&notify=false&message_format=text&message=";
url += StrPerEncord(escapeParameter(message + " (yey)").c_str());
Serial.print("connecting to ");
Serial.println(HOST);
if (!client.connect(HOST, HTTP_PORT)) {
Serial.println("connection failed");
return;
}
client.println("POST " + url + " HTTP/1.1");
client.println("Content-Type: application/json");
client.println("Content-Type: application/x-www-form-urlencoded");
client.println("Host: " + String(HOST));
client.println("Connection: close");
client.println();
unsigned long timeout = millis();
while (client.available() == 0) {
if (millis() - timeout > 5000) {
Serial.println(">>> Client Timeout !");
client.stop();
return;
}
}
// Read all the lines of the reply from server and print them to Serial
while(client.available()) {
String line = client.readStringUntil('\r');
Serial.print(line);
}
Serial.println();
Serial.println("closing connection");
}
String StrPerEncord(const char* c_str) {
uint16_t i = 0;
String str_ret = "";
char c1[3], c2[3], c3[3];
while (c_str[i] != '\0') {
if (c_str[i] >= 0xC2 && c_str[i] <= 0xD1) { //2バイト文字
sprintf(c1, "%2x", c_str[i]);
sprintf(c2, "%2x", c_str[i + 1]);
str_ret += "%" + String(c1) + "%" + String(c2);
i = i + 2;
} else if (c_str[i] >= 0xE2 && c_str[i] <= 0xEF) {
sprintf(c1, "%2x", c_str[i]);
sprintf(c2, "%2x", c_str[i + 1]);
sprintf(c3, "%2x", c_str[i + 2]);
str_ret += "%" + String(c1) + "%" + String(c2) + "%" + String(c3);
i = i + 3;
} else {
str_ret += String(c_str[i]);
i++;
}
}
return str_ret;
}
String escapeParameter(String param) {
param.replace("%", "%25");
param.replace("+", "%2B");
param.replace(" ", "+");
param.replace("\"", "%22");
param.replace("#", "%23");
param.replace("$", "%24");
param.replace("&", "%26");
param.replace("'", "%27");
param.replace("(", "%28");
param.replace(")", "%29");
param.replace("*", "%2A");
param.replace(",", "%2C");
param.replace("/", "%2F");
param.replace(":", "%3A");
param.replace(";", "%3B");
param.replace("<", "%3C");
param.replace("=", "%3D");
param.replace(">", "%3E");
param.replace("?", "%3F");
param.replace("@", "%40");
param.replace("[", "%5B");
param.replace("\\", "%5C");
param.replace("]", "%5D");
param.replace("^", "%5E");
param.replace("'", "%60");
param.replace("{", "%7B");
param.replace("|", "%7C");
param.replace("}", "%7D");
return param;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment