Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2020
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <PubSubClient.h>
  3.  
  4. int gomb1 = D5; //button is connected to GPIO pin D1
  5. int gomb2 = D2;
  6. // Update these with values suitable for your network.
  7. const char* ssid = "";//put your wifi ssid here
  8. const char* password = "";//put your wifi password here.
  9. const char* mqtt_server = "";
  10.  
  11. WiFiClient espClient;
  12. PubSubClient client(espClient);
  13. long lastMsg = 0;
  14. char msg[50];
  15.  
  16. void setup_wifi() {
  17. delay(100);
  18. // We start by connecting to a WiFi network
  19. Serial.print("Connecting to ");
  20. Serial.println(ssid);
  21. WiFi.begin(ssid, password);
  22. while (WiFi.status() != WL_CONNECTED)
  23. {
  24. delay(500);
  25. Serial.print(".");
  26. }
  27. randomSeed(micros());
  28. Serial.println("");
  29. Serial.println("WiFi connected");
  30. Serial.println("IP address: ");
  31. Serial.println(WiFi.localIP());
  32. }
  33.  
  34. void callback(char* topic, byte* payload, unsigned int length)
  35. {
  36.  
  37. } //end callback
  38.  
  39. void reconnect() {
  40. // Loop until we're reconnected
  41. while (!client.connected())
  42. {
  43. Serial.print("Attempting MQTT connection...");
  44. // Create a random client ID
  45. String clientId = "ESP8266Client-";
  46. clientId += String(random(0xffff), HEX);
  47. // Attempt to connect
  48. //if you MQTT broker has clientID,username and password
  49. //please change following line to if (client.connect(clientId,userName,passWord))
  50. if (client.connect(clientId.c_str()))
  51. {
  52. Serial.println("connected");
  53. //once connected to MQTT broker, subscribe command if any
  54. client.subscribe("nyomogomb");
  55. } else {
  56. Serial.print("failed, rc=");
  57. Serial.print(client.state());
  58. Serial.println(" try again in 5 seconds");
  59. // Wait 5 seconds before retrying
  60. delay(5000);
  61. }
  62. }
  63. } //end reconnect()
  64.  
  65. void setup() {
  66. Serial.begin(115200);
  67. setup_wifi();
  68. client.setServer(mqtt_server, 1883);
  69. client.setCallback(callback);
  70. pinMode(gomb1,INPUT);
  71. pinMode(gomb2,INPUT);
  72. }
  73.  
  74. void loop() {
  75. if (!client.connected()) {
  76. reconnect();
  77. }
  78. client.loop();
  79. long now = millis();
  80. int status;
  81. //send message every 2 second
  82. if (now - lastMsg > 250) {
  83. lastMsg = now;
  84. status=digitalRead(gomb1);
  85. String msg="Gomb helyzete: ";
  86. if(status==HIGH )
  87. {
  88. msg= msg+ "Megnyomva";
  89. char message[58];
  90. msg.toCharArray(message,58);
  91. Serial.println(message);
  92. //publish sensor data to MQTT broker
  93. client.publish("nyomogomb1", message);
  94. }
  95. else
  96. {
  97. msg= msg+ "Nincs megnyomva";
  98. char message[58];
  99. msg.toCharArray(message,58);
  100. Serial.println(message);
  101. // ne küldje el a NEM nyomott állapotot MQTT-n
  102. // client.publish("nyomogomb1", message);
  103. }
  104. }
  105.  
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement