Created
May 17, 2017 19:47
-
-
Save StefanIGit/67d857573205e5da3b4b1132b2293a1a to your computer and use it in GitHub Desktop.
WiFi remote serial monitor
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
/* This is a WiFi remote serial monitor | |
* It reads stuff from Serial and sends it by WiFi UDP broadcast to a receiver like | |
* Url : http://sockettest.sourceforge.net/ | |
* | |
* I use it on a ESP-01 to install in stuff :) | |
* | |
* by Stefan Schmidt 2017-05-17 | |
*/ | |
#include <ESP8266WiFi.h> | |
#include <WiFiUdp.h> | |
// put here your credentials and port | |
const char* ssid = ""; | |
const char* password = ""; | |
unsigned int UDP_PORT = 514; | |
const int serialSpeed = 115200; | |
const String loggername = "Logger 1"; | |
//#define SERIALDEBUG // uncomment to enable some debug infos like wifi connected | |
#ifdef SERIALDEBUG | |
#define DEBUGPRINT(x) Serial.print(x) //replace debug... with serial... | |
#define DEBUGPRINTLN(x) Serial.println(x) | |
#else | |
#define DEBUGPRINT(x) //replace debug... with ... nothing so nothing is done. | |
#define DEBUGPRINTLN(x) | |
#endif | |
WiFiUDP UDP; | |
char debugBuffer[255]; | |
IPAddress broadcastIp(255, 255, 255, 255); | |
int counter = 0; | |
String tempString = ""; | |
void setup() { | |
Serial.begin(serialSpeed); | |
delay(10); | |
DEBUGPRINTLN(); | |
DEBUGPRINTLN(); | |
DEBUGPRINT("Connecting to "); | |
DEBUGPRINTLN(ssid); | |
WiFi.begin(ssid, password); | |
// WiFi.config(IPAddress(192, 168, 1, 60), IPAddress(192, 168, 1, 1), IPAddress(255, 255, 255, 0)); // uncomment if you want a static ip else dhcp | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
DEBUGPRINT("."); | |
} | |
DEBUGPRINTLN(""); | |
DEBUGPRINTLN("WiFi connected"); | |
DEBUGPRINTLN("IP address: "); | |
DEBUGPRINTLN(WiFi.localIP()); | |
} | |
void loop() { | |
if (Serial.available()) { | |
tempString = Serial.readString(); | |
tempString = loggername +": " + tempString; | |
tempString.toCharArray(debugBuffer, 255); | |
debugSend(); | |
} | |
//sendSysLogMessage(2, 1, "name", "FIRMWARE", 10, counter++, "test"); | |
delay(1); | |
} | |
void debugStart() { | |
debugBuffer[0] = '\0'; | |
} | |
void debugSend() { | |
if (UDP.begin(UDP_PORT)){ | |
DEBUGPRINTLN("UPD Connected"); | |
UDP.beginPacket(broadcastIp, UDP_PORT); | |
UDP.write(debugBuffer); | |
UDP.endPacket(); | |
DEBUGPRINTLN(debugBuffer); | |
UDP.stop(); | |
} else { | |
DEBUGPRINTLN("UDP FAILED!"); | |
} | |
} | |
void debugPrint(char*txt, int nbr) { | |
char buf[100]; | |
sprintf(buf, "%s%i", txt, nbr); | |
strcat(debugBuffer, buf); | |
} | |
void debugPrint(char*txt, long nbr) { | |
char buf[100]; | |
sprintf(buf, "%s%i", txt, nbr); | |
strcat(debugBuffer, buf); | |
} | |
void debugPrint(char*txt, float nbr) { | |
char buf[100]; | |
sprintf(buf, "%s%i", txt, nbr); | |
strcat(debugBuffer, buf); | |
} | |
void debugPrintTxt(char* buf) { | |
strcat(debugBuffer, buf); | |
} | |
void debugPrintTxt(String hh) { | |
char buf[100]; | |
hh.toCharArray(buf, 100); | |
strcat(debugBuffer, buf); | |
} | |
void sendSysLogMessageReal(int severity, int facility, String hostName, String app, int procID, int msgID, String message) { | |
int priVal = (8 * facility) + severity; | |
app.replace(" ", "_"); | |
debugStart(); | |
debugPrint("<", priVal); //PRIVAL | |
debugPrintTxt(">"); | |
debugPrintTxt("1"); // ?? | |
debugPrintTxt(" - "); // Timestamp | |
debugPrintTxt(hostName); | |
debugPrintTxt(" "); | |
debugPrintTxt(app); | |
debugPrint(" ", procID); //PRIVAL | |
debugPrint(" ", msgID); //PRIVAL | |
debugPrintTxt(" "); | |
debugPrintTxt(message); | |
debugSend(); | |
} | |
void sendSysLogMessage(int severity, int facility, String hostName, String app, int procID, int msgID, String message) { | |
sendSysLogMessageReal(severity, facility, hostName, app, procID, msgID, message); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment