Analog voltage reading (ESP8266)


#1

Hi,

I’m trying to connect a piezo-disc to the thinger platform.

I can read the value from the disc via arduino. Now I’m on a Wemos D1 mini or D1 mini pro to use the wifi to connect to thinger.

In my arduino code I use the divider 1023 / 5 to see the voltage in the serial plotter.

I would like to see the same data on thinger.
So I tried editing the code to work on thinger. But as I thought in the following sketch (below)

    #define DEVICE_CREDENTIAL "your_device_credential"

   #define SSID "your_wifi_ssid"
  #define SSID_PASSWORD "your_wifi_ssid_password"

ThingerESP8266 thing(USERNAME, DEVICE_ID, DEVICE_CREDENTIAL);

const int PIEZO_PIN = A0; // Piezo output

void setup() 
{
  Serial.begin(9600);
  pinMode(BUILTIN_LED, OUTPUT);

  thing.add_wifi(SSID, SSID_PASSWORD);
  
    // piezo resource
  thing["piezo"] >> outputValue(piezoV);
}

void loop() 
{
  // Read Piezo ADC value in, and convert it to a voltage
  int piezoADC = analogRead(PIEZO_PIN);
  float piezoV = piezoADC / 1023.0 * 5.0;
  Serial.println(piezoV); // Print the voltage.


  thing.handle();
  
  delay(250);
}

The piezoV can’t be read because it doesn’t ‘exist’ yet, it only show up in the loop and i’m declaring it before the loop. As I am no real programmer, I’m wondering how to fix this and or what’s necessary to read the voltage from the piezo disc.

Any help would be very much appreciated.


#2

Try declaring float piezoV before void setup() - making it global
and delete float in loop.
Be careful with analog input of ESP it is only 1V capable not 5V.