commit 17acce0435927f9e61dc382fa3ec1575b2fd65ba Author: Kekskurse Date: Sat Nov 9 19:24:21 2019 +0100 First Commit diff --git a/Arduino.ino b/Arduino.ino new file mode 100644 index 0000000..c55d98c --- /dev/null +++ b/Arduino.ino @@ -0,0 +1,127 @@ +/* fenster-helligkeits-sensor */ + +#include +#include + +const char* SSID = ""; +const char* PSK = ""; +const char* MQTT_BROKER = ""; +const char* MQTT_USER = ""; +const char* MQTT_PASS = ""; + +int pin_fenster_left = 13; +int pin_fenster_right = 12; + +int pin_fenster_left_status = 2; +int pin_fenster_right_status = 2; +int last_send_helligkeits_sensor = 0; + +int loop_since_last_send_helligkeitssensor = 0; //Loop run ~every secound, count to 2 minutes + +WiFiClient espClient; +PubSubClient client(espClient); + +void setup() { + //Turn Status LED On (while connecting) + pinMode(LED_BUILTIN, OUTPUT); + digitalWrite(LED_BUILTIN, LOW); + + //Pin mode for hall-sensor + pinMode(pin_fenster_right, INPUT); + pinMode(pin_fenster_left, INPUT); + + //Serial Console for Debug Infos + Serial.begin(115200); + + //Connecting to WiFi + Serial.println(); + Serial.print("Connecting to "); + Serial.println(SSID); + + WiFi.begin(SSID, PSK); + + while (WiFi.status() != WL_CONNECTED) { + delay(500); + Serial.print("."); + } + + Serial.println(""); + Serial.println("WiFi connected"); + Serial.println("IP address: "); + Serial.println(WiFi.localIP()); + + //Setup MQTT Broker Connection + client.setServer(MQTT_BROKER, 1883); + + //Turn Status LED Off + digitalWrite(LED_BUILTIN, HIGH); + +} + +//Reconnect to MQTT Broker if connection failed +void reconnect() { + while (!client.connected()) { + // Turn Status LED on while connecting + digitalWrite(LED_BUILTIN, LOW); + //Reconnecting + Serial.print("Reconnecting..."); + if (!client.connect("ESP8266Client-Fenster-Helligkeits-Sensor", MQTT_USER, MQTT_PASS, "/sensor/fenster/status", 1, true, "0")) { + Serial.print("failed, rc="); + Serial.print(client.state()); + Serial.println(" retrying in 5 seconds"); + delay(5000); + } + } + client.publish("/sensor/fenster/status", "1", true); + digitalWrite(LED_BUILTIN, HIGH); +} +void loop() { + //Check MQTT Connection + if (!client.connected()) { + reconnect(); + } + //Loop MQTT Connection + client.loop(); + + //Check if window left status changed + if(pin_fenster_left_status != digitalRead(pin_fenster_left)) { + //Convert Value to char array + char cc[6]; + String(digitalRead(pin_fenster_left)).toCharArray(cc, 6); + //Send value to mqtt + client.publish("/sensor/fenster-links/value", cc, true); + //Set last value to current value + pin_fenster_left_status = digitalRead(pin_fenster_left); + } + + //Check if window right status changed + if(pin_fenster_right_status != digitalRead(pin_fenster_right)) { + //Convert Value to char array + char cc[6]; + String(digitalRead(pin_fenster_right)).toCharArray(cc, 6); + //Send value to mqtt + client.publish("/sensor/fenster-rechts/value", cc, true); + //Set last value to current value + pin_fenster_right_status = digitalRead(pin_fenster_right); + } + + + //Get Helligkeitssensor + int sensorValue = analogRead(A0); + int outputValue = map(sensorValue, 0, 1024, 0, 255); + int dif = outputValue - last_send_helligkeits_sensor; + + if(dif > 15 || dif < -15 || loop_since_last_send_helligkeitssensor > 300) { + char cc[6]; + String(outputValue).toCharArray(cc, 6); + client.publish("/sensor/helligkeit/1/value", cc, true); + //Set last value to current value + last_send_helligkeits_sensor = outputValue; + //Set loop since count to 0, so the next regular message will be send in 5 minutes + loop_since_last_send_helligkeitssensor = 0; + } + + loop_since_last_send_helligkeitssensor = loop_since_last_send_helligkeitssensor + 1; + delay(1000); + +} diff --git a/Readme.md b/Readme.md new file mode 100644 index 0000000..d1c6e2a --- /dev/null +++ b/Readme.md @@ -0,0 +1,10 @@ +# Fenster - Helligkeits sensor +Wemos Projekt um 2 Hall Sensoren und einen Photowiederstand ausließt. + +![Schaltplan](docs/Schaltplan.png) + +# Lochraster Platine + +![Platine 1](docs/20191109_181219.jpg) +![Platine 2](docs/20191109_181224.jpg) +![Platine 3](docs/20191109_181239.jpg) diff --git a/docs/20191109_181219.jpg b/docs/20191109_181219.jpg new file mode 100644 index 0000000..0ea4f5c Binary files /dev/null and b/docs/20191109_181219.jpg differ diff --git a/docs/20191109_181224.jpg b/docs/20191109_181224.jpg new file mode 100644 index 0000000..b5faac6 Binary files /dev/null and b/docs/20191109_181224.jpg differ diff --git a/docs/20191109_181239.jpg b/docs/20191109_181239.jpg new file mode 100644 index 0000000..cdc19fe Binary files /dev/null and b/docs/20191109_181239.jpg differ diff --git a/docs/Schaltplan.png b/docs/Schaltplan.png new file mode 100644 index 0000000..f0533d7 Binary files /dev/null and b/docs/Schaltplan.png differ