Skip to content

Instantly share code, notes, and snippets.

@gabonator
Created June 24, 2018 18:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gabonator/e4ffbaf08292ade2cc11bf44c1b073c1 to your computer and use it in GitHub Desktop.
Save gabonator/e4ffbaf08292ade2cc11bf44c1b073c1 to your computer and use it in GitHub Desktop.
ESP8266 uart monitor and uploader
#include <ESP8266WiFi.h>
const char* ssid = "*********";
const char* password = "*********";
const int bufferLength = 4096;
int bufferIndex = 0;
char buffer[bufferLength];
char bufferTemp[bufferLength];
long lastSend = 0;
void setup()
{
Serial.begin(115200);
delay(100);
connect();
}
void connect()
{
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
delay(500);
Serial.println("<wifi connected>");
}
void loop()
{
long now = millis();
_yield();
if (bufferIndex > bufferLength-bufferLength/4 || now-lastSend > 10*1000)
{
lastSend = now;
memcpy(bufferTemp, buffer, bufferLength);
int nLength = bufferIndex;
bufferIndex = 0;
upload(bufferTemp, nLength);
}
}
void _yield()
{
yield();
while (Serial.available())
{
char c = Serial.read();
if (bufferIndex < bufferLength)
{
buffer[bufferIndex++] = c;
}
}
}
void upload(char* buffer, int length)
{
WiFiClient client;
if (!client.connect("*********", 80)) {
Serial.println("<connection failed>");
return;
}
_yield();
Serial.print("<sending>");
client.println("POST /*****/?uartmon HTTP/1.1");
client.println("Host: *********");
client.println("Accept: */*");
client.println("Content-Type: application/x-www-form-urlencoded");
client.print("Content-Length: ");
client.println(length);
client.println();
client.write((byte*)buffer, length);
char last[5] = {0, 0, 0, 0, 0};
bool body = false;
while (client.connected())
{
while (client.available())
{
_yield();
char c = (char)client.read();
if (body)
Serial.print((char)c);
last[0]=last[1];
last[1]=last[2];
last[2]=last[3];
last[3]=c;
body = body | (strcmp(last, "\r\n\r\n")==0);
}
}
client.stop();
Serial.print("<ok>");
}
<?
header("Access-Control-Allow-Origin: *");
header('Access-Control-Allow-Methods: POST, GET, OPTIONS');
header('Access-Control-Allow-Headers: X-Requested-With, content-type');
$channel = preg_replace("/[^A-Za-z0-9 ]/", '', $_SERVER["QUERY_STRING"]);
$content = file_get_contents("php://input");
file_put_contents("data/last_".$channel, $content);
if ($content != "" && $content != "\n")
{
file_put_contents("data/".$channel."/".getCurrent(), $content, FILE_APPEND);
}
function getCurrent()
{
return date("Y-m-d").".txt";
}
function getTs()
{
return date("Y-m-d H:i:s");
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment