Always reading same battery voltage

I have a LoPy (firmware version = 1.6.9.b1) with a lipo battery.
Now I want to read the voltage of my battery, so I can send a message when the battery is below x. The battery is a 3.7V 1200Mah battery.

I tried to get the value by using this code, but I only get the same value everytime (5598.633 -> Mean of ADC readings (0-1400 mV)).
When I just try to read the adc value (like they say in the docs), it is also always the same value.

When I use a voltage meter on pin 16 I get: 1.27V, which is good right?

@mmarkvoort Other sensors do not hurt. When USB is connected, the battery voltage is higher, since it will be charged at that time.
And yes, if the voltage changes when you touch the connector, then you have a bad connector. By maybe that was just coincidence with the other fluctuations.

@mmarkvoort The ADC of the esp32 is known to be very bad. If if you apply a perfectly good signal (stable, low impedance), the reading will fluctuate a lot. That's why in the sample code the average is used.
A capacitor will only help if placed directly at P16. and it must be a low impedance capacitor (ceramic, short leads).

@mmarkvoort No, R is defined by the circuitry of the expansion board, and is fixed. The schematics show a voltage divider consisting of two resistors, one 56k, the other 115k, and so R can be calculated as:

R = 56 / (56 + 115)

If you want to find R by measuring, you have to take a Voltmeter and measure the real values at the battery and at P16, while the device is running from battery.

@mmarkvoort There are two factors top be considered.
a) the resistor divider. You said that with bettery only you have 1.2 V at P16. Could you measure the battery directly in that mode, connected to lopy and running. Then this ratio, Vp16/Vbat = R is known, and the battery voltage can for attn = 3 be calculated as:

@mmarkvoort I'm wondering whether you use the expansion board or not.
The basic range of the adc is 0..1V. With 3 db attenuation it is:
attn 1 = 3db: 10**(3/20) * 1 -> 0.. 1.41
attn 2 = 6db: 10**(6/20) * 1 -> 0.. 1.995
attn 3 = 11 db: 10**(11/20) * 1 -> 0..3.548
The expansion board has a voltage divider between battery and P16, consisting of a 56k/100k resistor, reducing the battery voltage by 56/(56+100) = 0.359.
So the 3.9 V of the battery are seen at P16 as 1.4 V. At attn 3 that should give a reading of 1.4/3.548 * 4096 = 1616. Since your value are different, I assume that the voltage divider has a different set of resistors. You could take the voltage directly at P16 to verify, and then you know the compensation factor.
Update, since my board's values differ from the documentation.
The capacity of the battery does not follow linear to the voltage. It's a kind of lying ' S' kind shape. http://www.thedroneinfo.com/2015/05/13/how-to-care-and-feeding-of-your-lipo-battery/

But I can't really understand what is going on. When I have the USB connected and the battery with attenuation on 1. I am always reading 4095. When I have the USB connected and the battery with attenuation on 3. I am reading 1503, 1508, 1533 etc.

What does these values mean? How is it related to the battery?
What does attenuation even mean? How can I determine the value of that?

@mmarkvoort The ADC range was set a few weeks ago to 12 bit. So in the sample code, you have to divide by 4096 instead of 1024, which yields 1400. You should see the ADC reading of 4096, which means, the ADC input is overloaded. Are you sure, that you set the attenuation right?
These should be the right code:

Also, the 1400 is just a guess for the point of the full range. Using a good Voltage meter, you should adjust this value.
Update: With battery I can read reasonable values, w/p battery I get 4095 in most cases. Do you have a battery connected?