evenjc

ESP32-WiFi-MQTT-Publish

May 23rd, 2023
918
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <WiFi.h>
  2. #include <PubSubClient.h>
  3.  
  4. const char* ssid = "ssid";
  5. const char* password = "password";
  6.  
  7. const char* mqtt_server = "ip-adressen-til-mqtt";
  8.  
  9. WiFiClient espClient;
  10. PubSubClient client(espClient);
  11.  
  12. void setup() {
  13.   Serial.begin(115200);
  14.   WiFi.begin(ssid, password);
  15.  
  16.   while (WiFi.status() != WL_CONNECTED) {
  17.     delay(500);
  18.     Serial.print(".");
  19.   }
  20.  
  21.   Serial.println("");
  22.   Serial.println("WiFi connected");
  23.   Serial.println("IP address: ");
  24.   Serial.println(WiFi.localIP());
  25.  
  26.   client.setServer(mqtt_server, 1883);
  27. }
  28.  
  29. void loop() {
  30.   // Sender value til topic/topic og venter 10 sek.
  31.   client.publish("topic/topic", "value");
  32.   delay(10000);
  33. }
  34.  
Advertisement
Add Comment
Please, Sign In to add comment