This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SPI.h> | |
#include <Ethernet.h> | |
#include <FiniteStateMachine.h> | |
byte mac[] = { | |
0x02, 0xAA, 0xBB, 0xCC, 0x00, 0x03 }; | |
byte ip[] = { | |
128, 122, 151, 9 }; | |
byte server[] = { | |
174,129,212,2 }; | |
Client client(server, 80); | |
int isConnected = 0; | |
State readMode = State(doNothing, doReadMode, doNothing); | |
State pingMode = State(doNothing, doPingMode, doNothing); | |
FSM machine = FSM(readMode); | |
int switchPin = 5; | |
int currentSwitchState = 0; // open | |
void setup() | |
{ | |
Ethernet.begin(mac, ip); | |
Serial.begin(9600); | |
delay(1000); | |
Serial.println("ready"); | |
} | |
void loop() | |
{ | |
machine.update(); | |
} | |
void doReadMode(){ | |
if(digitalRead(switchPin) == 1 && currentSwitchState == 0){ | |
Serial.println("switch closed"); | |
currentSwitchState = 1; | |
machine.transitionTo(pingMode); | |
} | |
if(digitalRead(switchPin) == 0 && currentSwitchState == 1){ | |
Serial.println("switch opened"); | |
currentSwitchState = 0; | |
machine.transitionTo(pingMode); | |
} | |
} | |
void doPingMode(){ | |
if(isConnected){ | |
if (client.available()) { | |
char c = client.read(); | |
Serial.print(c); | |
} | |
if (!client.connected()) { | |
Serial.println(); | |
Serial.println("disconnecting."); | |
client.stop(); | |
isConnected = 0; | |
machine.transitionTo(readMode); | |
} | |
} | |
else { | |
if (client.connect()) { | |
Serial.println("connected"); | |
client.println("POST /log HTTP/1.0"); | |
client.println("HOST: istheflooropen.com"); | |
client.println("Content-Type: application/x-www-form-urlencoded"); | |
client.println("Content-Length: 31"); | |
client.println(); | |
if(currentSwitchState == 1){ | |
Serial.println("secret=<CENSORED>&status=on"); | |
client.println("secret=<CENSORED>&status=on"); | |
} else { | |
Serial.println("secret=<CENSORED>&status=off"); | |
client.println("secret=<CENSORED>&status=off"); | |
} | |
client.println(); | |
isConnected = 1; | |
} | |
else { | |
//Serial.println("connection failed"); | |
// machine.transitionTo(readMode); | |
} | |
} | |
} | |
void doNothing(){} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment