Update Garage_Nano.ino

This commit is contained in:
2019-07-26 20:34:08 +00:00
parent 8dc3007790
commit e28e896176
+68 -23
View File
@@ -3,7 +3,7 @@
// Enter a MAC address and IP address for your controller below. // Enter a MAC address and IP address for your controller below.
byte mac[] = {0xA4, 0xDA, 0x49, 0xB8, 0x19, 0xA4}; byte mac[] = {0xA4, 0xDA, 0x49, 0xB8, 0x19, 0xA4};
IPAddress ip(192,168,178,190); IPAddress ip(192, 168, 178, 52);
EthernetServer server(80); EthernetServer server(80);
const int remotePin = 2; const int remotePin = 2;
@@ -48,24 +48,44 @@ void loop() {
Serial.println("Remote button pressed"); Serial.println("Remote button pressed");
triggerRemote(); triggerRemote();
} }
if (digitalRead(motorButton) == HIGH) { if (digitalRead(motorButton) == HIGH) {
Serial.println("Motor button pressed"); Serial.println("Motor button pressed");
triggerMotor(); triggerMotor();
} }
// listen for incoming clients // listen for incoming clients
EthernetClient client = server.available(); EthernetClient client = server.available();
if (client) { if (client) {
Serial.println("new client"); Serial.println("new client");
// an http request ends with a blank line // an http request ends with a blank line
bool currentLineIsBlank = true; bool currentLineIsBlank = true;
byte trigger = 0;
char uri[64];
byte idx = 0;
boolean uriLine = false;
while (client.connected()) { while (client.connected()) {
if (client.available()) { if (client.available()) {
char c = client.read(); char c = client.read();
// TODO extract GET URI if (currentLineIsBlank && c == 'G') {
uriLine = true;
uri[idx] = c;
idx++;
}
else if (uriLine) {
if (c == '\n') {
uriLine = false;
uri[idx] = '\0';
}
else {
uri[idx] = c;
idx++;
}
}
Serial.write(c); Serial.write(c);
// if you've gotten to the end of the line (received a newline // if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended, // character) and the line is blank, the http request has ended,
@@ -76,7 +96,22 @@ void loop() {
client.println("Content-Type: text/plain"); client.println("Content-Type: text/plain");
client.println("Connection: close"); // the connection will be closed after completion of the response client.println("Connection: close"); // the connection will be closed after completion of the response
client.println(); client.println();
client.println("Mufflon!"); if (uri[4] == '/' && uri[5] == 'r' && uri[6] == ' ') {
client.println("trigger remote");
trigger = 1;
}
else if (uri[4] == '/' && uri[5] == 'm' && uri[6] == ' ') {
client.println("trigger motor");
trigger = 2;
}
else if (uri[4] == '/' && uri[5] == 'a' && uri[6] == ' ') {
client.println("trigger all");
trigger = 3;
}
else {
client.println("not found");
trigger = 0;
}
break; break;
} }
if (c == '\n') { if (c == '\n') {
@@ -93,31 +128,41 @@ void loop() {
// close the connection: // close the connection:
client.stop(); client.stop();
Serial.println("client disconnected"); Serial.println("client disconnected");
if (trigger == 1) {
triggerRemote();
}
else if (trigger == 2) {
triggerMotor();
}
else if (trigger == 3) {
triggerAll();
}
} }
} }
void triggerRemote() { void triggerRemote() {
digitalWrite(remotePin, HIGH); digitalWrite(remotePin, HIGH);
digitalWrite(ledPin, LOW); digitalWrite(ledPin, HIGH);
delay(500); delay(500);
digitalWrite(remotePin, LOW); digitalWrite(remotePin, LOW);
digitalWrite(ledPin, HIGH); digitalWrite(ledPin, LOW);
} }
void triggerMotor() { void triggerMotor() {
digitalWrite(motorPin, HIGH); digitalWrite(motorPin, HIGH);
digitalWrite(ledPin, LOW); digitalWrite(ledPin, HIGH);
delay(500); delay(500);
digitalWrite(motorPin, LOW); digitalWrite(motorPin, LOW);
digitalWrite(ledPin, HIGH); digitalWrite(ledPin, LOW);
} }
void triggerAll() { void triggerAll() {
digitalWrite(remotePin, HIGH); digitalWrite(remotePin, HIGH);
digitalWrite(motorPin, HIGH); digitalWrite(motorPin, HIGH);
digitalWrite(ledPin, LOW); digitalWrite(ledPin, HIGH);
delay(500); delay(500);
digitalWrite(remotePin, LOW); digitalWrite(remotePin, LOW);
digitalWrite(motorPin, LOW); digitalWrite(motorPin, LOW);
digitalWrite(ledPin, HIGH); digitalWrite(ledPin, LOW);
} }