Created
August 12, 2020 05:49
-
-
Save boyphongsakorn/5239e34dedc18c5c4345b678e364156a to your computer and use it in GitHub Desktop.
ESP8266 And sinric.com To Open Computer By Google Assistant Action
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Version 0.1 - March 17 2018 | |
*/ | |
#include <Arduino.h> | |
#include <ESP8266WiFi.h> | |
#include <ESP8266WiFiMulti.h> | |
#include <WebSocketsClient.h> // https://github.com/kakopappa/sinric/wiki/How-to-add-dependency-libraries | |
#include <ArduinoJson.h> // https://github.com/kakopappa/sinric/wiki/How-to-add-dependency-libraries | |
#include <StreamString.h> | |
int Relay1 = D0; //D0 คือ Jump สำหรับส่งไฟให้เครื่อง Relayทำงาน | |
ESP8266WiFiMulti WiFiMulti; | |
WebSocketsClient webSocket; | |
WiFiClient client; | |
#define MyApiKey "ApiKey ในเว็บ sinric.com" // TODO: Change to your sinric API Key. Your API Key is displayed on sinric.com dashboard | |
#define MySSID "ชื่อไวไฟ" // TODO: Change to your Wifi network SSID | |
#define MyWifiPassword "รหัสไวไฟ" // TODO: Change to your Wifi network password | |
#define HEARTBEAT_INTERVAL 300000 // 5 Minutes | |
uint64_t heartbeatTimestamp = 0; | |
bool isConnected = false; | |
void turnOn(String deviceId) { //เวลาสั่งเปิดเครื่อง | |
if (deviceId == "ไอดีอุปกรณ์ในเว็บ sinric.com") // Device ID of first device | |
{ | |
Serial.print("Turn on device id: "); | |
Serial.println(deviceId); | |
digitalWrite(Relay1, LOW); // สั่งให้ Delay ติด | |
delay(1500); // ดีเลย์ 1500ms | |
digitalWrite(Relay1, HIGH); // สั่งให้ Delay ดับ | |
} | |
else { | |
Serial.print("Turn on for unknown device id: "); | |
Serial.println(deviceId); | |
} | |
} | |
void turnOff(String deviceId) { //เวลาสั่งปิดเครื่อง | |
if (deviceId == "ไอดีอุปกรณ์ในเว็บ sinric.com") // Device ID of first device | |
{ | |
Serial.print("Turn off Device ID: "); | |
Serial.println(deviceId); | |
digitalWrite(Relay1, LOW); // สั่งให้ Delay ติด | |
delay(1500); // ดีเลย์ 1500ms | |
digitalWrite(Relay1, HIGH); // สั่งให้ Delay ดับ | |
} | |
else { | |
Serial.print("Turn off for unknown device id: "); | |
Serial.println(deviceId); | |
} | |
} | |
void webSocketEvent(WStype_t type, uint8_t * payload, size_t length) { | |
switch(type) { | |
case WStype_DISCONNECTED: | |
isConnected = false; | |
Serial.printf("[WSc] Webservice disconnected from sinric.com!\n"); | |
break; | |
case WStype_CONNECTED: { | |
isConnected = true; | |
Serial.printf("[WSc] Service connected to sinric.com at url: %s\n", payload); | |
Serial.printf("Waiting for commands from sinric.com ...\n"); | |
} | |
break; | |
case WStype_TEXT: { | |
Serial.printf("[WSc] get text: %s\n", payload); | |
// Example payloads | |
// For Switch types | |
// {"deviceId":"xxx","action":"action.devices.commands.OnOff","value":{"on":true}} // https://developers.google.com/actions/smarthome/traits/onoff | |
// {"deviceId":"xxx","action":"action.devices.commands.OnOff","value":{"on":false}} | |
#if ARDUINOJSON_VERSION_MAJOR == 5 | |
DynamicJsonBuffer jsonBuffer; | |
JsonObject& json = jsonBuffer.parseObject((char*)payload); | |
#endif | |
#if ARDUINOJSON_VERSION_MAJOR == 6 | |
DynamicJsonDocument json(1024); | |
deserializeJson(json, (char*) payload); | |
#endif | |
String deviceId = json ["deviceId"]; | |
String action = json ["action"]; | |
if(action == "action.devices.commands.OnOff") { // Switch | |
String value = json ["value"]["on"]; | |
Serial.println(value); | |
if(value == "true") { | |
turnOn(deviceId); | |
} else { | |
turnOff(deviceId); | |
} | |
} | |
else if (action == "test") { | |
Serial.println("[WSc] received test command from sinric.com"); | |
} | |
} | |
break; | |
case WStype_BIN: | |
Serial.printf("[WSc] get binary length: %u\n", length); | |
break; | |
default: break; | |
} | |
} | |
void setup() { | |
Serial.begin(115200); | |
pinMode(Relay1, OUTPUT); // กำหนดขาทำหน้าที่ให้ขา D0 เป็น OUTPUT | |
digitalWrite(Relay1, HIGH); | |
WiFiMulti.addAP(MySSID, MyWifiPassword); | |
Serial.println(); | |
Serial.print("Connecting to Wifi: "); | |
Serial.println(MySSID); | |
// Waiting for Wifi connect | |
while(WiFiMulti.run() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
if(WiFiMulti.run() == WL_CONNECTED) { | |
Serial.println(""); | |
Serial.print("WiFi connected. "); | |
Serial.print("IP address: "); | |
Serial.println(WiFi.localIP()); | |
Serial.print("ESP Mac Address: "); | |
Serial.println(WiFi.macAddress()); | |
Serial.print("Subnet Mask: "); | |
Serial.println(WiFi.subnetMask()); | |
Serial.print("Gateway IP: "); | |
Serial.println(WiFi.gatewayIP()); | |
Serial.print("DNS: "); | |
Serial.println(WiFi.dnsIP()); | |
} | |
// server address, port and URL | |
webSocket.begin("iot.sinric.com", 80, "/"); //"iot.sinric.com", 80 | |
// event handler | |
webSocket.onEvent(webSocketEvent); | |
webSocket.setAuthorization("apikey", MyApiKey); | |
// try again every 5000ms if connection has failed | |
webSocket.setReconnectInterval(5000); // If you see 'class WebSocketsClient' has no member named 'setReconnectInterval' error update arduinoWebSockets | |
} | |
void loop() { | |
webSocket.loop(); | |
if(isConnected) { | |
uint64_t now = millis(); | |
// Send heartbeat in order to avoid disconnections during ISP resetting IPs over night. Thanks @MacSass | |
if((now - heartbeatTimestamp) > HEARTBEAT_INTERVAL) { | |
heartbeatTimestamp = now; | |
webSocket.sendTXT("H"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment