electronicsguy / ESP8266

ESP8266 Projects
242 stars 183 forks source link

Help- Multiple instances #93

Closed michaeloconnor1717 closed 4 years ago

michaeloconnor1717 commented 4 years ago

I am attempting to make a smart clock that gets information from two different servers (Google scripts and api.weather.gov). I wanted to know if that is possible because I have been unsuccessful so far. I can connect to them separately, but when I try to connect to both of them in the same script it drops the connection to the first one and fails to connect to the second one.

Sketch

TTPSRedirect* google = nullptr;
const char* host = "script.google.com";
const int httpsPort = 443;
const char* fingerprint = "";
String url = String("/macros/s/") + GScriptId + "/exec";

HTTPSRedirect* weather = nullptr;
const char* host1 = "api.weather.gov";
String url1 = "/gridpoints/LZK/49,94/forecast";

SoftwareSerial espToMega(16, 5);

// Define NTP Client to get time
#define utcOffsetInSeconds -18000
WiFiUDP ntpUDP;

NTPClient timeClient(ntpUDP, "pool.ntp.org", utcOffsetInSeconds);
void setup() {
  pinMode(10, INPUT);
  espToMega.begin(115200);
  Serial.begin(9600);
  delay(10);
  Serial.println('\n');

  // Connect to WiFi
  WiFi.begin(ssid, password);
  Serial.print("Connecting to ");
  Serial.print(ssid); Serial.println(" ...");

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

  Serial.println('\n');
  Serial.println("Connection established!");
  Serial.print("IP address:\t");
  Serial.println(WiFi.localIP());

  // Use HTTPSRedirect class to create a new TLS connection
  google = new HTTPSRedirect(httpsPort);
  google->setInsecure();
  google->setPrintResponseBody(true);
  google->setContentTypeHeader("application/json");

  espToMega.println("Connecting");
  Serial.print("Connecting to ");
  Serial.println(host);

  // Try to connect for a maximum of 5 times
  bool flag = false;
  for (int i = 0; i < 5; i++) {
    int retval = google->connect(host, httpsPort);
    if (retval == 1) {
      flag = true;
      Serial.println("Connected");
      break;
    }
    else
      Serial.println("Connection failed. Retrying...");
  }

  if (!flag) {
    Serial.print("Could not connect to server: ");
    Serial.println(host);
    return;
  }

  weather = new HTTPSRedirect(httpsPort);
  weather->setInsecure();
  weather->setPrintResponseBody(true);
  weather->setContentTypeHeader("application/json");

  espToMega.println("Connecting");
  Serial.print("Connecting to ");
  Serial.println(host1);

  bool flag1 = false;
  for (int i = 0; i < 5; i++) {
    int retval1 = weather->connect(host1, httpsPort);
    if (retval1 == 1) {
      flag1 = true;
      Serial.println("Connected");
      break;
    }
    else
      Serial.println("Connection failed. Retrying...");
  }

  if (!flag1) {
    Serial.print("Could not connect to server: ");
    Serial.println(host1);
    return;
  }

  espToMega.println("clear");
  espToMega.flush();
  delay(500);
  // delete HTTPSRedirect object
  delete google;
  google = nullptr;

  delete weather;
  weather = nullptr;

  timeClient.begin();
  timeClient.update();
  getInitialTime();
  delay(1000);
  requestEvents();
} 

Serial Output

12:51:05.174 -> 
12:51:05.208 -> Connection established!
12:51:05.208 -> IP address: 192.168.1.15
12:51:05.242 -> Connecting to script.google.com
12:51:06.300 -> Connected
12:51:06.334 -> Connecting to api.weather.gov
12:51:06.402 -> Connection failed. Retrying...
12:51:06.436 -> Connection failed. Retrying...
12:51:06.470 -> Connection failed. Retrying...
12:51:06.504 -> Connection failed. Retrying...
12:51:06.572 -> Connection failed. Retrying...
12:51:06.606 -> Could not connect to server: api.weather.gov
12:51:14.304 -> requesting data...
12:51:14.304 -> Error! Not connected to host.
12:51:14.338 -> Error while connecting
12:51:14.372 -> Events: Time Sent
12:51:44.255 -> ~0 01:29:00
12:52:14.292 -> requesting data...
12:52:14.327 -> Error! Not connected to host.
12:52:14.360 -> Error while connecting
12:52:14.394 -> Events:
github-actions[bot] commented 4 years ago

Welcome to HTTPSRedirect! Please provide enough info to debug the issue.

michaeloconnor1717 commented 4 years ago

esp8266 does not appear to have the capabilities to handle multiple connections simultaneously, but closing one and opening another (so just switching back and forth) works like a charm