HC-SR04 not working correctly with MQTT code in it

hello I am trying check to pass values coming from HC-SR04 ultrasonic sensor for distance and send those values to MQTT broker Mosquitto.

issue : i have 2 sketches one which is plain without publishing to MQTT , this is working perfectly fine and issuing correct distances on serial COM5, but when i add code relevant for MQTT , the sensor is generating 0 values. Can some one please help , I dont get any compile errors. can someone please validate.

Sketch 1 : plain without MQTT publish

//define pins numbers

const int trigPin = 10;
const int echoPin = 11;

//define variables
unsigned long duration;
int distance;

void setup() {
// put your setup code here, to run once:
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(13, OUTPUT);

Serial.begin(57600);
}

void sensor(){

digitalWrite(trigPin, LOW);
delayMicroseconds(5);

digitalWrite(trigPin, HIGH);
delayMicroseconds(20);

duration = pulseIn(echoPin, HIGH);

distance = duration*0.034/2;
if (distance > 10)
{Serial.println(“Empty”);
Serial.println(distance);
digitalWrite(13, LOW);
}
else{
Serial.println(“Full”);
Serial.println(distance);
digitalWrite(13, HIGH);
}
}
void loop() {
// put your main code here, to run repeatedly:
sensor();
}

Sketch -2 , same code above with MQTT publish

#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>

//define pins numbers

const int trigPin = 10;
const int echoPin = 11;

//define variables
unsigned long duration;
int distance;

// Update these with values suitable for your network.
byte mac = { 0x00, 0xAD, 0xfe, 0x41, 0x42, 0x43 };
IPAddress ip(192, 168, 1, 110);
IPAddress server(192, 168, 1, 11);

void callback(char* topic, byte* payload, unsigned int length) {
Serial.print(“Message arrived [”);
Serial.print(topic);
Serial.print("] ");
for (int i=0;i<length;i++) {
Serial.print((char)payload*);*

  • }*
  • Serial.println();*
    }

EthernetClient ethClient;
PubSubClient mqttClient(ethClient);

void reconnect() {

  • // Loop until we’re reconnected*
  • while (!mqttClient.connected()) {*
  • Serial.print(“Attempting MQTT connection…”);*
  • // Attempt to connect*
  • if (mqttClient.connect(“arduinoClient”)) {*
  • Serial.println(“connected”);*
  • // Once connected, publish an announcement…*
  • mqttClient.publish(“outTopic”,“publish sensor values”);*
  • // … and resubscribe*
  • mqttClient.subscribe(“inTopic”);*
  • } else {*
  • Serial.print(“failed, rc=”);*
  • Serial.print(mqttClient.state());*
  • Serial.println(" try again in 5 seconds");*
  • // Wait 5 seconds before retrying*
  • delay(5000);*
  • }*
  • }*
    }
    void setup()
    {

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

  • Serial.begin(57600);*

  • mqttClient.setServer(server, 1883);*

  • mqttClient.setCallback(callback);*

  • Ethernet.begin(mac, ip);*

  • // Allow the hardware to sort itself out*

  • delay(2000);*
    }
    void sensor(){
    digitalWrite(trigPin, LOW);
    delayMicroseconds(5);
    digitalWrite(trigPin, HIGH);
    delayMicroseconds(20);
    duration = pulseIn(echoPin, HIGH);
    distance = duration*0.034/2;
    if (distance > 10)
    {Serial.println(“Empty”);
    Serial.println(mqttClient.state());
    mqttClient.publish(“loc/a”,“Empty”);
    Serial.println(distance);
    digitalWrite(13, LOW);
    }
    else{
    Serial.println(“Full”);
    Serial.println(mqttClient.state());
    mqttClient.publish(“loc/a”,“Full”);
    Serial.println(distance);
    digitalWrite(13, HIGH);
    }
    }
    void loop(){

  • if(!mqttClient.connected())*

  • reconnect();*

  • sensor();*

mqttClient.loop();
}

How does the Arduino communicate with the Ethernet shield? That is, which pins does it use? Can you use those pins for other purposes at the same time? (Of course not!)

my bad Thank You i changed the pins to a new pair for trigger and echo 8 and 9.. it worked fine now .