-->
Page 1 of 1

Fast UART (Serial) TCP Bridge

PostPosted: Fri Sep 18, 2015 4:11 am
by Ripper121
This is a fast version of the WebClient (WIFIClient).
The standard client.print/ln is very slow, so i found a way to get it faster.
If you can make it more faster let us know :)

Max length Serial>TCP: 256 Byte


Code: Select all/*

#include <ESP8266WiFi.h>
ADC_MODE(ADC_VCC);
const char* ssid     = "MySSID";
const char* password = "MyPassword";

const char* host = "192.168.178.1";
const char* streamId   = "....................";
const char* privateKey = "....................";
unsigned char reconnectCount = 0;

WiFiClient client;

void setup() {
  Serial.begin(115200);
  delay(10);
  float batVolt=ESP.getVcc() / 1000.0;
  Serial.println();
  Serial.println();
  Serial.print("Battery: ");
  Serial.print(batVolt);
  Serial.println("V");
  // We start by connecting to a WiFi network

  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    if (reconnectCount >= 60) {
      ESP.restart();
    }
    Serial.print(".");
    reconnectCount++;
  }
  reconnectCount = 0;

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

  connectToServer();
  client.println("ESP8266 Connected");
}


void loop() {
  if (Serial.available()) {
    size_t len = Serial.available();
    uint8_t * sbuf = (uint8_t *)malloc(len);
    Serial.readBytes(sbuf, len);
    if (client.connected()) {
      client.write((uint8_t *)sbuf, len);
      yield();
    } else {
      delay(500);
      if (reconnectCount >= 60) {
        ESP.restart();
      }
      Serial.println("Connection lost!");
      Serial.println("Try to reconnect...");
      connectToServer();
      reconnectCount++;
    }
    free(sbuf);
  }

  if (client.available()) {
    char c = client.read();
    Serial.print(c);
  }
}

void connectToServer() {
  Serial.print("connecting to ");
  Serial.println(host);
  const int httpPort = 21;
  if (!client.connect(host, httpPort)) {
    Serial.println("connection failed");
    return;
  }
}