Skip to content

Commit

Permalink
First Release
Browse files Browse the repository at this point in the history
  • Loading branch information
Maggge committed Apr 3, 2022
1 parent 2b03293 commit 2c2f501
Show file tree
Hide file tree
Showing 13 changed files with 395 additions and 257 deletions.
142 changes: 72 additions & 70 deletions ESP_AT_Wifi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,62 +22,78 @@
#ifndef ESP32
#include "ESP_AT_Wifi.h"

ESP_AT_Wifi::ESP_AT_Wifi(HardwareSerial *serial, const char * hostname, const char * ssid, const char * pwd){
ESP_AT_Wifi::ESP_AT_Wifi(HardwareSerial *serial, const char * ssid, const char * pwd, WiFiMode mode){
ser = serial;
_hostname = hostname;
_ssid = ssid;
_pwd = pwd;
_APmode = false;
_mode = mode;
}

ESP_AT_Wifi::ESP_AT_Wifi(HardwareSerial *serial, const char * APname, const char * pwd){
ser = serial;
_hostname = APname;
_pwd = pwd;
_APmode = true;
}

bool ESP_AT_Wifi::begin(uint16_t port, const char *ip){
bool ESP_AT_Wifi::begin(uint16_t port, IPAddress ip, IPAddress subnetMask){
_port = port;
if(!WiFi.init(ser, 115200)) return false;

if(_APmode){
startAP(_ssid, _pwd);
Serial.println("Accesspoint startet!");
Serial.print("SSID: ");
Serial.print(_ssid);
Serial.print(" PWD: ");
Serial.println(_pwd);
if(!WiFi.init(ser, 115200)){
Serial.println(F("No WiFi module found"));
return false;
}
else{

if(_mode == Hybrid_MODE){
Serial.println(F("Start Wifi in Hybrid mode"));
if(!connectToAP(_ssid, _pwd)){
Serial.println("FAILED!");
if(startAP(STANDARD_AP_SSID, STANDARD_AP_PWD)){
Serial.println("Accesspoint startet!");
Serial.print("SSID: ");
Serial.print(STANDARD_AP_SSID);
Serial.print(" PWD: ");
Serial.println(STANDARD_AP_PWD);
Serial.println(F("FAILED!"));
if(!startAP(STANDARD_AP_SSID, STANDARD_AP_PWD)){
return false;
}
Serial.println(F("Accesspoint startet!"));
Serial.print(F("SSID: "));
Serial.print(STANDARD_AP_SSID);
Serial.print(F(" PWD: "));
Serial.println(STANDARD_AP_PWD);
}
else{
Serial.println(F("CONNECTED!"));
if(setIP(ip)){
Serial.print(F("IP Address: "));
Serial.println(ip);
}
if(WiFi.setHostname("MobaStation")){
Serial.println(F("Hostname: Mobastation"));
}
}
}
else if(_mode == Station_Mode){
Serial.println(F("Start Wifi in Station mode"));
if(!connectToAP(_ssid, _pwd)){
Serial.println(F("FAILED!"));
return false;
}
else{
Serial.println("CONNECTED!");
if(ip != NULL){
if(setIP(ip)){
Serial.print("IP Address: ");
Serial.println(ip);
}
Serial.println(F("CONNECTED!"));
if(setIP(ip)){
Serial.print(F("IP Address: "));
Serial.println(ip);
}
if(_hostname != NULL){
if(setHostname(_hostname)){
Serial.print("Hostname: ");
Serial.println(_hostname);
}
if(WiFi.setHostname("MobaStation")){
Serial.println(F("Hostname: Mobastation"));
}
}
}
}
}
else if(_mode == SoftAP_Mode){
Serial.println(F("Start Wifi in Accesspoint mode"));
if(!startAP(STANDARD_AP_SSID, STANDARD_AP_PWD)){
Serial.println(F("FAILED!"));
return false;
}
Serial.println(F("Accesspoint startet!"));
Serial.print(F("SSID: "));
Serial.print(STANDARD_AP_SSID);
Serial.print(F(" PWD: "));
Serial.println(STANDARD_AP_PWD);
}


Udp.begin(port);
Serial.print(F("MobaStation is listen on Port: "));
Serial.println(port);
return true;
}

Expand All @@ -86,9 +102,9 @@ bool ESP_AT_Wifi::connectToAP(const char * ssid, const char * pwd){
_ssid = ssid;
_pwd = pwd;

Serial.print("Connect to ");
Serial.print(F("Connect to "));
Serial.print(ssid);
Serial.print("...");
Serial.print(F("..."));

return WiFi.connectToAP(ssid, pwd);
}
Expand All @@ -101,44 +117,30 @@ bool ESP_AT_Wifi::setIP(const char *ip){
return WiFi.setIP(ip);
}

bool ESP_AT_Wifi::setHostname(const char *hostname){
_hostname = hostname;
return WiFi.setHostname(hostname);
}

bool ESP_AT_Wifi::startAP(const char * ssid, const char * pwd){
Serial.println("Start AccessPoint!");
Serial.println(F("Start AccessPoint!"));
_ssid = ssid;
_pwd = pwd;
WiFi.startAP(ssid, pwd);
return true;
if(WiFi.startAP(ssid, pwd)){
Serial.println(F("Accesspoint startet!"));
Serial.print(F("SSID: "));
Serial.print(ssid);
Serial.print(F(" PWD: "));
Serial.println(pwd);
return true;
}
return false;
}

bool ESP_AT_Wifi::send(IPAddress client, uint16_t DataLen, uint16_t Header, uint8_t *dataString, boolean withXOR){
uint8_t data[24]; //z21 send storage

//--------------------------------------------
//XOR bestimmen:
data[0] = DataLen & 0xFF;
data[1] = DataLen >> 8;
data[2] = Header & 0xFF;
data[3] = Header >> 8;
data[DataLen - 1] = 0; //XOR

for (byte i = 0; i < (DataLen-5+!withXOR); i++) { //Ohne Length und Header und XOR
if (withXOR)
data[DataLen-1] = data[DataLen-1] ^ *dataString;
data[i+4] = *dataString;
dataString++;
}
bool ESP_AT_Wifi::send(IPAddress client, uint8_t *data){
#ifdef DEBUG
Serial.print("Send Packet to ");
Serial.print(F("Send Packet to "));
Serial.println(client);
#endif
//--------------------------------------------
if(!Udp.beginPacket(client, _port)){
#ifdef DEBUG
Serial.println("Send Packet failed");
Serial.println(F("Send Packet failed"));
#endif
return false;
}
Expand Down
12 changes: 4 additions & 8 deletions ESP_AT_Wifi.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,22 @@ class ESP_AT_Wifi : public Eth_Interface {
HardwareSerial *ser;

WiFiUDP Udp;

bool _APmode;

WiFiMode _mode;

const char * _hostname;
const char * _ssid;
const char * _pwd;
uint16_t _port;

public:
ESP_AT_Wifi(HardwareSerial *serial, const char * hostname, const char * ssid, const char * pwd);
ESP_AT_Wifi(HardwareSerial *serial, const char * APname, const char * pwd);
ESP_AT_Wifi(HardwareSerial *serial, const char *ssid, const char *pwd, WiFiMode mode = Hybrid_MODE);

bool begin(uint16_t port, const char *ip = NULL);
bool send(IPAddress client, uint16_t DataLen, uint16_t Header, uint8_t *dataString, boolean withXOR);
bool begin(uint16_t port, IPAddress ip, IPAddress subnetMask);
bool send(IPAddress client, uint8_t *data);
bool receivePacket(UdpPacket *pkg);

bool setIP(IPAddress ip);
bool setIP(const char *ip);
bool setHostname(const char *hostname);
bool connectToAP(const char * ssid, const char * pwd);
bool startAP(const char * ssid, const char * pwd);

Expand Down
31 changes: 11 additions & 20 deletions Eth_Adapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,52 +21,43 @@

#include "Eth_Adapter.h"

#define DEBUG
//#define DEBUG

Eth_Adapter::Eth_Adapter(uint8_t cs, byte * mac){
this->cs = cs;
this->mac = mac;
}

bool Eth_Adapter::begin(uint16_t port, const char *ip = NULL){
bool Eth_Adapter::begin(uint16_t port, IPAddress ip, IPAddress subnetMask){
this->port = port;
this->ip.fromString(ip);
this->ip = ip;
Ethernet.init(cs);

Ethernet.begin(mac, this->ip);
Ethernet.setSubnetMask(subnetMask);

if(Ethernet.hardwareStatus() == EthernetNoHardware){
#ifdef DEBUG
Serial.println("Ethernet shield was not found.");
#endif
return false;
}

if(Ethernet.linkStatus() == LinkOFF){
#ifdef DEBUG
Serial.println("Ethernet cable is not connected.");
#endif
}

Udp.begin(port);
#ifdef DEBUG
Serial.print("Ethernet connected IP: ");
Serial.println(Ethernet.localIP());
#endif
return true;
}

bool Eth_Adapter::send(IPAddress client , uint16_t DataLen, uint16_t Header, byte *dataString, boolean withXOR){
uint8_t data[24]; //z21 send storage

//--------------------------------------------
//XOR bestimmen:
data[0] = DataLen & 0xFF;
data[1] = DataLen >> 8;
data[2] = Header & 0xFF;
data[3] = Header >> 8;
data[DataLen - 1] = 0; //XOR

for (byte i = 0; i < (DataLen-5+!withXOR); i++) { //Ohne Length und Header und XOR
if (withXOR)
data[DataLen-1] = data[DataLen-1] ^ *dataString;
data[i+4] = *dataString;
dataString++;
}
bool Eth_Adapter::send(IPAddress client , uint8_t *data){
#ifdef DEBUG
Serial.print("Send Packet to ");
Serial.println(client);
Expand Down
11 changes: 9 additions & 2 deletions Eth_Adapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,17 @@ class Eth_Adapter : public Eth_Interface{


public:
/**
* @brief Construct a new Eth_Adapter object
*
* @param cs
* @param mac
* @param backup interface, maybe Wifi. If the link is down on startup the backup Interface will be initialized
*/
Eth_Adapter(uint8_t cs, byte * mac);

bool begin(uint16_t port, const char *ip = NULL);
bool send(IPAddress client , uint16_t DataLen, uint16_t Header, byte *dataString, boolean withXOR);
bool begin(uint16_t port, IPAddress ip, IPAddress subnetMask);
bool send(IPAddress client , uint8_t *data);
bool receivePacket(UdpPacket *pkg);

};
Expand Down
6 changes: 3 additions & 3 deletions Eth_Interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@

class Eth_Interface{
public:
virtual bool begin(uint16_t port, const char *ip = NULL);
virtual bool send(IPAddress client , uint16_t DataLen, uint16_t Header, byte *dataString, boolean withXOR);
virtual bool receivePacket(UdpPacket *pkg);
virtual bool begin(uint16_t port, IPAddress ip, IPAddress subnetMask);
virtual bool send(IPAddress client , uint8_t *data);
virtual bool receivePacket(UdpPacket *pkg);
};

#endif
Loading

0 comments on commit 2c2f501

Please sign in to comment.