Added WebScoket sessions

Have added a place holder for temp data
Slider now working
Off Off slide switch should work.

Not a full test code rollout
This commit is contained in:
James 2018-11-25 04:04:37 +11:00
parent 297757c4a7
commit 603bb2eb14
6 changed files with 135 additions and 283 deletions

View file

@ -7,13 +7,13 @@
#include "TxManage.h"
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 +38,29 @@ 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");
}
unsigned char cVal;
void doWebServer(void) {
webSocket.loop();
server.handleClient();
}
void webturnOn() {
TxManage.queueOnRequest();
server.send(200, "text/plain", "Heater Turning on");
char c[] = { "23" };
webSocket.broadcastTXT(c, sizeof(c));
}
void webturnOff() {
TxManage.queueOffRequest();
server.send(200, "text/plan", "Turning off heater");
}
extern void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t length) {
if (type == WStype_TEXT) {
for (int i = 0; i < length; i++)
Serial.print((char)payload[i]);
Serial.println();
}
}

View file

@ -13,10 +13,16 @@
#include <WiFiClient.h>
#include <WebServer.h>
#include <ESPmDNS.h>
#include "ESPAsyncWebServer.h"
#include "Index.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();

File diff suppressed because one or more lines are too long

View file

@ -1,103 +0,0 @@
/*
WiFiTelnetToSerial - Example Transparent UART to Telnet Server for ESP32
Copyright (c) 2017 Hristo Gochkov. All rights reserved.
This file is part of the ESP32 WiFi library for Arduino environment.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <WiFi.h>
#include <WiFiMulti.h>
WiFiMulti wifiMulti;
//how many clients should be able to telnet to this ESP32
#define MAX_SRV_CLIENTS 1
const char* ssid = "Derpina";
const char* password = "randomsharedkey1";
WiFiServer server(23);
WiFiClient serverClients[MAX_SRV_CLIENTS];
void setup() {
Serial.begin(115200);
Serial.println("\nConnecting");
wifiMulti.addAP(ssid, password);
wifiMulti.addAP("ssid_from_AP_2", "your_password_for_AP_2");
wifiMulti.addAP("ssid_from_AP_3", "your_password_for_AP_3");
Serial.println("Connecting Wifi ");
for (int loops = 10; loops > 0; loops--) {
if (wifiMulti.run() == WL_CONNECTED) {
Serial.println("");
Serial.print("WiFi connected ");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
break;
}
else {
Serial.println(loops);
delay(1000);
}
}
if (wifiMulti.run() != WL_CONNECTED) {
Serial.println("WiFi connect failed");
delay(1000);
ESP.restart();
}
//start UART and the server
server.begin();
server.setNoDelay(true);
Serial.print("Ready! Use 'telnet ");
Serial.print(WiFi.localIP());
Serial.println(" 23' to connect");
}
int testval=0;
void loop() {
testval=testval+1;
uint8_t i;
Serial.println(testval);
if (wifiMulti.run() == WL_CONNECTED) {
//check if there are any new clients
if (server.hasClient()){
for(i = 0; i < MAX_SRV_CLIENTS; i++){
//find free/disconnected spot
if (!serverClients[i] || !serverClients[i].connected()){
if(serverClients[i]) serverClients[i].stop();
serverClients[i] = server.available();
if (!serverClients[i]) Serial.println("available broken");
Serial.print("New client: ");
Serial.print(i); Serial.print(' ');
Serial.println(serverClients[i].remoteIP());
break;
server.print(Serial.read());
}
}
if (i >= MAX_SRV_CLIENTS) {
//no free/disconnected spot so reject
server.available().stop();
}
}
}
else {
Serial.println("WiFi not connected!");
for(i = 0; i < MAX_SRV_CLIENTS; i++) {
if (serverClients[i]) serverClients[i].stop();
}
delay(1000);
}
}

View file

@ -1,50 +0,0 @@
//#define SERIAL DebugPort
#define SERIAL SerialAndTelnet
#include "BTCTelnetSpy.h"
TelnetSpy SerialAndTelnet;
void waitForConnection() {
while (WiFi.status() != WL_CONNECTED) {
delay(500);
SerialAndTelnet.print(".");
}
SerialAndTelnet.println(" Connected!");
}
void waitForDisconnection() {
while (WiFi.status() == WL_CONNECTED) {
delay(500);
SerialAndTelnet.print(".");
}
SerialAndTelnet.println(" Disconnected!");
}
void telnetConnected() {
SerialAndTelnet.println("Telnet connection established.");
}
void telnetDisconnected() {
SerialAndTelnet.println("Telnet connection closed.");
}
void initTelnetSpy() {
SerialAndTelnet.setWelcomeMsg("Welcome to the TelnetSpy example\n\n");
SerialAndTelnet.setCallbackOnConnect(telnetConnected);
SerialAndTelnet.setCallbackOnDisconnect(telnetDisconnected);
waitForConnection();
/*
SerialAndTelnet.println("Ready");
SerialAndTelnet.print("IP address: ");
SerialAndTelnet.println(WiFi.localIP());
SerialAndTelnet.println("\nType 'C' for WiFi connect.\nType 'D' for WiFi disconnect.\nType 'R' for WiFi reconnect.");
SerialAndTelnet.println("All other chars will be echoed. Play around...\n");
*/
}
void DoTelnetSpy() {
SerialAndTelnet.handle();
}

View file

@ -1,10 +0,0 @@
#include <Arduino.h>
#include "BTCWifi.h"
#include <TelnetSpy.h>
void initTelnetSpy();
void DoTelnetSpy();
void waitForConnection();
void waitForDisconnection();
void telnetConnected();
void telnetDisconnected();