Advertisement
Guest User

Untitled

a guest
Oct 12th, 2017
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. #include <PubSubClient.h>
  2. #include <ESP8266WiFi.h>
  3.  
  4. const char *ssid = "ssid";
  5. const char *pass = "password";
  6.  
  7. const char* mqtt_hostname = "hostname";
  8. const char* mqtt_username = "username";
  9. const char* mqtt_password = "password";
  10. const char* mqtt_topic = "topic";
  11.  
  12. String clientName;
  13. int teller, time_1;
  14.  
  15. WiFiClient wifiClient;
  16. PubSubClient client(wifiClient);
  17.  
  18. bool connected = false;
  19.  
  20. void setup() {
  21. Serial.begin(115200);
  22. if (WiFi.status() != WL_CONNECTED) {
  23. Serial.print("Connecting to ");
  24. Serial.print(ssid);
  25. Serial.println("");
  26. WiFi.begin(ssid, pass);
  27. while (WiFi.status() != WL_CONNECTED) {
  28. delay(500);
  29. Serial.print(".");
  30. };
  31. Serial.println("WiFi connected");
  32. }
  33.  
  34. // IPAddress serverIp = IPAddress(192,168,1,1);
  35. client.setServer(hostname, 1883);
  36. if (!client.connected()) {
  37. String clientId = "ESP8266Client-";
  38. clientId += String(random(0xffff), HEX);
  39. if (client.connect(clientId, mqtt_username, mqtt_password)) {
  40. connected = true;
  41. Serial.println("Done");
  42. }
  43. }
  44. }
  45.  
  46. void loop() {
  47. if(connected) {
  48. teller++;
  49. time_1 = millis();
  50. if (client.publish(topic, ""+teller)) {
  51. Serial.println((millis() - time_1));
  52. } else {
  53. Serial.println("failed");
  54. }
  55. }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement