Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <HCSR04.h> //Ultrasonic
- #include <PubSubClient.h> //Mqtt
- #include <Wire.h>
- #include <ESP8266WiFi.h>
- #include "SH1106Wire.h" //Display
- UltraSonicDistanceSensor distanceSensor(4, 5); //Ultrasonic Pins
- SH1106Wire display(0x3c, D3, D5); //Display ADDRESS, SDA, SCL
- // Replace with your network details
- const char* ssid = "2.40 Euro pro MB";
- const char* password = "123geheim456!";
- const char* mqtt_server = "192.168.178.168";
- String IP;
- String cm;
- //dont know what this does
- WiFiClient espClient;
- PubSubClient client(espClient);
- // Web Server on port 80
- WiFiServer server(80);
- void setup () {
- Serial.begin(74880); // We initialize serial connection so that we could print values from sensor.
- //Set MQTT Server
- client.setServer(mqtt_server, 1883);
- // Display
- // Initialising the UI will init the display too.
- display.init();
- display.flipScreenVertically();
- display.setFont(ArialMT_Plain_10);
- // Display end
- //Connect to WiFi
- Serial.println();
- Serial.print("Connecting to ");
- Serial.println(ssid);
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println("");
- Serial.println("WiFi connected");
- // Starting the web server
- server.begin();
- Serial.println("Web server running. Waiting for the ESP IP...");
- delay(10000);
- // Printing the ESP IP address
- Serial.println(WiFi.localIP());
- //THIS is everytime NAN - Dont know how to solve this
- IP= ""+ WiFi.localIP();
- }
- //MQTT reconnect
- void reconnect()
- {
- // Loop until we're reconnected
- while (!client.connected())
- {
- Serial.print("Attempting MQTT connection...");
- // Create a random client ID
- String clientId = "ESP8266Client-";
- clientId += String(random(0xffff), HEX);
- // Attempt to connect
- //if you MQTT broker has clientID,username and password
- //please change following line to if (client.connect(clientId,userName,passWord))
- if (client.connect(clientId.c_str()))
- {
- Serial.println("connected");
- //once connected to MQTT broker, subscribe command if any
- //client.subscribe("OsoyooCommand");
- } else {
- Serial.print("failed, rc=");
- Serial.print(client.state());
- Serial.println(" try again in 5 seconds");
- // Wait 5 seconds before retrying
- delay(5000);
- }
- }
- }
- void loop () {
- if (!client.connected()) {
- reconnect();
- }
- //Copied, no clue why this cant be something simple like
- // client.publish("cm",String(distanceSensor.measureDistanceCm(), 2) );
- //but it works
- String msg="";
- char MsgDis[25];
- msg= distanceSensor.measureDistanceCm();
- msg.toCharArray(MsgDis,25);
- client.publish("cm", MsgDis);
- // Every 500 miliseconds, do a measurement using the sensor and print the distance in centimeters.
- Serial.println(distanceSensor.measureDistanceCm());
- delay(500);
- //Wirte on Display
- // clear the display
- display.clear();
- // draw
- display.setTextAlignment(TEXT_ALIGN_CENTER);
- display.drawString(64, 11, "IP: " + IP);
- display.drawString(64, 22, String(distanceSensor.measureDistanceCm(), 2) + " cm");
- // write the buffer to thaccess aBFRAGEN
- display.display();
- //End Display
- // Listenning for new clients
- WiFiClient client = server.available();
- if (client) {
- Serial.println("New client");
- // bolean to locate when the http request ends
- boolean blank_line = true;
- while (client.connected()) {
- if (client.available()) {
- char c = client.read();
- if (c == '\n' && blank_line) {
- //do HTML, what ever you need
- //client.println("<head></head><body></body>);
- client.println(distanceSensor.measureDistanceCm());
- break;
- }
- if (c == '\n') {
- // when starts reading a new line
- blank_line = true;
- }
- else if (c != '\r') {
- // when finds a character on the current line
- blank_line = false;
- }
- }
- }
- // closing the client connection
- delay(1);
- client.stop();
- Serial.println("Client disconnected.");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement