3 Analog Gauge using Esp8266 and CD4051 IC

workatforce
Posts: 34
Joined: Wed Aug 30, 2023 2:56 pm

Post

Hello Pzygmunt,

I am trying to measure voltage on 3 AO pin used EsP8266 and CD4051, on Serial Monitor everything is working. Added 3 Channel but unable to send the data to server. Everything is working except not getting data. Please look at the code. used VScode to build.

Code: Select all

#include <SuplaDevice.h>
#include <supla/network/esp_wifi.h>
#include <supla/device/status_led.h>
#include <supla/storage/littlefs_config.h>
#include <supla/network/esp_web_server.h>
#include <supla/device/supla_ca_cert.h>
#include <supla/sensor/virtual_thermometer.h>
#include <supla/sensor/DS18B20.h>

// Includes for HTML elements
#include <supla/network/html/device_info.h>
#include <supla/network/html/protocol_parameters.h>
#include <supla/network/html/status_led_parameters.h>
#include <supla/network/html/wifi_parameters.h>
#include <supla/network/html/custom_parameter.h>
#include <supla/network/html/custom_text_parameter.h>
#include <supla/network/html/text_cmd_input_parameter.h>
#include <supla/network/html/select_cmd_input_parameter.h>

// Choose where Supla should store roller shutter data in persistent memory
// We recommend to use external FRAM memory
#include <supla/storage/eeprom.h>


#define STATUS_LED_GPIO 2
#define CD4051_a 14     // D5  CD4051 Pin 11 Data A
#define CD4051_b 12     // D6  CD4051 Pin 10 Data B
#define CD4051_c 13     // D7  CD4051 Pin  9 Data C

unsigned long voltage_lasttime; 
int voltage_mtbs = 1000;                // mean time between get_voltage
int epr = 0;  
int val = 0;
double Voltage_0_Send = -1;
double Voltage_1_Send = -1;
double Voltage_2_Send = -1;
double Voltage_Value = 0;

Supla::Eeprom eeprom;
// #include <supla/storage/fram_spi.h>
// Supla::FramSpi fram(STORAGE_OFFSET);

// This class provides Wi-Fi network connectivity for your device
Supla::ESPWifi wifi;

// This class provides web server to handle config mode
Supla::EspWebServer suplaServer;

// This class provides configuration storage (like SSID, password, all
// user defined parameters, etc. We use LittleFS.
Supla::LittleFsConfig configSupla;

Supla::Device::StatusLed statusLed(STATUS_LED_GPIO, true); // inverted state

// Function Get Voltage on 3 Analog pin using CD4051 IC and ESP8266 pin
void get_voltage(int cvs) {
         if (cvs == 0){  
            digitalWrite(CD4051_c, LOW);digitalWrite(CD4051_b, LOW);digitalWrite(CD4051_a, LOW);
            delay(2);
            val = 0;
            for(int i = 0; i < 5; i++) {
            val += analogRead(A0);
            delay(1);
            }
            val = val / 5;
            Voltage_Value = val  * (5.0 / 1023.0);
            Voltage_Value = round(Voltage_Value * 100.0) / 100.0;
            if (Voltage_Value != Voltage_0_Send){ 
            Voltage_0_Send = Voltage_Value;
            Serial.print("send voltage 0 = ");
            Serial.println(Voltage_0_Send,2);
            Supla::Sensor::Thermometer(Voltage_0_Send);
            //SuplaDevice.channelDoubleValueChanged = (0, Voltage_0_Send);
            }yield();}
            
        if (cvs == 1){
            digitalWrite(CD4051_c, LOW);digitalWrite(CD4051_b, LOW);digitalWrite(CD4051_a, HIGH);
            delay(2);
            val = 0;
            for(int i = 0; i < 5; i++) {
            val += analogRead(A0);
            delay(1);
            }
            val = val / 5;
            Voltage_Value = val  * (5.0 / 1023.0);
            Voltage_Value = round(Voltage_Value * 100.0) / 100.0;
            if (Voltage_Value != Voltage_1_Send){ 
            Voltage_1_Send = Voltage_Value;
            Serial.print("send voltage 1 = ");
            Serial.println(Voltage_1_Send,2);
           //SuplaDevice.channelDoubleValueChanged = (0, Voltage_0_Send); 
          Supla::Sensor::Thermometer(Voltage_1_Send); 
            }yield();}
            
        if (cvs == 2){
            digitalWrite(CD4051_c, LOW);digitalWrite(CD4051_b, HIGH);digitalWrite(CD4051_a, LOW);
            delay(2);
            val = 0;
            for(int i = 0; i < 5; i++) {
            val += analogRead(A0);
            delay(1);
            }
            val = val / 5;
            Voltage_Value = val  * (5.0 / 1023.0);
            Voltage_Value = round(Voltage_Value * 100.0) / 100.0;
            if (Voltage_Value != Voltage_2_Send){ 
            Voltage_2_Send = Voltage_Value;
            Serial.print("send voltage 2 = ");
            Serial.println(Voltage_2_Send,2);
            //SuplaDevice.channelDoubleValueChanged(2, Voltage_2_Send); 
            }yield();}
}

void setup() {

  Serial.begin(115200);

  // configure defualt Supla CA certificate
  SuplaDevice.setSuplaCACert(suplaCACert);
  SuplaDevice.setSupla3rdPartyCACert(supla3rdCACert);


  // HTML www component (they appear in sections according to creation
  // sequence).
  new Supla::Html::DeviceInfo(&SuplaDevice);
  new Supla::Html::WifiParameters;
 new Supla::Html::ProtocolParameters;
  new Supla::Html::StatusLedParameters;

  // Sensor Channel for server
  new Supla::Sensor::Thermometer;
  new Supla::Sensor::Thermometer;
  new Supla::Sensor::Thermometer;


  // Set custom device name
  SuplaDevice.setName("SUPLA 3 Analog using CD4051 IC");
  // Start!
  SuplaDevice.begin();
}

void loop() {
  SuplaDevice.iterate();
yield();
  if (millis() > voltage_lasttime + voltage_mtbs)  {    //--------------Voltage callback--------------------
          if (epr>7){epr = 0;}
          get_voltage(epr);
          epr = epr+1;
          voltage_lasttime = millis();
     }else {
       Voltage_0_Send = -1;Voltage_1_Send = -1;Voltage_2_Send = -1; 
     }
}

Please help me out on this.
User avatar
pzygmunt
Posts: 20302
Joined: Tue Jan 19, 2016 9:26 am
Location: Paczków
Been thanked: 59 times

Post

It seems to me that you should implement it as a three-phase energy meter with voltage measurement.
SUPLA.... Nareszcie w domu.
workatforce
Posts: 34
Joined: Wed Aug 30, 2023 2:56 pm

Post

pzygmunt wrote: Fri Oct 27, 2023 10:37 am It seems to me that you should implement it as a three-phase energy meter with voltage measurement.
Thankyou Pzygmunt for quickest reply.

But Still I want to know why i am unable to send the data to server

Code: Select all

SuplaDevice.channelDoubleValueChanged(0, Voltage_0_Send); 
stuck here which i commented in the shared code, i guess it has depreciated and old as in present day Supla is far ahead from this point. But still want to solve it out by using new Supla device structure. Let me know how to solve it. Thankyou.
workatforce
Posts: 34
Joined: Wed Aug 30, 2023 2:56 pm

Post

workatforce wrote: Fri Oct 27, 2023 12:16 pm
pzygmunt wrote: Fri Oct 27, 2023 10:37 am It seems to me that you should implement it as a three-phase energy meter with voltage measurement.
Thankyou Pzygmunt for quickest reply.

But Still I want to know why i am unable to send the data to server

Code: Select all

SuplaDevice.channelDoubleValueChanged(0, Voltage_0_Send); 
stuck here which i commented in the shared code, i guess it has depreciated and old as in present day Supla is far ahead from this point. But still want to solve it out by using new Supla device structure. Let me know how to solve it. Thankyou.

Code: Select all

t1->setValue(Voltage_0_Send);
Solved it by using this

Return to “Help”