This repository has been archived on 2024-02-16. You can view files and clone it, but cannot push or open issues or pull requests.
ITSynOpen/ITS_Open_direct_2fb/ITS_Open_direct_2fb.ino

401 lines
9.2 KiB
Arduino
Raw Normal View History

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 <Ethernet.h>
2022-08-16 20:24:24 +02:00
#include <SPI.h>
2014-08-30 18:59:20 +02:00
#include "APIKey.h"
#include "TimerOne.h"
2014-08-30 18:59:20 +02:00
2022-08-16 20:24:24 +02:00
// Must be unique on local network
byte mac[] = {0xD4, 0xBA, 0xD9, 0x9A, 0x7C, 0x95};
2014-08-30 18:59:20 +02:00
// Initialize Arduino Ethernet Client
EthernetClient client;
// Twitter response variables
#define SERVER_DOMAIN "spaceapi.it-syndikat.org" // URL
2022-08-16 20:24:24 +02:00
String currentLine = ""; // string to hold the text from server
2014-08-30 18:59:20 +02:00
// Specific variables
// Status
2015-12-27 04:38:28 +01:00
int hsopen;
int ethernetstatus;
2014-08-30 18:59:20 +02:00
2022-08-16 20:24:24 +02:00
int ledtimer = 0;
const int ledspan = 100;
2022-08-16 20:24:24 +02:00
int pingtimer = -1;
const int pingspan = 250;
int checktimer = 0;
2022-08-16 20:24:24 +02:00
const int checkspan = 1000;
int update = 0;
2022-08-16 20:24:24 +02:00
// debug options
2014-08-30 18:59:20 +02:00
boolean debug = true;
2022-08-16 20:24:24 +02:00
// LED and Switch pins
2014-08-30 18:59:20 +02:00
const int glight = 5;
const int rlight = 6;
const int topen = 2;
const int tclose = 3;
const int beeper = A5;
2022-08-16 20:24:24 +02:00
void setLeds() {
if (pingtimer >= 0) {
if (hsopen) { // If space is not closed, beep three times with
// beep
if ((pingtimer > 220) && ((pingtimer / 5) % 2 == 1)) {
digitalWrite(beeper, HIGH);
2022-08-16 20:24:24 +02:00
} else {
digitalWrite(beeper, LOW);
}
2022-08-16 20:24:24 +02:00
} else { // if closed, beep once
// beep
if ((pingtimer > 240) && ((pingtimer / 5) % 2 == 1)) {
digitalWrite(beeper, HIGH);
2022-08-16 20:24:24 +02:00
} else {
digitalWrite(beeper, LOW);
}
}
// blink like crazy
2022-08-16 20:24:24 +02:00
if ((pingtimer / 2) % 2 == 1) {
analogWrite(rlight, 255);
analogWrite(glight, 255);
2022-08-16 20:24:24 +02:00
} else {
2014-08-30 18:59:20 +02:00
analogWrite(rlight, 0);
analogWrite(glight, 0);
2014-08-30 18:59:20 +02:00
}
pingtimer--;
2022-08-16 20:24:24 +02:00
} else {
switch (ethernetstatus) {
case 0: { // ethernet fail
// blink red
if (ledtimer < (ledspan / 2)) {
2014-08-30 18:59:20 +02:00
analogWrite(rlight, 255);
2022-08-16 20:24:24 +02:00
} else {
2014-08-30 18:59:20 +02:00
analogWrite(rlight, 0);
}
analogWrite(glight, 0);
break;
}
2022-08-16 20:24:24 +02:00
case -1: { // ethernet unknown
// fade green
2022-08-16 20:24:24 +02:00
if (ledtimer < (ledspan / 2)) {
analogWrite(glight, (255 / (ledspan / 2) * ledtimer)); // fade up
} else {
analogWrite(glight, 255 - (255 / (ledspan / 2) *
(ledtimer - (ledspan / 2)))); // fade down
2017-03-06 13:28:30 +01:00
}
analogWrite(rlight, 0);
break;
}
2022-08-16 20:24:24 +02:00
case 1: { // ethernet ok
switch (hsopen) {
case 0: { // closed
// red
analogWrite(rlight, 255);
analogWrite(glight, 0);
break;
}
2022-08-16 20:24:24 +02:00
case 1: { // open
// green
analogWrite(rlight, 0);
analogWrite(glight, 255);
break;
}
2022-08-16 20:24:24 +02:00
case 2: { // wait
// fade red and green
2022-08-16 20:24:24 +02:00
if (ledtimer < (ledspan / 2)) {
int tmp = (255 / (ledspan / 2) * ledtimer);
analogWrite(glight, tmp); // fade up
analogWrite(rlight, tmp); // fade up
} else {
int tmp =
255 - (255 / (ledspan / 2) * (ledtimer - (ledspan / 2)));
analogWrite(glight, tmp); // fade down
analogWrite(rlight, tmp); // fade down
}
break;
}
2022-08-16 20:24:24 +02:00
case -1: { // unknown
// fade red
2022-08-16 20:24:24 +02:00
if (ledtimer < (ledspan / 2)) {
analogWrite(rlight, (255 / (ledspan / 2) * ledtimer)); // fade up
} else {
analogWrite(rlight,
255 - (255 / (ledspan / 2) *
(ledtimer - (ledspan / 2)))); // fade down
2017-03-06 13:28:30 +01:00
}
analogWrite(glight, 0);
break;
}
2017-03-06 13:28:30 +01:00
}
break;
}
}
ledtimer++;
2022-08-16 20:24:24 +02:00
if (ledtimer >= ledspan) {
ledtimer = 0;
2014-08-30 18:59:20 +02:00
}
}
2022-08-16 20:24:24 +02:00
checktimer++;
if (checktimer >= checkspan) {
checktimer = 0;
update = 1;
}
2014-08-30 18:59:20 +02:00
}
2022-08-16 20:24:24 +02:00
void setEth(int statuss) {
2014-08-30 18:59:20 +02:00
ethernetstatus = statuss;
2022-08-16 20:24:24 +02:00
if (debug) {
2014-08-30 18:59:20 +02:00
Serial.println("Setting eth ");
Serial.println(statuss);
}
setLeds();
}
2022-08-16 20:24:24 +02:00
void setRoom(int statuss) {
hsopen = statuss;
if (debug) {
2014-08-30 18:59:20 +02:00
Serial.print("Setting room ");
Serial.println(statuss);
}
setLeds();
}
2022-08-16 20:24:24 +02:00
void startEthernet() {
2017-03-06 13:28:30 +01:00
client.stop();
2022-08-16 20:24:24 +02:00
if (debug) {
2017-03-06 13:28:30 +01:00
Serial.println("Connecting Arduino to network...");
Serial.println();
2014-08-30 18:59:20 +02:00
}
2017-03-06 13:28:30 +01:00
setEth(-1);
delay(1000);
2014-08-30 18:59:20 +02:00
2017-03-06 13:28:30 +01:00
// Connect to network amd obtain an IP address using DHCP
2022-08-16 20:24:24 +02:00
if (Ethernet.begin(mac) == 0) {
if (debug) {
2017-03-06 13:28:30 +01:00
Serial.println("DHCP Failed, reset Arduino to try again");
Serial.println();
}
setEth(0);
2022-08-16 20:24:24 +02:00
} else {
if (debug) {
2017-03-06 13:28:30 +01:00
Serial.println("Arduino connected to network using DHCP");
Serial.println();
}
setEth(1);
}
2022-08-16 20:24:24 +02:00
if (debug) {
2017-03-06 13:28:30 +01:00
Serial.println("DONE");
Serial.println();
}
delay(1000);
2014-08-30 18:59:20 +02:00
}
2022-08-16 20:24:24 +02:00
// reads out the status returned by the server and sets the LED's appropriately.
// the mode is defied by the intended call:
2017-03-06 13:29:53 +01:00
void readServerStatus(int mode) {
2022-08-16 20:24:24 +02:00
char lastsign = '0';
2014-08-30 18:59:20 +02:00
boolean readStatus = false;
2022-08-16 20:24:24 +02:00
while (client.connected()) {
2014-08-30 18:59:20 +02:00
if (client.available()) {
// read incoming bytes:
char inChar = client.read();
// add incoming byte to end of line:
currentLine += inChar;
2017-03-06 13:28:30 +01:00
2022-08-16 20:24:24 +02:00
if (debug) {
Serial.print(inChar);
}
2014-08-30 18:59:20 +02:00
// if you get a newline, clear the line:
if (inChar == '\n') {
currentLine = "";
2022-08-16 20:24:24 +02:00
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##");
}
2014-08-30 18:59:20 +02:00
}
}
2017-03-06 13:28:30 +01:00
2014-08-30 18:59:20 +02:00
if (readStatus) {
2022-08-16 20:24:24 +02:00
if (currentLine.startsWith("true", 0)) {
if (debug) {
Serial.println("");
}
switch (mode) {
case 0:
2022-08-16 20:24:24 +02:00
setRoom(1);
break;
case 1:
2022-08-16 20:24:24 +02:00
if (debug) {
Serial.println("Wink Wink");
}
pingtimer = pingspan;
break;
}
2014-08-30 18:59:20 +02:00
// close the connection to the server:
client.stop();
2017-03-06 13:29:53 +01:00
return;
2014-08-30 18:59:20 +02:00
}
2022-08-16 20:24:24 +02:00
if (currentLine.startsWith("false", 0)) {
if (debug) {
Serial.println("");
}
switch (mode) {
case 0:
2022-08-16 20:24:24 +02:00
setRoom(0);
break;
case 1:
2022-08-16 20:24:24 +02:00
if (debug) {
Serial.println("No Wink");
}
break;
}
2014-08-30 18:59:20 +02:00
// close the connection to the server:
client.stop();
2017-03-06 13:29:53 +01:00
return;
2017-03-06 13:28:30 +01:00
}
2014-08-30 18:59:20 +02:00
}
2022-08-16 20:24:24 +02:00
if (inChar != '\r') { // removes /r so we dan test if the header end with
// two newlines, hacky but works.
2014-08-30 18:59:20 +02:00
lastsign = inChar;
2017-03-06 13:28:30 +01:00
}
2014-08-30 18:59:20 +02:00
}
}
2015-12-27 04:38:28 +01:00
setRoom(-1);
2014-08-30 18:59:20 +02:00
client.stop();
}
void TriggerServerReq(String req, int mode) {
2017-03-06 13:28:30 +01:00
// attempt to connect, and wait a millisecond:
2022-08-16 20:24:24 +02:00
if (debug) {
Serial.println("connecting to server... Status req");
}
if (client.connect(SERVER_DOMAIN, 80)) {
2022-08-16 20:24:24 +02:00
if (debug) {
Serial.println("making HTTP request: " + req);
2022-08-16 20:24:24 +02:00
}
// make HTTP request to server:
client.println(req + " HTTP/1.1");
client.println("HOST: " SERVER_DOMAIN);
2017-03-06 13:28:30 +01:00
client.println("Connection: close");
client.println();
readServerStatus(mode);
2022-08-16 20:24:24 +02:00
} else {
if (debug) {
Serial.println("Not connected...");
}
2017-03-06 13:28:30 +01:00
}
// note the time of this connect attempt:
}
void RequestState() {
TriggerServerReq("GET /api/status-s.php", 0);
2017-03-06 13:28:30 +01:00
}
// Status LED pins
2022-08-16 20:24:24 +02:00
void setup() {
2017-03-06 13:28:30 +01:00
pinMode(glight, OUTPUT);
pinMode(rlight, OUTPUT);
2022-08-16 20:24:24 +02:00
pinMode(beeper, OUTPUT);
digitalWrite(beeper, LOW);
2017-03-06 13:28:30 +01:00
// Buttons with pullup resistor
pinMode(topen, INPUT);
pinMode(tclose, INPUT);
2022-08-16 20:24:24 +02:00
digitalWrite(topen, HIGH);
digitalWrite(tclose, HIGH);
2017-03-06 13:28:30 +01:00
// Start Serial for debugging on the Serial Monitor
2022-08-16 20:24:24 +02:00
if (debug) {
2017-03-06 13:28:30 +01:00
Serial.begin(9600);
}
// Start Ethernet on Arduino
setEth(-1);
setRoom(2);
2022-08-16 20:24:24 +02:00
Timer1.initialize(20000); // 50 mal die Sekunde
2017-03-06 13:28:30 +01:00
Timer1.attachInterrupt(setLeds);
startEthernet();
delay(1000);
// Get current Status
RequestState();
}
2022-08-16 20:24:24 +02:00
void readButtons() {
if ((digitalRead(topen) == LOW) && (hsopen != 1)) {
// startEthernet();
2017-03-06 13:28:30 +01:00
setRoom(2);
TriggerServerReq("POST /api/update.php?open=true&apikey=" UPDATE_API_KEY, 0);
2017-03-06 13:28:30 +01:00
}
2022-08-16 20:24:24 +02:00
if ((digitalRead(tclose) == LOW) && (hsopen != 0)) {
// startEthernet();
2017-03-06 13:28:30 +01:00
setRoom(2);
TriggerServerReq("POST /api/update.php?open=false&apikey=" UPDATE_API_KEY, 0);
2017-03-06 13:28:30 +01:00
}
}
void RequestPing() {
TriggerServerReq("POST /api/ping-get.php?apikey=" PING_GET_API_KEY, 1);
2017-03-06 13:28:30 +01:00
}
2022-08-16 20:24:24 +02:00
void launchUpdate() {
if (update == 1) {
2017-03-06 13:28:30 +01:00
RequestPing();
RequestState();
update = 0;
}
}
2022-08-16 20:24:24 +02:00
void loop() {
2017-03-06 13:28:30 +01:00
readButtons();
launchUpdate();
}
2014-08-30 18:59:20 +02:00
2022-08-16 20:24:24 +02:00
// This just reads the server return and logs it to the serial
//@deprecated
2017-03-06 13:28:30 +01:00
void readServerReturn() {
2022-08-16 20:24:24 +02:00
// 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();
2014-08-30 18:59:20 +02:00
}