[SOLVED] Node MCU DC voltage sensor

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 :slight_smile:

#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();
}

Sorry i meant DC Batteries :slight_smile:

Hi there, you are never adding your value to the payload of the mqtt buffer. To solve the issue, you should call the add()method before of publishing, please refer to an example here.

All the best

Hi,

thank you for your prompt response,

so i have added the code to the script,but now its just publishing a constant value and not the value of the actual battery voltage. i have pasted my code below.

/****************************************
 * Include Libraries
 ****************************************/
#include "UbidotsESPMQTT.h"

/****************************************
 * Define Constants
 ****************************************/
#define TOKEN "BBFF-uEqn2f7j6Zrkfu4UvlpgMPZZmrd5CX" // Your Ubidots TOKEN
#define WIFINAME "Protest" //Your SSID
#define WIFIPASS "Protest01604" // Your Wifi Pass
#define Pin A0

Ubidots client(TOKEN);

/****************************************
 * Auxiliar Functions
 ****************************************/

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

/****************************************
 * 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() {
  // put your setup code here, to run once:
  client.ubidotsSetBroker("business.api.ubidots.com"); // Sets the broker properly for the business account
  client.setDebug(true); // Pass a true or false bool value to activate debug messages
  Serial.begin(115200);
  client.wifiConnection(WIFINAME, WIFIPASS);
  client.begin(callback);
  pinMode(analogInput, INPUT);
  }

void loop() {
  // put your main code here, to run repeatedly:
  if(!client.connected()){
      client.reconnect();
      }
      value = analogRead(analogInput);
      vout = (value * 5.0) / 1024.0; // see text
      vin = vout / (R2/(R1+R2)); 
      
  // Publish values to 2 different data sources
  
  client.add("Voltage", 3.6); //Insert your variable Labels and the value to be sent
  client.ubidotsPublish("source1");
  client.loop();
  }

hi there, did you notice that you are just sending a 3.6 in the add() method? You need to replace that value for the one read by your sensor.

All the best

Hi yes,

thank you i have resolved this now, appreciate all the help :slight_smile:

Hi @protest, I am glad to know that you could solve the problem. Do not hesitate to reach again the forums if you experience any additional issue.

All the best