[Solved] Can't make outgoing TCP connection in STATIONAP_MODE

BillyBoy
Posts: 2
Joined: Thu Jun 23, 2016 4:58 am

[Solved] Can't make outgoing TCP connection in STATIONAP_MODE

Postby BillyBoy » Thu Jun 23, 2016 5:20 am

Hi,

I've been pulling my hair out for a couple of days on this one, and for the life of me, I can't figure out what is going on.

I've got two(2) esp8266s.

One is running as AP and a web server on port 80. Works great.

The other is a client that is supposed to log onto the AP wifi as a station, connect to the AP web server, and print out whatever it gets back.

The client code works great if I "wifi_set_opmode( STATION_MODE )", but for some reason the client cannot get a TCP connection when it is run with "wifi_set_opmode( STATIONAP_MODE )". "espconn_connect(&conn)" succeeds with return code [0], but "stationReconCb" returns the err "ESPCONN_CONN (-11) – Failed connecting to a partner"

A couple of notes:
1) I've been using Arduino IDE to write, compile, and load code, but need to use the SDK for my larger project
2) For my larger project, I also need to run in STATIONAP_MODE.

Here is the client code:

Code: Select all

extern "C" {
#include "ets_sys.h"
#include "osapi.h"
#include "gpio.h"
#include "os_type.h"
#include "user_config.h"
#include "user_interface.h"
#include "uart.h"
#include "c_types.h"
#include "espconn.h"
#include "mem.h"
}

esp_tcp tcp;
espconn conn;
os_timer_t connectTimer;

void setup() {
  Serial.begin( 115200 );
  Serial.printf("\nIn setup()\nConnecting to WiFi\n");

  //Set station mode
  wifi_station_disconnect();
//  wifi_set_opmode( STATION_MODE );  // works just fine as a STATION
  wifi_set_opmode( STATIONAP_MODE );// does NOT work? will not tcp connect to another esp8266.

  char ssid[32] = "projectSSID";
  char password[64] = "projectPassword";

  //Set ap settings
  struct station_config stationConf;
  stationConf.bssid_set = 0;
  memcpy(&stationConf.ssid, ssid, 32);
  memcpy(&stationConf.password, password, 64);
  wifi_station_set_config(&stationConf);
  wifi_station_connect();

  os_timer_setfn( &connectTimer, connectTCP, NULL );
  os_timer_arm( &connectTimer, 1000, 0 );

  return;
}

void loop() {
}

void connectTCP( void *arg ) {
  struct ip_info ipconfig;

  os_timer_disarm( &connectTimer );

  wifi_get_ip_info(STATION_IF, &ipconfig);
  if ( wifi_station_get_connect_status() == STATION_GOT_IP && ipconfig.ip.addr != 0 ) {
    Serial.printf("\nGot local IP=%d.%d.%d.%d\n", IP2STR(&ipconfig.ip) );
    Serial.printf("Dest IP=%d.%d.%d.%d\n", IP2STR( &ipconfig.gw ) );

    conn.type = ESPCONN_TCP;
    conn.state = ESPCONN_NONE;
    conn.proto.tcp = &tcp;
    conn.proto.tcp->local_port = espconn_port();
    conn.proto.tcp->remote_port = 80;
    os_memcpy(conn.proto.tcp->local_ip, &ipconfig.ip, 4);
    os_memcpy(conn.proto.tcp->remote_ip, &ipconfig.gw, 4);

    Serial.printf("conn Print type=%d, state=%d, local_ip=%d.%d.%d.%d, local_port=%d, remote_ip=%d.%d.%d.%d remote_port=%d\n",
                  conn.type,
                  conn.state,
                  IP2STR(conn.proto.tcp->local_ip),
                  conn.proto.tcp->local_port,
                  IP2STR(conn.proto.tcp->remote_ip),
                  conn.proto.tcp->remote_port );

    espconn_regist_connectcb(&conn, stationConnectedCb);
    espconn_regist_disconcb(&conn, stationDisconCb);
    espconn_regist_reconcb(&conn, stationReconCb);
    espconn_regist_recvcb(&conn, stationRecvCb);
    espconn_regist_sentcb(&conn, stationSentCb);

    Serial.printf("espconn_connect()=%d\n", espconn_connect(&conn) );
  }
  else {
    Serial.print(".");
    os_timer_arm( &connectTimer, 1000, 0 );
  }
}

void stationConnectedCb(void *arg) {

  Serial.print("In stationConnectedCb\n");

  struct espconn *newConn = (struct espconn *)arg;

  char *data = "GET / HTTP/1.0\r\n\r\n\r\n";
  Serial.printf("Sending to server data=%s\n", data );
  sint8 d = espconn_sent(newConn, (uint8*)data, strlen(data));

  espconn_regist_recvcb(newConn, stationRecvCb);
}

void stationReconCb(void *arg, sint8 err) {
  struct espconn *failedConn = (struct espconn*)arg;
  Serial.printf("In stationReconCb err=%d\n", err);
  Serial.printf("failedConn type=%d, state=%d, local_ip=%d.%d.%d.%d, local_port=%d, remote_ip=%d.%d.%d.%d remote_port=%d\n",
                failedConn->type,
                failedConn->state,
                IP2STR(failedConn->proto.tcp->local_ip),
                failedConn->proto.tcp->local_port,
                IP2STR(failedConn->proto.tcp->remote_ip),
                failedConn->proto.tcp->remote_port );
}

void stationDisconCb(void *arg) {
  Serial.printf("In stationDisconCb\n");
  //  network_init();
}

void stationSentCb(void *arg) {
  Serial.print("In stationSentCb\n");
}

void stationRecvCb(void *arg, char *data, unsigned short len) {
  Serial.print("In stationRecvCb\n");

  struct espconn *recConn = (struct espconn *)arg;

  Serial.printf("Received Data=%s\n", data);
}


Any insight will be much appreciated!

Thanks.

ESP_Faye
Posts: 1646
Joined: Mon Oct 27, 2014 11:08 am

Re: Can't make outgoing TCP connection in STATIONAP_MODE

Postby ESP_Faye » Thu Jun 23, 2016 11:14 am

Hi,

You need to change the IP address of one of your ESP8266 softAP.

For example, It should be
ESP8266 A (softAP 192.168.5.1 +station 192.168.4.X) -- connect to --> ESP8266 B (softAP 192.168.4.1)
or
ESP8266 A (softAP 192.168.4.1 +station 192.168.5.X) -- connect to --> ESP8266 B (softAP 192.168.5.1)

More details in http://bbs.espressif.com/viewtopic.php?f=61&t=500.

BillyBoy
Posts: 2
Joined: Thu Jun 23, 2016 4:58 am

Re: Can't make outgoing TCP connection in STATIONAP_MODE

Postby BillyBoy » Fri Jun 24, 2016 12:56 am

Oh my gosh!

That was it. Thank you!

Who is online

Users browsing this forum: No registered users and 17 guests