Advertisement
Guest User

Untitled

a guest
May 18th, 2017
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <PubSubClient.h>
  3.  
  4. const char* ssid = "Gortz";
  5. const char* password = "password";
  6.  
  7. const char* mqtt_server = "gortz.org";
  8. const char* mqtt_username = "test";
  9. const char* mqtt_password = "test";
  10. char mqtt_pub_topic[50];
  11. int mqtt_pub_interval = 10000;
  12. byte mac[6];
  13.  
  14. WiFiClient espClient;
  15. PubSubClient client(espClient);
  16.  
  17. long lastMsg = 0;
  18. char sensorValueMeasurement[150];
  19. String myMacAddress;
  20.  
  21. const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
  22.  
  23. int sensorValue = 0; // value read from the pot
  24. int outputValue = 0;
  25.  
  26. void setup_wifi() {
  27. delay(100);
  28. // We start by connecting to a WiFi network
  29. Serial.print("Connecting to ");
  30. Serial.println(ssid);
  31. WiFi.begin(ssid, password);
  32. while (WiFi.status() != WL_CONNECTED)
  33. {
  34. delay(500);
  35. Serial.print(".");
  36. }
  37. randomSeed(micros());
  38. Serial.println("");
  39. Serial.println("WiFi connected");
  40. Serial.println("IP address: ");
  41. Serial.println(WiFi.localIP());
  42. WiFi.macAddress(mac);
  43. myMacAddress = mac2String(mac);
  44. sprintf (mqtt_pub_topic, "sensors/home/gortz/nodemcu/%s/sensors", myMacAddress.c_str());
  45. }
  46.  
  47. String mac2String(byte ar[]){
  48. String s;
  49. for (byte i = 0; i < 6; ++i)
  50. {
  51. char buf[3];
  52. sprintf(buf, "%2X", ar[i]);
  53. s = buf+s;
  54. }
  55. s.toLowerCase();
  56. return s;
  57. }
  58.  
  59. void reconnect() {
  60. // Loop until we're reconnected
  61. while (!client.connected())
  62. {
  63. Serial.print("Attempting MQTT connection...");
  64. // Create a random client ID
  65. String clientId = "ESP8266Client-";
  66. clientId += String(random(0xffff), HEX);
  67. // Attempt to connect
  68. //if you MQTT broker has clientID,username and password
  69. //please change following line to if (client.connect(clientId,userName,passWord))
  70. if (client.connect(clientId.c_str(),mqtt_username,mqtt_password))
  71. {
  72. Serial.println("connected");
  73. } else {
  74. Serial.print("failed, rc=");
  75. Serial.print(client.state());
  76. Serial.println(" try again in 5 seconds");
  77. // Wait 6 seconds before retrying
  78. delay(6000);
  79. }
  80. }
  81. }
  82.  
  83. void setup() {
  84. Serial.begin(115200);
  85. setup_wifi();
  86. client.setServer(mqtt_server, 1883);
  87. Serial.print("Connected to mqtt");
  88. }
  89.  
  90. void loop() {
  91. if (!client.connected()) {
  92. reconnect();
  93. }
  94. sensorValue = analogRead(analogInPin);
  95. outputValue = map(sensorValue, 0, 512, 0, 100);
  96.  
  97. sprintf (sensorValueMeasurement, "[{\"bn\" :\" %s \"}, {\"u\" : \"tellstick;soil_moisture\", \"v\" : %i}]",myMacAddress.c_str(),outputValue);
  98. client.publish(mqtt_pub_topic, sensorValueMeasurement);
  99. delay(mqtt_pub_interval);
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement