Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. #include <WiFi.h>
  2. #include <PubSubClient.h>
  3.  
  4. const char* sand = "false";
  5.  
  6. // Replace the next variables with your SSID/Password combination
  7. const char* ssid = "Sde-Guest";
  8. const char* password = "";
  9.  
  10.  
  11. //MQTT USERNAME, PASSWORD AND TOPIC
  12. const char* mqtt_username = "admin";
  13. const char* mqtt_password = "ciscogang123";
  14. const char* clientID = "Alarm";
  15.  
  16. // Add your MQTT Broker IP address, example:
  17. const char* mqtt_server = "10.142.110.108";
  18.  
  19. WiFiClient espClient;
  20. PubSubClient client(espClient);
  21.  
  22. void reconnect() {
  23. // Loop until we're reconnected
  24. while (!client.connected()) {
  25. Serial.println("Attempting MQTT connection...");
  26. // Attempt to connect
  27. if (client.connect(clientID, mqtt_username, mqtt_password)) {
  28. Serial.println("Connected");
  29. sand = "true";
  30. // ... and subscribe to topic
  31. client.subscribe("test");
  32. } else {
  33. Serial.print("failed, rc=");
  34. Serial.print(client.state());
  35. Serial.println(" try again in 5 seconds");
  36. // Wait 5 seconds before retrying
  37. delay(5000);
  38. }
  39. }
  40. }
  41.  
  42.  
  43. void setup() {
  44. // put your setup code here, to run once:
  45. Serial.begin(115200);
  46. setup_wifi();
  47. client.setServer(mqtt_server, 1883);
  48. }
  49.  
  50. void setup_wifi() {
  51. delay(1000);
  52. // We start by connecting to a WiFi network
  53. Serial.println();
  54. Serial.print("Connecting to ");
  55. Serial.println(ssid);
  56.  
  57. WiFi.begin(ssid, password);
  58. delay (5000);
  59. Serial.println("");
  60. Serial.println("WiFi connected");
  61. Serial.println("IP address: ");
  62. Serial.println(WiFi.localIP());
  63. client.subscribe("test");
  64.  
  65. }
  66.  
  67. void loop() {
  68. // put your main code here, to run repeatedly:
  69. if (sand == "true"){
  70. client.publish("test", "Hello from ESP");
  71. }
  72. client.publish("test", "Hello from ESP");
  73.  
  74.  
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement