Created
January 18, 2018 12:37
-
-
Save loehnertz/d6996cc62ab3077081a2a5c5a1fcd85d to your computer and use it in GitHub Desktop.
AUGS – Hardware
This file contains hidden or 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
// IMPORTS ANFANG | |
#include <ESP8266WiFi.h> | |
#include <ESP8266HTTPClient.h> | |
// IMPORTS ENDE | |
// PIN KONFIGURATION ANFANG | |
#define COLOR_S0 D0 | |
#define COLOR_S1 D1 | |
#define COLOR_S2 D2 | |
#define COLOR_S3 D3 | |
#define COLOR_READ_OUT D4 | |
// PIN KONFIGURATION ENDE | |
// KONSTANTEN ANFANG | |
const char* WIFI_NAME = "AUGS"; | |
const char* WIFI_PASSWORD = "augsistbeschteleben"; | |
const char* HOST = "46.101.105.206"; | |
const char* BASE_URI = "/report"; | |
const char* HOST_AUTH_STRING = "ZWx3OmFkbWlu"; | |
const char* FALLBACK_COLOR = "green"; | |
const int COLOR_SCALING = 20; | |
const int CONTAINER_ID = 2; | |
// KONSTANTEN ENDE | |
// VARIABLEN ANFANG | |
int frequencyValue = 0; | |
int colorValue = 0; | |
// VARIABLEN ENDE | |
// Initiale Einstellungen tätigen | |
void setup() { | |
// Seriellen Monitor initialisieren | |
Serial.begin(9600); | |
// Mit dem WLAN verbinden und prüfen, ob die Verbindung zustande kam | |
Serial.println((String)"Verbinde zum WLAN: " + WIFI_NAME); | |
WiFi.begin(WIFI_NAME, WIFI_PASSWORD); | |
while (WiFi.status() != WL_CONNECTED) { | |
Serial.print("."); | |
delay(1234); | |
} | |
Serial.println(); | |
Serial.println("WLAN erfolgreich verbunden!"); | |
// Die Pins werden für das Auslesen hinsichtlich ihrer Spannung und der Eingabe/Ausgabe konfiguriert | |
setup_pins(); | |
// Kurz warten bevor der Loop beginnt | |
delay(1234); | |
} | |
// Loop, der bis in die Unendlichkeit weiter läuft | |
void loop() { | |
// Analysiere das Altglas | |
analyze_used_glass(); | |
// Kurz warten, bevor die nächste Iteration des Loops startet | |
delay(1234); | |
} | |
// Analysiert die Glasfarbe und schickt den HTTP-Request ans Backend | |
void analyze_used_glass() { | |
// Initialisiere das Array mit den RGB-Farbwerten | |
int rgb_color[4] = {1, 2, 3, 4}; | |
// Lese die RGB-Farbwerte des Sensors aus | |
read_rgb_color(rgb_color); | |
// Ausgelesene Farbwerte in den seriellen Monitor ausgeben | |
Serial.print((String)"R: " + rgb_color[0] + "; "); | |
Serial.print((String)"G: " + rgb_color[1] + "; "); | |
Serial.print((String)"B: " + rgb_color[2] + "; "); | |
Serial.println(); | |
// Versuche, die Farbe zu erraten | |
int glass_color = guess_glass_color(rgb_color[0], rgb_color[1], rgb_color[2]); | |
// Sende den HTTP-Request an das Backend | |
if (glass_color == 1) { | |
report(CONTAINER_ID, "fill", "color=white"); | |
} else if (glass_color == 2) { | |
report(CONTAINER_ID, "fill", "color=green"); | |
} else if (glass_color == 3) { | |
report(CONTAINER_ID, "fill", "color=brown"); | |
} | |
} | |
// Lese die RGB-Farbwerte sowie den "clear"-Wert aus, um den Sensor wieder zurückzusetzen | |
void *read_rgb_color(int color_values[4]) { | |
color_values[0] = read_color("red"); | |
color_values[1] = read_color("green"); | |
color_values[2] = read_color("blue"); | |
color_values[3] = read_color("clear"); | |
} | |
// Je nach Parameter wird der Farbwert für Rot, Grün oder Blau ausgelesen | |
int read_color(char* color) { | |
if (color == "red") { | |
digitalWrite(COLOR_S2, LOW); | |
digitalWrite(COLOR_S3, LOW); | |
frequencyValue = pulseIn(COLOR_READ_OUT, LOW); | |
colorValue = map(frequencyValue, 70, 120, 255, 0); | |
} else if (color == "green") { | |
digitalWrite(COLOR_S2, HIGH); | |
digitalWrite(COLOR_S3, HIGH); | |
frequencyValue = pulseIn(COLOR_READ_OUT, LOW); | |
colorValue = map(frequencyValue, 100, 199, 255, 0); | |
} else if (color == "blue") { | |
digitalWrite(COLOR_S2, LOW); | |
digitalWrite(COLOR_S3, HIGH); | |
frequencyValue = pulseIn(COLOR_READ_OUT, LOW); | |
colorValue = map(frequencyValue, 38, 84, 255, 0); | |
} else if (color == "clear") { | |
digitalWrite(COLOR_S2, HIGH); | |
digitalWrite(COLOR_S3, LOW); | |
colorValue = 0; | |
} else { | |
colorValue = 0; | |
} | |
return colorValue; | |
} | |
// Versuche, die Glasfarbe anhand von Farbbereichen zu bestimmen | |
char guess_glass_color(int redColor, int greenColor, int blueColor) { | |
int guessedColor; | |
if ((redColor > -4500 && redColor < -3750) && (greenColor > -2250 && greenColor < -1750) && (blueColor > -3500 && blueColor < -2750)) { | |
Serial.println("BRAUN erkannt!"); | |
guessedColor = 3; | |
} else if ((redColor > -1750 && redColor < -1250) && (greenColor > -750 && greenColor < -250) && (blueColor > -1500 && blueColor < -750)) { | |
Serial.println("WEISS erkannt!"); | |
guessedColor = 1; | |
} else if ((redColor > -3750 && redColor < -3000) && (greenColor > -1500 && greenColor < -1000) && (blueColor > -2750 && blueColor < -2250)) { | |
Serial.println("GRUEN erkannt!"); | |
guessedColor = 2; | |
} else { | |
Serial.println("NICHTS erkannt!"); | |
guessedColor = 0; | |
} | |
return guessedColor; | |
} | |
// Senden einen HTTP-Request ans Backend | |
void report(int containerId, char* action, char* data) { | |
// Bereite den HTTP-Request vor | |
HTTPClient http; | |
http.begin((String)"http://" + HOST + BASE_URI + "/" + containerId + "/" + action); | |
http.addHeader("Content-Type", "application/x-www-form-urlencoded"); | |
http.addHeader("Authorization", (String)"Basic " + HOST_AUTH_STRING); | |
// Sende den HTTP-Request und werte die Response aus | |
int responseCode = http.POST(data); | |
Serial.println((String)"POST-Request antwortete mit dem HTTP-Statuscode '" + responseCode + "'"); | |
http.end(); | |
} | |
// Stellt die Pins für die Messung ein | |
void setup_pins() { | |
// Pins zur Skalierung der Messung | |
pinMode(COLOR_S0, OUTPUT); | |
pinMode(COLOR_S1, OUTPUT); | |
// Pins zum Einstellen der auszulesenden Farbe | |
pinMode(COLOR_S2, OUTPUT); | |
pinMode(COLOR_S3, OUTPUT); | |
// Pin zum Auslesen | |
pinMode(COLOR_READ_OUT, INPUT); | |
// Skalierung der Messung | |
if (COLOR_SCALING == 0) { | |
digitalWrite(COLOR_S0, LOW); | |
digitalWrite(COLOR_S1, LOW); | |
} else if (COLOR_SCALING == 2) { | |
digitalWrite(COLOR_S0, LOW); | |
digitalWrite(COLOR_S1, HIGH); | |
} else if (COLOR_SCALING == 20) { | |
digitalWrite(COLOR_S0, HIGH); | |
digitalWrite(COLOR_S1, LOW); | |
} else if (COLOR_SCALING == 100) { | |
digitalWrite(COLOR_S0, HIGH); | |
digitalWrite(COLOR_S1, HIGH); | |
} else { | |
Serial.println("The scaling got to be either 0, 2, 20 or 100!"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment