i'm practicing with ESP8266 since few months,in Arduino IDE. I tried to understand fundamentals reading Neil Kolban Book,but still i can't master the callback mechanism,and lot of other stuff,because my lack of experience in networks . Now communication is between my PC and NodeMCU,using TCP/IP,and i'm trying to detect data reception by a led blink(or a print on terminal)without any wifi related function called inside the loop. I am referring to this example https://internetofhomethings.com/homethings/?tag=using-sdk-functions-with-arduino-ide,using only a basic part of the code,
but for some reason,if i try to connect by a PC or a cellphone or a ESP12 StationPoint, it works only 5 times from reset.
Using a PC or cellphone, if i type in the address bar of a browser 192.168.4.15 i have the blink after pressing Enter key,but only for 5 times. If i print received data:
** GET / HTTP/1.1 Host: 192.168.4.15 Connection: keep-alive Cache-Control: max-age=0 DNT: 1 Upgrade-Insecure-Requests: 1 User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36 Accept: text/html,application/xhtml +xml,application/xml;q=0.9,image/avif,image/webp,image/apng,/;q=0.8,application/signed- exchange;v=b3;q=0.9 Accept-Encoding: gzip, deflate Accept-Language: it-IT,it;q=0.9,en-US;q=0.8,en;q=0.7**
Please can you check the code?I'm spending hours every day trying to understand something . Many thanks, Diego
#include <ESP8266WiFi.h>
#define SVRPORT 80
unsigned char blink_flag;
// Include API-Headers
extern "C" {
#include "ets_sys.h"
#include "os_type.h"
#include "osapi.h"
//#include "mem_manager.h"
//#include "mem.h"
//#include "string.h"
#include "user_interface.h"
#include "cont.h"
#include "espconn.h"
#include "eagle_soc.h"
#include <pgmspace.h>
void * pvPortZalloc(int size,char *, int);
}
/* _ _
/\ (_) | |
/ \ ___ ___ ___ ___ ___ _ __ ___ _ _ __ | |_
/ /\ \ / __/ __/ _ \/ __/ __| | '_ \ / _ \| | '_ \| __|
/ ____ \ (_| (_| __/\__ \__ \ | |_) | (_) | | | | | |_
/_/ \_\___\___\___||___/___/ | .__/ \___/|_|_| |_|\__|
| |
|_| */
byte ledPin = 2;
const IPAddress ipadd(192,168,4,15);
const IPAddress ipgat(192,168,4,1);
const IPAddress ipsub(255,255,255,0);
//***************************************************
void setup() {
Serial.begin(115200);
pinMode(ledPin, OUTPUT);
WiFi.mode(WIFI_AP);
WiFi.softAP("MyTest_AP_Id", "MyTest_AP_pw");
WiFi.softAPConfig(ipadd, ipgat, ipsub);
SdkWebServer_Init(SVRPORT);
Serial.println();
Serial.println("accesspoint_bare_01.ino");
Serial.println("Server started.");
Serial.print("IP: "); Serial.println(WiFi.softAPIP());
Serial.print("MAC:"); Serial.println(WiFi.softAPmacAddress());
blink_led(4,400);
}
//************************************************************************************
void loop() {
if(blink_flag)//if SdkWebServer_recv is triggered do a recognizable blink
{
blink_flag=0;//to stop blinking until new data is received
blink_led(8,100);
}
}
/********************************************************
* SDK API Web Server Initialization
* Function: SdkWebServer_Init(int port)
********************************************************/
void SdkWebServer_Init(int port) {
LOCAL struct espconn esp_conn;
LOCAL esp_tcp esptcp;
//Fill the connection structure, including "listen" port
esp_conn.type = ESPCONN_TCP; //This is the type of connection we are going to use
esp_conn.state = ESPCONN_NONE;//The state of the connection will change over time but we initialize it to have an initial empty state by supplying ESPCONN_NONE.
esp_conn.proto.tcp = &esptcp;//structure called "esp_tcp". This structure contains TCP specific
//settings. For our story, this is where we supply the port number which our TCP connection will
//listen upon for client connections. This is supplied in the property called "local_port".
esp_conn.proto.tcp->local_port = port;//80 for TCP/IP
esp_conn.recv_callback = NULL;
esp_conn.sent_callback = NULL;
esp_conn.reverse = NULL;
//Register the connection timeout(0=no timeout)
espconn_regist_time(&esp_conn,0,0);
//Register connection callback
espconn_regist_connectcb(&esp_conn, SdkWebServer_listen);//funzioni di libreria
//espconn_regist_disconcb (&esp_conn, MiaDiscCallback);
//Start Listening for connections
espconn_accept(&esp_conn);
Serial.print("Web Server initialized: Type = SDK API\n");
}
/********************************************************
* SDK API Web Server TCP Client Connection Callback
* Function: SdkWebServer_listen(void *arg)
********************************************************/
void SdkWebServer_listen(void *arg)
{
struct espconn *pesp_conn = ( espconn *)arg;
espconn_regist_recvcb(pesp_conn, SdkWebServer_recv);
espconn_regist_reconcb(pesp_conn, SdkWebServer_recon);
espconn_regist_disconcb(pesp_conn, SdkWebServer_discon);
}
//************************************************************************************
void SdkWebServer_recv(void *arg, char *pData, unsigned short len)
{
struct espconn *ptrespconn = ( espconn *)arg;
espconn_set_opt(ptrespconn, ESPCONN_REUSEADDR);
blink_flag=1;
}
/********************************************************
* SDK API Web Server TCP Connection Closed Callback
* Function: SdkWebServer_discon(void *arg)
********************************************************/
void SdkWebServer_discon(void *arg)
{
struct espconn *pesp_conn = ( espconn *)arg;
}
/********************************************************
* SDK API Web Server TCP Disconnect on error Callback
* Function: SdkWebServer_recon(void *arg, sint8 err)
********************************************************/
void SdkWebServer_recon(void *arg, sint8 err)
{
struct espconn *pesp_conn = ( espconn *)arg;
}
//************************************************************************************
void blink_led(unsigned char reps,unsigned int del)
{
while(reps--)
{
digitalWrite(ledPin,HIGH);
delay(del);
digitalWrite(ledPin,LOW);
delay(del);
}
}
//************************************************************************************