0

Connection to Adafruit IO and HTTP GET
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Connection to Adafruit IO and HTTP GET

by mpc823 on Tue Jun 04, 2019 6:57 pm

I'm programming the PyPortal in Arduino and want to have an MQTT connection up and connected, but at the same time also periodically fetch the weather via an HTTP GET. But it appears that whenever I do the fetch, it closes my MQTT connection. So is it possible to have both up at the same time? I thought I should simply be able to declare another WIFiClient and use that, but that seems to interfere with the existing Adafruit_IO connection.

mpc823
 
Posts: 46
Joined: Thu May 05, 2016 9:10 am

Re: Connection to Adafruit IO and HTTP GET

by brubell on Wed Jun 05, 2019 12:38 pm

Could you post your code? You might be correct on your assumption that it interferes with the pre-existing HTTP client.

brubell
 
Posts: 531
Joined: Fri Jul 17, 2015 10:33 pm

Re: Connection to Adafruit IO and HTTP GET

by mpc823 on Wed Jun 05, 2019 9:44 pm

Sure, here's the basic code that's relevant.

In the secrets.h file:
Code: Select all | TOGGLE FULL SIZE
// uncomment the following line if you are using airlift
#define USE_AIRLIFT

// comment out the following lines if you are using fona or ethernet
#include "AdafruitIO_WiFi.h"

AdafruitIO_WiFi io(IO_USERNAME, IO_KEY, SECRET_SSID, SECRET_PASS, SPIWIFI_SS, SPIWIFI_ACK, ESP32_RESETN, ESP32_GPIO0, &SPIWIFI);

And in the main file I have the following:

In the setup() function I do:
Code: Select all | TOGGLE FULL SIZE
getWeather(doc);

// Start MQTT connection to io.adafruit.com
  io.connect();
  Serial.println("Just called io connect");
  garage1->onMessage(handleGarage1);
  garage2->onMessage(handleGarage2);

  while(io.mqttStatus() < AIO_CONNECTED) {
    Serial.print(".");
    delay(500);
  }

  // Because Adafruit IO doesn't support the MQTT retain flag, we can use the
  // get() function to ask IO to resend the last value for this feed to just
  // this MQTT client after the io client is connected.
  garage1->get();
  garage2->get();


In the loop() function I call:
Code: Select all | TOGGLE FULL SIZE
// Keep connected to io.adafruit.com
  io.run();
  aio_status_t ioStatus = io.status();
  if (ioStatus != AIO_CONNECTED) {
    if (disconnectTime != 0) {
      Serial.print(millis() - disconnectTime);
      Serial.println(io.mqttStatus());
      Serial.println(ioStatus);
      Serial.println(io.statusText());
      disconnectTime = 0;
    }
  }
 

And I call this function whenever I want to grab the weather during the main loop:
Code: Select all | TOGGLE FULL SIZE
bool getWeather(DynamicJsonDocument &doc)
{
  WiFiSSLClient client;
  Serial.print("connected(): ");
  Serial.println(client.connected());
  if (client.connect(darksky, 443))
  {
    Serial.println("Connected to Darksky");
    client.print("GET /forecast/");
    client.print(DARKSKY_ID);
    client.println("/35.7695,-78.8355?exclude=minutely,flags,alerts,hourly HTTP/1.1");
    client.println("Host: api.darksky.net");
    client.println("Connection: close");
    client.println();
  }

  // Check HTTP status
  char status[32] = {0};
  client.readBytesUntil('\r', status, sizeof(status));
  if (strcmp(status, "HTTP/1.1 200 OK") != 0) {
    Serial.print(F("Unexpected response: "));
    Serial.println(status);
    return false;
  }

  // Skip HTTP headers
  char endOfHeaders[] = "\r\n\r\n";
  if (!client.find(endOfHeaders)) {
    Serial.println(F("Invalid response"));
    return false;
  }
  // Allocate the JSON document
  // Use arduinojson.org/v6/assistant to compute the capacity.
 
  // Parse JSON object
  DeserializationError error = deserializeJson(doc, client);
  if (error) {
    Serial.print(F("deserializeJson() failed: "));
    Serial.println(error.c_str());
    return false;
  }

  client.stop();
}


I initially get the weather first, then set up Adafruit IO in the setup functions. I then enter the loop() function and get the weather every 15 minutes. But once I get the weather after setting up AdafruitIO, my MQTT is connection is gone.

I think it likely is that the ESP32 only can do one connection at a time. But want to ensure that is the case.

mpc823
 
Posts: 46
Joined: Thu May 05, 2016 9:10 am

Please be positive and constructive with your questions and comments.


cron