Hallo, ich benötige Hilfe bei einem Sketch. Ich möchte gerne den Druck (pressure_bar) über MQTT an IOBROKER übertragen. Leider macht er das nicht, sondern er überträgt sensorVal.
#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>
#include <string.h>
// Update these with values suitable for your network.
byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
IPAddress ip(192, 168, 178, 149);
const char* server = "192.168.178.124";
char in_message[100];
EthernetClient ethClient;
PubSubClient mqttClient(ethClient);
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("]\n ");
for (int i=0;i<length;i++) {
//Serial.print((char)payload[i]);
in_message[i]=char(payload[i]);
}
//Serial.print(payload);
}
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Ethernet.begin(mac, ip);
// Allow the hardware to sort itself out
delay(1500);
//mqttClient.setServer(server, 1883);
mqttClient.setServer("192.168.178.124", 1883);
mqttClient.setCallback(callback);
if (mqttClient.connect("arduino-1")) {
// connection succeeded
Serial.println("Connected ");
boolean r= mqttClient.subscribe("test");
Serial.println("subscribe ");
Serial.println(r);
}
else {
// connection failed
// mqttClient.state() will provide more information
// on why it failed.
Serial.println("Connection failed ");
}
}
void loop()
{
//
int sensorVal=analogRead(A1);
float voltage = (sensorVal*5.0)/1024.0;
// serial.print("Volts: ");
// serialplay.print(voltage);
float pressure_pascal = (3.0*((float)voltage-0.47))*1000000.0;
float pressure_bar = pressure_pascal/10e5;
char sensorVal_string[4];
sprintf(sensorVal_string, "%d", pressure_bar);
Serial.println("publishing string");
Serial.println(pressure_bar);
char out_msg[100] ="Druck = ";
strcat(out_msg, sensorVal_string);
boolean rc = mqttClient.publish("test", out_msg );
byte outmsg[]={0xff,0xfe};
Serial.println("publishing bytes");
rc = mqttClient.publish("testbyte", outmsg,2);
Serial.print("in_message= ");
Serial.print(in_message);
delay(10000);
mqttClient.loop();
}
Vielen Dank Vorab
VG