Add Garage_Nano.ino
This commit is contained in:
@@ -0,0 +1,133 @@
|
||||
#include <SPI.h>
|
||||
#include <Ethernet.h>
|
||||
|
||||
// Enter a MAC address and IP address for your controller below.
|
||||
byte mac[] = {0xA4, 0xDA, 0x49, 0xB8, 0x19, 0xA4};
|
||||
IPAddress ip(192,168,178,190);
|
||||
EthernetServer server(80);
|
||||
|
||||
const int remotePin = 2;
|
||||
const int motorPin = 3;
|
||||
|
||||
const int ledPin = 5;
|
||||
|
||||
const int remoteButton = 6;
|
||||
const int motorButton = 7;
|
||||
|
||||
void setup() {
|
||||
Serial.begin(9600);
|
||||
|
||||
// Check PINs
|
||||
Serial.print("MISO: ");
|
||||
Serial.println(PIN_SPI_MISO);
|
||||
Serial.print("MOSI: ");
|
||||
Serial.println(PIN_SPI_MOSI);
|
||||
Serial.print("SCK: ");
|
||||
Serial.println(PIN_SPI_SCK);
|
||||
Serial.print("SS: ");
|
||||
Serial.println(PIN_SPI_SS);
|
||||
|
||||
pinMode(remotePin, OUTPUT);
|
||||
pinMode(motorPin, OUTPUT);
|
||||
pinMode(ledPin, OUTPUT);
|
||||
pinMode(remoteButton, INPUT);
|
||||
pinMode(motorButton, INPUT);
|
||||
|
||||
// disable SD card if one in the slot
|
||||
pinMode(4, OUTPUT);
|
||||
digitalWrite(4, HIGH);
|
||||
|
||||
Serial.println("Starting ethernet");
|
||||
Ethernet.begin(mac, ip);
|
||||
|
||||
Serial.println(Ethernet.localIP());
|
||||
}
|
||||
|
||||
void loop() {
|
||||
if (digitalRead(remoteButton) == HIGH) {
|
||||
Serial.println("Remote button pressed");
|
||||
triggerRemote();
|
||||
}
|
||||
|
||||
if (digitalRead(motorButton) == HIGH) {
|
||||
Serial.println("Motor button pressed");
|
||||
triggerMotor();
|
||||
}
|
||||
|
||||
// listen for incoming clients
|
||||
EthernetClient client = server.available();
|
||||
if (client) {
|
||||
Serial.println("new client");
|
||||
// an http request ends with a blank line
|
||||
bool currentLineIsBlank = true;
|
||||
while (client.connected()) {
|
||||
if (client.available()) {
|
||||
char c = client.read();
|
||||
Serial.write(c);
|
||||
// if you've gotten to the end of the line (received a newline
|
||||
// character) and the line is blank, the http request has ended,
|
||||
// so you can send a reply
|
||||
if (c == '\n' && currentLineIsBlank) {
|
||||
// send a standard http response header
|
||||
client.println("HTTP/1.1 200 OK");
|
||||
client.println("Content-Type: text/html");
|
||||
client.println("Connection: close"); // the connection will be closed after completion of the response
|
||||
client.println("Refresh: 5"); // refresh the page automatically every 5 sec
|
||||
client.println();
|
||||
client.println("<!DOCTYPE HTML>");
|
||||
client.println("<html>");
|
||||
// output the value of each analog input pin
|
||||
for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
|
||||
int sensorReading = analogRead(analogChannel);
|
||||
client.print("analog input ");
|
||||
client.print(analogChannel);
|
||||
client.print(" is ");
|
||||
client.print(sensorReading);
|
||||
client.println("<br />");
|
||||
}
|
||||
client.println("</html>");
|
||||
break;
|
||||
}
|
||||
if (c == '\n') {
|
||||
// you're starting a new line
|
||||
currentLineIsBlank = true;
|
||||
} else if (c != '\r') {
|
||||
// you've gotten a character on the current line
|
||||
currentLineIsBlank = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
// give the web browser time to receive the data
|
||||
delay(1);
|
||||
// close the connection:
|
||||
client.stop();
|
||||
Serial.println("client disconnected");
|
||||
}
|
||||
}
|
||||
|
||||
void triggerRemote() {
|
||||
digitalWrite(remotePin, HIGH);
|
||||
digitalWrite(ledPin, LOW);
|
||||
delay(500);
|
||||
digitalWrite(remotePin, LOW);
|
||||
digitalWrite(ledPin, HIGH);
|
||||
}
|
||||
|
||||
void triggerMotor() {
|
||||
digitalWrite(motorPin, HIGH);
|
||||
digitalWrite(ledPin, LOW);
|
||||
delay(500);
|
||||
digitalWrite(motorPin, LOW);
|
||||
digitalWrite(ledPin, HIGH);
|
||||
}
|
||||
|
||||
void triggerAll() {
|
||||
digitalWrite(remotePin, HIGH);
|
||||
digitalWrite(motorPin, HIGH);
|
||||
digitalWrite(ledPin, LOW);
|
||||
delay(500);
|
||||
digitalWrite(remotePin, LOW);
|
||||
digitalWrite(motorPin, LOW);
|
||||
digitalWrite(ledPin, HIGH);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user