Example sketches for the new Arduino IDE for ESP8266

Moderator: igrr

User avatar
By dduehren
#94447 There are lots of examples of client-server with STA-AP mode. But has anyone got a working example of an 8266 in STA mode using either http.begin(client, target string) or WiFiClient client.connect(host,port) to connect to another 8266 on the same LAN, and therefore connected in STA mode, but running a web server?

1. I have a very simple sketch that always fails. See below
2. A very sophisticated app where I see it succeed once and then never work without restarting.
It suggests that some kind of WiFi state or configuration has to be present and which can get modified easily. The simple sketch doesn’t reproduce this behavior.
3. And that was with version 2.7.4 and I haven’t yet had time to test everything on version 3.x.x. I recently updated to the main branch and set up the Gdb debugger to try to figure it out.
4. Note that if the target is my raspberry pi or a server I have running on Digital Ocean, it always connects and succeeds. But if it's another 8266, it always fails, with the noted exception above.
5. If I use client.connect, the connection also fails.
6. Targets that fail include my code running a ESPAsyncWebServer server, a Tasmota smart switch. Both of them work with http messages sent from a browser.
And don't tell me to use MQTT because I'm building a product where I don't want that overhead, and I'm not sending the data anywhere. I just want to control a switch from my other 8266.
7. My sense is that somehow this "Elephant in the Room" example isn't well supported but the tcp transport layer should be quite independent of the radio connection mode. Once two devices are connected to the LAN they should be able to send bi-directional tcp messages, in this case with HTTP protocols running.
Code: Select all/**
    RealBasicHttp.ino
    Created on: 24.05.2015
*/
#include <Arduino.h>
#include "Arduino_DebugUtils.h"
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <WiFiClient.h>
#include <ESP8266HTTPClient.h>
#include <ESP8266Ping.h>

const char string_BSQ[] PROGMEM = "\"";
const char string_Host[] PROGMEM ="Host: ";
const char string_GET[] PROGMEM ="GET ";
const char string_switchCmd[] PROGMEM = "/cm?cmnd=Power";
const char string_ContentLen[] PROGMEM ="Content-Length: ";
const char string_ContentTypeTxt[] PROGMEM ="Content-Type: text/plain";
const char string_HTTP1P1[] PROGMEM =" HTTP/1.1";
const char string_urlBase[] PROGMEM = "http://";
//const char string_ipBase[] PROGMEM = "192.168.0.35";  //Raspberry Pi

const uint16_t serverPort = 80;  //5000 for Rasp Pi, 80 for Ubuntu server
const char string_off[] PROGMEM = "off";
const char string_on[] PROGMEM = "On";

#define ipLen 17
#define HTTPport 80
#define MTempSize 160
char MTemp[MTempSize];  //General purpose buffer for many operations
char workBuffer[21]; //General purpose buffer when two are needed
const IPAddress test_ip(192,168,0,71);

bool RlyOnOff(bool OnOff, uint8_t relNo);

HTTPClient httpSW;
WiFiClient client;
char switchIP[ipLen];

int httpCode;
void setup() {
Serial.begin(115200);

  establishContact();
  Serial.println();

  strcpy(switchIP, "191.168.0.28");  //ip address of the wireless switch

  Serial.println("Connecting to wifi");
  wifi_set_phy_mode (PHY_MODE_11G);
  WiFi.mode(WIFI_STA);
  WiFi.begin("TigerScout", "5415492012");
  // wait for WiFi connection
  if(WiFi.status() != WL_CONNECTED){
    Serial.print(".");
    delay(500);
  }
  Serial.println("Wifi Connected");   

}
bool ONoff = true;
void loop() {
    Serial.println("Begin loop");
    if(Ping.ping(test_ip)){
        Debug.print(DBG_INFO, F("ping succeeded"));
      } else {
        Debug.print(DBG_INFO, F("ping failed"));
      }
//      RlyOnOff(ONoff, 0); 
      ONoff = !ONoff;
    delay (10000);
}

void establishContact() {
  int counter = 0;
  while ((Serial.available()<= 0)&&(counter<2)) {
    Serial.println(F("0,0,0"));   // send an initial string
    delay(3000);
    counter++;
  }
}

bool RlyOnOff(bool OnOff, uint8_t relNo){
  //relNo is for future expansion, requires multiple swtichIPs
    int httpCode = 0;
    httpSW.setReuse(false);
    Debug.print(DBG_INFO, "RlyOnOff, Switch IP: %s", switchIP);
    sprintf_P(MTemp, "%s%s:%d%s" ,string_urlBase,switchIP,HTTPport,string_switchCmd);
//    sprintf_P(MTemp, "%s%s:%d%s" ,string_urlBase,srvIP,serverPort,string_switchCmd);
    strcat(MTemp,"_");  //"%20" for Tasmota devices
    if(OnOff){
      strcat_P(MTemp, string_on);
    } else {
      strcat_P(MTemp,string_off);
    }
    Serial.println(MTemp);
    httpSW.begin(client, MTemp);
    httpCode = httpSW.GET();
       if ((httpCode > 0)){
            httpSW.writeToStream(&Serial);
            Serial.println(" ");
            Debug.print(DBG_INFO, F("HTTP %d"), httpCode);
            httpSW.end();
            return true;
          } else {
            Debug.print(DBG_ERROR, F("HTTP GET invalid response http code %d %s"),httpCode, httpSW.errorToString(httpCode).c_str());
            // something wrong with connection
            httpSW.end();
            return false;
          }
 }
User avatar
By dduehren
#94463 JurajA wrote: TCP/IP doesn't care what device is the WiFi AP. the WiFiClient, WiFiServer, UDP are at TCP/IP level. there is no difference for TCP/IP if the AP is one of the esp or some other device (router, pgone hot spot)

Yes but does it care if neither device is the wifii AP because it's a router common to both wifi STA 8266s? Seems I'm having problems getting people to think of the 8266s in only STA mode. ie. They are clients to a router that's the AP and want to send http, tcp between them, just like my PC sends http, tcp to a STA 8266 on the same LAN.