Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Arduino.h>
- #include <WiFi.h>
- #include <PubSubClient.h>
- #include <RCSwitch.h>
- #include <Adafruit_Si7021.h>
- #define RELAY_PIN1 27
- //Verbindungsinfos
- const char* ssid = "wintergarten4_0";
- const char* password = "schoener_wohnen";
- const char* mqttServer = "192.168.155.1";
- const int mqttPort = 1883;
- const char* mqttUser = "klima";
- const char* mqttPassword = "klimawandel";
- //Mqtttopics
- const char* temp_sense_top="wintergarten/klima/sensors/temperature";
- const char* humid_sense_top="wintergarten/klima/sensors/humidity";
- const char* outlet1_act_top="wintergarten/klima/actuators/outlets/outlet1";
- const char* outlet2_act_top="wintergarten/klima/actuators/outlets/outlet2";
- const char* humid_act_top="wintergarten/klima/actuators/humidity/humidifier";
- //Konstanten
- const int UPDATE_CYCLE=1000;//5*60000; //Zeitspanne für Updates in ms
- //Variablen
- long lastMsg = 0;
- float temp=0;
- unsigned char humidity=0;
- bool sensorReady =false;
- //Verbindungen
- //MQTT
- WiFiClient espClient;
- PubSubClient client(espClient);
- //RCSwitch
- RCSwitch rcSwitch;
- //SiSensor
- Adafruit_Si7021 si_sensor;
- void setup_wifi() {
- delay(10);
- // We start by connecting to a WiFi network
- Serial.println();
- Serial.print("Connecting to ");
- Serial.println(ssid);
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println("");
- Serial.println("WiFi connected");
- Serial.println("IP address: ");
- Serial.println(WiFi.localIP());
- }
- void setup_rcSwitches(){
- rcSwitch = RCSwitch();
- rcSwitch.enableTransmit(4); //Verbindung über PIN 4
- //rcSwitch.setPulseLength(311);
- //rcSwitch.setProtocol(1); // protocol + pulseLength feste Werte für Ansteuerung
- }
- void setup_pins(){
- pinMode(RELAY_PIN1,OUTPUT);
- }
- void setup_sensor(){
- si_sensor = Adafruit_Si7021();
- sensorReady=si_sensor.begin();
- }
- void reconnect() {
- // Loop until we're reconnected
- while (!client.connected()) {
- Serial.print("Attempting MQTT connection...");
- // Attempt to connect
- if (client.connect("Klimabox", mqttUser, mqttPassword )) {
- Serial.println("connected");
- // Subscribe
- client.subscribe(humid_act_top);
- client.subscribe(outlet1_act_top);
- client.subscribe(outlet2_act_top);
- } else {
- Serial.print("failed, rc=");
- Serial.print(client.state());
- Serial.println(" try again in 5 seconds");
- // Wait 5 seconds before retrying
- delay(5000);
- }
- }
- }
- /**
- * Schaltet die gegebene Funksteckdose
- */
- void switch_outlet(int id,int newstate){
- Serial.println("Switch outlet");
- char* dest="00000";
- switch(id){
- case 1: //Steckdose 1
- dest="10000";
- break;
- case 2: //Steckdose 2
- dest="01000";
- break;
- }
- if( newstate){//ON
- rcSwitch.switchOn("00011",dest);
- }else{//Off
- rcSwitch.switchOff("00011",dest);
- }
- }
- /**
- * Schaltet den Luftbefeuchter
- */
- void switch_humidifier(int newstate){
- if(newstate>0){
- Serial.println("HumON");
- digitalWrite(RELAY_PIN1,HIGH);
- }else{
- Serial.println("HumOFF");
- digitalWrite(RELAY_PIN1,LOW);
- }
- }
- /**
- * Funktion zum Lesen der Temperatur
- */
- float readTemp(){
- return si_sensor.readTemperature();
- }
- /**
- * Funktion zum Lesen der Luftfeuchtigkeit
- */
- float readHumid(){
- return si_sensor.readHumidity();
- }
- void callback(char* topic, byte* message, unsigned int length) {
- String messageTemp;
- for (int i = 0; i < length; i++) {
- messageTemp += (char)message[i];
- }
- Serial.println();
- //Auf Topics reagieren
- if (String(topic) == String(outlet1_act_top)) {
- if(messageTemp == "1"){
- switch_outlet(1,1);
- }
- else if(messageTemp == "0"){
- switch_outlet(1,0);
- }
- }
- if (String(topic) == String(outlet2_act_top)) {
- if(messageTemp == "1"){
- switch_outlet(2,1);
- }
- else if(messageTemp == "0"){
- switch_outlet(2,0);
- }
- }
- if (String(topic) == String(humid_act_top)) {
- if(messageTemp == "1"){
- switch_humidifier(1);
- }
- else if(messageTemp == "0"){
- switch_humidifier(0);
- }
- }
- }
- void setup() {
- Serial.begin(115200);
- setup_wifi();
- client.setServer(mqttServer, mqttPort);
- client.setCallback(callback);
- setup_rcSwitches();
- setup_pins();
- }
- void loop() {
- if (!client.connected()) {
- reconnect();
- }
- client.loop();
- long now = millis();
- if ((now - lastMsg > UPDATE_CYCLE || now < lastMsg)&&sensorReady) {
- lastMsg = now;
- String help_s;
- //Temperatur updaten
- temp=readTemp();
- help_s=String(temp);
- client.publish(temp_sense_top, help_s.c_str());
- //Luftfeutchtigkeit updaten
- humidity=readHumid();
- help_s=String(humidity);
- client.publish(humid_sense_top, help_s.c_str());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement