Skip to content
Snippets Groups Projects
Commit f9fb9646 authored by Egmont Schreiter's avatar Egmont Schreiter
Browse files

Arduino Sketch

parent 8e14f259
No related branches found
No related tags found
No related merge requests found
/*************************************
* Quelltext um die Anzahl von Besuchern
* in der 2D Welt auf RGB LEDs zu signalisieren
*
* https://edugit.org/eshszg/arduino_2dwelt_usercount
* GNU GENERAL PUBLIC LICENSE Version 3
* ************************************/
// ESP32 oder ESP8266?
#define ESP32
// Boardverwaltung:
// ESP8266: NodeMCU 1.0 (ESP-12E Module)
// ESP32: NodeMCU-32S
//
// Werkzeuge --> Bibliotheksverwaltung: HTTPClient, NeoPixel
#include <Adafruit_NeoPixel.h>
#include <HTTPClient.h>
// Pin an dem die WS2812 RGB LEDs hängen
#define PIN 15
// #### Links die irgendwie zum Stricken des Quelltextes geholfen haben
// https://tttapa.github.io/ESP8266/Chap13%20-%20OTA.html
// https://polluxlabs.net/arduino-tutorials/json-abrufen-dekodieren-mit-arduinojson/
// https://polluxlabs.net/esp8266-projekte/ein-newsticker-per-api-call-json-und-esp32/
//https://polluxlabs.net/arduino-tutorials/json-abrufen-dekodieren-mit-arduinojson/
// Beispiel URLs zum abrufen
// https://pusher.kraut.world/metrics.json
// https://wak-lab.org/krautworld_space_user_counter/?s=wak-lab.json
// https://wak-lab.org/krautworld_space_user_counter/?s=main.json
// https://wak-lab.org/krautworld_space_user_counter/?s=sachsen.json
// https://wak-lab.org/krautworld_space_user_counter/?s=zittau.json
// https://wak-lab.org/krautworld_space_user_counter/?s=werkraum.json
// When we setup the NeoPixel library, we tell it how many pixels, and which pin to use to send signals.
// Note that for older NeoPixel strips you might need to change the third parameter--see the strandtest
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(3, PIN, NEO_GRB + NEO_KHZ800);
#ifdef ESP32
#include <WiFi.h>
#include <WiFiMulti.h>
WiFiMulti wifiMulti;
#else
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
ESP8266WiFiMulti wifiMulti; // Create an instance of the ESP8266WiFiMulti class, called 'wifiMulti'
#endif
#if __has_include ("passworte.h")
#include "passworte.h"
#else
// define here your WLAN credentials for later use,
// or put them in the file "passworte.h" in the same directory
// add all yout WLAN credentials in one line
#define PASSWORTE_Wifi wifiMulti.addAP("WLAN-SSID", "password"); wifiMulti.addAP("FreeInet", "");
#endif
WiFiClient client;
const byte led = 2;
unsigned long previousTime_json = millis();
unsigned long interval_json = 5 * 1000;
unsigned long previousTime_neopixel = millis();
unsigned long interval_neopixel = 100;
unsigned int neopixel_count = 0, neo_r = 20, neo_g = 0, neo_b = 0;
unsigned int room_count0, room_count1, room_count2, room_count3;
void setup() {
Serial.begin(115200); // Start the Serial communication to send messages to the computer
delay(10);
Serial.println('\n');
//wifiMulti.addAP("wlan ssid", "password"); // add Wi-Fi networks you want to connect to
//wifiMulti.addAP("Freifunk", "");
// inlcude wifi credentials from #define
PASSWORTE_Wifi
Serial.println("Connecting ...");
int i = 0;
while (wifiMulti.run() != WL_CONNECTED) { // Wait for the Wi-Fi to connect
delay(250);
Serial.print('.');
}
Serial.println('\n');
Serial.print("Connected to ");
Serial.println(WiFi.SSID()); // Tell us what network we're connected to
Serial.print("IP address:\t");
Serial.println(WiFi.localIP()); // Send the IP address of the ESP to the computer
pinMode(led, OUTPUT);
digitalWrite(led, 1); // LED off
pinMode(12, INPUT_PULLUP);
pinMode(13, INPUT_PULLUP);
pinMode(14, INPUT_PULLUP);
pinMode(15, INPUT_PULLUP);
pixels.begin(); // This initializes the NeoPixel library.
}
void loop() {
//ArduinoOTA.handle();
// periodisch aller x Sekunden versuchen die Server zu erreichen
unsigned long diff = millis() - previousTime_json;
if (wifiMulti.run() == WL_CONNECTED && \
diff > interval_json) { // Only if the Wi-Fi is connected
Serial.print("JSON: ");
Serial.println(digitalRead(13));
room_count0 = count_2d(0);
room_count1 = count_2d(1);
room_count2 = count_2d(2);
room_count3 = count_2d(3);
// Thingspeak Code Ende
Serial.println('\n');
Serial.print("Connected to ");
Serial.println(WiFi.SSID()); // Tell us what network we're connected to
Serial.print("IP address:\t");
Serial.println(WiFi.localIP()); // Send the IP address of the ESP8266/ESP32 to the computer
previousTime_json += diff;
}
diff = millis() - previousTime_neopixel;
if (wifiMulti.run() == WL_CONNECTED && \
diff > interval_neopixel) {
neopixel_count++; // ungenutzt
// Hier irgendwie eine Zuordnung von Anzahl an Besuchern und Farben machen :-)
pixels.setPixelColor(0, pixels.Color( room_count0 * 10, room_count1 * 10, 0));
pixels.setPixelColor(1, pixels.Color( 0, room_count2 * 10, 0));
pixels.setPixelColor(2, pixels.Color( 0, 0, room_count3 * 20));
pixels.show();
previousTime_neopixel += diff;
}
}
unsigned int count_2d(int parameter) {
if ((WiFi.status() == WL_CONNECTED)) {
HTTPClient http;
String url = "";
switch (parameter) {
case 0:
url = "https://wak-lab.org/krautworld_space_user_counter/?s=main.json";
break;
case 1:
url = "https://wak-lab.org/krautworld_space_user_counter/?s=sachsen.json";
break;
case 2:
url = "https://wak-lab.org/krautworld_space_user_counter/?s=zittau.json";
break;
case 3:
url = "https://wak-lab.org/krautworld_space_user_counter/?s=werkraum.json";
break;
default:
url = "https://wak-lab.org/krautworld_space_user_counter/?s=main.json";
}
Serial.print("Param=");
Serial.print(parameter);
Serial.print(" httpCode=");
http.begin(url);
int httpCode = http.GET();
if (httpCode == 200) {
Serial.print(httpCode);
}
String payload = http.getString();
Serial.print(" payload=");
Serial.print(payload);
Serial.print(" payload.toInt=");
Serial.println(payload.toInt());
return payload.toInt();
} else {
Serial.println("Wifi not connected...");
}
return 0;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment