Skip to content

Allow for coexistence with other servers#62

Open
probonopd wants to merge 1 commit into
marcoschwartz:masterfrom
probonopd:patch-1
Open

Allow for coexistence with other servers#62
probonopd wants to merge 1 commit into
marcoschwartz:masterfrom
probonopd:patch-1

Conversation

@probonopd

Copy link
Copy Markdown

I think this is better because multiple different servers can coexist this way. I am using it together with an over-the-air update mechanism (OTA) like this:

void loop() {
  // Handle REST calls
  WiFiClient client = server.available();
  if (client) {
    while (!client.available()) {
      delay(1);
    }
  }
  rest.handle(client);
  checkOTA();
}

With the OTA function being:

void checkOTA() {
  if (OTA.parsePacket()) {
    IPAddress remote = OTA.remoteIP();
    int cmd  = OTA.parseInt();
    int port = OTA.parseInt();
    int size   = OTA.parseInt();
    Serial.printf("Update Start: %d\n", size);
    if (!Update.begin(size)) {
      Update.printError(Serial);
      return;
    }
    WiFiClient client;
    if (client.connect(remote, port)) {
      Serial.setDebugOutput(true);
      uint32_t written;
      while (!Update.isFinished()) {
        written = Update.write(client);
        if (written > 0) client.print(written, DEC);
      }
      Serial.setDebugOutput(false);
      if (Update.end()) {
        client.print("OK");
        Serial.printf("Update Success\n");
      } else {
        Update.printError(client);
        Update.printError(Serial);
      }
    } else {
      Serial.printf("Connect Failed\n");
    }
  }
}

Note that the OTA mechanism is scheduled to change soon anyway, but I think the change in the loop()of the example would make sense anyway.

@marcoschwartz

Copy link
Copy Markdown
Owner

Hello, could you develop a bit about what this would allow to do? Like sending messages between aREST devices ?

@probonopd

Copy link
Copy Markdown
Author

The example I posted above is a slight change from the example sketch and allows for the esp8266 over-the-air update mechanism to co-exist with aREST. But the same also applies if you want to run, e.g., a HTTP server for serving web pages, a telnet server, and aREST all in one sketch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants