Post topics, source code that relate to the Arduino Platform

User avatar
By jobenas
#42901 Greetings,

I've been trying to setup a tcp socket server with the arduino ide (using the esp8266 arduino core) and the WifiManager library to make an easy setup on different networks. The code is simple enough, I set up the server and wait for a client. If a client connects and sends data to the server that data must be sent through the serial port. Here's the code:

Code: Select all#include <ESP8266WiFi.h>          //https://github.com/esp8266/Arduino

//needed for library
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>         //https://github.com/tzapu/WiFiManager

WiFiServer server(4998);
WiFiClient client;

char data[1500];
int ind = 0;

void setup() {
    // put your setup code here, to run once:
    Serial.begin(38400);

    //WiFiManager
    //Local intialization. Once its business is done, there is no need to keep it around
    WiFiManager wifiManager;
    //reset saved settings
    //wifiManager.resetSettings();
   
    //set custom ip for portal
    //wifiManager.setAPConfig(IPAddress(10,0,1,1), IPAddress(10,0,1,1), IPAddress(255,255,255,0));

    //fetches ssid and pass from eeprom and tries to connect
    //if it does not connect it starts an access point with the specified name
    //here  "AutoConnectAP"
    //and goes into a blocking loop awaiting configuration
    wifiManager.autoConnect("AutoConnectAP");
    //or use this for auto generated name ESP + ChipID
    //wifiManager.autoConnect();

   
    //if you get here you have connected to the WiFi
    Serial.println("Connection established");

    server.begin();
    Serial.println("Server started");
    Serial.setDebugOutput(true);
}

void loop() {
    // put your main code here, to run repeatedly:
    if(!client.connected())
  {
    //try to connect to a new client
    client = server.available();
  }
  else
  {
    if(client.available() > 0)
    {
      while(client.available())
      {
        data[ind] = client.read();
        ind++;
      }   
      client.flush();
 
     
     
      //Serial.println(data);
      for(int j=0;j < ind; j++)
      {
        Serial.print(data[j]);
      }
      ind = 0;
      client.print("OK!");     
    }

  }
}


After a full day more or less, the esp8266 becomes unresponsive. Either it completely disconnects from the network or in some cases (the ones that are weirder) remains connected to the network but doesn't respond to client messages. It was suggested to me that using yield() within loop() could help. But from the documentation I understand that the internal processes of the esp are done at the end of each loop() iteration. I would really appreciate any help on the matter.
User avatar
By Shivank
#67177 Hi,
Great post,
I am trying to make a ESP - Laptop connection through wifi. I need to know what basic setup is needed like: - software in laptop for sending messages to ESP.
I am using Arduino ide to program ESP.
Please suggest link, video or any tutorial.
User avatar
By QuickFix
#67191 Uhm Telnet?
I have to admit I haven't tested the sketch, but by the looks of it, it only echo's data from a TCP connected client to the serial port.

So (in theory):
  • Upload sketch to ESP, making sure you've set correct settings for your network
  • Start ESP (you should get the text "Connection established" and "Server started" over serial)
  • Make sure Telnet is installed (if using Windows:
    • Control Panel\All Control Panel Items\Programs and Features
    • Select Windows features
    • Telnet client)
  • Open a command-prompt and enter:
    Code: Select alltelnet ip.address.of.esp 4998

    Of course ip.address.of.esp is the actual IP-address of your ESP :idea:, eg.
    Code: Select alltelnet 192.168.4.1 4998
  • Knock yourself out 8-)
But again: this is how I read the code: I haven't played with it myself.
Remember though that TS is having an unresolved problem with the code, so use it at your own risk. :idea:

[UPDATE]
I've just tested it and it works like I thought:
Image