Advertisement
Guest User

Untitled

a guest
Apr 10th, 2020
308
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <HCSR04.h> //Ultrasonic
  2. #include <PubSubClient.h> //Mqtt
  3. #include <Wire.h>
  4. #include <ESP8266WiFi.h>
  5. #include "SH1106Wire.h"   //Display
  6.  
  7. UltraSonicDistanceSensor distanceSensor(4, 5);  //Ultrasonic Pins
  8. SH1106Wire display(0x3c, D3, D5);     //Display ADDRESS, SDA, SCL
  9.  
  10. // Replace with your network details
  11. const char* ssid = "2.40 Euro pro MB";
  12. const char* password = "123geheim456!";
  13. const char* mqtt_server = "192.168.178.168";
  14. String IP;
  15. String cm;
  16.  
  17. //dont know what this does
  18. WiFiClient espClient;
  19. PubSubClient client(espClient);
  20.  
  21. // Web Server on port 80
  22. WiFiServer server(80);
  23.  
  24.  
  25. void setup () {
  26.   Serial.begin(74880);  // We initialize serial connection so that we could print values from sensor.
  27.  
  28.   //Set MQTT Server
  29.   client.setServer(mqtt_server, 1883);
  30.  
  31.   // Display
  32.   // Initialising the UI will init the display too.
  33.   display.init();
  34.   display.flipScreenVertically();
  35.   display.setFont(ArialMT_Plain_10);
  36.   // Display end
  37.  
  38.  
  39.   //Connect to WiFi
  40.   Serial.println();
  41.   Serial.print("Connecting to ");
  42.   Serial.println(ssid);
  43.   WiFi.begin(ssid, password);
  44.   while (WiFi.status() != WL_CONNECTED) {
  45.     delay(500);
  46.     Serial.print(".");
  47.   }
  48.   Serial.println("");
  49.   Serial.println("WiFi connected");
  50.  
  51.   // Starting the web server
  52.   server.begin();
  53.   Serial.println("Web server running. Waiting for the ESP IP...");
  54.   delay(10000);
  55.  
  56.   // Printing the ESP IP address
  57.   Serial.println(WiFi.localIP());
  58.   //THIS is everytime NAN - Dont know how to solve this
  59.   IP= ""+ WiFi.localIP();
  60. }
  61.  
  62.  
  63. //MQTT reconnect
  64. void reconnect()
  65. {
  66.   // Loop until we're reconnected
  67.   while (!client.connected())
  68.   {
  69.     Serial.print("Attempting MQTT connection...");
  70.     // Create a random client ID
  71.     String clientId = "ESP8266Client-";
  72.     clientId += String(random(0xffff), HEX);
  73.     // Attempt to connect
  74.     //if you MQTT broker has clientID,username and password
  75.     //please change following line to    if (client.connect(clientId,userName,passWord))
  76.     if (client.connect(clientId.c_str()))
  77.     {
  78.       Serial.println("connected");
  79.      //once connected to MQTT broker, subscribe command if any
  80.       //client.subscribe("OsoyooCommand");
  81.     } else {
  82.       Serial.print("failed, rc=");
  83.       Serial.print(client.state());
  84.       Serial.println(" try again in 5 seconds");
  85.       // Wait 5 seconds before retrying
  86.       delay(5000);
  87.     }
  88.   }
  89. }
  90.  
  91.  
  92.  
  93. void loop () {
  94.    if (!client.connected()) {
  95.     reconnect();
  96.   }
  97.  
  98.  
  99.       //Copied, no clue why this cant be something simple like
  100.       // client.publish("cm",String(distanceSensor.measureDistanceCm(), 2) );
  101.       //but it works
  102.      String msg="";
  103.      char MsgDis[25];    
  104.      msg= distanceSensor.measureDistanceCm();
  105.      msg.toCharArray(MsgDis,25);
  106.      client.publish("cm", MsgDis);
  107.  
  108.  
  109.   // Every 500 miliseconds, do a measurement using the sensor and print the distance in centimeters.
  110.   Serial.println(distanceSensor.measureDistanceCm());
  111.  
  112.   delay(500);
  113.  
  114.  
  115.   //Wirte on Display
  116.   // clear the display
  117.   display.clear();
  118.  
  119.   // draw
  120.   display.setTextAlignment(TEXT_ALIGN_CENTER);
  121.   display.drawString(64, 11, "IP: " + IP);
  122.   display.drawString(64, 22, String(distanceSensor.measureDistanceCm(), 2) + " cm");
  123.  
  124.   // write the buffer to thaccess aBFRAGEN
  125.   display.display();
  126.   //End Display
  127.  
  128.   // Listenning for new clients
  129.   WiFiClient client = server.available();
  130.  
  131.   if (client) {
  132.     Serial.println("New client");
  133.     // bolean to locate when the http request ends
  134.     boolean blank_line = true;
  135.     while (client.connected()) {
  136.       if (client.available()) {
  137.         char c = client.read();
  138.  
  139.         if (c == '\n' && blank_line) {
  140.           //do HTML, what ever you need
  141.           //client.println("<head></head><body></body>);
  142.           client.println(distanceSensor.measureDistanceCm());
  143.           break;
  144.         }
  145.         if (c == '\n') {
  146.           // when starts reading a new line
  147.           blank_line = true;
  148.         }
  149.         else if (c != '\r') {
  150.           // when finds a character on the current line
  151.           blank_line = false;
  152.         }
  153.       }
  154.     }
  155.     // closing the client connection
  156.     delay(1);
  157.     client.stop();
  158.     Serial.println("Client disconnected.");
  159.   }
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement