MQTT + HC-SR04 + Arduino UNO


I am trying to set a water level reading with a connection to an MQTT broker.
If I test the code in 2 different files everything works, mqtt connection, range finder.
If I put these files together this does not work.

thanks !

kind regards

2 files together:

#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>
// Function prototypes
void subscribeReceive(char* topic, byte* payload, unsigned int length);

// Set your MAC address and IP address here
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 160);

// Make sure to leave out the http and slashes!
const char* server = "";

// Ethernet and MQTT related objects
EthernetClient ethClient;
PubSubClient mqttClient(ethClient);

//Begin Water Measuring
 int trigPin = 12;    // Trigger
 int echoPin = 11;    // Echo
 long duration, cm, height, width, diameter, liters, procent;
//End Water Measuring

void setup()
 Serial.begin (9600);

//Begin Water Measuring
 //Define inputs and outputs
   pinMode(trigPin, OUTPUT);
   pinMode(echoPin, INPUT);
//End Water Measuring 
 // Start the ethernet connection
 Ethernet.begin(mac, ip);              
 // Ethernet takes some time to boot!

 // Set the MQTT server to the server stated above ^
 mqttClient.setServer(server, 1883);   

 // Attempt to connect to the server with the ID "myClientID"
 if (mqttClient.connect("myClientID")) 
   Serial.println("Connection has been established, well done");

   // Establish the subscribe event
   Serial.println("Looks like the server connection failed...");


void loop()

//Begin Water Measuring
 // The sensor is triggered by a HIGH pulse of 10 or more microseconds.
 // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
   digitalWrite(trigPin, LOW);
   digitalWrite(trigPin, HIGH);
   digitalWrite(trigPin, LOW);

 // Read the signal from the sensor: a HIGH pulse whose
 // duration is the time (in microseconds) from the sending
 // of the ping to the reception of its echo off of an object.
   pinMode(echoPin, INPUT);
   duration = pulseIn(echoPin, HIGH);

 // Convert the time into a distance
   cm = (duration/2) / 29.1;     // Divide by 29.1 or multiply by 0.0343

 //Message String
   char distString[20];
   dtostrf(cm, 4, 0, distString);
//End Water Measuring

//MQTT Begin  

 // Ensure that we are subscribed to the topic

 // Attempt to publish a value to the topic
 if(mqttClient.publish("WaterLeveling", distString))
   Serial.println("Publish message success");
   Serial.println("Could not send message :(");
// This is needed at the top of the loop!
//  mqttClient.loop();
//MQTT End

 // Dont overload the server!

void subscribeReceive(char* topic, byte* payload, unsigned int length)
 // Print the topic
 Serial.print("Topic: ");

 // Print the message
 Serial.print("Message: ");
 for(int i = 0; i < length; i ++)

 // Print a newline


Waterleveling_Measure.ino (3.18 KB)

I can’t see your code.
Please post it in code tags.

done !
thanks !

You’re using pins 11 and 12 for the ultrasound.
Doesn’t the SPI want those pins?

You're using pins 11 and 12 for the ultrasound.
Doesn't the SPI want those pins?

If i test thr sketch with only the measurement section it works ...

Which tends to add credence to my observation.

Wich pins should i use ?

Which tends to add credence to my observation.

i used pins 8 & 9. It works !

Many thanks !!!