-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathwemos.ino
220 lines (153 loc) · 4.4 KB
/
wemos.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <WiFiUdp.h>
#include <functional>
#include "switch.h"
#include "UpnpBroadcastResponder.h"
#include "CallbackFunction.h"
// prototypes
boolean connectWifi();
//on/off callbacks
bool deckingCurtainOn();
bool deckingCurtainOff();
// Change this before you flash
const char* ssid = "Your_Network_SSID";
const char* password = "Your-super-secret-password";
boolean wifiConnected = false;
UpnpBroadcastResponder upnpBroadcastResponder;
Switch *office = NULL;
//Switch *kitchen = NULL;
bool isdeckingCurtainOn = false;
//bool isKitchenLightstsOn = false;
// Motor -------------------------
#define SLEEP 16 //6 actually enable
#define STEP 13 //4
#define DIR 0 //2
#define STEPS_PER_ROTATION 300
boolean outstandingWork = false;
unsigned long TimerTravel; //"ALWAYS use unsigned long for timers"
//End of motor--------------------
void setup()
{
Serial.begin(9600);
// Initialise wifi connection
wifiConnected = connectWifi();
if(wifiConnected){
upnpBroadcastResponder.beginUdpMulticast();
// Define your switches here. Max 10
// Format: Alexa invocation name, local port no, on callback, off callback
office = new Switch("decking curtains", 80, deckingCurtainOn, deckingCurtainOff);
Serial.println("Adding switches upnp broadcast responder");
upnpBroadcastResponder.addDevice(*office);
// upnpBroadcastResponder.addDevice(*kitchen);
}
// Motor -------------------
pinMode(SLEEP,OUTPUT);
pinMode(STEP,OUTPUT);
pinMode(DIR,OUTPUT);
//end motor ---------------
}
void loop()
{
if(wifiConnected){
upnpBroadcastResponder.serverLoop();
office->serverLoop();
}
if (((millis()-TimerTravel) <= 7100UL) && (outstandingWork == true)) // Set the 'UL' figure to the tine it takes to open or close your curtain
{
Serial.println(F("Moving Curtain"));
moveCurtain();
}
else
{
outstandingWork = false;
//Serial.println(F("Finished doing my work!"));
//Serial.println(F("SLEEP HIGH"));
digitalWrite(SLEEP,HIGH);
}
}
bool deckingCurtainOn() {
Serial.println("Switch 1 turn on ...");
Serial.print("Curtain turned on.... Closing the curtain...");
closeCurtain();
isdeckingCurtainOn = true;
return isdeckingCurtainOn;
}
bool deckingCurtainOff() {
Serial.println("Switch 1 turn off ...");
Serial.print("Curtain turned off... Opening the curtain...");
openCurtain();
isdeckingCurtainOn = false;
return isdeckingCurtainOn;
}
// connect to wifi – returns true if successful or false if not
boolean connectWifi(){
boolean state = true;
int i = 0;
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.println("");
Serial.println("Connecting to WiFi");
// Wait for connection
Serial.print("Connecting ...");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
if (i > 10){
state = false;
break;
}
i++;
}
if (state){
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
}
else {
Serial.println("");
Serial.println("Connection failed.");
}
return state;
}
void openCurtain() {
Serial.print("Still Opening the curtain...");
Serial.println(F("dir HIGH"));
digitalWrite(DIR,HIGH);
TimerTravel= millis();
Serial.println(F("SLEEP LOW"));
digitalWrite(SLEEP,LOW);
delay(500);
outstandingWork = true;
//delay(1000); // waits for a second
}
void closeCurtain() {
Serial.print("Still closing the curtain...");
Serial.println(F("dir LOW"));
digitalWrite(DIR,LOW);
TimerTravel= millis();
Serial.println(F("SLEEP LOW"));
digitalWrite(SLEEP,LOW);
delay(500);
outstandingWork = true;
//delay(1000); // waits for a second
}
// for the motor -------------
void stepNow(int totalSteps) {
Serial.print(totalSteps);
Serial.println(F(" steps."));
int i;
for(i=0;i<totalSteps;++i) {
digitalWrite(STEP,HIGH);
delayMicroseconds(1100);
digitalWrite(STEP,LOW);
delayMicroseconds(1100);
}
}
//----------------------------
void moveCurtain() {
delayMicroseconds(500);
stepNow(STEPS_PER_ROTATION);
}