Post topics, source code that relate to the Arduino Platform

User avatar
By olegivani4
#42750 Hello guys,

I have ESP-01 connected to my Arduino Uno and I want to send some data from Uno to remote server.
On remote host I have application which is reading specific socket port.
From Uno using AT commands I'm connecting to remote host and then sending data each second. Everything work good first 30 seconds, but then ESP gets crazy and starts to send to remote server AT commands ("AT+CI") instead of data. but TCP connection is still open and remote servers is receiving "AT+CI" string.

setup
AT+CIPSTART="TCP","100.100.100.100",1234
OK

loop
AT+CIPSEND=5
>
1:3
+IPD...

Do somebody have any idea why it's happening?
User avatar
By olegivani4
#42774 Arduino code
Code: Select all#include <SoftwareSerial.h>

class Esp {
  public:
    Esp(long baud);
    void start();
    void enableDebug();
    void reset();
    boolean connectedOnStart();
    boolean wifiConnect(String ssid, String password);
    boolean tcpConnect(String server, int port);
    boolean sendMessage(String message);
    void changeMode(int mode);
  private:
    long _baud;
    boolean _debug = false;
    void printBuffer();
    void clearBuffer();
    char buffer[512];
    int bufferIndex = -1;
    boolean readAndWait(String str, int timeout);
    boolean waitFor(String str);
};

const unsigned long baud = 115200;
SoftwareSerial mySerial(3, 2); // USB to TTL for debugging
Esp esp(baud);

void setup() {
  esp.start();
  esp.changeMode(1);
  esp.enableDebug();
  esp.reset();

  if (!esp.connectedOnStart()) {
    esp.wifiConnect("ssid", "password");
  }

  esp.tcpConnect("100.100.100.100", 1234);
}

void loop() {
  String m = "";
  m += millis();
  esp.sendMessage(m);
}

Esp::Esp(long baud) {
  _baud = baud;
}

void Esp::start() {
  // Open serial communications and wait for port to open:
  Serial.begin(_baud);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
}

void Esp::reset() {
  Serial.println("AT+RST");
}

void Esp::enableDebug() {
  _debug = true;
  mySerial.begin(_baud);
  mySerial.println("App started");
}

boolean Esp::connectedOnStart() {
  return readAndWait("GOT IP", 10);
}

boolean Esp::readAndWait(String str, int timeout) {
  int started = millis();
  int maxTime = 1000 * timeout;
  boolean bufferChanged;
  boolean res = false;

  while ((millis() - started) < maxTime) {
    bufferChanged = false;
    if (Serial.available()) {
      buffer[++bufferIndex] = Serial.read();
      bufferChanged = true;
    }

    if (waitFor(str)) {
      res = true;
      break;
    }

    if (bufferIndex == 510) {
      printBuffer();
      clearBuffer();
    }
  }

  if (bufferIndex > -1) {
    printBuffer();
    clearBuffer();
  }

  return res;
}

void Esp::printBuffer(){
  if (_debug) {
    buffer[++bufferIndex] = '\0';
    mySerial.print(buffer);
  }
}

void Esp::clearBuffer(){
  for(int i = 0; i <= bufferIndex; i++) {
    buffer[i] = (char)0;
  }
  bufferIndex = -1;
}

boolean Esp::waitFor(String str) {
  int length = str.length();
  for (int i = 0; i < length; i++) {
    if (buffer[bufferIndex - (length - 1 - i)] != str.charAt(i)) {
      return false;
    }
  }
  return true;
}

void Esp::changeMode(int mode) {
  String cmd = "AT+CWMODE=";
  cmd += mode;
  Serial.println(cmd);

  readAndWait("OK\r\n", 2);
}

boolean Esp::wifiConnect(String ssid, String password) {
  String cmd = "AT+CWJAP=\"";
  cmd += ssid;
  cmd += "\",\"";
  cmd += password;
  cmd += "\"";
  Serial.println(cmd);
  return readAndWait("OK\r\n", 10);
}

boolean Esp::tcpConnect(String server, int port) {
  String cmd = "AT+CIPSTART=\"TCP\",\"";
  cmd += server;
  cmd += "\",";
  cmd += port;
  Serial.println(cmd);
  return readAndWait("OK\r\n", 10);
}

boolean Esp::sendMessage(String message) {
  String cmd = "AT+CIPSEND=";
  cmd += (message.length() + 2);
  Serial.println(cmd);

  readAndWait(">", 5);
  Serial.println(message);

  return readAndWait("+IPD", 5);
}


Socket server code (node.js)
Code: Select allvar net = require('net');
var HOST = '0.0.0.0';
var PORT = 1234;

net.createServer(function(sock) {
    console.log('CONNECTED: ' + sock.remoteAddress +':'+ sock.remotePort);
    sock.on('data', function(data) {
        console.log('DATA ' + sock.remoteAddress + ': ' + data);
        sock.write('OK');
    });

    sock.on('close', function(data) {
        console.log('CLOSED: ' + sock.remoteAddress +' '+ sock.remotePort);
    });

}).listen(PORT, HOST);

console.log('Server listening on ' + HOST +':'+ PORT);
User avatar
By olegivani4
#42912 I have also tryed to close TCP connection after 25 sec and reconnect again. Second connection got crazy after 5 sec. So in total we have same 30 sec.

Also have tryed to put different delays ( 1 sec and 5 sec) and had same results.

My ESP has SDK 1.3 AI Thinker