Skip to content
This repository has been archived by the owner on Feb 24, 2022. It is now read-only.

Commit

Permalink
Merge branch 'WhereIWork'
Browse files Browse the repository at this point in the history
  • Loading branch information
LeBellier committed Sep 12, 2017
2 parents 5f17286 + ead14d0 commit 2dd6cd5
Show file tree
Hide file tree
Showing 10 changed files with 1,213 additions and 208 deletions.
29 changes: 0 additions & 29 deletions src/Aspect.h

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ ESP8266TelnetServer::~ESP8266TelnetServer() {
void ESP8266TelnetServer::begin() {
telnet_srv.begin();
telnet_srv.setNoDelay(true);
DEBUG_INIT_PRINTLN("Please connect Telnet Client.");
DEBUG_SVR_TELNET("Please connect Telnet Client.");
}

void ESP8266TelnetServer::handleClient() {
Expand All @@ -27,17 +27,26 @@ void ESP8266TelnetServer::handleClient() {
if (!hasConnectedClient()) {
stopClient();
serverClient = telnet_srv.available();
DEBUG_PRINTLN("New Telnet client");
DEBUG_SVR_TELNET("New Telnet client");
serverClient.flush(); // clear input buffer, else you get strange characters
}
}

while (serverClient.available()) { // get data from Client

char charRX = serverClient.read();
if (charRX == 'q') {
stopClient();
if (charRX == 13 || charRX == 10) {
if (nbChar > 0) {
readCallBack(artnetPacket, nbChar);
nbChar = 0;
}
} else if (nbChar == MAX_BUFFER_TELNET) {
DEBUG_SVR_TELNET("Too many char in this message");
nbChar = 0;
} else if (charRX >= 0) {
artnetPacket[nbChar] = charRX;
nbChar++;
}
DEBUG_PRINT((String ) charRX);
}
}
bool ESP8266TelnetServer::hasConnectedClient() {
Expand All @@ -47,7 +56,7 @@ bool ESP8266TelnetServer::hasConnectedClient() {
void ESP8266TelnetServer::stopClient() {
if (serverClient) {
serverClient.stop();
DEBUG_PRINTLN("Telnet Client Stop");
DEBUG_SVR_TELNET("Telnet Client Stop");
}
}

Expand All @@ -66,3 +75,23 @@ size_t ESP8266TelnetServer::write(const uint8_t* buffer, size_t size) {
delay(10); // to avoid strange characters left in buffer
return size;
}
void ESP8266TelnetServer::setDebug(bool param) {
_debug = param;
}

//read data callback
void ESP8266TelnetServer::setReadCallback(void (*func)(char*, uint8_t)) {
readCallBack = func;
}
template<typename Generic>
void ESP8266TelnetServer::DEBUG_SVR_TELNET(Generic text) {
if (_debug) {
if (hasConnectedClient()) {
print("*TelS*: ");
println(text);
} else {
Serial.print("*TelS*: ");
Serial.println(text);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@
#define _ESP8266TELNETSERVER_H_

#include <ESP8266WiFi.h>
#include <Aspect.h>
#include <String.h>

// Buffers
#define MAX_BUFFER_TELNET 255

#define END_CHAR 'o'

class ESP8266TelnetServer: public Print {

Expand All @@ -27,9 +32,20 @@ class ESP8266TelnetServer: public Print {
size_t write(uint8_t) override;
size_t write(const uint8_t *, size_t) override;

void setDebug(bool);
//called when read some char ends by \n
void setReadCallback(void (*func)(char*, uint8_t));

private:
WiFiClient serverClient;
void (*readCallBack)(char*, uint8_t) = NULL;

char artnetPacket[MAX_BUFFER_TELNET];
uint8_t nbChar = 0;

bool _debug = true;
template<typename Generic>
void DEBUG_SVR_TELNET(Generic text);
};

#endif /* _ESP8266TELNETSERVER_H_ */
2 changes: 0 additions & 2 deletions src/MatrixStrip.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
#include <Adafruit_NeoPixel.h>
#include "MatrixStrip.h"
#include "ConstCharColor.h"

MatrixStrip::MatrixStrip(uint8_t pinStrip, uint8_t nbRows, uint8_t nbColumns) :
Adafruit_NeoPixel(nbRows * nbColumns, pinStrip, NEO_GRB + NEO_KHZ800), nbRows(
Expand Down
185 changes: 112 additions & 73 deletions src/ServerManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,88 +5,106 @@
* Author: Bruno
*/

#include <ServerManager.h>
#include "ServerManager.h"

ServerManager::ServerManager() {

}
ServerManager::~ServerManager() {
}

void ServerManager::initDnsHttpFtpOtaTelnetServers(char* dnsName, char* ftpUser,
char* ftpPasseWord, char*otaHostName, char*otaPasseWord) {

//init spiffs (spi file system)
if (!SPIFFS.begin()) {
DEBUG_INIT_PRINTLN("Can't open de File system");
}

//init DNS
if (mdns.begin(dnsName, WiFi.localIP())) {
DEBUG_INIT_PRINTLN("MDNS responder started");
DEBUG_INIT_PRINT("You can use the name: http://");
DEBUG_INIT_PRINT(dnsName);
DEBUG_INIT_PRINTLN(".local/");
}

//init Http Server
// in the setup of the .ino
//serverManager.getHttpServer().onNotFound(handleRequestOnFile);
//serverManager.getHttpServer().on("/pixel", HTTP_GET, pixelRequest); // and all other adresse page you want out of file
httpServer.begin();
DEBUG_INIT_PRINTLN("HTTP server started");

//init Ftp Server
ftpSrv.begin(ftpUser, ftpPasseWord); //username, password for ftp. set ports in ESP8266FtpServer.h (default 21, 50009 for PASV)
DEBUG_INIT_PRINT("FTP server started; MdP:");
DEBUG_INIT_PRINT(ftpPasseWord);
DEBUG_INIT_PRINT(", User:");
DEBUG_INIT_PRINTLN(ftpUser);

//init Ota Server
//ArduinoOTA.setPort(8266);// Port defaults to 8266
ArduinoOTA.setHostname(otaHostName); // Hostname defaults to esp8266-[ChipID]
ArduinoOTA.setPassword(otaPasseWord); // No authentication by default
ArduinoOTA.begin();
DEBUG_INIT_PRINTLN("OTA server started");

//init Telnet Server
telnetServeur.begin();

#ifdef DEBUG_INIT
delay(20);
#endif
void ServerManager::setServersON(uint8_t status) {
serversON = status;

if (!SPIFFS.begin()) { //init spiffs (spi file system)
DEBUG_SVR_M("Can't open de File system");
}

if (serversON & SERVER_TELNET) { //init Telnet Server
telnetSvr.reset(new ESP8266TelnetServer());
telnetSvr->begin();
}
if (serversON & SERVER_DNS) { //init DNS
dnsSvr.reset(new MDNSResponder());
//init DNS
if (dnsSvr->begin(dnsName, WiFi.localIP())) {
DEBUG_SVR_M("MDNS responder started");
String msg = F("You can use the name: http://{1}.local/");
msg.replace("{1}", dnsName);
DEBUG_SVR_M(msg);
}
}
if (serversON & SERVER_HTTP) { //init Http Server
httpSvr.reset(new ESP8266WebServer());
// in the setup of the .ino
//httpSvr->onNotFound(handleRequestFile);
//serverManager.getHttpServer().on("/pixel", HTTP_GET, pixelRequest); // and all other adresse page you want out of file
httpSvr->begin();
DEBUG_SVR_M("HTTP server started");
}

if (serversON & SERVER_FTP) { //init Ftp Server
ftpSvr.reset(new FtpServer());
ftpSvr->begin(userName, passeword); //username, password for ftp. set ports in ESP8266FtpServer.h (default 21, 50009 for PASV)
String msg = F("FTP server started; MdP:{1}, User:{2}");
msg.replace("{2}", userName);
msg.replace("{1}", passeword);
DEBUG_SVR_M(msg);
}

if (serversON & SERVER_OTA) { //init Ota Server
//ArduinoOTA.setPort(8266);// Port defaults to 8266
ArduinoOTA.setHostname(userName); // Hostname defaults to esp8266-[ChipID]
ArduinoOTA.setPassword(passeword); // No authentication by default
ArduinoOTA.begin();
String msg = F("OTA server started; MdP:{1}, User:{2}");
msg.replace("{2}", userName);
msg.replace("{1}", passeword);
DEBUG_SVR_M(msg);
}

if (_debug) {
delay(20);
}
}

void ServerManager::updateServers() {
mdns.update();
ftpSrv.handleFTP();
httpServer.handleClient();
void ServerManager::update() {
ArduinoOTA.handle();
telnetServeur.handleClient();
if (serversON & SERVER_TELNET) {
telnetSvr->handleClient();
}
if (serversON & SERVER_DNS) {
dnsSvr->update();
}
if (serversON & SERVER_HTTP) {
httpSvr->handleClient();
}
if (serversON & SERVER_FTP) {
ftpSvr->handleFTP();
}
}
String ServerManager::printRequest() {
String message = "URI: ";
message += httpServer.uri();
message += httpSvr->uri();
message += "\r\n Method: ";
message += (httpServer.method() == HTTP_GET) ? "GET" : "POST";
message += (httpSvr->method() == HTTP_GET) ? "GET" : "POST";
message += "\r\n Arguments: ";
message += httpServer.args();
for (uint8_t i = 0; i < httpServer.args(); i++) {
message += "\r\n NAME:" + httpServer.argName(i) + " VALUE:"
+ httpServer.arg(i);
message += httpSvr->args();
for (uint8_t i = 0; i < httpSvr->args(); i++) {
message += "\r\n NAME:" + httpSvr->argName(i) + " VALUE:" + httpSvr->arg(i);
}
return message;
}
void ServerManager::handleRequestFile() {
String uriAsked = httpServer.uri();
printlnDebug(uriAsked);
String uriAsked = httpSvr->uri();
DEBUG_SVR_M(uriAsked);

//check the request
if (!loadFromSpiffs(uriAsked)) { // no file at the uri found
String message = "File Not Detected ";
message += printRequest();
httpServer.send(404, "text/plain", message);
printlnDebug(message);
httpSvr->send(404, "text/plain", message);
DEBUG_SVR_M(message);
}
}

Expand Down Expand Up @@ -123,29 +141,50 @@ bool ServerManager::loadFromSpiffs(String path) {
return false;
}

if (httpServer.hasArg("download"))
if (httpSvr->hasArg("download"))
dataType = "application/octet-stream";
if (httpServer.streamFile(dataFile, dataType) != dataFile.size()) {
printlnDebug("Sent less data than expected!");
if (httpSvr->streamFile(dataFile, dataType) != dataFile.size()) {
DEBUG_SVR_M("Sent less data than expected!");
return false;
}
dataFile.close();
return true;
}

void ServerManager::printDebug(String s) {
if (telnetServeur.hasConnectedClient()) {
telnetServeur.print(s);
} else {
DEBUG_PRINT(s);
}
uint8_t ServerManager::isServersON() {
return serversON;
}

void ServerManager::setDnsName(char*param) {
dnsName = param;
}
void ServerManager::setUserName(char*param) {
userName = param;
}
void ServerManager::setPasseword(char*param) {
passeword = param;
}
void ServerManager::setDebug(bool param) {
_debug = param;
}

void ServerManager::printDebug(String text) {
DEBUG_SVR_M(text);
}
void ServerManager::setReadTelnetCallback(void (*func)(char*, uint8_t)) {
telnetSvr->setReadCallback(func);
}

void ServerManager::printlnDebug(String s) {
if (telnetServeur.hasConnectedClient()) {
telnetServeur.println(s);
} else {
DEBUG_PRINTLN(s);
template<typename Generic>
void ServerManager::DEBUG_SVR_M(Generic text) {
if (_debug) {
if (telnetSvr->hasConnectedClient()) {
telnetSvr->print("*SM*: ");
telnetSvr->println(text);
} else {
Serial.print("*SM*: ");
Serial.println(text);
}
}
}

Expand Down
Loading

0 comments on commit 2dd6cd5

Please sign in to comment.