Statistics: Posted by BillyBoy — Fri Jun 24, 2016 12:56 am
Statistics: Posted by ESP_Faye — Thu Jun 23, 2016 11:14 am
Code:
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);
}
Statistics: Posted by BillyBoy — Thu Jun 23, 2016 5:20 am