Hi,
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 = "192.168.1.16";
// 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!
delay(3000);
// 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
mqttClient.setCallback(subscribeReceive);
}
else
{
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);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
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
Serial.print(cm);
Serial.print("cm");
Serial.println();
//Message String
char distString[20];
dtostrf(cm, 4, 0, distString);
//End Water Measuring
//MQTT Begin
// Ensure that we are subscribed to the topic
mqttClient.subscribe("WaterLeveling");
// Attempt to publish a value to the topic
if(mqttClient.publish("WaterLeveling", distString))
{
Serial.println("Publish message success");
}
else
{
Serial.println("Could not send message :(");
}
// This is needed at the top of the loop!
// mqttClient.loop();
//MQTT End
// Dont overload the server!
delay(4000);
}
void subscribeReceive(char* topic, byte* payload, unsigned int length)
{
// Print the topic
Serial.print("Topic: ");
Serial.println(topic);
// Print the message
Serial.print("Message: ");
for(int i = 0; i < length; i ++)
{
Serial.print(char(payload[i]));
}
// Print a newline
Serial.println("");
}
Waterleveling_Measure.ino (3.18 KB)