Last active
November 24, 2015 22:55
-
-
Save micw/641908000443e6b8d128 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ESP8266WiFi.h> | |
#include <WiFiUdp.h> | |
#include <PacketSerial.h> | |
boolean initialized; | |
PacketSerial serial; | |
WiFiUDP Udp; | |
/** | |
* MessageFormat: | |
* - MessageType: 1 byte = 0 | |
* - MessageLength: 1 byte | |
* - MessageText: n bytes | |
* | |
*/ | |
void sendMessage(uint8_t code,String message) { | |
} | |
void sendError(String message) { | |
sendMessage(1,message); | |
} | |
void sendInfo(String message) { | |
sendMessage(0,message); | |
} | |
/** | |
* MessageFormat: | |
* - MessageType: 1 byte = 1 | |
* - 1 byte WifiSid-Length | |
* - N byte WifiSid | |
* - 1 byte WifiPassword-Length | |
* - N byte WifiPassword | |
* - 2 Byte UDP Receive Port | |
*/ | |
size_t ssidMaxLength=32; | |
char ssid[ssidMaxLength+1]; | |
size_t passwordMaxLength=64; | |
char password[passwordMaxLength+1]; | |
void doSetup(const uint8_t* buffer, size_t size) { | |
if (size<6) { | |
sendError("Data too short"); | |
return; | |
} | |
size_t pos=1; | |
uint8_t ssidLength=buffer[pos++]; | |
if (ssidLength>ssidMaxLength) { | |
sendError("SSID is too long"); | |
return; | |
} | |
if (pos+ssidLength>size) { | |
sendError("SSID is longer than data"); | |
return; | |
} | |
memcpy(ssid, buffer+pos, ssidLength); | |
ssid[ssidLength]=0; | |
pos+=ssidLength; | |
if (pos+2>=size) { | |
sendError("Data too short to contain password"); | |
return; | |
} | |
uint8_t passwordLength=buffer[pos++]; | |
if (passwordLength>passwordMaxLength) { | |
sendError("Password is too long"); | |
return; | |
} | |
if (pos+passwordLength>size) { | |
sendError("Password is longer than data"); | |
} | |
memcpy(password, buffer+pos, passwordLength); | |
password[passwordLength]=0; | |
pos+=passwordLength; | |
if (pos+3>=size) { | |
sendError("Data too short to contain receive port"); | |
return; | |
} | |
unsigned int receivePort=buffer[pos++]<<8 | buffer[pos++]; | |
} | |
void onPacket(const uint8_t* buffer, size_t size) { | |
if (size<1) return; | |
uint8_t msgType=buffer[0]; | |
switch (msgType) { | |
case 1: | |
doSetup(buffer,size); | |
break; | |
} | |
} | |
void setup() { | |
initialized=false; | |
serial.setPacketHandler(&onPacket); | |
serial.begin(115200); | |
} | |
void loop() { | |
serial.update(); | |
} | |
/* | |
* Sketch für einen ESP-8266 (ESP-01), welcher als Serial/UDP Gateway fungiert: | |
* | |
* - verbindet sich mit einem WLAN | |
* - Lauscht an einem vorgegebenen UDP-Port und sendet alle empfangenen Nachrichten an den seriellen Port | |
* - Sendet alle via seriellem Port eingehende Nachrichten via Broadcast an einen vorgegebenen UDP-Port | |
* - Serielles Nachrichtenformat: | |
* - Verwendet die Library "Serialpacket" | |
* - 1. Byte der Nachricht = Typ | |
* - Typ 0: Debug (Outbound-Message) | |
* - Typ 1: Setup (Inbound-Message) | |
* - 1 Byte WifiSid-Length | |
* - N Byte WifiSid | |
* - 1 Byte WifiPassword-Length | |
* - N Byte WifiPassword | |
* - 2 Byte UDP Receive Port | |
* - Typ 2: Status: Connected (Outbound-Message) | |
* - 4 Byte Ip Address | |
* - Typ 3: Status: Disconnected (Outbound-Message) | |
* - no payload | |
* - Typ 4: UDP-Package (Inbound/Outbound-Message) | |
* - 4 Byte remote ip address (Target address for sending, source address for receiving) | |
* - N Byte payload | |
*/ | |
#include <ESP8266WiFi.h> | |
#include <WiFiUdp.h> | |
#include <PacketSerial.h> | |
boolean initialized; | |
PacketSerial serial; | |
WiFiUDP Udp; | |
/** | |
* MessageFormat: | |
* - MessageType: 1 byte = 0 | |
* - MessageLength: 1 byte | |
* - MessageText: n bytes | |
* | |
*/ | |
void sendMessage(uint8_t code,String message) { | |
} | |
void sendError(String message) { | |
sendMessage(1,message); | |
} | |
void sendInfo(String message) { | |
sendMessage(0,message); | |
} | |
/** | |
* MessageFormat: | |
* - MessageType: 1 byte = 1 | |
* - 1 byte WifiSid-Length | |
* - N byte WifiSid | |
* - 1 byte WifiPassword-Length | |
* - N byte WifiPassword | |
* - 2 Byte UDP Receive Port | |
*/ | |
size_t ssidMaxLength=32; | |
char ssid[ssidMaxLength+1]; | |
size_t passwordMaxLength=64; | |
char password[passwordMaxLength+1]; | |
void doSetup(const uint8_t* buffer, size_t size) { | |
if (size<6) { | |
sendError("Data too short"); | |
return; | |
} | |
size_t pos=1; | |
uint8_t ssidLength=buffer[pos++]; | |
if (ssidLength>ssidMaxLength) { | |
sendError("SSID is too long"); | |
return; | |
} | |
if (pos+ssidLength>size) { | |
sendError("SSID is longer than data"); | |
return; | |
} | |
memcpy(ssid, buffer+pos, ssidLength); | |
ssid[ssidLength]=0; | |
pos+=ssidLength; | |
if (pos+2>=size) { | |
sendError("Data too short to contain password"); | |
return; | |
} | |
uint8_t passwordLength=buffer[pos++]; | |
if (passwordLength>passwordMaxLength) { | |
sendError("Password is too long"); | |
return; | |
} | |
if (pos+passwordLength>size) { | |
sendError("Password is longer than data"); | |
} | |
memcpy(password, buffer+pos, passwordLength); | |
password[passwordLength]=0; | |
pos+=passwordLength; | |
if (pos+3>=size) { | |
sendError("Data too short to contain receive port"); | |
return; | |
} | |
unsigned int receivePort=buffer[pos++]<<8 | buffer[pos++]; | |
} | |
void onPacket(const uint8_t* buffer, size_t size) { | |
if (size<1) return; | |
uint8_t msgType=buffer[0]; | |
switch (msgType) { | |
case 1: | |
doSetup(buffer,size); | |
break; | |
} | |
} | |
void setup() { | |
initialized=false; | |
serial.setPacketHandler(&onPacket); | |
serial.begin(115200); | |
} | |
void loop() { | |
serial.update(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment