Smart parking using iot

Hello,
I'm beginner in the field of iot
I'm trying to connect ultrasonic sensor with esp8266, Arduino and Mqtt.
This is my code

#include <NewPing.h>
#include <ESP8266WiFi.h>
#include<WiFiClient.h>
#include<PubSubClient.h>

void callback(chartopic,bytepayload,unsigned int length1);
void reconnect();
const charssid="xxxx ";
const char
password="xxxxx";
const int trigPin=2;
const int echoPin=0;
long duration;
int distance;

unsigned long lastMillis=0;
const char*mqtt_server="Mqtt broker";
int mqtt_port=1883;
WiFiClient espClient;

PubSubClient client(espClient);

void setup(){

pinMode(trigPin,OUTPUT);
pinMode(echoPin,INPUT);

Serial.begin(9600);
delay(10);
Serial.println();
Serial.println();
Serial.print("connecting to");
Serial.print("ssid");
WiFi.begin("ssid","Password");
while (WiFi.status() !=WL_CONNECTED){
delay(500);
Serial.print(".");
}

Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address");
Serial.println(WiFi.localIP());
delay(3000);

client.setServer("Mqtt broker",1883);
client.setCallback(callback);

reconnect();

}

void callback(char* topic, byte* payload, unsigned int length)
{
String str;
Serial.print("Message Arrived [i]");
Serial.print(topic);
Serial.println(".");
for(int i=0; i<length; i++)
{
str+=((char)payload[i]);
}
Serial.println(str);
}
void reconnect()
{
while(WiFi.status()!=WL_CONNECTED)
{
delay(50);
Serial.print(".");
}
while(!client.connected())
{
if(client.connect("test"))
{
Serial.println("Connected...");
client.subscribe("test");
}
else
{
Serial.print("failed, rc=");
Serial.println(client.state());
delay(100);
}
}
}
void loop(){

    digitalWrite(trigPin, LOW);
    delayMicroseconds(2);
    
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(trigPin, LOW);

duration= pulseIn(echoPin,HIGH);

distance=duration*0.034/2;

Serial.print("distance= ");
Serial.println("distance");

if(distance<=10){

Serial.print("parking is not available");
client.publish("test","!!Parking is not available");
delay(2000);

}
else{
Serial.println("PARKING AVAIALBLE");
client.publish("test","PARKING AVAILABLE");
delay(2000);
}

if(!client.connected()){
reconnect();
}
client.loop();
client.subscribe("test");
}

And my output is coming like this .........................

Hello, do yourself a favour and please read How to get the best out of this forum and modify your post accordingly (including code tags and necessary documentation of your ask).

When you’ve done the above, after formatting , you’ll see you have procedures with the same name .
If you format the code ( tools/format) and tidy it up so all your functions are in one place and not scattered between setup and loop, it will be easier to read and errors more obvious.

Have a look at how functions work , you have defined at least one function twice , you don’t call them with “void....”

Link

Change what you print.
Serial.print("*")
("+")
("'")
("#")
So you know where it hangs

It is hanging at connecting to WiFi part

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.