ESP8266 AccessPoint always fails after exactly 5 times of correct communications - esp8266

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);
}
}
//************************************************************************************

Why are you using the SDK functions with Arduino code? There are good Arduino Core web >servers that are much easier to use
Why are you using the SDK functions with Arduino code? There are good Arduino Core web >servers that are much easier to use
Hello .I dont know what is better to use,i'm not an expert ESP8266 user..
I need that for an application that would need another post to be explained and i tried to put the question simple.
That application failed wifi after few minutes,reading on web I've found that it was probably because i managed wrongly WiFi inside the loop.
The example I've started from seemed inviting because all about wifi looks managed by callbacks.
Is there any illegal use of SDK functions in Arduino code?
I was not able to configure any other environment as Eclipse,it was a nightmare of many days of environment variables,paths and forbidden folder names with spaces.
Probably i will try other Arduino examples that i recently was told about,the ones using inside the loop
void loop() {
server.handleClient(); //Handling of incoming client requests
}
Anyway i'm still looking for an explanation about the reason why my code fails exactly after 5 times,regardless of receiving 20 bytes or 400 bytes(and this makes me think that is not because the 1460 bytes of TCP blocks,but i dont want to do any assumption).
Thanks.

Related

ESP8266 create another WiFiClientSecure always fail https request

I have some ESP8266 and ESP32.
I use them with a MQTT (SSL) server and a web server.
I use two WiFiClientSecure to avoid to be disconnected to my MQTT server.
One WiFiClientSecure instance is dedicate to the MQTT server and the other one to HTTPS requests
The ESP32 works well, I can connect to my MQTT server and send HTTPS request without issue.
The ESP8266 (Wemos d1 mini) doesn't work as expected. The HTTPS request always fail.
It only works if I only use one instance of WiFiClientSecure. However during the HTTPS request I am disconnected of the MQTT server.
Is this behavior is due to hardware limitation ?
Here is the revelant part of code
WiFiClientSecure espClient;
WiFiClientSecure espClient2;
PubSubClient mqttClient(espClient);
const char* rootCABuff = \
"-----BEGIN CERTIFICATE-----\n" \
...
"-----END CERTIFICATE-----\n";
void setup() {
/* Connect to wifi then set cert*/
espClient.setCACert(rootCABuff);
espClient2.setCACert(rootCABuff);
/* Define clock or will fail*/
setClock();
...
client.setServer("myserver", myport); // MQTT server
...
}
void loop() {
...
if (!mqttclient.connected()) {
reconnect();
}
else
{
mqttclient.loop();
}
...
/*Sometimes a https request is needed*/
HTTPClient http;
http.setTimeout(10000);
char* baserequest = "https://myserver.com";
httpRequestData="somedata";
Serial.print("[HTTPS] begin...\n");
if (http.begin(espClient2, baserequest)) { // HTTPS
http.addHeader("Accept", "application/json");
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
Serial.print("[HTTPS] POST...\n");
int httpCode = http.POST(httpRequestData);
http.end();
} else {
Serial.printf("[HTTPS] Unable to connect\n");
}
...
}

How to send this JSon via POST to server with EtherCard library and Arduino Uno?

I need to post this data to my remote server:
StaticJsonBuffer<200> jsonBuffer;
DynamicJsonBuffer jBuffer;
JsonObject& root = jsonBuffer.createObject();
root["latitude"]= gps.location.lat(),
root["longitude"]= gps.location.lng();
root.prettyPrintTo(Serial);
I can't find any working tutorial to do it. I've connected my ethernet ENC28J60 module and it works fine but I don't know how to send POST data with header content type application json via this library. Can you help me?
if you search alway you will find my solution :
static word reponse() {
doc.clear();
doc["Etat"] = "ok";
doc["CodeMp3"] = "00";
bfill = ether.tcpOffset();
bfill.emit_p(PSTR(
"HTTP/1.0 200 OK\r\n"
"Content-Type: application/json\r\n"
"Connection: close\r\n"
"Content-Length: $D\r\n"
"\r\n"), measureJson(doc));
serializeJson(doc, bfill);
return bfill.position();
}
void loop () {
word len = ether.packetReceive();
word pos = ether.packetLoop(len);
if (pos){ // check if valid tcp data is received
getMsgSever(pos,len);
ether.httpServerReply(reponse()); // send web page data
}
}

NO_POLL websocket server on esp8266 rtos sdk

I'm trying to use the nopoll library provided with the RTOS sdk for the esp8266, to build a websockets server.
I've followed the example provided on the no_poll website namely:
noPollConn * listener = nopoll_listener_new (ctx, "0.0.0.0", "1234");
if (! nopoll_conn_is_ok (listener)) {
// some error handling here
}
nopoll_ctx_set_on_msg (ctx, listener_on_message, NULL);
nopoll_loop_wait (ctx, 0);
with an appropriate callback:
void listener_on_message (noPollCtx * ctx, noPollConn * conn, noPollMsg * msg, noPollPtr user_data) {
printf ("Listener received (size: %d, ctx refs: %d): (first %d bytes, fragment: %d) '%s'\n",
nopoll_msg_get_payload_size (msg),
nopoll_ctx_ref_count (ctx), shown, nopoll_msg_is_fragment (msg), example);
nopoll_conn_send_text (conn, "Message received", 16);
return;
}
But the process does nothing!
Has anyone else been able to make this work?
Or is it just broken?
In the examples directory for the RTOS, a websockets send example is provided, which is a cut down version of the test suite for the standard NO_POLL library. I noticed that the tests which are removed include the
tests for server creation.
Any help which you can provide would be welcomed!
BR

Http client mode with Contiki?

I want to make a webAPI call from a sensor using http, is it possible to do http requests using Contiki OS?
As far as I've searched I found only coap client examples.
Check the examples/http-socket example, it shows how to use CRUD methods such as PUT, GET, etc.
Here's the link to the example (working with the latest master commit)
This example relies on IP64, but can be changed to work with IPv6, basically you need to include the http-socket library. Here are the more relevant parts of the example:
#include "contiki-net.h"
#include "http-socket.h"
#include "ip64-addr.h"
#include <stdio.h>
static struct http_socket s;
static int bytes_received = 0;
static void
callback(struct http_socket *s, void *ptr,
http_socket_event_t e,
const uint8_t *data, uint16_t datalen)
{
if(e == HTTP_SOCKET_ERR) {
printf("HTTP socket error\n");
} else if(e == HTTP_SOCKET_DATA) {
bytes_received += datalen;
printf("HTTP socket received %d bytes of data\n", datalen);
}
}
PROCESS_THREAD(http_example_process, ev, data)
{
PROCESS_BEGIN();
/* Initializes the socket */
http_socket_init(&s);
/* GET request */
http_socket_get(&s, "http://www.contiki-os.org/", 0, 0,
callback, NULL);
/* Waits forever for the HTTP callback */
while(1) {
PROCESS_WAIT_EVENT_UNTIL(0);
}
PROCESS_END();
}
Yes you can do that:
What I understand is that you are looking for Websense Example in Contiki OS.it uses HTTP protocl.
A: so find this file.
~/contiki/examples/zolertia/z1/ipv6/z1-websense/z1-websense.c
Burn it on Sender Mote.
Burn border-router.c file located in /home/superuser/contiki/examples/ipv6/rpl-border-router/
Connect Border Router with tunnelslip with command make connect-router.
use the HTTP IPV6 url shown by tunnelslip on connection.
this url in browser will give you address of motes connected to it.
use that sender mote address in web browser and see the mote output.
B: or from contiki/cooja simulator:
launch this project file. this is working demo for the websense.
~contiki/examples/zolertia/z1/ipv6/z1-websense/example-z1-websense.csc
and repeat from step 3.
for further you can ask me.

Arduino WiFi shield post with header problems

I'm trying to do a post from the arduino wifi shield to my java servlet. The servlet functions with url get, and jquery post, but I can't sort the headers out in my arduino code. Any help will be greatly appreciated!
The server returns 200, but I'm not getting the payload "content" as value. I'm not exactly sure what I'm doing wrong but I'm pretty sure it's in how my headers are setup. I've spent the last two days trying to get it.
#include <SPI.h>
#include <WiFi.h>
char ssid[] = "jesussavesforjust19.95"; // your network SSID (name)
char pass[] = "********"; // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0; // your network key Index number (needed only for WEP)
int status = WL_IDLE_STATUS;
IPAddress server(192,168,10,149); // numeric IP for Google (no DNS)
WiFiClient client;
void setup() {
Serial.begin(9600);
// attempt to connect to Wifi network:
while ( status != WL_CONNECTED) {
Serial.println("Attempting to connect to SSID: ");
Serial.println(ssid);
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
Serial.println("Connected to wifi");
printWifiStatus();
sendData("0600890876");
}
void loop() {
// if there's incoming data from the net connection.
// send it out the serial port. This is for debugging
// purposes only:
if (client.available()) {
char c = client.read();
Serial.println(c);
}
//String dataString = "060088765";
// if you're not connected, and ten seconds have passed since
// your last connection, then connect again and send data:
if(!client.connected())
{
Serial.println();
Serial.println("disconnecting.");
client.stop();
//sendData(dataString);
for(;;)
;
}
}
// this method makes a HTTP connection to the server:
void sendData(String thisData) {
// if there's a successful connection:
Serial.println("send data");
if (client.connect(server, 8080)) {
String content = "value=0600887654";
Serial.println(content);
Serial.println("connected");
client.println("POST /hos HTTP/1.1");
client.println("Host:localhost");
client.println("Connection:Keep-Alive");
client.println("Cache-Control:max-age=0");
client.println("Content-Type: application/x-www-form-urlencoded\n");
client.println("Content-Length: ");
client.println(content.length());
client.println("\n\n");
client.println(content);
}
else {
// if you couldn't make a connection:
Serial.println("form connection failed");
Serial.println();
Serial.println("disconnecting.");
client.stop();
}
}
void printWifiStatus() {
// print the SSID of the network you're attached to:
Serial.println("SSID: ");
Serial.println(WiFi.SSID());
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.println("IP Address: ");
Serial.println(ip);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.println("signal strength (RSSI):");
Serial.println(rssi);
Serial.println(" dBm");
}
Perhaps, some of your "Serial.println" and "client.println" commands should be "Serial.print" and "client.print" instead. For example:
client.print("Content-Length: ");
client.println(content.length());
would avoid adding a line break between the text and the number.
This is maybe more advice on an approach than an answer.
If I was doing something like this I would not start on the Arduino. The endless compile, download, run, look at print()'s would drive me crazy. I would fully prototype the client/server interaction in whatever you have at your fingertips, preferably something with a debugger. (Java, Python, PHP, VB, whatever you know that you can slap together)
Second, I would run Wireshark on the server so that I could see exactly what was being sent and responded.
Then I would port the same interaction over to the Arduino. Again inspect with Wireshark to confirm you are getting what you expected. If you send the same bytes, you should get the same response.
Even if you choose to implement straight on Arduino, consider having Wireshark to capture the actual network traffic.
With Wireshark, you might see that the Arduino println() is not sending the correct line end for the server.
Also, there is no guarantee that last println() is actually sent. The network stack implementation is free to buffer as it sees fit. You might need a flush(). A packet trace would show this.
With a packet capture you might find that time matters. In theory TCP is a stream and you should be able to send that POST data 1 character at a time in 1 packet and everything would work. But the Arduino might be so slow executing those println()'s by the server's standards that it times out. In such case you would see the server respond before the Arduino even finished sending.

Resources