2014-08-30 18:59:20 +02:00
|
|
|
/*
|
2015-12-27 04:38:28 +01:00
|
|
|
IT-Syndikat Open device Firmware for the wooden Device with two fat buttons
|
2014-08-30 18:59:20 +02:00
|
|
|
|
|
|
|
Network Requirements:
|
|
|
|
|
|
|
|
* Ethernet port on Router
|
|
|
|
* DHCP enabled on Router
|
|
|
|
* Unique MAC Address for Arduino
|
2015-12-27 04:38:28 +01:00
|
|
|
|
2014-08-30 18:59:20 +02:00
|
|
|
Additional Credits:
|
|
|
|
Example sketches from Arduino team, Ethernet by Adrian McEwen
|
2015-12-27 04:38:28 +01:00
|
|
|
Based on the Ethernet to Thingspeak exaple by Hans Scharler
|
2014-08-30 18:59:20 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <SPI.h>
|
|
|
|
#include <Ethernet.h>
|
|
|
|
|
|
|
|
#include "APIKey.h"
|
|
|
|
|
2015-12-27 04:38:28 +01:00
|
|
|
byte mac[] = { 0xD4, 0xBA, 0xD9, 0x9A, 0x7C, 0x95 }; // Must be unique on local network
|
2014-08-30 18:59:20 +02:00
|
|
|
|
|
|
|
|
|
|
|
// Initialize Arduino Ethernet Client
|
|
|
|
EthernetClient client;
|
|
|
|
|
|
|
|
// Twitter response variables
|
2015-12-27 04:38:28 +01:00
|
|
|
char serverName[] = "it-syndikat.org"; // URL
|
2014-08-30 18:59:20 +02:00
|
|
|
String currentLine = ""; // string to hold the text from server
|
|
|
|
|
|
|
|
// Specific variables
|
|
|
|
// Status
|
2015-12-27 04:38:28 +01:00
|
|
|
int hsopen;
|
|
|
|
int ethernetstatus;
|
2014-08-30 18:59:20 +02:00
|
|
|
|
|
|
|
//debug options
|
|
|
|
boolean debug = true;
|
|
|
|
|
|
|
|
//LED and Switch pins
|
|
|
|
const int glight = 5;
|
|
|
|
const int rlight = 6;
|
|
|
|
|
|
|
|
const int topen = 2;
|
|
|
|
const int tclose = 3;
|
|
|
|
|
|
|
|
// Status LED pins
|
|
|
|
|
|
|
|
void setup()
|
|
|
|
{
|
|
|
|
|
|
|
|
pinMode(glight, OUTPUT);
|
|
|
|
pinMode(rlight, OUTPUT);
|
|
|
|
|
2015-12-27 04:38:28 +01:00
|
|
|
// Buttons with pullup resistor
|
2014-08-30 18:59:20 +02:00
|
|
|
pinMode(topen, INPUT);
|
|
|
|
pinMode(tclose, INPUT);
|
|
|
|
digitalWrite(topen,HIGH);
|
|
|
|
digitalWrite(tclose,HIGH);
|
|
|
|
|
|
|
|
// Start Serial for debugging on the Serial Monitor
|
|
|
|
if(debug){
|
|
|
|
Serial.begin(9600);
|
|
|
|
}
|
|
|
|
// Start Ethernet on Arduino
|
|
|
|
setEth(-1);
|
2015-12-27 04:38:28 +01:00
|
|
|
setRoom(2);
|
2014-08-30 18:59:20 +02:00
|
|
|
startEthernet();
|
|
|
|
|
|
|
|
delay(1000);
|
|
|
|
|
2015-12-27 04:38:28 +01:00
|
|
|
// Get current Status
|
2015-12-27 05:17:29 +01:00
|
|
|
RequestState();
|
2014-08-30 18:59:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void loop()
|
|
|
|
{
|
|
|
|
readButtons();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void readButtons(){
|
|
|
|
int ret=0;
|
|
|
|
if((digitalRead(topen)==LOW)&&(hsopen!=1)){
|
|
|
|
//startEthernet();
|
|
|
|
setRoom(2);
|
|
|
|
TriggerServerUpdate(true);
|
|
|
|
}
|
|
|
|
if((digitalRead(tclose)==LOW)&&(hsopen!=0)){
|
|
|
|
//startEthernet();
|
|
|
|
setRoom(2);
|
|
|
|
TriggerServerUpdate(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void startEthernet()
|
|
|
|
{
|
|
|
|
client.stop();
|
|
|
|
if(debug){
|
|
|
|
Serial.println("Connecting Arduino to network...");
|
|
|
|
Serial.println();
|
|
|
|
}
|
|
|
|
setEth(-1);
|
|
|
|
delay(1000);
|
|
|
|
|
|
|
|
// Connect to network amd obtain an IP address using DHCP
|
|
|
|
if (Ethernet.begin(mac) == 0)
|
|
|
|
{
|
|
|
|
if(debug){
|
|
|
|
Serial.println("DHCP Failed, reset Arduino to try again");
|
|
|
|
Serial.println();
|
|
|
|
}
|
|
|
|
setEth(0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(debug){
|
|
|
|
Serial.println("Arduino connected to network using DHCP");
|
|
|
|
Serial.println();
|
|
|
|
}
|
|
|
|
setEth(1);
|
|
|
|
}
|
2015-12-27 05:17:29 +01:00
|
|
|
if(debug){
|
|
|
|
Serial.println("DONE");
|
|
|
|
Serial.println();
|
|
|
|
}
|
2014-08-30 18:59:20 +02:00
|
|
|
delay(1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
void setLeds(){
|
|
|
|
switch(ethernetstatus){
|
|
|
|
case 0:{ // ethernet fail
|
2015-12-27 04:38:28 +01:00
|
|
|
// should become blink red
|
2014-08-30 18:59:20 +02:00
|
|
|
analogWrite(rlight, 63);
|
|
|
|
analogWrite(glight, 0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case -1:{ // ethernet unknown
|
2015-12-27 04:38:28 +01:00
|
|
|
// should become fade green
|
2014-08-30 18:59:20 +02:00
|
|
|
analogWrite(rlight, 0);
|
|
|
|
analogWrite(glight, 63);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 1:{ // ethernet ok
|
|
|
|
switch(hsopen){
|
|
|
|
case 0:{ // closed
|
|
|
|
analogWrite(rlight, 255);
|
|
|
|
analogWrite(glight, 0);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 1:{ // open
|
|
|
|
analogWrite(rlight, 0);
|
|
|
|
analogWrite(glight, 255);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 2:{ // wait
|
2015-12-27 04:38:28 +01:00
|
|
|
// should become fade red and green
|
2014-08-30 18:59:20 +02:00
|
|
|
analogWrite(rlight, 127);
|
|
|
|
analogWrite(glight, 127);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case -1:{ // unknown
|
2015-12-27 04:38:28 +01:00
|
|
|
// should become fade red
|
2014-08-30 18:59:20 +02:00
|
|
|
analogWrite(rlight, 63);
|
|
|
|
analogWrite(glight, 63);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void setEth(int statuss){
|
|
|
|
ethernetstatus = statuss;
|
|
|
|
if(debug){
|
|
|
|
Serial.println("Setting eth ");
|
|
|
|
Serial.println(statuss);
|
|
|
|
}
|
|
|
|
setLeds();
|
|
|
|
}
|
|
|
|
|
|
|
|
void setRoom(int statuss){
|
2015-12-27 04:38:28 +01:00
|
|
|
hsopen= statuss;
|
2014-08-30 18:59:20 +02:00
|
|
|
if(debug){
|
|
|
|
Serial.print("Setting room ");
|
|
|
|
Serial.println(statuss);
|
|
|
|
}
|
|
|
|
setLeds();
|
|
|
|
}
|
|
|
|
|
2015-12-27 05:17:29 +01:00
|
|
|
void RequestState() {
|
|
|
|
TriggerServerReq("/status-s.php",0);
|
|
|
|
}
|
2014-08-30 18:59:20 +02:00
|
|
|
|
2015-12-27 05:17:29 +01:00
|
|
|
void TriggerServerReq(String s, int mode) {
|
2014-08-30 18:59:20 +02:00
|
|
|
// attempt to connect, and wait a millisecond:
|
|
|
|
if(debug){Serial.println("connecting to server... Status req");}
|
|
|
|
if (client.connect(serverName, 80)) {
|
|
|
|
if(debug){Serial.println("making HTTP request...");}
|
|
|
|
// make HTTP GET request to server:
|
2015-12-27 05:17:29 +01:00
|
|
|
client.println("GET "+ s +" HTTP/1.1");
|
2014-08-30 18:59:20 +02:00
|
|
|
client.println("HOST: it-syndikat.org");
|
|
|
|
client.println("Connection: close");
|
|
|
|
client.println();
|
2015-12-27 05:17:29 +01:00
|
|
|
readServerStatus(mode);
|
2014-08-30 18:59:20 +02:00
|
|
|
}else{
|
|
|
|
if(debug){Serial.println("Not connected...");}
|
|
|
|
}
|
|
|
|
// note the time of this connect attempt:
|
|
|
|
}
|
|
|
|
|
|
|
|
void TriggerServerUpdate(boolean stat) {
|
|
|
|
// attempt to connect, and wait a millisecond:
|
|
|
|
if(debug){Serial.println("connecting to server... Update Req");}
|
|
|
|
if (client.connect(serverName, 80)) {
|
|
|
|
if(debug){Serial.println("making HTTP request...");}
|
|
|
|
// make HTTP GET request to server:
|
|
|
|
String s =(stat?"true":"false");
|
|
|
|
client.println("GET /update.php?open=" + s + "&apikey="+serverAPIKey+" HTTP/1.1");
|
|
|
|
client.println("HOST: it-syndikat.org");
|
|
|
|
client.println("Connection: close");
|
|
|
|
client.println();
|
2015-12-27 05:17:29 +01:00
|
|
|
readServerStatus(0);
|
2014-08-30 18:59:20 +02:00
|
|
|
}else{
|
|
|
|
if(debug){Serial.println("Not connected...");}
|
|
|
|
}
|
|
|
|
// note the time of this connect attempt:
|
|
|
|
}
|
|
|
|
|
|
|
|
//reads out the status returned by the server and sets the LED's appropriately.
|
2015-12-27 05:17:29 +01:00
|
|
|
//the mode is defied by the intended call:
|
|
|
|
int readServerStatus(int mode) {
|
2014-08-30 18:59:20 +02:00
|
|
|
char lastsign='0';
|
|
|
|
boolean readStatus = false;
|
|
|
|
while(client.connected()) {
|
|
|
|
if (client.available()) {
|
|
|
|
// read incoming bytes:
|
|
|
|
char inChar = client.read();
|
|
|
|
|
|
|
|
// add incoming byte to end of line:
|
|
|
|
currentLine += inChar;
|
|
|
|
|
|
|
|
if(debug){Serial.print(inChar);}
|
|
|
|
|
|
|
|
// if you get a newline, clear the line:
|
|
|
|
if (inChar == '\n') {
|
|
|
|
currentLine = "";
|
|
|
|
if(lastsign == '\n'){ // /r/n /r/n is the end of a header
|
|
|
|
readStatus= true; //start to parse the content of the line
|
|
|
|
if(debug){Serial.println("##END OF HEADER##");}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (readStatus) {
|
|
|
|
if(currentLine.startsWith("true", 0)){
|
|
|
|
if(debug){Serial.println("");}
|
2015-12-27 05:17:29 +01:00
|
|
|
switch(mode){
|
|
|
|
case 0:
|
|
|
|
setRoom(1);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
if(debug){Serial.println("Wink Wink");}
|
|
|
|
break;
|
|
|
|
}
|
2014-08-30 18:59:20 +02:00
|
|
|
// close the connection to the server:
|
|
|
|
client.stop();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if(currentLine.startsWith("false", 0)){
|
|
|
|
if(debug){Serial.println("");}
|
2015-12-27 05:17:29 +01:00
|
|
|
switch(mode){
|
|
|
|
case 0:
|
|
|
|
setRoom(0);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
if(debug){Serial.println("No Wink");}
|
|
|
|
break;
|
|
|
|
}
|
2014-08-30 18:59:20 +02:00
|
|
|
// close the connection to the server:
|
|
|
|
client.stop();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (inChar != '\r') {// removes /r so we dan test if the header end with two newlines, hacky but works.
|
|
|
|
lastsign = inChar;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-12-27 04:38:28 +01:00
|
|
|
setRoom(-1);
|
2014-08-30 18:59:20 +02:00
|
|
|
client.stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//This just reads the server return and logs it to the serial
|
2015-12-27 05:17:29 +01:00
|
|
|
// @deprecated
|
2014-08-30 18:59:20 +02:00
|
|
|
void readServerReturn() {
|
|
|
|
//if(debug){Serial.println("readServerReturn ... ");}
|
|
|
|
while(client.connected()) {
|
|
|
|
if (client.available()) {
|
|
|
|
char c = client.read();
|
|
|
|
if(debug){Serial.print(c);}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(debug){Serial.println();}
|
|
|
|
if(debug){Serial.println("disconnecting.");}
|
|
|
|
client.stop();
|
|
|
|
}
|
|
|
|
|