Having problems with converting the analog voltage to a string and then to char so I can read a chart in node red.
I am using an ESP 32. On the debug screen in node red the word voltage appears. In the serial port window I am getting the correct voltage readings (watts) readings.
Have tried all sorts of combinations of analog conversions from the internet without success
Have you any suggestions?
I have also installed a temp probe for my water heater using a ESP8266 and it is working in node red.
#include <WiFi.h>
#include <PubSubClient.h>
// Change the credentials below
// so your ESP32 connects to your router
const char* ssid = "xxxxxxxxxx";
const char* password = "xxxxxxxxxY";
// Change the variable to your Raspberry Pi IP address
// so it connects to your MQTT broker
const char* mqtt_server = "test.mosquitto.org";
// Initializes the espClient.
WiFiClient esp32;
PubSubClient client(esp32);
// This connects your ESP32 to your router
void setup_wifi() {
delay(10);
Serial.begin(115200);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("WiFi connected - ESP IP address: ");
Serial.println(WiFi.localIP());
}
void readSensors() {
// read the input on analog pin solarpin, c/t 30 amp with output of 1volt
const int solarPin = 39;//Analog pin
int solarValue;// do not change
solarValue = analogRead (solarPin);
float voltage = 0; // do not change
voltage = (1 / 4095.0 * 30 * 240) * solarValue;// converts to watts
Serial.print(voltage);
Serial.println(" Watts");
delay(500);
}
// This reconnects your ESP32 to your MQTT broker
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
if (client.connect("esp32")) {
Serial.println("connected");
// Subscribe or resubscribe to a topic
// You can subscribe to more topics
client.subscribe("home/solar/power");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void setup() {
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
//Turn off wifi access point
WiFi.mode(WIFI_STA);
}
// ensures that you esp32 is connected to wifi and mqtt broker
void loop() {
//connect wifi if not connected
if (WiFi.status() != WL_CONNECTED) {
delay(1);
setup_wifi();
return;
}
if (!client.connected()) {
reconnect();
if (!client.loop())
client.connect("esp32");
}
readSensors();
client.publish("home/solar/power","voltage");
delay(2000);
}
Thanks
Peter Fitzgerald