Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2020
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.11 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <PubSubClient.h>
  3.  
  4. #define pinTrigger 0 // D3
  5. #define pinEcho 5 //D1
  6.  
  7. // Parametri network //
  8. const char* ssid = " SID RETE WIFI";
  9. const char* password = "PASSWD RETE WIFI";
  10.  
  11. // Parametri mosquitto //
  12. const char* mqtt_server = "IP MOSQUITTO";
  13. const char* mqtt_user = "USER_MOSQUITTO";
  14. const char* mqtt_password = "PASSWORD MOSQUITTO";
  15.  
  16. long durata, cm;
  17. char cmstr[16];
  18.  
  19. WiFiClient espClient;
  20. PubSubClient client(espClient);
  21. long lastMsg = 0;
  22. char msg[50];
  23. int value = 0;
  24.  
  25. void setup_wifi() {
  26.  
  27. delay(10);
  28. // Connessione alla rete
  29. Serial.println();
  30. Serial.print("Connettendo a ");
  31. Serial.println(ssid);
  32.  
  33. WiFi.begin(ssid, password);
  34.  
  35. while (WiFi.status() != WL_CONNECTED) {
  36. delay(500);
  37. Serial.print(".");
  38. }
  39.  
  40. randomSeed(micros());
  41.  
  42. Serial.println("");
  43. Serial.println("WiFi connesso");
  44. Serial.println("Indirizzo IP : ");
  45. Serial.println(WiFi.localIP());
  46. }
  47.  
  48. void callback(char* topic, byte* payload, unsigned int length) {
  49. Serial.print("Message arrived [");
  50. Serial.print(topic);
  51. Serial.print("] ");
  52. for (int i = 0; i < length; i++) {
  53. Serial.print((char)payload[i]);
  54. }
  55. Serial.println();
  56.  
  57. }
  58.  
  59. void reconnect() {
  60. // Loop until we're reconnected
  61. while (!client.connected()) {
  62. Serial.print("Attempting MQTT connection...");
  63. // Create a random client ID
  64. String clientId = "ESP8266Client-";
  65. clientId += String(random(0xffff), HEX);
  66. // Attempt to connect
  67. if (client.connect(clientId.c_str(),mqtt_user,mqtt_password)) {
  68. Serial.println("connected");
  69. // Once connected, publish an announcement...
  70. client.publish("stufaTopic", "Inizio");
  71. // ... and resubscribe
  72. client.subscribe("inTopic");
  73. } else {
  74. Serial.print("failed, rc=");
  75. Serial.print(client.state());
  76. Serial.println(" try again in 5 seconds");
  77. // Wait 5 seconds before retrying
  78. delay(5000);
  79. }
  80. }
  81. }
  82.  
  83. void setup() {
  84.  
  85. setup_wifi();
  86.  
  87. client.setServer(mqtt_server, 1883);
  88. client.setCallback(callback);
  89.  
  90. pinMode(pinTrigger, OUTPUT); // Sets the trigPin as an Output
  91. pinMode(pinEcho, INPUT); // Sets the echoPin as an Input
  92.  
  93. Serial.begin(115200);
  94.  
  95.  
  96. }
  97.  
  98. void loop() {
  99.  
  100. if (!client.connected()) {
  101. reconnect();
  102. }
  103. client.loop();
  104.  
  105. long now = millis();
  106. if (now - lastMsg > 5000) {
  107. lastMsg = now;
  108.  
  109. // Il ping e' triggerato da un HIGH di 2 o piu' microsec
  110. // Precediamo con un LOW corto per avere un HIGH pulito
  111.  
  112. digitalWrite(pinTrigger, LOW);
  113. delayMicroseconds(2);
  114.  
  115. digitalWrite(pinTrigger, HIGH);
  116. delayMicroseconds(10);
  117. digitalWrite(pinTrigger, LOW);
  118.  
  119. durata = pulseIn(pinEcho, HIGH);
  120. cm = microsecToCm(durata);
  121.  
  122. Serial.print("Durata : ");
  123. Serial.println(durata);
  124. Serial.print("Cm : ");
  125. Serial.println(cm);
  126.  
  127.  
  128. itoa(cm, cmstr, 10);
  129.  
  130. Serial.print("Publish message: ");
  131. Serial.println(cmstr);
  132. client.publish("stufaTopic", cmstr);
  133. }
  134. }
  135.  
  136. long microsecToCm (long microsec)
  137. { return microsec / 29 / 2 ; }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement