Skip to content

Instantly share code, notes, and snippets.

@maglub
Created September 20, 2016 01:30
Show Gist options
  • Save maglub/25a2f92b1d70e07dcb7d2894e1be524e to your computer and use it in GitHub Desktop.
Save maglub/25a2f92b1d70e07dcb7d2894e1be524e to your computer and use it in GitHub Desktop.
/*
*/
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress server(192,168,1,101); //
//char server[] = "www.galgbacken.net"; // name address for Google (using DNS)
// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192, 168, 1, 179);
EthernetClient client;
// this constant won't change:
const int buttonPin = 2; // the pin that the pushbutton is attached to
const int ledPin = 4; // the pin that the LED is attached to
//XXX "communications LED"
const int commLedPin = 5;
// Variables will change:
int buttonState = 0; // current state of the button
int lastButtonState = 0; // previous state of the button
void sendMillis(){
time = millis();
//prints time since program started
Serial.println(time);
}
void setup() {
// initialize the button pin as a input:
pinMode(buttonPin, INPUT);
// initialize the LED as an output:
pinMode(ledPin, OUTPUT);
// XXX initialize the commLedPin as an output:
pinMode(commLedPin,OUTPUT);
// initialize serial communication:
Serial.begin(9600);
}
void loop() {
//XXX This piece could be moved to setup()
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// try to congifure using IP address instead of DHCP:
Ethernet.begin(mac, ip);
}
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
if (buttonState == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
//XXX Immediately change the state of the ledPin, so that the user feels that it is snappy
digitalWrite(ledPin, HIGH);
Serial.println("on");
//XXX Turn on the communications LED to show that you do some communications
digitalWrite(commLedPin, HIGH);
Serial.println("... Sending HTTP command for on");
sendMillis();
client.connect(server,80);
sendMillis();
client.println("GET /control?cmd=GPIO,2,1 HTTP/1.1");
sendMillis();
client.println("Host:192.168.1.101");
sendMillis();
client.stop();
Serial.println("... done Sending HTTP command for on");
//XXX Turn on the communications LED to show that you do some communications
digitalWrite(commLedPin, LOW);
} else {
// if the current state is LOW then the button
// wend from on to off:
//XXX Immediately change the state of the ledPin, so that the user feels that it is snappy
digitalWrite(ledPin, LOW);
Serial.println("off");
digitalWrite(commLedPin, HIGH);
Serial.println("... Sending HTTP command for on");
sendMillis();
client.connect(server,80);
sendMillis();
client.println("GET /control?cmd=GPIO,2,0 HTTP/1.1");
sendMillis();
client.println("Host:192.168.1.101");
sendMillis();
client.stop();
sendMillis();
Serial.println("... done Sending HTTP command for on");
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment