WIFI MODULE CODE

 #include "UbidotsEsp32Mqtt.h"


const char *WIFI_SSID = " M1269Galaxy67";      // Put your Wi-Fi SSID here
const char *WIFI_PASS = "raji1234";      // Put your Wi-Fi password here
const char *UBIDOTS_TOKEN = "BBUS-jDOPvemaBYQifSNqldosg3xGe3XtPB";  // Put your Ubidots TOKEN here

const char *DEVICE_LABEL = "iot-energy-meter";   // Device label for all variables

const char *VRMS_VARIABLE_LABEL = "VRMS"; // Variable label for VRMS
const char *IRMS_VARIABLE_LABEL = "IRMS"; // Variable label for IRMS
const char *POWER_VARIABLE_LABEL = "POWER"; // Variable label for POWER
const char *POWER_FACTOR_VARIABLE_LABEL = "POWER_FACTOR"; // Variable label for POWER_FACTOR
const char *ENERGY_VARIABLE_LABEL = "ENERGY"; // Variable label for ENERGY

const int PUBLISH_FREQUENCY = 2000; // Update rate in milliseconds

Ubidots ubidots(UBIDOTS_TOKEN);

void setup() {
  Serial.begin(9600);
  ubidots.connectToWifi(WIFI_SSID, WIFI_PASS);
  ubidots.setup();
}

void loop() {
  if (!ubidots.connected()) {
    ubidots.reconnect();
  }

  if (Serial.available() > 0) {
    String rxed = Serial.readStringUntil('\n');

    String vrms_string = rxed.substring(0, 6);
    String irms_string = rxed.substring(6, 12);
    String powerfactor_string = rxed.substring(12, 18);
    String power_string = rxed.substring(18, 24);
    String energy_string = rxed.substring(24, 30);

    float vrms = vrms_string.toFloat();
    float irms = irms_string.toFloat();
    float power = power_string.toFloat();
    float powerfactor = powerfactor_string.toFloat();
    float energy = energy_string.toFloat();

    Serial.print("Vrms: ");
    Serial.print(vrms);
    Serial.print(", ");

    Serial.print("Irms: ");
    Serial.print(irms);
    Serial.print(", ");

    Serial.print("Power factor: ");
    Serial.print(powerfactor);
    Serial.print(", ");

    Serial.print("Power: ");
    Serial.print(power);
    Serial.print(", ");

    Serial.print("Energy: ");
    Serial.println(energy);

    ubidots.add(VRMS_VARIABLE_LABEL, vrms);
    ubidots.add(IRMS_VARIABLE_LABEL, irms);
    ubidots.add(POWER_VARIABLE_LABEL, power);
    ubidots.add(POWER_FACTOR_VARIABLE_LABEL, powerfactor);
    ubidots.add(ENERGY_VARIABLE_LABEL, energy);
    ubidots.publish(DEVICE_LABEL);

    delay(500);
  }

  ubidots.loop();
}

Comments

Popular posts from this blog

BASICS OF ARDUINO UNO - LED BLINKING

ARDUINO UNO CODE TO DISPLAY HELLO WORLD !

ESP 32