Giter Site home page Giter Site logo

Conflict with WiFiClient? about fauxmoesp HOT 10 CLOSED

vintlabs avatar vintlabs commented on August 14, 2024
Conflict with WiFiClient?

from fauxmoesp.

Comments (10)

pvint avatar pvint commented on August 14, 2024

Original comment by Don Slattery (Bitbucket: donslattery, GitHub: donslattery).


It looks like the editor trashed my pound signs. You'll have to pretend they're there for the purposes of reading the code and add them back to execute.

from fauxmoesp.

pvint avatar pvint commented on August 14, 2024

Original comment by Don Slattery (Bitbucket: donslattery, GitHub: donslattery).


Thanks for the help with formatting. I've not posted before.

from fauxmoesp.

pvint avatar pvint commented on August 14, 2024

Original comment by Xose Pérez (Bitbucket: [Xose Pérez](https://bitbucket.org/Xose Pérez), ).


Hi
I would use SyncClient instead since fauxmoESP is built on top of ESPAsyncTCP library (example here: https://github.com/me-no-dev/ESPAsyncTCP/blob/master/examples/SyncClient/SyncClient.ino)
Also: instead of calling test() in the fauxmo callback, set a volatile flag there and check it in your main loop. This way you return quickly from the callback.

from fauxmoesp.

pvint avatar pvint commented on August 14, 2024

Original comment by Don Slattery (Bitbucket: donslattery, GitHub: donslattery).


I’ll give that a try and see what happens. Thanks for your help. Thanks for the performance suggestion, also.

from fauxmoesp.

pvint avatar pvint commented on August 14, 2024

Original comment by Don Slattery (Bitbucket: donslattery, GitHub: donslattery).


Not a bug

from fauxmoesp.

pvint avatar pvint commented on August 14, 2024

Original comment by Don Slattery (Bitbucket: donslattery, GitHub: donslattery).


Using SyncClient solved the problem. Thank you!

from fauxmoesp.

pvint avatar pvint commented on August 14, 2024

Original comment by Zak Malone (Bitbucket: zmalone, GitHub: zmalone).


I couldn't get this working with SyncClient. Don, can you post an example of your working code?

from fauxmoesp.

pvint avatar pvint commented on August 14, 2024

Original comment by Zak Malone (Bitbucket: zmalone, GitHub: zmalone).


Never mind. Got it working with WiFiClient using Xose Pérez's tip to do this outside of the fauxmo callback. Here's my working example of using the ESP8266 as a proxy to my real WEMO.

#!arduino

#include <ESP8266WiFi.h>
#include "fauxmoESP.h"
#include "credentials.h"

#define SERIAL_BAUDRATE     115200

#define FAUXMO_DEVICE       "proxy"
#define WEMO_HOST           "Belkin-WEMO-127"
#define WEMO_PORT           49153
#define WEMO_COMMAND_FORMAT   "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" "\
                              "s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">"\
                              "<s:Body><u:SetBinaryState xmlns:u=\"urn:Belkin:service:basicevent:1\">"\
                              "<BinaryState>%d</BinaryState></u:SetBinaryState></s:Body></s:Envelope>"

bool do_wemo_state_change = false;
int wemo_state = 0;

WiFiClient client;

fauxmoESP fauxmo;

void wifiSetup() {
  WiFi.mode(WIFI_STA);
  Serial.println("[WIFI] STATION Mode");

  Serial.printf("[WIFI] Connecting to %s ", WIFI_SSID);
  WiFi.begin(WIFI_SSID, WIFI_PASS);

  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(500);
  }
  Serial.println();

  Serial.printf("[WIFI] Connected.  SSID: %s, IP address: %s\n", WiFi.SSID().c_str(), WiFi.localIP().toString().c_str());
}

void setup() {
  Serial.begin(SERIAL_BAUDRATE);
  Serial.println();
  Serial.println();

  wifiSetup();

  fauxmo.addDevice(FAUXMO_DEVICE);
  Serial.printf("[FAUXMO] %s Device registered.\n", FAUXMO_DEVICE);

  fauxmo.onMessage([](unsigned char device_id, const char * device_name, bool state) {
    wemo_state = state ? 1 : 0;
    do_wemo_state_change = true;
  });
}

void wemo_state_change(){
  Serial.printf("\n[WEMO] Starting connection to WEMO host: %s...", WEMO_HOST);
  if( client.connect(WEMO_HOST, WEMO_PORT) ) {
    Serial.println("\n[WEMO] Connected.");

    // Set up SOAP command
    char wemo_command[300];
    int wemo_command_length = sprintf(wemo_command, WEMO_COMMAND_FORMAT, wemo_state);

    // This will send the request to the server
    client.setTimeout(2);
    client.println("POST /upnp/control/basicevent1 HTTP/1.1");
    client.println("Host: " + String(WEMO_HOST) + ":" + String(WEMO_PORT));
    client.println("User-Agent: ESP8266/1.0");
    client.println("Connection: close");
    client.println("Content-type: text/xml; charset=\"utf-8\"");
    client.print("Content-Length: ");
    client.println(wemo_command_length);
    client.println("SOAPACTION: \"urn:Belkin:service:basicevent:1#SetBinaryState\"");
    client.println();
    client.println(wemo_command);

    while(client.connected() && client.available() == 0){
      delay(1);
    }

    // Read all the lines of the reply from server and print them to Serial
    while(client.available()){
      String line = client.readStringUntil('\n');
      Serial.print("[WEMO] - ");
      Serial.println(line);
    }

    if (client.connected()) {
      Serial.println("[WEMO] Closing connection");
      client.stop();
    }
  }
  else {
    Serial.println("\n[WEMO] Connection unsuccessful.");
  }
}


void loop() {
  fauxmo.handle();

  if (do_wemo_state_change) {
    wemo_state_change();
    do_wemo_state_change=false;
  }

}

from fauxmoesp.

pvint avatar pvint commented on August 14, 2024

Original comment by Zak Malone (Bitbucket: zmalone, GitHub: zmalone).


Hi Don -

Thanks! Sorry, I didn't see this reply until after I'd posted my followup with code on the issues thread. I think we both got to the same place to get around the issue, though -- moving the code outside the callback function.

from fauxmoesp.

pvint avatar pvint commented on August 14, 2024

Original comment by Xose Pérez (Bitbucket: [Xose Pérez](https://bitbucket.org/Xose Pérez), ).


In general it's a good practice to keep your callbacks and interrupt methods as slim as possible...

Closing

from fauxmoesp.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.