Post topics, source code that relate to the Arduino Platform

User avatar
By hakha4
#14297 Hi
Just started with esp8266 using Itead lib and managed to write a sketch for connecting to a server and listen on a port BUT I can' t get it to work if both client/server runs simultanesly.
I have a home control system using MLServer as base. My aim is to be able to send sensordata FROM Arduino but also be able to read commands from MLServer. Send to port 23 and listen on port 12000
My question is , can this be done? or am I struggling with something thats impossible?
I've been reading the most on Google but never find a clue on my specific issue. Any idea,link to point me in right direction appreciated
User avatar
By chrisjx
#14350 Sorry I have no answer but I am looking at the same issue.

I would like to send my sensor data from arduino to esp8266, then have esp8266 send data to a TCP listener on an installation of Node-Red (http://nodered.org/).

I would also like to be able to make the esp8266 a tcp listener so that I can actuate things with the arduino based on instructions received by the TCP listener.

I have tried but have failed at trying to make the ESP8266 connect to the node-red tcp listener.

Maybe you have some advice, or maybe other users on this forum have some advice.

Thanks,
Chris.
User avatar
By hakha4
#14377 Hi Chris
I'm suprised that this issue isn't more discussed. I belive that a lot of project's need bidirectonal communication. Haven't tested but a 'dirty' way could be one ESP8266 for client and one for server but then you need an xtra serial port and the cost's / space of one more ESP8266.

Below is a test sketch reading temp/humidity. In setup the ESP connects and server listen on my port without errors BUT if I enable recieve data part it reboots over and over again. If I disable this part humidity/tempdata is sent as expected.
Buffer issues ?? I've tested back and forth with trial and terror method but I'm stucked. What library are you using ?

Code: Select all#include "ESP8266.h"
#include <SoftwareSerial.h>
#include <dht.h>

dht DHT;
#define DHT11_PIN 2

SoftwareSerial wifiPort(10, 11); // RX, TX


#define SSID        "XXXXXXX"
#define PASSWORD    "YYYYYYYY"
#define HOST_NAME   "192.168.1.4"// Pi Gateway
#define HOST_PORT   (32340)
#define SERVER_PORT (12000)
ESP8266 wifi(wifiPort);

 
 String StringToSend;
 String Node_Id = "3";
 String myIP;
void setup(void)
{


  Serial.begin(9600);
  Serial.print("Setup begin\r\n");

  Serial.print("FW Version:");
  Serial.println(wifi.getVersion().c_str());

  if (wifi.setOprToStationSoftAP()) {
    Serial.print("To station + softap ok\r\n");
  } else {
    Serial.print("To station + softap err\r\n");
  }

  if (wifi.joinAP(SSID, PASSWORD)) {
    Serial.print("Join AP success\r\n");
    Serial.print("IP:");
    myIP = wifi.getLocalIP().c_str();
    Serial.println( myIP);
   
  } else {
    Serial.print("Join AP failure\r\n");
  }
  if (wifi.enableMUX()) {
        Serial.print("multiple ok\r\n");
    } else {
        Serial.print("multiple err\r\n");
    }
//server
if (wifi.startTCPServer(SERVER_PORT)) {
        Serial.print("start tcp server at port : ");
        Serial.print(SERVER_PORT);
        Serial.print(" ok\r\n");
    } else {
       Serial.print("start tcp server at port : ");
        Serial.print(SERVER_PORT);
        Serial.print(" ERROR\r\n");
    }
   
    if (wifi.setTCPServerTimeout(10)) {
        Serial.print("set tcp server timout 10 seconds\r\n");
       }
     else {
        Serial.print("set tcp server timout err\r\n");
    }
    ///////////////////////////////

  Serial.print("Setup end\r\n");
delay(1000);
}

void loop(void)
{
  char humidityData[10];
  char tepmData[10];
 
   
  int chk = DHT.read11(DHT11_PIN);
  switch (chk)
  {
    case DHTLIB_OK: 
      Serial.print("OK,\t");
      break;
    case DHTLIB_ERROR_CHECKSUM:
      Serial.print("Checksum error,\t");
      break;
    case DHTLIB_ERROR_TIMEOUT:
      Serial.print("Time out error,\t");
      break;
    default:
      Serial.print("Unknown error,\t");
      break;
  }
 // DISPLAY DATA
 
   
  dtostrf(DHT.humidity,2,2,humidityData);
  dtostrf(DHT.temperature,2,2,tepmData);
  Serial.print("Humidity % : ");
  Serial.print(humidityData);
  Serial.print(" Temperature C' :");
  Serial.println(tepmData);
  delay(2000);

 
 static uint8_t mux_id = 0;
   
    if (wifi.createTCP(mux_id, HOST_NAME, HOST_PORT)) {
    Serial.print("Create tcp ok\r\n");
  } else {
    Serial.print("Create tcp err\r\n");
  }

 

// test string
String node_IP;
node_IP = myIP.substring(13);
 StringToSend = "Scripting|From_Wifi~Eval~HT~" + Node_Id +"~" + node_IP + "~" + humidityData + "~" + tepmData + "\r\n";
 
 /////////////// convert string to char array ///////////////////////////
 int str_len = StringToSend.length() + 1;
 char char_array[str_len];
 StringToSend.toCharArray(char_array, str_len);
 ////////////////////////////////////////////////////////////////////////
 

/////////////// send data //////////////////////////////////////////////
   uint8_t buffer[1024] = {0};
  wifi.send(mux_id,(const uint8_t*)char_array, str_len);
 
 
////////////////////////////////////////////////////////////////////////
 

Serial.print("Sending : ");
Serial.print(StringToSend);
Serial.println();
/*
/////////////////// recieve data //////////////////////////////////////
   uint8_t Rec_buffer[32] = {0};
 uint32_t len = wifi.recv(&mux_id, buffer, sizeof(buffer), 100);
//  uint32_t len = wifi.recv(buffer, sizeof(buffer), 100);
  //*
  if (len > 0) {
    Serial.print("Received:[");
    for (uint32_t i = 0; i < len; i++) {
      Serial.print((char)Rec_buffer[i]);
    }
    Serial.print("]\r\n");
   
  }
 ////////// END recieve data /////////////////////////////////////////

  if (wifi.releaseTCP(mux_id)) {
            Serial.print("release tcp ");
            Serial.print(mux_id);
            Serial.println(" ok");
        } else {
            Serial.print("release tcp");
            Serial.print(mux_id);
            Serial.println(" err");
        }

  mux_id++;
    if (mux_id >= 5) {
        mux_id = 0;
    }
  delay(2000);

}