Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for coexistence with other servers #62

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

probonopd
Copy link

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
Owner

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

@probonopd
Copy link
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