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

Problems connecting to Insecure server while also connecting to a secure mqtt broker (with a certificate) #9054

Open
ahmetcemturan opened this issue Dec 17, 2023 · 0 comments

Comments

@ahmetcemturan
Copy link

ahmetcemturan commented Dec 17, 2023

Hi All,
My problem is this: I am connecting to an MQTT broker with a certificate.. That works ok.
But then further down I have to send data to a few weather services, they accept https and http requests.
The code that is sent is correct. It worked before I added the certificate and it works when I copy and paste it int chrome.

I really dont know what to do. I have found a few similar things but none have worked or I was unsuccessful implementing them...

the sending part is basically this:

    Link = "https://www.windguru.cz/upload/api.php?";
    getData = Link + getData + "uid=" + String(Station_1_uid); // WindGuru ID
    getData = getData + "&salt=" + String(time);               // Time
    getData = getData + "&hash=" + md5.toString();             // Identification
    getData = getData + "&wind_min=" + String(WindMin1ms * kKnots, 2);
    if (!isnan(bmpT))
      getData = getData + "&temperature=" + String(bmpT, 1); // temperature
#ifdef DataDEBUG
    Serial.println(getData);
#endif
    std::unique_ptr<BearSSL::WiFiClientSecure> client(new BearSSL::WiFiClientSecure);
    client->setInsecure();
    HTTPClient https;              // must be declared after WiFiClient for correct destruction order, because used by http.begin(client,...)
    https.begin(*client, getData); // Specify request destination
    int httpCode = https.GET();    // Send the request
#ifdef DataDEBUG
    Serial.println(httpCode); // ACT!
                              // Serial.printf("[HTTP] GET failed, error: %s\n", https.errorToString(httpCode).c_str());
#endif
    if (httpCode > 0)
    {                                     // Check the returning code
      String payload = https.getString(); // Get the request response payload
#ifdef DataDEBUG
      Serial.println(payload); // Print the response payload
#endif
      if (mqttClient.connected() && (payload != "OK"))
        mqttClient.publish((MQTT_TOPIC + "/debug").c_str(), payload.c_str());
    }
    https.end(); // Close connection
  }
  else
  {
    Serial.println("No wind data available");
    return false; // fail
  }
  return true; // done
}

Thanks for any help.
(board ESP8266 07s)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant