Run clang-format, add config

This commit is contained in:
Xiretza 2022-08-16 20:24:24 +02:00
parent 0c468f3e4f
commit b06c799e21
2 changed files with 174 additions and 145 deletions

View file

@ -0,0 +1 @@
BasedOnStyle: Chromium

View file

@ -12,14 +12,14 @@ Example sketches from Arduino team, Ethernet by Adrian McEwen
Based on the Ethernet to Thingspeak exaple by Hans Scharler Based on the Ethernet to Thingspeak exaple by Hans Scharler
*/ */
#include <SPI.h>
#include <Ethernet.h> #include <Ethernet.h>
#include <SPI.h>
#include "APIKey.h" #include "APIKey.h"
#include "TimerOne.h" #include "TimerOne.h"
byte mac[] = { 0xD4, 0xBA, 0xD9, 0x9A, 0x7C, 0x95 }; // Must be unique on local network // Must be unique on local network
byte mac[] = {0xD4, 0xBA, 0xD9, 0x9A, 0x7C, 0x95};
// Initialize Arduino Ethernet Client // Initialize Arduino Ethernet Client
EthernetClient client; EthernetClient client;
@ -33,21 +33,21 @@ String currentLine = ""; // string to hold the text from server
int hsopen; int hsopen;
int ethernetstatus; int ethernetstatus;
int ledtimer =0; int ledtimer = 0;
const int ledspan =100; const int ledspan = 100;
int pingtimer =-1; int pingtimer = -1;
const int pingspan =250; const int pingspan = 250;
int checktimer = 0; int checktimer = 0;
const int checkspan =1000; const int checkspan = 1000;
int update = 0; int update = 0;
//debug options // debug options
boolean debug = true; boolean debug = true;
//LED and Switch pins // LED and Switch pins
const int glight = 5; const int glight = 5;
const int rlight = 6; const int rlight = 6;
@ -56,90 +56,93 @@ const int tclose = 3;
const int beeper = A5; const int beeper = A5;
void setLeds(){ void setLeds() {
if (pingtimer>=0) if (pingtimer >= 0) {
{ if (hsopen) { // If space is not closed, beep three times with
if(hsopen){ // If space is not closed, beep three times with // beep
//beep if ((pingtimer > 220) && ((pingtimer / 5) % 2 == 1)) {
if((pingtimer> 220)&&((pingtimer/5)%2 ==1)){
digitalWrite(beeper, HIGH); digitalWrite(beeper, HIGH);
}else{ } else {
digitalWrite(beeper, LOW); digitalWrite(beeper, LOW);
} }
}else{ // if closed, beep once } else { // if closed, beep once
//beep // beep
if((pingtimer> 240)&&((pingtimer/5)%2 ==1)){ if ((pingtimer > 240) && ((pingtimer / 5) % 2 == 1)) {
digitalWrite(beeper, HIGH); digitalWrite(beeper, HIGH);
}else{ } else {
digitalWrite(beeper, LOW); digitalWrite(beeper, LOW);
} }
} }
// blink like crazy // blink like crazy
if((pingtimer/2)%2 ==1){ if ((pingtimer / 2) % 2 == 1) {
analogWrite(rlight, 255); analogWrite(rlight, 255);
analogWrite(glight, 255); analogWrite(glight, 255);
}else{ } else {
analogWrite(rlight, 0); analogWrite(rlight, 0);
analogWrite(glight, 0); analogWrite(glight, 0);
} }
pingtimer--; pingtimer--;
}else{ } else {
switch(ethernetstatus){ switch (ethernetstatus) {
case 0:{ // ethernet fail case 0: { // ethernet fail
//blink red // blink red
if(ledtimer < (ledspan/2)){ if (ledtimer < (ledspan / 2)) {
analogWrite(rlight, 255); analogWrite(rlight, 255);
}else{ } else {
analogWrite(rlight, 0); analogWrite(rlight, 0);
} }
analogWrite(glight, 0); analogWrite(glight, 0);
break; break;
} }
case -1:{ // ethernet unknown case -1: { // ethernet unknown
// fade green // fade green
if(ledtimer < (ledspan/2)){ if (ledtimer < (ledspan / 2)) {
analogWrite(glight, (255/(ledspan/2)*ledtimer));//fade up analogWrite(glight, (255 / (ledspan / 2) * ledtimer)); // fade up
}else{ } else {
analogWrite(glight, 255-(255/(ledspan/2)*(ledtimer-(ledspan/2))));//fade down analogWrite(glight, 255 - (255 / (ledspan / 2) *
(ledtimer - (ledspan / 2)))); // fade down
} }
analogWrite(rlight, 0); analogWrite(rlight, 0);
break; break;
} }
case 1:{ // ethernet ok case 1: { // ethernet ok
switch(hsopen){ switch (hsopen) {
case 0:{ // closed case 0: { // closed
//red // red
analogWrite(rlight, 255); analogWrite(rlight, 255);
analogWrite(glight, 0); analogWrite(glight, 0);
break; break;
} }
case 1:{ // open case 1: { // open
// green // green
analogWrite(rlight, 0); analogWrite(rlight, 0);
analogWrite(glight, 255); analogWrite(glight, 255);
break; break;
} }
case 2:{ // wait case 2: { // wait
// fade red and green // fade red and green
if(ledtimer < (ledspan/2)){ if (ledtimer < (ledspan / 2)) {
int tmp=(255/(ledspan/2)*ledtimer); int tmp = (255 / (ledspan / 2) * ledtimer);
analogWrite(glight, tmp);//fade up analogWrite(glight, tmp); // fade up
analogWrite(rlight, tmp);//fade up analogWrite(rlight, tmp); // fade up
}else{ } else {
int tmp = 255-(255/(ledspan/2)*(ledtimer-(ledspan/2))); int tmp =
analogWrite(glight, tmp);//fade down 255 - (255 / (ledspan / 2) * (ledtimer - (ledspan / 2)));
analogWrite(rlight, tmp);//fade down analogWrite(glight, tmp); // fade down
analogWrite(rlight, tmp); // fade down
} }
break; break;
} }
case -1:{ // unknown case -1: { // unknown
// fade red // fade red
if(ledtimer < (ledspan/2)){ if (ledtimer < (ledspan / 2)) {
analogWrite(rlight, (255/(ledspan/2)*ledtimer));//fade up analogWrite(rlight, (255 / (ledspan / 2) * ledtimer)); // fade up
}else{ } else {
analogWrite(rlight, 255-(255/(ledspan/2)*(ledtimer-(ledspan/2))));//fade down analogWrite(rlight,
255 - (255 / (ledspan / 2) *
(ledtimer - (ledspan / 2)))); // fade down
} }
analogWrite(glight, 0); analogWrite(glight, 0);
break; break;
@ -149,39 +152,38 @@ void setLeds(){
} }
} }
ledtimer++; ledtimer++;
if ( ledtimer >= ledspan ) { if (ledtimer >= ledspan) {
ledtimer = 0; ledtimer = 0;
} }
} }
checktimer++; checktimer++;
if ( checktimer >= checkspan ) { if (checktimer >= checkspan) {
checktimer = 0; checktimer = 0;
update=1; update = 1;
} }
} }
void setEth(int statuss){ void setEth(int statuss) {
ethernetstatus = statuss; ethernetstatus = statuss;
if(debug){ if (debug) {
Serial.println("Setting eth "); Serial.println("Setting eth ");
Serial.println(statuss); Serial.println(statuss);
} }
setLeds(); setLeds();
} }
void setRoom(int statuss){ void setRoom(int statuss) {
hsopen= statuss; hsopen = statuss;
if(debug){ if (debug) {
Serial.print("Setting room "); Serial.print("Setting room ");
Serial.println(statuss); Serial.println(statuss);
} }
setLeds(); setLeds();
} }
void startEthernet() void startEthernet() {
{
client.stop(); client.stop();
if(debug){ if (debug) {
Serial.println("Connecting Arduino to network..."); Serial.println("Connecting Arduino to network...");
Serial.println(); Serial.println();
} }
@ -189,35 +191,32 @@ void startEthernet()
delay(1000); delay(1000);
// Connect to network amd obtain an IP address using DHCP // Connect to network amd obtain an IP address using DHCP
if (Ethernet.begin(mac) == 0) if (Ethernet.begin(mac) == 0) {
{ if (debug) {
if(debug){
Serial.println("DHCP Failed, reset Arduino to try again"); Serial.println("DHCP Failed, reset Arduino to try again");
Serial.println(); Serial.println();
} }
setEth(0); setEth(0);
} } else {
else if (debug) {
{
if(debug){
Serial.println("Arduino connected to network using DHCP"); Serial.println("Arduino connected to network using DHCP");
Serial.println(); Serial.println();
} }
setEth(1); setEth(1);
} }
if(debug){ if (debug) {
Serial.println("DONE"); Serial.println("DONE");
Serial.println(); Serial.println();
} }
delay(1000); delay(1000);
} }
//reads out the status returned by the server and sets the LED's appropriately. // reads out the status returned by the server and sets the LED's appropriately.
//the mode is defied by the intended call: // the mode is defied by the intended call:
void readServerStatus(int mode) { void readServerStatus(int mode) {
char lastsign='0'; char lastsign = '0';
boolean readStatus = false; boolean readStatus = false;
while(client.connected()) { while (client.connected()) {
if (client.available()) { if (client.available()) {
// read incoming bytes: // read incoming bytes:
char inChar = client.read(); char inChar = client.read();
@ -225,26 +224,34 @@ void readServerStatus(int mode) {
// add incoming byte to end of line: // add incoming byte to end of line:
currentLine += inChar; currentLine += inChar;
if(debug){Serial.print(inChar);} if (debug) {
Serial.print(inChar);
}
// if you get a newline, clear the line: // if you get a newline, clear the line:
if (inChar == '\n') { if (inChar == '\n') {
currentLine = ""; currentLine = "";
if(lastsign == '\n'){ // /r/n /r/n is the end of a header if (lastsign == '\n') { // /r/n /r/n is the end of a header
readStatus= true; //start to parse the content of the line readStatus = true; // start to parse the content of the line
if(debug){Serial.println("##END OF HEADER##");} if (debug) {
Serial.println("##END OF HEADER##");
}
} }
} }
if (readStatus) { if (readStatus) {
if(currentLine.startsWith("true", 0)){ if (currentLine.startsWith("true", 0)) {
if(debug){Serial.println("");} if (debug) {
switch(mode){ Serial.println("");
}
switch (mode) {
case 0: case 0:
setRoom(1); setRoom(1);
break; break;
case 1: case 1:
if(debug){Serial.println("Wink Wink");} if (debug) {
Serial.println("Wink Wink");
}
pingtimer = pingspan; pingtimer = pingspan;
break; break;
} }
@ -252,14 +259,18 @@ void readServerStatus(int mode) {
client.stop(); client.stop();
return; return;
} }
if(currentLine.startsWith("false", 0)){ if (currentLine.startsWith("false", 0)) {
if(debug){Serial.println("");} if (debug) {
switch(mode){ Serial.println("");
}
switch (mode) {
case 0: case 0:
setRoom(0); setRoom(0);
break; break;
case 1: case 1:
if(debug){Serial.println("No Wink");} if (debug) {
Serial.println("No Wink");
}
break; break;
} }
// close the connection to the server: // close the connection to the server:
@ -267,7 +278,8 @@ void readServerStatus(int mode) {
return; return;
} }
} }
if (inChar != '\r') {// removes /r so we dan test if the header end with two newlines, hacky but works. if (inChar != '\r') { // removes /r so we dan test if the header end with
// two newlines, hacky but works.
lastsign = inChar; lastsign = inChar;
} }
} }
@ -278,43 +290,47 @@ void readServerStatus(int mode) {
void TriggerServerReq(String s, int mode) { void TriggerServerReq(String s, int mode) {
// attempt to connect, and wait a millisecond: // attempt to connect, and wait a millisecond:
if(debug){Serial.println("connecting to server... Status req");} if (debug) {
Serial.println("connecting to server... Status req");
}
if (client.connect(SERVER_DOMAIN, 80)) { if (client.connect(SERVER_DOMAIN, 80)) {
if(debug){Serial.println("making HTTP request...");} if (debug) {
Serial.println("making HTTP request...");
}
// make HTTP GET request to server: // make HTTP GET request to server:
client.println("GET "+ s +" HTTP/1.1"); client.println("GET " + s + " HTTP/1.1");
client.println("HOST: " SERVER_DOMAIN); client.println("HOST: " SERVER_DOMAIN);
client.println("Connection: close"); client.println("Connection: close");
client.println(); client.println();
readServerStatus(mode); readServerStatus(mode);
}else{ } else {
if(debug){Serial.println("Not connected...");} if (debug) {
Serial.println("Not connected...");
}
} }
// note the time of this connect attempt: // note the time of this connect attempt:
} }
void RequestState() { void RequestState() {
TriggerServerReq("/api/status-s.php",0); TriggerServerReq("/api/status-s.php", 0);
} }
// Status LED pins // Status LED pins
void setup() void setup() {
{
pinMode(glight, OUTPUT); pinMode(glight, OUTPUT);
pinMode(rlight, OUTPUT); pinMode(rlight, OUTPUT);
pinMode(beeper, OUTPUT); pinMode(beeper, OUTPUT);
digitalWrite(beeper,LOW); digitalWrite(beeper, LOW);
// Buttons with pullup resistor // Buttons with pullup resistor
pinMode(topen, INPUT); pinMode(topen, INPUT);
pinMode(tclose, INPUT); pinMode(tclose, INPUT);
digitalWrite(topen,HIGH); digitalWrite(topen, HIGH);
digitalWrite(tclose,HIGH); digitalWrite(tclose, HIGH);
// Start Serial for debugging on the Serial Monitor // Start Serial for debugging on the Serial Monitor
if(debug){ if (debug) {
Serial.begin(9600); Serial.begin(9600);
} }
// Start Ethernet on Arduino // Start Ethernet on Arduino
@ -334,64 +350,76 @@ void setup()
void TriggerServerUpdate(boolean stat) { void TriggerServerUpdate(boolean stat) {
// attempt to connect, and wait a millisecond: // attempt to connect, and wait a millisecond:
if(debug){Serial.println("connecting to server... Update Req");} if (debug) {
Serial.println("connecting to server... Update Req");
}
if (client.connect(SERVER_DOMAIN, 80)) { if (client.connect(SERVER_DOMAIN, 80)) {
if(debug){Serial.println("making HTTP request...");} if (debug) {
Serial.println("making HTTP request...");
}
// make HTTP GET request to server: // make HTTP GET request to server:
String s =(stat?"true":"false"); String s = (stat ? "true" : "false");
client.println("GET /api/update.php?open=" + s + "&apikey=" UPDATE_API_KEY + " HTTP/1.1"); client.println("GET /api/update.php?open=" + s + "&apikey=" UPDATE_API_KEY +
" HTTP/1.1");
client.println("HOST: " SERVER_DOMAIN); client.println("HOST: " SERVER_DOMAIN);
client.println("Connection: close"); client.println("Connection: close");
client.println(); client.println();
readServerStatus(0); readServerStatus(0);
}else{ } else {
if(debug){Serial.println("Not connected...");} if (debug) {
Serial.println("Not connected...");
}
} }
// note the time of this connect attempt: // note the time of this connect attempt:
} }
void readButtons(){ void readButtons() {
if((digitalRead(topen)==LOW)&&(hsopen!=1)){ if ((digitalRead(topen) == LOW) && (hsopen != 1)) {
//startEthernet(); // startEthernet();
setRoom(2); setRoom(2);
TriggerServerUpdate(true); TriggerServerUpdate(true);
} }
if((digitalRead(tclose)==LOW)&&(hsopen!=0)){ if ((digitalRead(tclose) == LOW) && (hsopen != 0)) {
//startEthernet(); // startEthernet();
setRoom(2); setRoom(2);
TriggerServerUpdate(false); TriggerServerUpdate(false);
} }
} }
void RequestPing() { void RequestPing() {
TriggerServerReq("/api/ping-get.php?apikey=" PING_GET_API_KEY,1); TriggerServerReq("/api/ping-get.php?apikey=" PING_GET_API_KEY, 1);
} }
void launchUpdate(){ void launchUpdate() {
if(update==1){ if (update == 1) {
RequestPing(); RequestPing();
RequestState(); RequestState();
update = 0; update = 0;
} }
} }
void loop() void loop() {
{
readButtons(); readButtons();
launchUpdate(); launchUpdate();
} }
//This just reads the server return and logs it to the serial // This just reads the server return and logs it to the serial
// @deprecated //@deprecated
void readServerReturn() { void readServerReturn() {
//if(debug){Serial.println("readServerReturn ... ");} // if(debug){Serial.println("readServerReturn ... ");}
while(client.connected()) { while (client.connected()) {
if (client.available()) { if (client.available()) {
char c = client.read(); char c = client.read();
if(debug){Serial.print(c);} if (debug) {
Serial.print(c);
} }
} }
if(debug){Serial.println();} }
if(debug){Serial.println("disconnecting.");} if (debug) {
Serial.println();
}
if (debug) {
Serial.println("disconnecting.");
}
client.stop(); client.stop();
} }