Created
September 8, 2020 09:26
-
-
Save greggjaskiewicz/82360ae5f6781d6e1967aeb6e4272f7c to your computer and use it in GitHub Desktop.
temperature display wifi bme280 and oled display
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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_GFX.h> | |
#include <Adafruit_SSD1306.h> | |
#include <Adafruit_Sensor.h> | |
#include <Adafruit_BME280.h> | |
#define SCREEN_WIDTH 128 // OLED display width, in pixels | |
#define SCREEN_HEIGHT 64 // OLED display height, in pixels | |
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins) | |
#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin) | |
Adafruit_SSD1306 *display; | |
#ifndef STASSID | |
#define STASSID "PRISM" | |
#define STAPSK "MazEgg8378" | |
#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; | |
float tempGraph[SCREEN_WIDTH] = {0}; | |
static float lastTemperature = std::numeric_limits<float>::infinity(); | |
Adafruit_BME280 *bme; | |
void setupDisplay() { | |
display = new Adafruit_SSD1306(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); | |
// SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally | |
if (!display->begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64 | |
Serial.println(F("SSD1306 allocation failed")); | |
for (;;); // Don't proceed, loop forever | |
} | |
// Show initial display buffer contents on the screen -- | |
// the library initializes this with an Adafruit splash screen. | |
display->clearDisplay(); | |
display->display(); | |
display->clearDisplay(); | |
delay(200); // Pause for 2 seconds | |
// Clear the buffer | |
display->clearDisplay(); | |
memset(tempGraph, 0, SCREEN_WIDTH * sizeof(float)); | |
} | |
void setup() { | |
Serial.begin(9600); | |
Wire.begin(D2, D1); | |
bme = new Adafruit_BME280(); | |
bool status = bme->begin(0x76, &Wire); | |
// bool status = bme->begin(); | |
setupDisplay(); | |
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 displayGraph(float graph[SCREEN_WIDTH]) { | |
// display->drawLine(0, 0, display->width()-1, i, SSD1306_WHITE); | |
// display->display(); | |
int16_t i; | |
// clear the bottom | |
for (i = 0; i < SCREEN_WIDTH; i++) { | |
float value = graph[i]; | |
int barHeight = (int)(((float)display->height() / 2) * value); | |
display->drawLine(i, display->height(), i, display->height() - barHeight, SSD1306_WHITE); | |
} | |
} | |
void printBigText(String text) { | |
display->clearDisplay(); | |
display->setTextColor(SSD1306_WHITE); // Draw white text | |
display->setCursor(10, 10); // Start at top-left corner | |
display->setTextSize(2); // Draw 2X-scale text | |
display->setTextColor(SSD1306_WHITE); | |
display->print(text); | |
displayGraph(tempGraph); | |
display->display(); | |
} | |
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() { | |
static int loop = 0; | |
// 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); | |
printBigText(d); | |
loop++; | |
if (loop % 100 == 0) { | |
pushNew(temp); | |
} | |
} else { | |
lcd->print("temp fail! "); | |
printBigText("temp fail! "); | |
} | |
server.handleClient(); | |
MDNS.update(); | |
} | |
// buffer, push values in - and have access to the last 128 values | |
void pushNew(float temp) { | |
// normalise between 0 and 40 | |
float normalised = temp / 40.0; | |
memmove(tempGraph, tempGraph + 1, sizeof(tempGraph) - sizeof(float)); | |
tempGraph[SCREEN_WIDTH - 1] = normalised; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment