Advertisement
Guest User

Untitled

a guest
Nov 28th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.86 KB | None | 0 0
  1. #include <Arduino.h>
  2. #include <WiFi.h>
  3. #include <PubSubClient.h>
  4. #include <RCSwitch.h>
  5. #include <Adafruit_Si7021.h>
  6.  
  7. #define RELAY_PIN1 27
  8.  
  9. //Verbindungsinfos
  10. const char* ssid = "wintergarten4_0";
  11. const char* password = "schoener_wohnen";
  12. const char* mqttServer = "192.168.155.1";
  13. const int mqttPort = 1883;
  14. const char* mqttUser = "klima";
  15. const char* mqttPassword = "klimawandel";
  16.  
  17. //Mqtttopics
  18. const char* temp_sense_top="wintergarten/klima/sensors/temperature";
  19. const char* humid_sense_top="wintergarten/klima/sensors/humidity";
  20. const char* outlet1_act_top="wintergarten/klima/actuators/outlets/outlet1";
  21. const char* outlet2_act_top="wintergarten/klima/actuators/outlets/outlet2";
  22. const char* humid_act_top="wintergarten/klima/actuators/humidity/humidifier";
  23.  
  24. //Konstanten
  25. const int UPDATE_CYCLE=1000;//5*60000; //Zeitspanne für Updates in ms
  26.  
  27. //Variablen
  28. long lastMsg = 0;
  29. float temp=0;
  30. unsigned char humidity=0;
  31. bool sensorReady =false;
  32.  
  33. //Verbindungen
  34. //MQTT
  35. WiFiClient espClient;
  36. PubSubClient client(espClient);
  37. //RCSwitch
  38. RCSwitch rcSwitch;
  39. //SiSensor
  40. Adafruit_Si7021 si_sensor;
  41.  
  42. void setup_wifi() {
  43. delay(10);
  44. // We start by connecting to a WiFi network
  45. Serial.println();
  46. Serial.print("Connecting to ");
  47. Serial.println(ssid);
  48.  
  49. WiFi.begin(ssid, password);
  50.  
  51. while (WiFi.status() != WL_CONNECTED) {
  52. delay(500);
  53. Serial.print(".");
  54. }
  55.  
  56. Serial.println("");
  57. Serial.println("WiFi connected");
  58. Serial.println("IP address: ");
  59. Serial.println(WiFi.localIP());
  60. }
  61.  
  62. void setup_rcSwitches(){
  63. rcSwitch = RCSwitch();
  64. rcSwitch.enableTransmit(4); //Verbindung über PIN 4
  65. //rcSwitch.setPulseLength(311);
  66. //rcSwitch.setProtocol(1); // protocol + pulseLength feste Werte für Ansteuerung
  67. }
  68.  
  69. void setup_pins(){
  70. pinMode(RELAY_PIN1,OUTPUT);
  71. }
  72.  
  73. void setup_sensor(){
  74. si_sensor = Adafruit_Si7021();
  75. sensorReady=si_sensor.begin();
  76. }
  77. void reconnect() {
  78. // Loop until we're reconnected
  79. while (!client.connected()) {
  80. Serial.print("Attempting MQTT connection...");
  81. // Attempt to connect
  82. if (client.connect("Klimabox", mqttUser, mqttPassword )) {
  83. Serial.println("connected");
  84. // Subscribe
  85. client.subscribe(humid_act_top);
  86. client.subscribe(outlet1_act_top);
  87. client.subscribe(outlet2_act_top);
  88. } else {
  89. Serial.print("failed, rc=");
  90. Serial.print(client.state());
  91. Serial.println(" try again in 5 seconds");
  92. // Wait 5 seconds before retrying
  93. delay(5000);
  94. }
  95. }
  96. }
  97. /**
  98. * Schaltet die gegebene Funksteckdose
  99. */
  100. void switch_outlet(int id,int newstate){
  101. Serial.println("Switch outlet");
  102. char* dest="00000";
  103. switch(id){
  104. case 1: //Steckdose 1
  105. dest="10000";
  106. break;
  107. case 2: //Steckdose 2
  108. dest="01000";
  109. break;
  110. }
  111. if( newstate){//ON
  112. rcSwitch.switchOn("00011",dest);
  113. }else{//Off
  114. rcSwitch.switchOff("00011",dest);
  115. }
  116. }
  117. /**
  118. * Schaltet den Luftbefeuchter
  119. */
  120. void switch_humidifier(int newstate){
  121. if(newstate>0){
  122. Serial.println("HumON");
  123. digitalWrite(RELAY_PIN1,HIGH);
  124. }else{
  125. Serial.println("HumOFF");
  126. digitalWrite(RELAY_PIN1,LOW);
  127. }
  128. }
  129. /**
  130. * Funktion zum Lesen der Temperatur
  131. */
  132. float readTemp(){
  133. return si_sensor.readTemperature();
  134. }
  135. /**
  136. * Funktion zum Lesen der Luftfeuchtigkeit
  137. */
  138. float readHumid(){
  139. return si_sensor.readHumidity();
  140. }
  141.  
  142. void callback(char* topic, byte* message, unsigned int length) {
  143. String messageTemp;
  144.  
  145. for (int i = 0; i < length; i++) {
  146. messageTemp += (char)message[i];
  147. }
  148. Serial.println();
  149.  
  150. //Auf Topics reagieren
  151. if (String(topic) == String(outlet1_act_top)) {
  152. if(messageTemp == "1"){
  153. switch_outlet(1,1);
  154. }
  155. else if(messageTemp == "0"){
  156. switch_outlet(1,0);
  157. }
  158. }
  159. if (String(topic) == String(outlet2_act_top)) {
  160. if(messageTemp == "1"){
  161. switch_outlet(2,1);
  162. }
  163. else if(messageTemp == "0"){
  164. switch_outlet(2,0);
  165. }
  166. }
  167. if (String(topic) == String(humid_act_top)) {
  168. if(messageTemp == "1"){
  169. switch_humidifier(1);
  170. }
  171. else if(messageTemp == "0"){
  172. switch_humidifier(0);
  173. }
  174. }
  175. }
  176.  
  177. void setup() {
  178. Serial.begin(115200);
  179. setup_wifi();
  180. client.setServer(mqttServer, mqttPort);
  181. client.setCallback(callback);
  182. setup_rcSwitches();
  183. setup_pins();
  184. }
  185.  
  186. void loop() {
  187. if (!client.connected()) {
  188. reconnect();
  189. }
  190. client.loop();
  191.  
  192. long now = millis();
  193. if ((now - lastMsg > UPDATE_CYCLE || now < lastMsg)&&sensorReady) {
  194. lastMsg = now;
  195. String help_s;
  196. //Temperatur updaten
  197. temp=readTemp();
  198. help_s=String(temp);
  199. client.publish(temp_sense_top, help_s.c_str());
  200.  
  201. //Luftfeutchtigkeit updaten
  202. humidity=readHumid();
  203. help_s=String(humidity);
  204. client.publish(humid_sense_top, help_s.c_str());
  205. }
  206. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement