Merged with code changes from RLJ_WebServer_I2C_OLED to obtain I2C OLED An javascript web server

This commit is contained in:
rljonesau 2018-11-26 21:26:38 +11:00
parent b77bbb127e
commit 5bcddf1a24
21 changed files with 227 additions and 99 deletions

1
.gitignore vendored
View file

@ -4,3 +4,4 @@
*.o
*.a
*.cache
/Arduino/BTCDieselHeater/.vscode/*.json

View file

@ -1,7 +0,0 @@
{
"board": "esp32:esp32:esp32",
"port": "192.168.0.45",
"sketch": "BTCDieselHeater.ino",
"output": "..\\build",
"configuration": "PSRAM=disabled,PartitionScheme=default,FlashMode=qio,FlashFreq=80,FlashSize=4M,UploadSpeed=921600,DebugLevel=none"
}

View file

@ -1,21 +0,0 @@
{
"configurations": [
{
"name": "Win32",
"includePath": [
"C:\\Program Files (x86)\\Arduino\\tools\\**",
"C:\\Program Files (x86)\\Arduino\\hardware\\arduino\\avr\\**",
"C:\\Users\\ray\\AppData\\Local\\Arduino15\\packages\\esp32\\tools\\**",
"C:\\Users\\ray\\AppData\\Local\\Arduino15\\packages\\esp32\\hardware\\esp32\\1.0.0\\**",
"C:\\Users\\ray\\AppData\\Local\\Arduino15\\packages\\arduino\\tools\\**",
"C:\\Users\\ray\\AppData\\Local\\Arduino15\\packages\\arduino\\hardware\\sam\\1.6.11\\**"
],
"forcedInclude": [],
"intelliSenseMode": "msvc-x64",
"compilerPath": "C:/Program Files (x86)/Microsoft Visual Studio/2017/Community/VC/Tools/MSVC/14.15.26726/bin/Hostx64/x64/cl.exe",
"cStandard": "c11",
"cppStandard": "c++17"
}
],
"version": 4
}

View file

@ -1,24 +0,0 @@
{
"sketch": "SenderTrial2.ino",
"port": "COM4",
"board": "arduino:sam:arduino_due_x_dbg",
"output": "c:\\Users\\ray\\AppData\\Local\\Arduino\\",
"files.associations": {
"list": "cpp",
"vector": "cpp",
"xhash": "cpp",
"xstring": "cpp",
"xtree": "cpp",
"algorithm": "cpp",
"initializer_list": "cpp",
"xutility": "cpp",
"istream": "cpp",
"memory": "cpp",
"tuple": "cpp",
"type_traits": "cpp",
"utility": "cpp",
"xlocmon": "cpp",
"xlocnum": "cpp",
"xloctime": "cpp"
}
}

View file

@ -1,12 +0,0 @@
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "echo",
"type": "shell",
"command": "echo Hello"
}
]
}

View file

@ -37,12 +37,19 @@
/////////////////////////////////////////////////////////////////////////////////////////////////////
// Hardware SPI constructor
C128x64_OLED::C128x64_OLED(int8_t DC, int8_t CS, int8_t RST) : Adafruit_SH1106(DC, CS, RST)
{
m_pFontInfo = NULL;
}
// I2C constructor
C128x64_OLED::C128x64_OLED(int8_t SDA, int8_t SCL) : Adafruit_SH1106(SDA, SCL)
{
m_pFontInfo = NULL;
}
size_t C128x64_OLED::write(uint8_t c)
{
if(m_pFontInfo) {

View file

@ -6,7 +6,8 @@
class C128x64_OLED : public Adafruit_SH1106 {
const FONT_INFO* m_pFontInfo;
public:
C128x64_OLED(int8_t DC, int8_t CS, int8_t RST);
C128x64_OLED(int8_t DC, int8_t CS, int8_t RST); // Hardware SPI constructor
C128x64_OLED(int8_t SDA, int8_t SCL); // I2C constructor
void drawDotFactoryChar(int16_t x, int16_t y, unsigned char c, uint16_t color, uint16_t bg, const FONT_INFO* pFontDescriptor, int& xsize, int& ysize);
void setFontInfo(const FONT_INFO* pFontInfo) { m_pFontInfo = pFontInfo; };

View file

@ -350,7 +350,8 @@ void Adafruit_SH1106::begin(uint8_t vccstate, uint8_t i2caddr, bool reset) {
TWI1->TWI_CWGR = ((VARIANT_MCK / (2 * 400000)) - 4) * 0x101;
#endif
} else {
Wire.begin(sda, scl);
Serial.println("Opening I2C port");
Wire.begin(sda, scl, 800000);
}
}

View file

@ -56,3 +56,11 @@
#define SUPPORT_OEM_CONTROLLER 1
///////////////////////////////////////////////////////////////////////////////
// SH1106 128x64 OLED support
//
// 0: I2C,
// 1: HW SPI
//
#define OLED_HW_SPI 0

View file

@ -86,6 +86,8 @@
#define FAILEDSSID "BTCESP32"
#define FAILEDPASSWORD "thereisnospoon"
#define RX_DATA_TIMOUT 50
//comment this out to remove TELNET
//#define TELNET
@ -220,7 +222,6 @@ void setup() {
lastAnimationTime = millis();
ScreenManager.init();
// initOLED();
#if USE_WIFI == 1
@ -398,7 +399,7 @@ void loop()
// precautionary state machine action if all 24 bytes were not received
// whilst expecting a frame from the blue wire
if(RxTimeElapsed > 50) {
if(RxTimeElapsed > RX_DATA_TIMOUT) {
if( CommState.is(CommStates::OEMCtrlRx) ||
CommState.is(CommStates::HeaterRx1) ||
CommState.is(CommStates::HeaterRx2) ) {
@ -453,7 +454,7 @@ void loop()
}
#if SUPPORT_OEM_CONTROLLER == 1
if(BlueWireData.available() && (RxTimeElapsed > 100)) {
if(BlueWireData.available() && (RxTimeElapsed > RX_DATA_TIMOUT+10)) {
#ifdef REPORT_OEM_RESYNC
DebugPort.print("Re-sync'd with OEM Controller. ");
DebugPort.print(RxTimeElapsed);
@ -897,7 +898,6 @@ void checkDisplayUpdate()
if(bUpdateDisplay) {
if(pTxFrame && pRxFrame) {
ScreenManager.update(*pTxFrame, *pRxFrame);
// updateOLED(*pTxFrame, *pRxFrame);
bUpdateDisplay = false;
}
}
@ -906,7 +906,6 @@ void checkDisplayUpdate()
if(tDelta >= 100) {
lastAnimationTime += 100;
ScreenManager.animate();
// animateOLED();
}
}

View file

@ -5,15 +5,20 @@
#include "BTCWebServer.h"
#include "DebugPort.h"
#include "TxManage.h"
#include "helpers.h"
#include "pins.h"
#include "Index.h"
extern void Command_Interpret(const char* pLine); // decodes received command lines, implemented in main .ino file!
WebServer server(80);
WebSocketsServer webSocket = WebSocketsServer(81);
const int led = 13;
void handleRoot() {
digitalWrite(led, 1);
server.send(200, "text/plain", "Chnage URL to /on to poweron heater... /off to poweroff");
digitalWrite(led, 0);
String s = MAIN_PAGE; //Read HTML contents
server.send(200, "text/html", s); //Send web page
}
void handleNotFound() {
@ -38,34 +43,41 @@ void initWebServer(void) {
if (MDNS.begin("BTCHeater")) {
DebugPort.println("MDNS responder started");
}
server.on("/on", webturnOn);
server.on("/off", webturnOff);
server.on("/", handleRoot);
server.on("/inline", []() {
server.send(200, "text/plain", "this works as well");
});
server.onNotFound(handleNotFound);
server.begin();
webSocket.begin();
webSocket.onEvent(webSocketEvent);
DebugPort.println("HTTP server started");
}
void doWebServer(void) {
static unsigned long lastTx = 0;
webSocket.loop();
server.handleClient();
if(millis() > lastTx) { // moderate the delivery of new messages - we simply cannot send every pass of the main loop!
lastTx = millis() + 1000;
char msg[16];
sprintf(msg, "%.1f", getActualTemperature());
webSocket.broadcastTXT(msg);
// char c[] = { "23" };
// webSocket.broadcastTXT(c, sizeof(c));
}
void webturnOn() {
TxManage.queueOnRequest();
server.send(200, "text/plain", "Heater Turning on");
}
void webturnOff() {
TxManage.queueOffRequest();
server.send(200, "text/plan", "Turning off heater");
void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length) {
if (type == WStype_TEXT) {
char cmd[16];
memset(cmd, 0, 16);
for (int i = 0; i < length && i < 15; i++) {
cmd[i] = payload[i];
// Serial.print((char)payload[i]);
}
// Serial.println();
Serial.println(cmd);
Command_Interpret(cmd); // send to the main heater controller decode routine
}
}

View file

@ -13,10 +13,15 @@
#include <WiFiClient.h>
#include <WebServer.h>
#include <ESPmDNS.h>
#include "ESPAsyncWebServer.h"
#include <WebSocketsServer.h>
#endif
void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length);
void initWebServer();
void doWebServer();
void handleRoot();

View file

@ -0,0 +1,145 @@
const char MAIN_PAGE[] PROGMEM = R"=====(
<!DOCTYPE html>
<html>
<head>
<script>
var Socket;
function init() {
Socket = new WebSocket('ws://' + window.location.hostname + ':81/');
Socket.onmessage = function(event){
document.getElementById("TempCurrent").innerHTML = event.data;
}
}
</script>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
.switch {
position: relative;
display: inline-block;
width: 60px;
height: 34px;
}
.switch input {
opacity: 0;
width: 0;
height: 0;
}
.slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #ccc;
-webkit-transition: .4s;
transition: .4s;
}
.slider:before {
position: absolute;
content: "";
height: 26px;
width: 26px;
left: 4px;
bottom: 4px;
background-color: white;
-webkit-transition: .4s;
transition: .4s;
}
input:checked + .slider {
background-color: #2196F3;
}
input:focus + .slider {
box-shadow: 0 0 1px #2196F3;
}
input:checked + .slider:before {
-webkit-transform: translateX(26px);
-ms-transform: translateX(26px);
transform: translateX(26px);
}
.slider.round {
border-radius: 34px;
}
.slider.round:before {
border-radius: 50%;
}
</style>
<title>Chinese Diesel Heater Web Controller Interface</title>
</head>
<body onload="javascript:init()">
<h1>Chinese Diesel Heater Web Control</h1>
<div>OFF <label class="switch">
<input type="checkbox" id="onofftoggle" onclick="OnOffCheck()">
<span class="slider round"></span>
</label> ON
</div>
<p id="text" style="display:none">Heater Is ON</p>
<div>
<h2>Temperature Control</h2>
</div>
<input id="slide" type="range" min="1" max="100" step="1" value="10">
<div>
<b>Desired Temp: </b>
<Span id="sliderAmount"></Span>
<div>
</div>
<b>Current Temp: </b><span id="TempCurrent">
<script>
// Function to check the power on/off slide switch.
function OnOffCheck(){
// Get the checkbox status and place in the checkbox variable
var checkBox = document.getElementById("onofftoggle");
// Send a message to the Devel console of web browser for debugging
console.log(document.getElementById("onofftoggle").checked);
// Get the output text
var text = document.getElementById("text");
// If the checkbox is checked, display the output text
// We also need to send a message back into the esp as we cannot directly run Arduino Functions from within the javascript
if (checkBox.checked == true){
//Insert Code Here To Turn On The Heater
// Socket.send("P1");
Socket.send("[CMD]ON");
text.style.display = "block";
}
else{
//Insert Code Here To Turn Off The Heater
text.style.display = "none";
// Socket.send("P0");
Socket.send("[CMD]OFF");
}
}
var slide = document.getElementById('slide');
sliderDiv = document.getElementById("sliderAmount");
slide.oninput = function() {
sliderDiv.innerHTML = this.value;
}
</script>
</body>
</html>
)=====";

View file

@ -107,6 +107,8 @@ CScreen2::show(const CProtocol& CtlFrame, const CProtocol& HtrFrame)
if((_showMode == 0) && (_showSetMode == 0)) {
showRunState();
}
_display.display();
}

View file

@ -14,5 +14,6 @@ class CScreen2 : public CScreen
public:
CScreen2(C128x64_OLED& display, CScreenManager& mgr);
void show(const CProtocol& CtlFrame, const CProtocol& HtrFrame);
void animate() {};
void keyHandler(uint8_t event);
};

View file

@ -112,6 +112,8 @@ CScreen3::show(const CProtocol& CtlFrame, const CProtocol& HtrFrame)
}
}
_display.display();
}

View file

@ -13,5 +13,6 @@ class CScreen3 : public CScreen {
public:
CScreen3(C128x64_OLED& display, CScreenManager& mgr);
void show(const CProtocol& CtlFrame, const CProtocol& HtrFrame);
void animate() {};
void keyHandler(uint8_t event);
};

View file

@ -28,6 +28,7 @@ CScreen4::show(const CProtocol& CtlFrame, const CProtocol& HtrFrame)
_display.printRightJustified("Not active");
}
_display.display();
}

View file

@ -8,5 +8,6 @@ class CScreen4 : public CScreen {
public:
CScreen4(C128x64_OLED& display, CScreenManager& mgr);
void show(const CProtocol& CtlFrame, const CProtocol& HtrFrame);
void animate() {};
void keyHandler(uint8_t event);
};

View file

@ -16,6 +16,7 @@
#include "KeyPad.h"
#include "helpers.h"
#include "clock.h"
#include "BTCConfig.h"
#define MAXIFONT tahoma_16ptFontInfo
#define MINIFONT miniFontInfo
@ -39,9 +40,8 @@
// You **MUST comment out the SPIClass SPI(VSPI);** at the end of the ESP32 SPI library
// then we declare "SPI" here, which will use HSPI!!!!
// 128 x 64 OLED support
SPIClass SPI; // default constructor opens HSPI on standard pins : MOSI=13,CLK=14,MISO=12(unused)
//C128x64_OLED display(OLED_DC_pin, -1, OLED_CS_pin);
CScreenManager::CScreenManager()
{
@ -68,13 +68,18 @@ CScreenManager::~CScreenManager()
void
CScreenManager::init()
{
SPI.setFrequency(8000000);
// 128 x 64 OLED support (Hardware SPI)
_pDisplay = new C128x64_OLED(OLED_DC_pin, -1, OLED_CS_pin);
// SH1106_SWITCHCAPVCC = generate display voltage from 3.3V internally
#if OLED_HW_SPI == 1
SPI.setFrequency(8000000);
_pDisplay = new C128x64_OLED(OLED_DC_pin, -1, OLED_CS_pin);
_pDisplay->begin(SH1106_SWITCHCAPVCC, 0, false);
#else
_pDisplay = new C128x64_OLED(OLED_SDA_pin, OLED_SCL_pin);
_pDisplay->begin(SH1106_SWITCHCAPVCC);
#endif
// Show initial display buffer contents on the screen --
_pDisplay->display();

View file

@ -11,8 +11,8 @@ const uint8_t Rx1Pin = 16;
const uint8_t Tx1Pin = 17;
const uint8_t Rx2Pin = 18;
const uint8_t Tx2Pin = 19;
const uint8_t OLED_SDA = 21; // I2C std pins
const uint8_t OLED_SCK = 22; // "
const uint8_t OLED_SDA_pin = 21; // I2C std pins
const uint8_t OLED_SCL_pin = 22; // "
const uint8_t HC05_SensePin = 23;
const uint8_t OLED_DC_pin = 26;
const uint8_t OLED_CS_pin = 27;