Skip to content

Instantly share code, notes, and snippets.

@greggjaskiewicz
Created September 6, 2020 13:54
Show Gist options
  • Save greggjaskiewicz/f1ce60410264159566540fe7a6b7e37e to your computer and use it in GitHub Desktop.
Save greggjaskiewicz/f1ce60410264159566540fe7a6b7e37e to your computer and use it in GitHub Desktop.
nodeMCU_BME280_display_wifi
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <limits>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#ifndef STASSID
#define STASSID "foo"
#define STAPSK "bar"
#endif
const char* ssid = STASSID;
const char* password = STAPSK;
ESP8266WebServer server(80);
int D0 = 16;
int D1 = 5;
int D2 = 4;
int D3 = 0;
int D4 = 2;
int D5 = 14;
int D6 = 12;
int D7 = 13;
int D8 = 15;
int RX = 3;
LiquidCrystal_I2C *lcd;
static float lastTemperature = std::numeric_limits<float>::infinity();
Adafruit_BME280 *bme;
void setup() {
Serial.begin(9600);
Wire.begin(D2, D1);
bme = new Adafruit_BME280();
bool status = bme->begin(0x76, &Wire);
// bool status = bme->begin();
lcd = new LiquidCrystal_I2C(0x27, 16, 2);
lcd->begin(16, 2);
lcd->home();
// Print a message to the lcd->
lcd->setCursor(0, 1);
lcd->print("Measuring temp!");
lcd->backlight();
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
lcd->setCursor(0, 0);
if (status == false) {
lcd->setCursor(0, 1);
lcd->print("BME280 failed ");
Serial.print("SensorID was: 0x");
Serial.println(bme->sensorID(), 16);
while (1) {
yield();
}
}
lcd->setCursor(0, 0);
lcd->print("connecting");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
lcd->print(".");
delay(400);
}
lcd->setCursor(0, 0);
String status1 = String("IP:" + WiFi.localIP().toString() + " ");
lcd->print(status1);
if (MDNS.begin("esp8266")) {
lcd->setCursor(0, 1);
lcd->print("connected ");
} else {
lcd->setCursor(0, 1);
lcd->print("MDNS failed ");
delay(2000);
}
server.on("/", handleRoot);
server.begin();
}
void handleRoot() {
String tempString = String(lastTemperature, 3);
String d = String("It's " + tempString + " C here!");
if (lastTemperature == std::numeric_limits<float>::infinity()) {
d = String("no measurements done yet");
}
// digitalWrite(led, 1);
server.send(200, "text/plain", d);
// digitalWrite(led, 0);
}
// the loop function runs over and over again forever
void loop() {
// digitalWrite(D4, HIGH);
delay(50);
// digitalWrite(D4, LOW);
delay(50);
lcd->setCursor(0, 1);
float temp = bme->readTemperature();
if (isnan(temp) == false) {
lastTemperature = temp;
String tempString = String(temp, 3);
String d = String(tempString + " 'C ");
lcd->print(d);
} else {
lcd->print("temp fail! ");
}
server.handleClient();
MDNS.update();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment