· 6 years ago · Feb 16, 2020, 03:44 AM
1/*
2 Version 0.4 - April 26 2019
3 Stolen and edited by Raccoonfacts
4 Alexa Toilet flusher
5 Plz note all curse words and
6 bad or wonky code is due to
7 RaccoonFacts being an immature,
8 lazy, moron! thanks sub k cool
9 www.youtube.com/raccoonfacts
10
11*/
12
13#include <Arduino.h>
14#include <ESP8266WiFi.h>
15#include <ESP8266WiFiMulti.h>
16#include <WebSocketsClient.h> // https://github.com/kakopappa/sinric/wiki/How-to-add-dependency-libraries
17#include <ArduinoJson.h> // https://github.com/kakopappa/sinric/wiki/How-to-add-dependency-libraries (use the correct version)
18#include <StreamString.h>
19#include <Servo.h>
20ESP8266WiFiMulti WiFiMulti;
21WebSocketsClient webSocket;
22WiFiClient client;
23Servo toiletServo;
24#define MyApiKey "d8430da7-5616-4c33-9a06-4bbc2268f9ce" // TODO: Change to your sinric API Key. Your API Key is displayed on sinric.com dashboard
25#define MySSID "Kings LANding" // TODO: Change to your Wifi network SSID
26#define MyWifiPassword "Hitler88" // TODO: Change to your Wifi network password
27
28#define HEARTBEAT_INTERVAL 300000 // 5 Minutes
29int pos = 0;
30uint64_t heartbeatTimestamp = 0;
31bool isConnected = false;
32int servoPin = D13; //for what ever reason not all pins on my board worked with a servo, but this one did
33
34
35// deviceId is the ID assgined to your smart-home-device in sinric.com dashboard. Copy it from dashboard and paste it here
36
37void turnOn(String deviceId) {
38
39
40pinMode(D8, OUTPUT);
41toiletServo.attach(servoPin);
42
43
44
45 if (deviceId == "5e364ae4168ca72bf7600d9f") // Device ID of first device
46 {
47 Serial.print("Turn on device id: ");
48 digitalWrite(D8, HIGH); //This code is just to test the functionality of sinric
49 delay(500);
50 digitalWrite(D8, LOW);
51 delay(500);
52 digitalWrite(D8, HIGH);
53 Serial.println(deviceId);
54 Serial.println("The fucking Light is on Moron!");
55
56 for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees
57 // in steps of 1 degree
58 toiletServo.write(pos); // tell servo to go to position in variable 'pos'
59 delay(15);
60 }
61 delay(1000);
62 for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees
63 toiletServo.write(pos); // tell servo to go to position in variable 'pos'
64 delay(15);
65 }
66 delay(1000);
67 }
68 else if (deviceId == "5axxxxxxxxxxxxxxxxxxx") // Device ID of second device
69 //left this in just in case I wanted more items to my toilet
70 {
71 Serial.print("Turn on device id: ");
72 Serial.println(deviceId);
73 }
74 else {
75 Serial.print("Turn on for unknown device id: ");
76 Serial.println(deviceId);
77 }
78}
79
80void turnOff(String deviceId) {
81 if (deviceId == "5e364ae4168ca72bf7600d9f") // Device ID of first device
82 {
83
84 Serial.print("Turn off Device ID: ");
85 digitalWrite(D8, LOW);
86 Serial.println(deviceId);
87 Serial.println("device is off moron");
88 toiletServo.write(0);
89 /*like I said I failed all coding classes i've ever taken..well I got a D in java
90 * so plz fix this code. Right now it works, but I struggled telling Alexa turn toilet off
91 * k thx
92 */
93 }
94 else if (deviceId == "5axxxxxxxxxxxxxxxxxxx") // Device ID of second device
95 {
96 Serial.print("Turn off Device ID: ");
97 Serial.println(deviceId);
98 }
99 else {
100 Serial.print("Turn off for unknown device id: ");
101 Serial.println(deviceId);
102 }
103}
104
105void webSocketEvent(WStype_t type, uint8_t * payload, size_t length) {
106 switch(type) {
107 case WStype_DISCONNECTED:
108 isConnected = false;
109 Serial.printf("[WSc] Webservice disconnected from sinric.com!\n");
110 break;
111 case WStype_CONNECTED: {
112 isConnected = true;
113 Serial.printf("[WSc] Service connected to sinric.com at url: %s\n", payload);
114 Serial.printf("Waiting for commands from sinric.com ...\n");
115 }
116 break;
117 case WStype_TEXT: {
118 Serial.printf("[WSc] get text: %s\n", payload);
119 // Example payloads
120
121 // For Switch or Light device types
122 // {"deviceId": xxxx, "action": "setPowerState", value: "ON"} // https://developer.amazon.com/docs/device-apis/alexa-powercontroller.html
123
124 // For Light device type
125 // Look at the light example in github
126 // Go to bottom of this code for link to github
127#if ARDUINOJSON_VERSION_MAJOR == 5
128 DynamicJsonBuffer jsonBuffer;
129 JsonObject& json = jsonBuffer.parseObject((char*)payload);
130#endif
131#if ARDUINOJSON_VERSION_MAJOR == 6
132 DynamicJsonDocument json(1024);
133 deserializeJson(json, (char*) payload);
134#endif
135 String deviceId = json ["deviceId"];
136 String action = json ["action"];
137
138 if(action == "setPowerState") { // Switch or Light
139 String value = json ["value"];
140 if(value == "ON") {
141 turnOn(deviceId);
142 } else {
143 turnOff(deviceId);
144 }
145 }
146 else if (action == "SetTargetTemperature") {
147 String deviceId = json ["deviceId"];
148 String action = json ["action"];
149 String value = json ["value"];
150 }
151 else if (action == "test") {
152 Serial.println("[WSc] received test command from sinric.com");
153 }
154 }
155 break;
156 case WStype_BIN:
157 Serial.printf("[WSc] get binary length: %u\n", length);
158 break;
159 }
160}
161
162void setup() {
163 Serial.begin(115200);
164
165 WiFiMulti.addAP(MySSID, MyWifiPassword);
166 Serial.println();
167 Serial.print("Connecting to Wifi: ");
168 Serial.println(MySSID);
169
170 // Waiting for Wifi connect
171 while(WiFiMulti.run() != WL_CONNECTED) {
172 delay(500);
173 Serial.print(".");
174 }
175 if(WiFiMulti.run() == WL_CONNECTED) {
176 Serial.println("");
177 Serial.print("WiFi connected. ");
178 Serial.print("IP address: ");
179 Serial.println(WiFi.localIP());
180 }
181
182 // server address, port and URL
183 webSocket.begin("iot.sinric.com", 80, "/");
184
185 // event handler
186 webSocket.onEvent(webSocketEvent);
187 webSocket.setAuthorization("apikey", MyApiKey);
188
189 // try again every 5000ms if connection has failed
190 webSocket.setReconnectInterval(5000); // If you see 'class WebSocketsClient' has no member named 'setReconnectInterval' error update arduinoWebSockets
191}
192
193void loop() {
194 webSocket.loop();
195
196 if(isConnected) {
197 uint64_t now = millis();
198
199 // Send heartbeat in order to avoid disconnections during ISP resetting IPs over night. Thanks @MacSass
200 if((now - heartbeatTimestamp) > HEARTBEAT_INTERVAL) {
201 heartbeatTimestamp = now;
202 webSocket.sendTXT("H");
203 }
204 }
205}
206// Yo hello from the bottom of the page
207// Link to all examples for sinric https://github.com/kakopappa/sinric/tree/master/arduino_examples
208// If you want a push button: https://github.com/kakopappa/sinric/blob/master/arduino_examples/switch_with_push_button.ino void setup() {
209 // put your setup code here, to run once: