public

  • Download Gist
istheflooropen_firmware.pde
Processing
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
#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=8t7iekbjaefgi&status=on");
client.println("secret=8t7iekbjaefgi&status=on");
} else {
Serial.println("secret=8t7iekbjaefgi&status=off");
client.println("secret=8t7iekbjaefgi&status=off");
}
 
client.println();
isConnected = 1;
}
else {
//Serial.println("connection failed");
// machine.transitionTo(readMode);
}
}
}
 
void doNothing(){}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.