Newbie Help... trying to display onto OLED and on web page
Posted: Sun Nov 15, 2020 2:13 pm
Hello!
This is the first time I've posted anything on here and I am really new to the ESP8266 NodeMCU board (or any Arduino type board), so please forgive my ignorance. I have run through quite a few tutorials and I don't seem to have trouble with any of them and I actually found two that do PART of what I want to accomplish, but I haven't found any that put these together into one code.
That being said, I have successfully made a sketch that will display temp and humidity readings on an OLED display and I have successfully made a sketch to display the temp and humidity in a web page via WiFi. I have tried numerous times to combine the two, only using what I thought was required for each, and have no luck.
If anyone can straighten me out on this, I would sure appreciate it. Please remember that I am a Newbie, so if you can explain any acronyms if used, it will save an unnecessary reply asking for such.
The code I am trying to run currently is below. I get it to connect to my Network (the IP is listed when I look at my router's home page), but that's it. The display just says "Temperature: 0.00 C Humidity: 0.00%" and the web page will not load. In the serial window it gives me this info:
Thank you for your time.
Ron.
This is the first time I've posted anything on here and I am really new to the ESP8266 NodeMCU board (or any Arduino type board), so please forgive my ignorance. I have run through quite a few tutorials and I don't seem to have trouble with any of them and I actually found two that do PART of what I want to accomplish, but I haven't found any that put these together into one code.
That being said, I have successfully made a sketch that will display temp and humidity readings on an OLED display and I have successfully made a sketch to display the temp and humidity in a web page via WiFi. I have tried numerous times to combine the two, only using what I thought was required for each, and have no luck.
If anyone can straighten me out on this, I would sure appreciate it. Please remember that I am a Newbie, so if you can explain any acronyms if used, it will save an unnecessary reply asking for such.
The code I am trying to run currently is below. I get it to connect to my Network (the IP is listed when I look at my router's home page), but that's it. The display just says "Temperature: 0.00 C Humidity: 0.00%" and the web page will not load. In the serial window it gives me this info:
13:21:47.915 -> WiFi connected
13:21:47.915 -> AP IP address is: 192.168.50.50HTTP server started
13:21:49.883 ->
13:21:49.883 -> Connected to RT-AC66U_B1_58_2G
13:21:49.931 -> IP address: 192.168.50.50
13:21:49.931 -> HTTP server started
13:22:59.677 -> WS:ac
13:22:59.677 -> :rn 466
13:22:59.677 -> :ref 1
13:22:59.677 -> WS:av status=4 WCav=466
13:22:59.677 -> :ref 2
13:22:59.677 -> :ur 2
13:22:59.677 -> :ref 2
13:22:59.677 -> :ur 2
13:22:59.677 -> :c0 1, 466
13:22:59.677 ->
13:22:59.677 -> --------------- CUT HERE FOR EXCEPTION DECODER ---------------
13:22:59.677 ->
13:22:59.677 -> Exception (3):
13:22:59.677 -> epc1=0x4000bf64 epc2=0x00000000 epc3=0x00000000 excvaddr=0x40246765 depc=0x00000000
13:22:59.677 ->
13:22:59.677 -> >>>stack>>>
13:22:59.677 ->
13:22:59.677 -> ctx: cont
13:22:59.677 -> sp: 3ffffcf0 end: 3fffffc0 offset: 0190
13:22:59.724 -> 3ffffe80: 00000001 49545000 40246765 40208d13
13:22:59.724 -> 3ffffe90: 3fff0e30 00000001 3fff0ddc 40204210
13:22:59.724 -> 3ffffea0: 3fffff00 3fffff00 80fffee0 40202158
13:22:59.724 -> 3ffffeb0: 00000001 4020c1d8 3fff0ddc 4020c1a6
13:22:59.724 -> 3ffffec0: 3fffff00 3ffee644 3fff0ddc 401000e1
13:22:59.724 -> 3ffffed0: 3fff0ddc 3ffee644 3fff0ddc 40202194
13:22:59.724 -> 3ffffee0: 3ffe0000 00000000 80fee628 80fee8f4
13:22:59.724 -> 3ffffef0: 3fff0ddc 3ffee644 3ffee604 4020440a
13:22:59.724 -> 3fffff00: 3fff002f 80ffff10 81000000 00000000
13:22:59.724 -> 3fffff10: 80fffe00 3fff1a00 0000008f 80005054
13:22:59.770 -> 3fffff20: 3ffee644 00000001 4020c1d8 00000001
13:22:59.770 -> 3fffff30: 00000001 00000000 3fff1594 40205a54
13:22:59.770 -> 3fffff40: 00000000 3fff1594 3ffee604 3ffee8f4
13:22:59.770 -> 3fffff50: 00000001 3ffee628 3ffee604 40204849
13:22:59.770 -> 3fffff60: 4020cc08 00000000 00001388 40208725
13:22:59.770 -> 3fffff70: 00000000 3fff1594 3ffee5b8 3ffee714
13:22:59.770 -> 3fffff80: 3fffdad0 00000000 3ffee8b4 40204933
13:22:59.770 -> 3fffff90: 3fffdad0 00000000 3ffee8b4 3ffee8f4
13:22:59.770 -> 3fffffa0: 3fffdad0 00000000 3ffee8b4 40209d44
13:22:59.770 -> 3fffffb0: feefeffe feefeffe 3ffe8508 4010157d
13:22:59.817 -> <<<stack<<<
13:22:59.817 ->
13:22:59.817 -> --------------- CUT HERE FOR EXCEPTION DECODER ---------------
13:22:59.817 ->
13:22:59.817 -> ets Jan 8 2013,rst cause:2, boot mode:(3,6)
13:22:59.817 ->
13:22:59.817 -> load 0x4010f000, len 3584, room 16
13:22:59.817 -> tail 0
13:22:59.817 -> chksum 0xb0
13:22:59.817 -> csum 0xb0
13:22:59.817 -> v2843a5ac
13:22:59.817 -> ~ld
13:23:01.872 ->
13:23:01.872 -> Configuring access point...
13:23:01.872 ->
13:23:01.872 -> Connecting to RT-AC66U_B1_58_2G
Code: Select all
// LIBRARIES NEEDED TO RUN THE CODE
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "DHT.h"
// DEFINES THE SCREEN WIDTH AND HEIGHT FOR THE DISPLAY
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// DEFINEs VARIABLES USED IN THE CODE
#define myID "RT-AC66U_B1_58_2G"
#define myPW "*******"
#define LED 2 // On board LED
#define DHTTYPE DHT22 // Change this to DHT11 if using that sensor
float humidity, temperature;
uint8_t DHTPin = 14; // Defines the pin number that is used to read the sensor data
DHT dht(DHTPin, DHTTYPE);
// SSID and Password of your WiFi router
const char* ssid = myID;
const char* password = myPW;
/* Enter the static ip that you want to set */
IPAddress ip(192, 168, 50, 50);
IPAddress gateway(192, 168, 50, 1);
IPAddress subnet(255, 255, 255, 0);
ESP8266WebServer server(80); //Server on port 80
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
// THIS IS WHERE THE WEB PAGE IS CONSTRUCTED.
const char MAIN_page[] PROGMEM = R"=====(
<!doctype html> // This is used to tell the web browser which version of html we are using
<html> // Everything written between <html> and </html> will be read by the browser
<head>
<title>Bin One</title>
<h1 style="text-align:center; color:red;">Iot Design Pro</h1>
<h3 style="text-align:center;">NodeMCU Data Logger</h3>
<style>
canvas{
-moz-user-select: none;
-webkit-user-select: none;
-ms-user-select: none;
}
/* Data Table Styling*/
#dataTable {
font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
border-collapse: collapse;
width: 100%;
text-align: center;
}
#dataTable td, #dataTable th {
border: 1px solid #ddd;
padding: 8px;
}
#dataTable tr:nth-child(even){background-color: #f2f2f2;}
#dataTable tr:hover {background-color: #ddd;}
#dataTable th {
padding-top: 12px;
padding-bottom: 12px;
text-align: center;
background-color: #050505;
color: white;
}
</style>
</head>
<body>
<div>
<table id="dataTable">
<tr><th>Time</th><th>Temperaure (°C)</th><th>Humidity (%)</th></tr>
</table>
</div>
<br>
<br>
<script> // The <script> tags are used to include jQuery/JavaScript code
var Tvalues = [];
var Hvalues = [];
var timeStamp = [];
setInterval(function() {
// Call a function repetatively with 5 Second interval
getData(); // This function is used to get the data from the NodeMCU and update the webpage table.
}, 5000); // 5000mSeconds update rate
// The XMLHttpRequest object is used to request data from the web server. Using this we can
// update a web page without reloading the page.
function getData() {
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
//Push the data in array
var time = new Date().toLocaleTimeString();
var txt = this.responseText;
var obj = JSON.parse(txt);
Tvalues.push(obj.Temperature);
Hvalues.push(obj.Humidity);
timeStamp.push(time);
// Update Data Table
var table = document.getElementById("dataTable");
var row = table.insertRow(1); // Add after headings
var cell1 = row.insertCell(0);
var cell2 = row.insertCell(1);
var cell3 = row.insertCell(2);
cell1.innerHTML = time;
cell2.innerHTML = obj.Temperature;
cell3.innerHTML = obj.Humidity;
}
};
xhttp.open("GET", "readData", true); // Handle readData server on ESP8266
xhttp.send();
}
</script>
</body>
</html>
)=====";
// THE handleRoot FUNCTION IS EXECUTED WHEN WE OPEN THE WEBPAGE BROWSER USING THE NodeMCU IP ADDRESS
void handleRoot() {
String s = MAIN_page; //Read HTML contents
server.send(200, "text/html", s); //Send web page
}
// THE readData FUNCTION IS USED TO READ THE DATA FROM THE SENSOR AND SEND IT TO THE WEBPAGE.
// In this loop, NodeMCU stores the DHT sensor values into two float variables: temperature & humidity
// This it converts into the string and stores the data into another string variable called "data".
// This is then sent to the Webpage whenever requested.
void readData() {
String data = "{\"Temperature\":\""+ String(temperature) +"\", \"Humidity\":\""+ String(humidity) +"\"}";
digitalWrite(LED,!digitalRead(LED)); // Toggle LED on data request ajax
server.send(200, "text/plane", data); // Send ADC value, temperature and humidity JSON to client ajax request
delay(2000);
temperature = dht.readTemperature();
humidity = dht.readHumidity();
Serial.print(humidity, 1);
Serial.print(temperature, 1);
}
// IN THE void setup() FUNCTION, WE INITIALIZE THE BAUD RATE, THE DHT SENSOR USING .begin(), THE
// WEBPAGE USING server.begin(), AND THEN CONNECT THE MODULE WITH THE Wi-Fi USING THE ID AND PASSWORD
void setup ()
{
delay(2000);
Serial.begin(115200);
Serial.println();
Serial.print("Configuring access point...");
// Connection to wireless network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
//Configuring the WI-FI with the specified static IP.
WiFi.config(ip, gateway, subnet);
//Start the WI-FI connection with specified ACCESS-POINT
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.print("AP IP address is: ");
Serial.print(ip);
server.on("/", handleRoot);
server.begin();
Serial.println("HTTP server started");
// Addresses pin used to get sensor data as input
pinMode(DHTPin, INPUT);
dht.begin();
// THIS CHECKS FOR THE DISPLAY CONNECTION
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
delay(2000);
display.clearDisplay();
display.setTextColor(WHITE);
//Onboard LED port Direction output
pinMode(LED,OUTPUT);
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
// If connection successful show IP address in serial monitor
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP()); // IP address assigned to your ESP
// THE FIRST server.on() FUNCTION IS USED TO CALL THE handleRoot() FUNCTION WHEN A CLIENT REQUESTS A
// URI (Uniform Resource Identifier). In this case it is the "/". The second server.on() function
// is used to call the readData() function whenever a POST request is made to the URI "/readData".
server.on("/", handleRoot); //Which routine to handle at root location. This is display page
server.on("/readData", readData); //This page is called by java Script AJAX
server.begin(); //Start server
Serial.println("HTTP server started");
}
// THE void loop(void) FUNCTION CONTINUOUSLY LISTENS FOR HTTP REQUESTS FROM CLIENTS
void loop(void){
server.handleClient(); //Handle client requests
if (isnan(humidity) || isnan(temperature)) { // Checks for sensor readings and reports to display if no readings are found.
Serial.println("Failed to read from DHT sensor!");
}
// Clears the display
display.clearDisplay();
// display temperature
display.setTextSize(1);
display.setCursor(0,0);
display.print("Temperature: ");
display.setTextSize(2);
display.setCursor(0,10);
display.print(temperature);
display.print(" ");
display.setTextSize(1);
display.cp437(true);
display.write(167);
display.setTextSize(2);
display.print("C");
// display humidity
display.setTextSize(1);
display.setCursor(0, 35);
display.print("Humidity: ");
display.setTextSize(2);
display.setCursor(0, 45);
display.print(humidity);
display.print(" %");
display.display();
}
Thank you for your time.
Ron.