Hello @all,
first: this is my first ESP project and also the first with Arduino.
I want to connect a nodeMCU32 via MQTT to my openhab System and
"work" with data from a lighting detector AS3935. So i use google for
some sample code (one for the AS3935 and one for MQTT) and be able
to connect the AS3935 to the ESP32 and both via MQTT to my broker.
So sending "Text" is not the problem, but i want to send also the distance
(which i can display in the serial monitor)
Maybe i should say here that i yesterday testet the sample code for the
AS3935 in a thunderstorm and can display the distance etc. in the serial
monitor, so the code worked. And now i want to "publish" the distance in an
MQTT message and get an error code.
byte distance = lightning.distanceToStorm();
Serial.print("Approximately: ");
Serial.print(distance);
client.publish("esp321/Entfernung",distance);
So this is my error message, search by google did not help me
invalid conversion from 'byte {aka unsigned char}' to 'const char*' [-fpermissive]
I am one hundred percent sure that the code can be thinned out by one or two settings.
But before I go there, my first thought was: step by step.
And now i hope that somebody could help me
Here is the complete code:
#include <Wire.h>
#include <SparkFun_AS3935.h>
#include <WiFi.h>
#include <PubSubClient.h>
const char* ssid = "SSID";
const char* password = "PASSWORD";
const char* mqttServer = "IP-Adress";
const int mqttPort = 1883;
const char* mqttUser = "USERNAME";
const char* mqttPassword = "PASSWORD";
#define AS3935_ADDR 0x03
#define INDOOR 0x12
#define OUTDOOR 0xE
#define LIGHTNING_INT 0x08
#define DISTURBER_INT 0x04
#define NOISE_INT 0x01
WiFiClient espClient;
PubSubClient client(espClient);
SparkFun_AS3935 lightning(AS3935_ADDR);
// Interrupt pin for lightning detection
const int lightningInt = 4;
int noiseFloor = 2;
// This variable holds the number representing the lightning or non-lightning
// event issued by the lightning detector.
int intVal = 0;
void WiFiEvent(WiFiEvent_t event) {
Serial.printf("[WiFi-event] event: %d\n", event);
switch(event) {
case SYSTEM_EVENT_STA_GOT_IP:
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
Serial.println("WiFi lost connection");
WiFi.begin(ssid, password);
break;
}
if (event > 7 ) {
WiFi.begin(ssid, password);
}
}
void setup()
{
// When lightning is detected the interrupt pin goes HIGH.
pinMode(lightningInt, INPUT);
Serial.begin(115200);
delay(10);
WiFi.onEvent(WiFiEvent);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.println("Connecting to WiFi..");
}
Serial.println("Connected to the WiFi network");
client.setServer(mqttServer, mqttPort);
while (!client.connected()) {
Serial.println("Connecting to MQTT...");
if (client.connect("ESP32Client", mqttUser, mqttPassword )) {
Serial.println("connected");
} else {
Serial.print("failed with state ");
Serial.print(client.state());
delay(2000);
}
}
Wire.begin(); // Begin Wire before lightning sensor.
if( !lightning.begin() ) { // Initialize the sensor.
Serial.println ("Lightning Detector did not start up, freezing!");
client.publish("esp321/AS3935_Status", "Blitzsensor nicht gestartet");
while(1);
}
else
Serial.println("Schmow-ZoW, Lightning Detector Ready!");
client.publish("esp321/AS3935_Status", "Blitzsensor bereit");
// The lightning detector defaults to an indoor setting at
// the cost of less sensitivity, if you plan on using this outdoors
// uncomment the following line:
//lightning.setIndoorOutdoor(OUTDOOR);
}
void loop()
{
if(digitalRead(lightningInt) == HIGH){
// Hardware has alerted us to an event, now we read the interrupt register
// to see exactly what it is.
intVal = lightning.readInterruptReg();
if(intVal == NOISE_INT){
Serial.println("Noise.");
client.publish("esp321/Noise", "Noise");
//reduceNoise(); //See note below above reduceNoise function.
}
else if(intVal == DISTURBER_INT){
Serial.println("Disturber.");
client.publish("esp321/Disturber", "Disturber");
}
else if(intVal == LIGHTNING_INT){
Serial.println("Lightning Strike Detected!");
client.publish("esp321/Detection", "Blitz erkannt");
// Lightning! Now how far away is it? Distance estimation takes into
// account any previously seen events in the last 15 seconds.
byte distance = lightning.distanceToStorm();
Serial.print("Approximately: ");
Serial.print(distance);
client.publish("esp321/Entfernung",distance);
Serial.println("km away!");
}
}
delay(100); //Let's not be too crazy.
}