Post topics, source code that relate to the Arduino Platform

User avatar
By pbos
#4514 I have the same problem. I want to use my Arduino as a sensor probe as well as sending it commands to control switches.
As a probe the Arduino should act as a TCP client and connect to a server where I can collect the sensor data.

When controlling switches, the Arduino should act as a TCP server, receiving commands to change the state of the pins.

I can successfully run sample code to do either the on or the other (client or server). But when trying to combine the two it is as if the TCP layer gets confused and stops responding.

Can the chip both listen to a port and send data on a port to another application or even a device with a different IP address?
User avatar
By jleroux
#4542 The code I am using. Like I said, I use the ESP8266 library from INEXTH ( akexorcist )



#include <ESP8266_TCP.h>

// ESP8266 Class
ESP8266_TCP wifi;

// Target Access Point
#define ssid "ESP_AP"
#define pass "123456789"

// Connect this pin to CH_PD pin on ESP8266
#define PIN_RESET 6

void setup()
{
delay(3000);

// We use Serial1 to interface with ESP8266
// and use Serial to debugging
Serial.begin(9600);
Serial1.begin(9600);
wifi.begin(&Serial1, &Serial, PIN_RESET);


// Check that ESP8266 is available
if(wifi.test())
{
// Connect to target Access Point
String ip = connectAP();

// Open TCP Server on port 2000 and 30 seconds for connection timeout (Max 2880)
wifi.openTCPServer(2000, 30);
}
else
{
// ESP8266 isn't available
Serial.println("Check module connection and restart to try again...");
while(true);
}
}

void loop()
{
// Check for any data has coming to ESP8266
int dataState = wifi.isNewDataComing(WIFI_SERVER);
if(dataState != WIFI_NEW_NONE) {
if(dataState == WIFI_NEW_CONNECTED) {
// Connected with TCP Client Side
Serial.println("Status : Connected");
} else if(dataState == WIFI_NEW_DISCONNECTED) {
// Disconnected from TCP Client Side
Serial.println("Status : Disconnected");
} else if(dataState == WIFI_NEW_MESSAGE) {
// Got a message from TCP Client Side
Serial.println("ID : " + String(wifi.getId()));
Serial.println("Message : " + wifi.getMessage());
wifi.connectTCP("192.168.1.2", 2000);
wifi.send("12345678");

} else if(dataState == WIFI_NEW_SEND_OK) {
// Message transfer has successful
Serial.println("SENT!!!!");
}
}
}

// Access Point Connection Function that you can loop connect to Access Point until successful
String connectAP()
{
String ip = "0.0.0.0";
while(ip.equals("0.0.0.0"))
{
String ip = wifi.connectAccessPoint(ssid, pass);
if(!ip.equals("0.0.0.0"))
{
break;
}
}
return ip;
}
User avatar
By ashwinispatil
#17297
villTech wrote:why cant you run both at same time?
esp8266 is capable of that.
after you start a server, you can start a client at same time.


I am using nodemcu firmware. I am trying to run one esp as client as well as server simultaneously.
Is it posible.?
If yes then how it would be. please suggest.