From memory the Python code sent a string "get data" and the Nodemcu replied. The python code was run every 10min via a cron job and the Nodemcu woke up from sleep every 9min. This was time enough for the sensor to warm up and allow for any problems with timing.
This is an example page of my current setup output. I used the same graphing software for the code below as well and had all the outputs on one graph - scaling the pressure : http://rubblewebs.co.uk/IOT/
You can "drag" over the graph to see more detail.
This was my first real program and can probably be improved.
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include "Timer.h" // Don't need as this was when I was incorporating a RTC?
Adafruit_BME280 bme; // I2C
const char* ssid = "";
const char* password = "";
float h, t, p;
char temperatureFString[6];
char humidityString[6];
char pressureString[7];
char outputData[20];
WiFiUDP Udp;
unsigned int localUdpPort = 8888; // local port to listen on
char incomingPacket[255]; // buffer for incoming packets
const int sleepTime = 5.4e8; // 9min - micro format!
void getWeather() {
/*
Adafruit library
Temperature is always a floating point, in Centigrade.
Pressure is a 32 bit integer with the pressure in Pascals.
100 Pascals = 1 hPa = 1 millibar.
Humidity is in % Relative Humidity
*/
h = bme.readHumidity();
t = bme.readTemperature();
p = (bme.readPressure() / 100.0F);
dtostrf(t, 5, 1, temperatureFString);
dtostrf(h, 5, 1, humidityString);
dtostrf(p, 6, 1, pressureString);
sprintf(outputData, "%s,%s,%s", temperatureFString, humidityString, pressureString);
delay(100);
}
void setup()
{
Serial.begin(115200);
Serial.println();
delay(10);
// BME settings
Wire.begin(D3, D4);
Wire.setClock(100000);
Serial.printf("Connecting to %s ", ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println(" connected");
Udp.begin(localUdpPort);
Serial.printf("Now listening at IP %s, UDP port %d\n", WiFi.localIP().toString().c_str(), localUdpPort);
}
void loop()
{
if (!bme.begin()) {
Serial.println("Could not find a valid BME280 sensor, check wiring!");
while (1);
}
int packetSize = Udp.parsePacket();
if (packetSize)
{
// receive incoming UDP packets
Serial.printf("Received %d bytes from %s, port %d\n", packetSize, Udp.remoteIP().toString().c_str(), Udp.remotePort());
int len = Udp.read(incomingPacket, 255);
if (len > 0)
{
incomingPacket[len] = 0;
}
Serial.printf("UDP packet contents: %s\n", incomingPacket);
getWeather();
// send back a reply, to the IP address and port we got the packet from
Udp.beginPacket(Udp.remoteIP(), Udp.remotePort());
Udp.write(outputData);
Udp.endPacket();
delay(1000); // Need a delay otherwise the data is not sent
ESP.deepSleep(sleepTime);
delay(1000); // Need a delay otherwise the loop starts again before deep sleep
}
}