-
Notifications
You must be signed in to change notification settings - Fork 0
/
CofFi.ino
214 lines (173 loc) · 4.87 KB
/
CofFi.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
/*************************************************************
CofFi by pidgey.be
*************************************************************/
#include <ESP8266WiFi.h>
#include <Servo.h>
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "";
char pass[] = "";
// Remote site information
const char http_site[] = "www.xxx.xx";
const int http_port = 80;
WiFiClient client;
int coffeeStandby = 90;
int coffeeOn = 20;
int coffeeGo = 161;
int makeTime = 35; //seconds
int heatupTime = 60;
int servoPin = 4;
int servoTransPin = 5;
int debug = 1;
int manual = 0;
int incomingByte = 0;
int total = 0;
int pollingTime = 60; //seconds
void setup()
{
pinMode(2,INPUT_PULLUP); //trying to make sure these pins are high or low on deepsleep restart to avoid errors
pinMode(0,INPUT_PULLUP);
pinMode(servoTransPin, OUTPUT);
digitalWrite(servoTransPin, LOW);
pinMode(15,INPUT);
digitalWrite(15, LOW);
if(debug) {Serial.begin(9600); Serial.println("hello");}
//initServo();
connectWifi();
if(manual && debug) manualControl();
else checkForCoffeeRequest();
goSleep();
}
void loop()
{
}
void goSleep()
{
if(getPage(String("CofFi_get.php?deepsleep=w")))
{
String response = readResponse();
pollingTime = response.toInt();
if(pollingTime==0) pollingTime = 60;
}
if(debug) Serial.println(String("going to sleep for ")+pollingTime+"s...");
ESP.deepSleep(1000L*1000*pollingTime, WAKE_RF_DEFAULT); //us
}
void manualControl()
{
while(true)
{
if (Serial.available() > 0) {
// read the incoming byte:
total = 0;
int i=1;
while(Serial.available()>0)
{
incomingByte = Serial.parseInt();
}
Serial.print("I received: ");
Serial.println(incomingByte, DEC);
myservo.attach(servoPin);
digitalWrite(servoTransPin, HIGH);
setServo(incomingByte);
setServo(coffeeStandby);
digitalWrite(servoTransPin, LOW);
myservo.detach();
}
}
}
void initServo()
{
myservo.attach(servoPin); // attaches the servo on GIO2 to the servo object
setServo(coffeeStandby);
myservo.detach();
}
void connectWifi()
{
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
if(debug) Serial.print(".");
}
}
void setServo(int degr)
{
if(debug) {Serial.print("going to "); Serial.print(degr); }
int current = myservo.read();
int stepp = 1;
if((degr-current) <0) stepp = -1; //turn clockwise or anticlockwise
for(int pos = current; pos != degr; pos = pos+stepp)
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
void checkForCoffeeRequest()
{
if(getPage("CofFi_get.php"))
{
String response = readResponse();
if(debug) Serial.println("response is "+response);
if(response.indexOf("CofFi")!=-1)
{
int id = response.substring(response.indexOf(" ")+1).toInt(); //get coffee id out of response
makeCoffee(id);
}
}
}
void makeCoffee(int id)
{
getPage(String("CofFi_set.php?id=")+id+"&start=1");
myservo.attach(servoPin);
digitalWrite(servoTransPin, HIGH);
//turn device on
setServo(coffeeOn);
setServo(coffeeStandby);
getPage(String("CofFi_set.php?id=")+id+"&heatup=1");
delay(1000*heatupTime); //warm up time
//start coffee
setServo(coffeeGo);
setServo(coffeeStandby);
getPage(String("CofFi_set.php?id=")+id+"&making=1");
delay(1000*makeTime); //coffeemaking time
//stop coffee
setServo(coffeeGo);
setServo(coffeeStandby);
//turn off
setServo(coffeeOn);
setServo(coffeeStandby);
getPage(String("CofFi_set.php?id=")+id+"&done=1");
digitalWrite(servoTransPin, LOW);
myservo.detach();
}
String readResponse()
{
String lees;
delay(500);
while ( client.available() )
{
char c = client.read();
if (c=='\n') {lees="";} //skip header
else lees += c;
}
if(debug) Serial.println("+"+lees+"+");
return lees;
}
// Perform an HTTP GET request to a remote page
bool getPage(String page) {
// Attempt to make a connection to the remote server
if ( !client.connect(http_site, http_port) ) {
return false;
}
// Make an HTTP GET request
String getter = "GET /domotix/"+page+" HTTP/1.1";
if(debug) Serial.println("going to "+getter);
client.println(getter);
client.println("Host: www.xxx.be");
client.println("User-Agent: PidgeyCofFi");
client.println("Connection: close");
client.println();
return true;
}