-->
Page 1 of 1

Cannot interconnect to C# Socket on ESP8266 Arduino Core

PostPosted: Wed Mar 13, 2019 5:24 pm
by shejan0
Hello ESP8266 Community Forum,

I am trying to interconnect a C# program that utilizes the TcpClient and Socket functionality, and an ESP8266 board with code from the ESP8266 Arduino Core and NodeMCU Firmware. I have tried different networks, ports, and even different programs with the same internal systems. The 2 codes I am using are written below.

C# code
Code: Select allvoid loadUI(){ //This is not the entire program but this is the section that communicates with the Node
         enterIPLabel.Visible=false;
         errorBox.Text="Connecting to Leader node";
         tcpcom=new TcpClient(leadernode.ToString(),25565); //leadernode is the IP Address of the NodeMCU
         tcpcom.Connect(leadernode.ToString(),25565); //just in case
         while(!tcpcom.Connected);
         
         tcpcom.Client.Send(System.Text.Encoding.ASCII.GetBytes("GetController"));
         errorBox.Text="Awaiting Node Response";
         while(tcpcom.Available<=0); //hopefully this should stop this method from executing until the response is made
         //FILL IN CODE FOR RETURN STATEMENT
         tabController.Visible=true;
         NodeMap.Visible=true;
      }


and the Arduino Code is:

Code: Select all#include <ESP8266WiFi.h>
String ConnectedMAC[8];
IPAddress ConnectedIP[8];
WiFiClient clientforcom;
void setup() {
  WiFi.mode(WIFI_AP_STA);
  Serial.begin(9600);
  int f = WiFi.scanNetworks();
  Serial.println("Scanning Networks");
  delay(5000);
  for (int n = 0; n < f; n++) {
    Serial.println(WiFi.SSID(n));
  }
  WiFi.begin(ssidstring, passtring);
  delay(2000);
  while (WiFi.status() != WL_CONNECTED) {
    delay(100);
    Serial.print(".");
  }
  Serial.print("The IP of this node is: ");
  Serial.println(WiFi.localIP());
}


void loop() {

  MACManagement();
  WiFiServer serverforclient(WiFi.localIP(), 25565);
  while (!serverforclient.hasClient()) { //this does not work with Socket or delay is too long
    delay(50);
    Serial.println("Waiting for client");
  }
  clientforcom = serverforclient.available();
  while (clientforcom.available() <= 0) {
    delay(50);
    Serial.println("Waiting for response");
  }
  String response = clientforcom.readString();
  if (response.startsWith("GetController")) {
    Serial.println("Sending String");
    clientforcom.println("Returned String");
  }
}


void MACManagement() {

  unsigned char number_client;
  struct station_info *stat_info;

  struct ip_addr *IPaddress;
  IPAddress address;
  int i = 0;

  number_client = wifi_softap_get_station_num();
  stat_info = wifi_softap_get_station_info();

  Serial.print(" Total Connected Clients are = ");
  Serial.println(number_client);

  while (stat_info != NULL) {

    IPaddress = &stat_info->ip;
    address = IPaddress->addr;

    Serial.print("client= ");

    Serial.print(i);
    Serial.print(" IP adress is = ");
    Serial.print((address));
    ConnectedIP[i] = address;
    Serial.print(" with MAC adress is = ");

    Serial.print(stat_info->bssid[0], HEX); Serial.print(" ");
    Serial.print(stat_info->bssid[1], HEX); Serial.print(" ");
    Serial.print(stat_info->bssid[2], HEX); Serial.print(" ");
    Serial.print(stat_info->bssid[3], HEX); Serial.print(" ");
    Serial.print(stat_info->bssid[4], HEX); Serial.print(" ");
    Serial.print(stat_info->bssid[5], HEX); Serial.print(" ");
    ConnectedMAC[i] = String(stat_info->bssid[0], HEX) + ":" + String(stat_info->bssid[1], HEX) + ":" + String(stat_info->bssid[2], HEX) + ":" + String(stat_info->bssid[3], HEX) + ":" + String(stat_info->bssid[4], HEX) + ":" + String(stat_info->bssid[5], HEX);
    stat_info = STAILQ_NEXT(stat_info, next);
    i++;
    Serial.println();
  }
}


Whenever I have the ESP8266 board with the program at the "Waiting for client" stage, I open the C# code, and enter the IP Address into the leadernode variable (which I have confirmed is not broken). However, when communicating to the node, the C# program freezes and returns this error:

Code: Select allSystem.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it


I don't understand why the Node is refusing my connection. Am I missing something on the node code, or is my C# code for connection wrong?

I am completely lost, and any help would be extremely helpful.

Thanks.