Publishing a value to Mosquitto

Hey everyone

I am a newbie working with the Particle and have managed to publish a series of messages to mosquito but actually need the values of the sensor not just a message …some help would be greatly appreciated

the code

#include "MQTT/MQTT.h"
#include "math.h"

const int ledPin1 = D1 ;
const int ledPin2 = D2 ;
const int analogInPin1 = A0;
const int analogInPin2 = A1;

#define ThermisterPin 2
char message_buff[100];
char* tempC;

double Thermistor(int RawADC) {
double Temp;
Temp = log(12000.0 * ((1024.0 / RawADC - 1)));
Temp = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * Temp * Temp )) * Temp );
Temp = Temp -273.15;//Convert Kelvin to Celcius
//Temp = (Temp * 9.0) / 5.0 + 32.0;//Convert Celcius to Fahrenheit
return Temp;
}
//*******************//

//*******************//
int sensorValue = 0;
int outputValue = 0;
int ledState = LOW;
int ledState2 = LOW;

void callback(char* topic, byte* payload, unsigned int length);

// if want to use IP address,
 byte server[] = { 192,168, 1,108 };
 MQTT client(server, 1883, callback);



// recieve message
void callback(char* topic, byte* payload, unsigned int length) {
    char p[length + 1];
    memcpy(p, payload, length);
    p[length] = NULL;
    String message(p);

    if (message.equals("RED"))    
        RGB.color(255, 0, 0);
    else if (message.equals("GREEN"))    
        RGB.color(0, 255, 0);
    else if (message.equals("BLUE"))    
        RGB.color(0, 0, 255);
    else    
        RGB.color(255, 255, 255);
    delay(1000);
}

//**********************//

//*********************//


void setup() {
    
    RGB.control(true);
    Serial.begin(9600);
    // connect to the server
    client.connect("sparkclient");

    // publish/subscribe
    if (client.isConnected()) {
        client.publish("outTopic/message","hello world");
        client.subscribe("inTopic/message");
    }
    
    pinMode(ledPin1, OUTPUT);
    pinMode(ledPin2, OUTPUT);
    
   
}

void loop() {
    
    Serial.println(int(Thermistor(analogRead(0))));//display Fahrenheit
    delay(1000);
    float t1 = analogRead(analogInPin1);
    
    sensorValue = analogRead(analogInPin1);
    outputValue = map(sensorValue, 0, 5000, 0, 255);
    Serial.println(sensorValue);
    
    if (sensorValue < 3000) {
        digitalWrite (ledPin2, HIGH);
        digitalWrite (ledPin1, LOW);
        client.publish("mymosquitto/Particle", "Larry Happy");        

        delay(3000);
    }
    else if (sensorValue > 3000) {
        digitalWrite (ledPin1, HIGH);
        digitalWrite (ledPin2, LOW);
        client.publish("mymosquitto/Particle", "Larry needs Water");
        
        delay(3000);
    }

    if (client.isConnected())
    Serial.println("connected");
        client.loop();
        
}
1 Like

What have you tried and isn’t working?

sprintf() would be a keyword to look up (in standard C/C++ manuals/tutorials)

Hey Guys below is the code Im using for my Arduino and it is publishing via an Ethernet card .when I paste the formulae and modify it to collect the values I get an error

if (!client.connected())

  tempC = dtostrf(((((analogRead(tempPinIn) * .5) / 1023) - 0.5) * 100), 5, 2, message_buffer);
  if (millis() > (time + 6000)) {
    time = millis();
    client.publish("mymosquitto/arduino/temperature2", tempC);
    Serial.print(tempC);
  }
  //  tempC1 = dtostrf(((((analogRead(tempPinIn1) * 5.0) / 1024) - 0.5) * 100), 5, 2, message_buffer);
  //  if (millis() > (time + 7000)) {
  //    time = millis();
  //    client.publish("mymosquitto/arduino/soilsensor", tempC1);
  //    Serial.print(tempC1);
  //  }

  tempC2 = dtostrf(((((analogRead(tempPinIn1) * 10.0) / 1024) - 0.5) * 100), 5, 2, message_buffer);
  Serial.print(" %\t");
  if (millis() > (time + 5000)) {
    time = millis();
    client.publish("mymosquitto/arduino/lightsensor", tempC2);
    Serial.print(tempC2);

  }
  client.loop();

  //**********************//
  if (!client.connected()) {
    reconnect();

  }

  client.loop();

  //check if 5 seconds has elapsed since the last time we read the sensors.
  if (millis() > readTime + 20000) {
    sensorRead();
  }

}

void sensorRead() {
  readTime = millis();

  float h1 = dht1.readHumidity();
  float h2 = dht2.readHumidity();
 // float h3 = dht3.readHumidity();

  float t1 = dht1.readTemperature();
  float t2 = dht2.readTemperature();
//  float t3 = dht3.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  float f1 = dht1.readTemperature(true);
  float f2 = dht2.readTemperature(true);
//  float f3 = dht3.readTemperature(true);
  // Check if any reads failed and exit early (to try again).
  if (isnan(h1) || isnan(t1) || isnan(f1)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }



  char buffer[10];
  dtostrf(t1, 0, 0, buffer);
  client.publish("mymosquitto/arduino/temperature1", buffer);
  Serial.println(buffer);
  dtostrf(h1, 0, 0, buffer);
  client.publish("mymosquitto/arduino/humidity1", buffer);

  client.publish("mymosquitto/arduino/lightsensor", tempC2);

  //client.publish("inTopic/humidity",sprintf(buf, "%f", h));
  Serial.print("Humidity1: ");
  Serial.print(h1);
  Serial.print(" %\t");
  Serial.print("Temperature1: ");
  Serial.print(t1);
  Serial.print(" *C ");
  delay(1000);
  //*************************//
  if (isnan(h2) || isnan(t2) || isnan(f2)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  // Compute heat index in Fahrenheit (the default)
  //  float hif = dht.computeHeatIndex(f2, h2);
  // Compute heat index in Celsius (isFahreheit = false)
  //  float hic = dht.computeHeatIndex(t2, h2, false);

  //  char buffer[10];
  dtostrf(t2, 0, 0, buffer);
  client.publish("mymosquitto/arduino/temperature2", buffer);
  Serial.println(buffer);
  dtostrf(h2, 0, 0, buffer);
  client.publish("mymosquitto/arduino/humidity2", buffer);

  Serial.print("Humidity2: ");
  Serial.print(h2);
  Serial.print(" %\t");
  Serial.print("Temperature2: ");
  Serial.print(t2);
  Serial.print(" *C ");
  delay(1000);
  //**************************//
 // if (isnan(h3) || isnan(t3) || isnan(f3)) {
 //   Serial.println("Failed to read from DHT sensor!");
 //   return;
//  }


  //char buffer[10];
//  dtostrf(t3, 0, 0, buffer);
//  client.publish("mymosquitto/arduino/temperature3", buffer);
//  Serial.println(buffer);
//  dtostrf(h3, 0, 0, buffer);
//  client.publish("mymosquitto/arduino/humidity3", buffer);

  //client.publish("inTopic/humidity",sprintf(buf, "%f", h3));
//  Serial.print("Humidity3: ");
//  Serial.print(h3);
 // Serial.print(" %\t");
 // Serial.print("Temperature3: ");
//  Serial.print(t3);
//  Serial.print(" *C ");
//  delay(10000);
 //************************//
}

the problem is this string here

dtostrf(t1, 0, 0, buffer); Particle doesn't know what this is ..(I barely do) lol

Sure, you can try anything but the suggested solutions :wink:

1 Like

I use sprint() as @ScruffR suggested

char message[56];
sprintf(message, "%d--%3.2f Supply voltage | %d--%3.2f Battery voltage", supplyAnalogValue, supplyVoltage, batteryAnalogValue, batteryVoltage);
    
mqttClient.publish(eventTopic, message); 

thanks guys ill give it a shot

OK Guys thanks a lot for your help here is the publishing code to share


#include "MQTT/MQTT.h"
#include "math.h"
#include <stdio.h>

const int ledPin1 = D1 ;
const int ledPin2 = D2 ;
const int ledPin3 = D7 ;
const int analogInPin1 = A0;
const int analogInPin2 = A1;

#define ThermisterPin 2

char message_buff[100];
char* tempC;
char* Temp;
double Thermistor(int RawADC) {
double Temp;
Temp = log(12000.0 * ((1024.0 / RawADC - 1)));
Temp = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * Temp * Temp )) * Temp );
Temp = Temp -273.15;//Convert Kelvin to Celcius
//Temp = (Temp * 9.0) / 5.0 + 32.0;//Convert Celcius to Fahrenheit
return Temp;
}
//*******************//

int main ()
{
  char buffer [100];
  int n, a=5, b=3;
  n=sprintf (buffer, "%d plus %d is %d", a, b, a+b);
  printf ("[%s] is a string %d chars long\n",buffer,n);
  return 0;
}
//*******************//
int sensorValue = 0;
int outputValue = 0;

int sensorValue2 = 0;
int outputValue2 = 0;

int ledState = LOW;
int ledState2 = LOW;
int ledState3 = HIGH;

void callback(char* topic, byte* payload, unsigned int length);

// if want to use IP address,
 byte server[] = { 192,168, 1,108 };
 MQTT client(server, 1883, callback);



// recieve message
void callback(char* topic, byte* payload, unsigned int length) {
    char p[length + 1];
    memcpy(p, payload, length);
    p[length] = NULL;
    String message(p);

    if (message.equals("RED"))    
        RGB.color(255, 0, 0);
    else if (message.equals("GREEN"))    
        RGB.color(0, 255, 0);
    else if (message.equals("BLUE"))    
        RGB.color(0, 0, 255);
    else    
        RGB.color(255, 255, 255);
    delay(1000);
}

//**********************//

//*********************//


void setup() {
    
    RGB.control(true);
    Serial.begin(9600);
    // connect to the server
    client.connect("sparkclient");

    // publish/subscribe
    if (client.isConnected()) {
        client.publish("outTopic/message","hello world");
        client.subscribe("inTopic/message");
    }
    
    pinMode(ledPin1, OUTPUT);
    pinMode(ledPin2, OUTPUT);
    pinMode(ledPin3, OUTPUT);
    
   
}

void loop() {
    
    
char message[56];


sensorValue2 = analogRead(analogInPin2);
outputValue2 = map(sensorValue, 0, 5000, 0, 255);
Serial.println(sensorValue2);
sprintf(message, " %d ", sensorValue2);
client.publish("mymosquitto/Particle2", message);

    //******************//    
    sensorValue = analogRead(analogInPin1);
    outputValue = map(sensorValue, 0, 5000, 0, 255);
    Serial.println(sensorValue);
   sprintf(message, " %d " , sensorValue );
   client.publish("mymosquitto/Particle1", message);
    //******************//
    
    if (sensorValue < 1400) {
        digitalWrite (ledPin2, HIGH);
        digitalWrite (ledPin1, LOW);
        client.publish("mymosquitto/Particle", "Larry Happy");


        delay(10000);
    }
    else if (sensorValue > 1400) {
        digitalWrite (ledPin1, HIGH);
        digitalWrite (ledPin2, LOW);
        client.publish("mymosquitto/Particle", "Larry needs Water");

        delay(10000);
    }

    if (client.isConnected())
    Serial.println("connected");
        client.loop();
        


}
1 Like
// This #include statement was automatically added by the Particle IDE.
//#include "lib1.h"

// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain
#include "Adafruit_DHT.h"
#define DHTPIN 0     // what pin we're connected to


void callback(char* topic, byte* payload, unsigned int length);


// Uncomment whatever type you're using!
//#define DHTTYPE DHT11		// DHT 11 
#define DHTTYPE DHT22		// DHT 22 (AM2302)

    
    
const int ledPin1 = D1 ;
const int ledPin2 = D2 ;
const int ledPin3 = D7 ;
const int analogInPin1 = A0;
const int analogInPin2 = A1;
char* message;
#define ThermisterPin 2
int main ()
{
  char buffer [100];
  int n, a=5, b=3;
  n=sprintf (buffer, "%d plus %d is %d", a, b, a+b);
  printf ("[%s] is a string %d chars long\n",buffer,n);
  return 0;
}
//*******************//
int sensorValue = 0;
int outputValue = 0;

int sensorValue2 = 0;
int outputValue2 = 0;

int sensorValue3 = 0;
int sensorValue4 = 0;
int ledState = LOW;
int ledState2 = LOW;
int ledState3 = HIGH;


DHT dht(DHTPIN, DHTTYPE);

void setup() {
	Serial.begin(9600); 
	Serial.println("DHT22 test!");

	dht.begin();
}

void loop() {
// Wait a few seconds between measurements.
delay(2000);
char message[56];
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a 
// very slow sensor)
	float h = dht.getHumidity();
// Read temperature as Celsius
	float t = dht.getTempCelcius();
// Read temperature as Farenheit
	float f = dht.getTempFarenheit();
  
// Check if any reads failed and exit early (to try again).
	if (isnan(h) || isnan(t) || isnan(f)) {
		Serial.println("Failed to read from DHT sensor!");
		return;
	}

// Compute heat index
// Must send in temp in Fahrenheit!
	float hi = dht.getHeatIndex();
	float dp = dht.getDewPoint();
	float k = dht.getTempKelvin();

	Serial.print("Humid: "); 
	Serial.print(h);
	Serial.print("% - ");
	Serial.print("Temp: "); 
	Serial.print(t);
	Serial.print("*C ");
	Serial.print(f);
	Serial.print("*F ");
	Serial.print(k);
	Serial.print("*K - ");
	Serial.print("DewP: ");
	Serial.print(dp);
	Serial.print("*C - ");
	Serial.print("HeatI: ");
	Serial.print(hi);
	Serial.println("*C");
	
	sensorValue3 = (t);
	sprintf(message, " %d ", sensorValue3);
	Particle.publish("Temperature", message, PRIVATE);
	
	sensorValue4 = (h);
	sprintf(message, " %d ", sensorValue4);
	Particle.publish("Humididty" , message ,PRIVATE);
	


sensorValue2 = analogRead(analogInPin2);
outputValue2 = map(sensorValue, 0, 5000, 0, 255);
Serial.println(sensorValue2);
sprintf(message, " %d ", sensorValue2);
Particle.publish("mymosquitto/Particle2", message);

    //******************//    
    sensorValue = analogRead(analogInPin1);
    outputValue = map(sensorValue, 0, 5000, 0, 255);
    Serial.println(sensorValue);
     sprintf(message, " %d " , sensorValue );
    Particle.publish("mymosquitto/Particle1", message);
    //******************//
    
    if (sensorValue < 1400) {
        digitalWrite (ledPin2, HIGH);
        digitalWrite (ledPin1, LOW);
        Particle.publish("mymosquitto/Particle", "Larry Happy");


        delay(1000);
    }
    else if (sensorValue > 1400) {
        digitalWrite (ledPin1, HIGH);
        digitalWrite (ledPin2, LOW);
        Particle.publish("mymosquitto/Particle", "Larry needs Water");

        delay(1000);
    }


Serial.println(Time.timeStr());
        
}