Advertisement
Guest User

Untitled

a guest
Jun 13th, 2018
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.54 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <PubSubClient.h>
  3.  
  4. #define input 5
  5. #define output 4
  6.  
  7. //Notwendige Daten zum Verbinden
  8. char* ssid = your_ssid;
  9. char* password = your_password;
  10. char* mqttServer = your_mqttserver;
  11. int mqttPort = 1883;
  12. char* mqttUser = your_User;
  13. char* mqttPassword = your_password;
  14. char* topic = your_status_detection_topic;
  15.  
  16. WiFiClient espClient;
  17. PubSubClient client(espClient);
  18.  
  19. int inputState;
  20. int lastState;
  21.  
  22. char message[50];
  23.  
  24. void setup_wifi() {
  25.  
  26.   delay(10);
  27.   // We start by connecting to a WiFi network
  28.   Serial.println();
  29.   Serial.print("Connecting to ");
  30.   Serial.println(ssid);
  31.  
  32.   WiFi.begin(ssid, password);
  33.  
  34.   while (WiFi.status() != WL_CONNECTED) {
  35.     delay(500);
  36.     Serial.print(".");
  37.   }
  38.  
  39.   randomSeed(micros());
  40.  
  41.   Serial.println("");
  42.   Serial.println("WiFi connected");
  43.   Serial.println("IP address: ");
  44.   Serial.println(WiFi.localIP());
  45. }
  46.  
  47. void callback(char* topic, byte* payload, unsigned int length) {
  48.   Serial.print("Message arrived [");
  49.   Serial.print(topic);
  50.   Serial.print("] ");
  51.   for (int i = 0; i < length; i++) {
  52.     Serial.print((char)payload[i]);
  53.   }
  54.   Serial.println();
  55.  
  56.   digitalWrite(output, HIGH);
  57.   delay(500);
  58.   digitalWrite(output, LOW);
  59. }
  60.  
  61. void reconnect() {
  62.   // Loop until we're reconnected
  63.   while (!client.connected()) {
  64.     Serial.print("Attempting MQTT connection...");
  65.     // Attempt to connect
  66.     if (client.connect("ESP32Client", mqttUser, mqttPassword)) {
  67.       Serial.println("connected");
  68.       // Once connected, publish an announcement...
  69.       client.subscribe("pc/power");
  70.     } else {
  71.       Serial.print("failed, rc=");
  72.       Serial.print(client.state());
  73.       Serial.println(" try again in 5 seconds");
  74.       // Wait 5 seconds before retrying
  75.       delay(5000);
  76.     }
  77.   }
  78. }
  79.  
  80. void setup() {
  81.   pinMode(BUILTIN_LED, OUTPUT);
  82.   pinMode(input, INPUT);
  83.   pinMode(output, OUTPUT);
  84.   Serial.begin(115200);
  85.   setup_wifi();
  86.   client.setServer(mqttServer, 1883);
  87.   client.setCallback(callback);
  88. }
  89.  
  90. void loop() {
  91.  
  92.   if (!client.connected()) {
  93.     reconnect();
  94.   }
  95.   client.loop();
  96.  
  97.   inputState = digitalRead(input);
  98.   if (inputState != lastState ) {
  99.     if (inputState == 1) {
  100.       client.publish(topic, "ON");
  101.       Serial.println("posted");
  102.       Serial.println(WiFi.status());
  103.       Serial.println(client.connected());
  104.     } else {
  105.       client.publish(topic, "OFF");
  106.       Serial.println("posted");
  107.       Serial.println(WiFi.status());
  108.       Serial.println(client.connected());
  109.     }
  110.  
  111.   }
  112.   lastState = inputState;
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement