Hi All,
this is my first post here after days and days of frustration!
i have easily made temperature sensors that i have connected to ubidots and can see the information on the dashboard.
My problem is that im trying to creating a voltage meter to measure the voltage of AC batteries whilst the Nodemcu is plugged into the mains.
so i bought some voltage sensors and have 1 connected to one node mcu
when i upload the code for reading voltage all works fine. (example below)
int analogInput = A0;
float vout = 0.0;
float vin = 0.0;
float R1 = 30000.0; //
float R2 = 7500.0; //
int value = 0;
void setup(){
pinMode(analogInput, INPUT);
Serial.begin(9600);
Serial.print("DC VOLTMETER");
}
void loop(){
// read the value at analog input
value = analogRead(analogInput);
vout = (value * 5.0) / 1024.0; // see text
vin = vout / (R2/(R1+R2));
Serial.print("INPUT V= ");
Serial.println(vin,2);
delay(500);
}
Now my problem is when i try to edit the ubidots connection code and add the voltage code it all goes wrong, can i please have some help of my betters in this community, i have pasted what i have been trying to do and failing below, much appreciated
#define TOKEN "" // Your Ubidots TOKEN
#define WIFINAME "Protest" // Your SSID
#define WIFIPASS "Protest01604" // Your Wifi Pass
#define MQTTCLIENTNAME "SSETESTBED2019" // Your MQTT Client Name, it must be unique so we recommend to choose a random ASCCI name
#define Pin A0
Ubidots client(TOKEN, MQTTCLIENTNAME);
/****************************************
Auxiliar Functions
****************************************/
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
}
/****************************************
Main Functions
****************************************/
int analogInput = A0;
float vout = 0.0;
float vin = 0.0;
float R1 = 30000.0; //
float R2 = 7500.0; //
int value = 0;
void setup(){
Serial.begin(9600);
client.wifiConnection(WIFINAME, WIFIPASS);
pinMode(analogInput, INPUT);
}
void loop(){
// read the value at analog input
if (!client.connected()) {
client.reconnect();
}
value = analogRead(analogInput);
vout = (value * 5.0) / 1024.0; // see text
vin = vout / (R2/(R1+R2));
Serial.print("INPUT V= ");
Serial.println(vin,2);
delay(500);
client.ubidotsPublish("control");
client.loop();
}