· 5 years ago · Sep 29, 2020, 07:06 PM
1/*
2 Version 0.1 - Feb 10 2018
3*/
4
5#include <Arduino.h>
6#include <ESP8266WiFi.h>
7#include <ESP8266WiFiMulti.h>
8#include <WebSocketsClient.h> // https://github.com/kakopappa/sinric/wiki/How-to-add-dependency-libraries
9#include <ArduinoJson.h> // https://github.com/kakopappa/sinric/wiki/How-to-add-dependency-libraries
10
11ESP8266WiFiMulti WiFiMulti;
12WebSocketsClient webSocket;
13const int Luce-12;
14WiFiClient client;
15
16#define MyApiKey "Apikey " // TODO: Change to your sinric API Key. Your API Key is displayed on sinric.com dashboard
17#define MySSID "nome wifi " // TODO: Change to your Wifi network SSID
18#define MyWifiPassword "mia password wifi" // TODO: Change to your Wifi network password
19
20#define API_ENDPOINT "http://sinric.com"
21#define HEARTBEAT_INTERVAL 300000 // 5 Minutes
22
23uint64_t heartbeatTimestamp = 0;
24bool isConnected = false;
25
26void turnOn(String deviceId) {
27 if (deviceId == "Device ID") // Device ID of first device
28 {
29 Serial.print("Turn on device id: ");
30 Serial.println(deviceId);
31 digitalWrite(Luce,HIGH);
32 }
33
34 else {
35 Serial.print("Turn on for unknown device id: ");
36 Serial.println(deviceId);
37 }
38}
39
40void turnOff(String deviceId) {
41 if (deviceId == "Device ID") // Device ID of first device
42 {
43 Serial.print("Turn off Device ID: ");
44 Serial.println(deviceId);
45 digitalWrite(Luce,LOW);
46 }
47
48 else {
49 Serial.print("Turn off for unknown device id: ");
50 Serial.println(deviceId);
51 }
52}
53
54void webSocketEvent(WStype_t type, uint8_t * payload, size_t length) {
55 switch(type) {
56 case WStype_DISCONNECTED:
57 isConnected = false;
58 Serial.printf("[WSc] Webservice disconnected from sinric.com!\n");
59 break;
60 case WStype_CONNECTED: {
61 isConnected = true;
62 Serial.printf("[WSc] Service connected to sinric.com at url: %s\n", payload);
63 Serial.printf("Waiting for commands from sinric.com ...\n");
64 }
65 break;
66 case WStype_TEXT: {
67 Serial.printf("[WSc] get text: %s\n", payload);
68 // Example payloads
69
70 // For Light device type
71 // {"deviceId": xxxx, "action": "setPowerState", value: "ON"} // https://developer.amazon.com/docs/device-apis/alexa-powercontroller.html
72 // {"deviceId": xxxx, "action": "AdjustBrightness", value: 3} // https://developer.amazon.com/docs/device-apis/alexa-brightnesscontroller.html
73 // {"deviceId": xxxx, "action": "setBrightness", value: 42} // https://developer.amazon.com/docs/device-apis/alexa-brightnesscontroller.html
74 // {"deviceId": xxxx, "action": "SetColor", value: {"hue": 350.5, "saturation": 0.7138, "brightness": 0.6501}} // https://developer.amazon.com/docs/device-apis/alexa-colorcontroller.html
75 // {"deviceId": xxxx, "action": "DecreaseColorTemperature"} // https://developer.amazon.com/docs/device-apis/alexa-colortemperaturecontroller.html
76 // {"deviceId": xxxx, "action": "IncreaseColorTemperature"} // https://developer.amazon.com/docs/device-apis/alexa-colortemperaturecontroller.html
77 // {"deviceId": xxxx, "action": "SetColorTemperature", value: 2200} // https://developer.amazon.com/docs/device-apis/alexa-colortemperaturecontroller.html
78
79 DynamicJsonBuffer jsonBuffer;
80 JsonObject& json = jsonBuffer.parseObject((char*)payload);
81 String deviceId = json ["deviceId"];
82 String action = json ["action"];
83
84 if(action == "setPowerState") { // Switch or Light
85 String value = json ["value"];
86 if(value == "ON") {
87 turnOn(deviceId);
88 } else {
89 turnOff(deviceId);
90 }
91 }
92 else if(action == "SetColor") {
93 // Alexa, set the device name to red
94 // get text: {"deviceId":"xxxx","action":"SetColor","value":{"hue":0,"saturation":1,"brightness":1}}
95 String hue = json ["value"]["hue"];
96 String saturation = json ["value"]["saturation"];
97 String brightness = json ["value"]["brightness"];
98
99 Serial.println("[WSc] hue: " + hue);
100 Serial.println("[WSc] saturation: " + saturation);
101 Serial.println("[WSc] brightness: " + brightness);
102 }
103 else if(action == "SetBrightness") {
104
105 }
106 else if(action == "AdjustBrightness") {
107
108 }
109 else if (action == "test") {
110 Serial.println("[WSc] received test command from sinric.com");
111 }
112 }
113 break;
114 case WStype_BIN:
115 Serial.printf("[WSc] get binary length: %u\n", length);
116 break;
117 }
118}
119
120void setup() {
121 Serial.begin(115200);
122 pinMode(Luce,OUTPUT);
123
124 WiFiMulti.addAP(MySSID, MyWifiPassword);
125 Serial.println();
126 Serial.print("Connecting to Wifi: ");
127 Serial.println(MySSID);
128
129 // Waiting for Wifi connect
130 while(WiFiMulti.run() != WL_CONNECTED) {
131 delay(500);
132 Serial.print(".");
133 }
134 if(WiFiMulti.run() == WL_CONNECTED) {
135 Serial.println("");
136 Serial.print("WiFi connected. ");
137 Serial.print("IP address: ");
138 Serial.println(WiFi.localIP());
139 }
140
141 // server address, port and URL
142 webSocket.begin("iot.sinric.com", 80, "/");
143
144 // event handler
145 webSocket.onEvent(webSocketEvent);
146 webSocket.setAuthorization("apikey", MyApiKey);
147
148 // try again every 5000ms if connection has failed
149 webSocket.setReconnectInterval(5000); // If you see 'class WebSocketsClient' has no member named 'setReconnectInterval' error update arduinoWebSockets
150}
151
152void loop() {
153 webSocket.loop();
154
155 if(isConnected) {
156 uint64_t now = millis();
157
158 // Send heartbeat in order to avoid disconnections during ISP resetting IPs over night. Thanks @MacSass
159 if((now - heartbeatTimestamp) > HEARTBEAT_INTERVAL) {
160 heartbeatTimestamp = now;
161 webSocket.sendTXT("H");
162 }
163 }
164}
165