Last active
April 6, 2019 22:44
-
-
Save oschettler/dd0560e088dd413de4b538328f6914a0 to your computer and use it in GitHub Desktop.
Rate-o-Matic with M5SICK-C and servo. Connect to address shown on display and vote. Home button for next vote
This file contains 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
/** | |
* Survey - Display a survey result | |
* | |
* License: MIT | |
* | |
* Copyright (c) 2019 Olav Schettler | |
* | |
* Hiermit wird unentgeltlich jeder Person, die eine Kopie der Software und der zugehörigen | |
* Dokumentationen (die "Software") erhält, die Erlaubnis erteilt, sie uneingeschränkt zu nutzen, | |
* inklusive und ohne Ausnahme mit dem Recht, sie zu verwenden, zu kopieren, zu verändern, | |
* zusammenzufügen, zu veröffentlichen, zu verbreiten, zu unterlizenzieren und/oder zu verkaufen, | |
* und Personen, denen diese Software überlassen wird, diese Rechte zu verschaffen, unter den folgenden | |
* Bedingungen: | |
* Der obige Urheberrechtsvermerk und dieser Erlaubnisvermerk sind in allen Kopien oder Teilkopien | |
* der Software beizulegen. | |
* DIE SOFTWARE WIRD OHNE JEDE AUSDRÜCKLICHE ODER IMPLIZIERTE GARANTIE BEREITGESTELLT, EINSCHLIEẞLICH | |
* DER GARANTIE ZUR BENUTZUNG FÜR DEN VORGESEHENEN ODER EINEM BESTIMMTEN ZWECK SOWIE JEGLICHER | |
* RECHTSVERLETZUNG, JEDOCH NICHT DARAUF BESCHRÄNKT. IN KEINEM FALL SIND DIE AUTOREN ODER COPYRIGHTINHABER | |
* FÜR JEGLICHEN SCHADEN ODER SONSTIGE ANSPRÜCHE HAFTBAR ZU MACHEN, OB INFOLGE DER ERFÜLLUNG EINES VERTRAGES, | |
* EINES DELIKTES ODER ANDERS IM ZUSAMMENHANG MIT DER SOFTWARE ODER SONSTIGER VERWENDUNG DER SOFTWARE ENTSTANDEN. | |
*/ | |
#include <M5StickC.h> | |
#include <WiFi.h> | |
#include <WebServer.h> | |
#include <ESP32Servo.h> | |
#define NCHOICES 5 | |
int survey[NCHOICES]; | |
int survey_id = 1; | |
int survey_count; | |
float survey_average; | |
boolean button_pressed = false; | |
Servo myservo; | |
const char* ssid = "...."; | |
const char* password = "...."; | |
WebServer server(80); | |
const int led = 10; | |
const int servo = 26; | |
void calc() { | |
survey_count = 0; | |
survey_average = 0.0; | |
for (int i = 0; i < NCHOICES; i++) { | |
survey_count += survey[i]; | |
survey_average += (i+1) * survey[i]; | |
} | |
if (survey_count > 0) { | |
survey_average /= survey_count; | |
} | |
} | |
void show() { | |
calc(); | |
M5.Lcd.fillScreen(BLACK); | |
M5.Lcd.setTextColor(RED); | |
M5.Lcd.setTextSize(2); | |
M5.Lcd.setCursor(0, 0); | |
M5.Lcd.printf("%1.1f - %d\n", | |
survey_average, survey_count); | |
M5.Lcd.setTextColor(WHITE); | |
M5.Lcd.setTextSize(1); | |
for (int i = 0; i < NCHOICES; i++) { | |
M5.Lcd.printf("%d-%d ", i+1, survey[i]); | |
} | |
M5.Lcd.setCursor(124, 10); | |
M5.Lcd.printf("# %d", survey_id); | |
M5.Lcd.setCursor(0, 60); | |
M5.Lcd.print("http://"); | |
M5.Lcd.print(WiFi.localIP()); | |
int pos = round(180.0 * survey_average / NCHOICES); | |
Serial.print("Servo: "); | |
Serial.println(pos); | |
myservo.write(pos); | |
} | |
void handleRoot() { | |
digitalWrite(led, 0); | |
String html = "<!doctype html><html>"; | |
html += "<head><meta charset=\"utf-8\">"; | |
html += "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">"; | |
html += "<style>#content{text-align:center;} table{display:inline-block;}</style></head>"; | |
html += "<body><div id=\"content\"><h1>Abstimmung #" + String(survey_id) + "</h1>"; | |
if (survey_count > 0) { | |
html += "<p>Ergebnis: " + String(survey_average, 1) + "<br>Teilnehmer: " + String(survey_count) + "</p>"; | |
html += "<table><tr><th>Bewertung</th><th>Anzahl</th></tr>"; | |
for (int i = 0; i < NCHOICES; i++) { | |
html += "<tr><td>" + String(i+1) + "</td><td>" + String(survey[i]) + "</td></tr>"; | |
} | |
html += "</table>"; | |
} | |
html += "<form id=\"choice\" method=\"POST\" action=\"/choice\"><p>Wie findest du das hier?</p>"; | |
html += "<select name=\"choice\" onchange=\"onchoice()\">"; | |
html += "<option value=\"-1\">-- bitte wählen --</option>"; | |
html += "<option value=\"0\">richtig schlecht</option>"; | |
html += "<option value=\"1\">mittelmäßig</option>"; | |
html += "<option value=\"2\">ganz gut</option>"; | |
html += "<option value=\"3\">super</option>"; | |
html += "<option value=\"4\">total krass</option>"; | |
html += "</select></form></div>"; | |
html += "<script>function onchoice() { localStorage.setItem('survey_" + String(survey_id) + "', true); "; | |
html += "document.getElementById('choice').submit(); }"; | |
html += "if (localStorage.getItem('survey_" + String(survey_id) + "')) { "; | |
html += "let choice = document.getElementById('choice'); "; | |
html += "choice.innerHTML = '<p>Du hast deine Meinung gesagt.</p>'; }</script></body></html>"; | |
server.send(200, "text/html", html); | |
digitalWrite(led, 1); | |
} | |
void handleNotFound() { | |
digitalWrite(led, 0); | |
String message = "File Not Found\n\n"; | |
message += "URI: "; | |
message += server.uri(); | |
message += "\nMethod: "; | |
message += (server.method() == HTTP_GET) ? "GET" : "POST"; | |
message += "\nArguments: "; | |
message += server.args(); | |
message += "\n"; | |
for (uint8_t i = 0; i < server.args(); i++) { | |
message += " " + server.argName(i) + ": " + server.arg(i) + "\n"; | |
} | |
server.send(404, "text/plain", message); | |
digitalWrite(led, 1); | |
} | |
void handleChoice() { | |
digitalWrite(led, 0); | |
if (server.hasArg("choice")) { | |
int choice = server.arg("choice").toInt(); | |
if (choice >= 0 && choice < NCHOICES) { | |
survey[choice] += 1; | |
} | |
} | |
show(); | |
server.sendHeader("Location", "/"); | |
server.send(302, "text/plain", "Look elsewhere"); | |
digitalWrite(led, 1); | |
} | |
void setup(void) { | |
M5.begin(); | |
//myservo.attach(servo, 544, 2400); | |
myservo.attach(servo, 544, 2200); | |
myservo.write(0); | |
delay(15); | |
pinMode(M5_BUTTON_HOME, INPUT); | |
M5.Lcd.setRotation(3); | |
M5.Lcd.fillScreen(BLACK); | |
M5.Lcd.setCursor(0, 0, 2); | |
M5.Lcd.printf("Verbinde mit\n%s...\n", ssid); | |
pinMode(led, OUTPUT); | |
digitalWrite(led, 1); | |
Serial.begin(115200); | |
WiFi.mode(WIFI_STA); | |
WiFi.begin(ssid, password); | |
Serial.println(""); | |
// Wait for connection | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println(""); | |
Serial.print("Connected to "); | |
Serial.println(ssid); | |
Serial.print("IP address: "); | |
Serial.println(WiFi.localIP()); | |
for (int i = 0; i < NCHOICES; i++) { | |
survey[i] = 0; | |
} | |
show(); | |
server.on("/", HTTP_GET, handleRoot); | |
server.on("/choice", HTTP_POST, handleChoice); | |
server.onNotFound(handleNotFound); | |
server.begin(); | |
Serial.println("HTTP server started"); | |
} | |
void loop(void) { | |
server.handleClient(); | |
if (digitalRead(M5_BUTTON_HOME) == LOW) { | |
button_pressed = true; | |
} | |
if (button_pressed && digitalRead(M5_BUTTON_HOME) == HIGH) { | |
button_pressed = false; | |
survey_id++; | |
for (int i = 0; i < NCHOICES; i++) { | |
survey[i] = 0; | |
} | |
show(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment