Cannot make https calls once connected to IOT Central through MQTT #115
Description
openedon Apr 6, 2020
Wanted implementing ESP 8266 firmware update
upon receiving a command from IOT Central to device, devices would pull a .bin file from Azure storage https URL (no other security for now to keep it simple).
Issue:
Not able to connect to Azure storage https endpoint through "command received" event handler
Diagnostic so far:
Steps:
- to understand where is failure, moved makeHttpRequest method to before and after connect_client(SCOPE_ID, DEVICE_ID, DEVICE_KEY) (in code below)
- used a known https url (with fingerprint hardcoded in the code) to eliminate quirks, if any, of Azure storage URLs
Observations:
https request works before connect_client(SCOPE_ID, DEVICE_ID, DEVICE_KEY), but not after. Logs as below:
Connecting to WiFi..
[HTTP] GET
[HTTP] GET code: 200
removing most HTML for brevity
hostname: iot hub host name parsed from device key by iot-central-firmware
deviceId: device id parsed from device key by iot-central-firmware
username: device id parsed from device key by iot-central-firmware
password: password parsed from device key by iot-central-firmware
[HTTP] GET
[HTTP] GET code: -1
[HTTP] GET failed, error: connection refused
reviewed code of iot-central-firmware and WiFiClient class. But not able to understand why https calls don't work after connect_client(SCOPE_ID, DEVICE_ID, DEVICE_KEY) call
Any explanation would be great.
Thanks
ESP8286 Code is like this:
#include <ESP8266WiFiMulti.h>
#include <ESP8266HTTPClient.h>
#include "src/iotc/common/string_buffer.h"
#include "src/iotc/iotc.h"
#define WIFI_SSID your-wifi-ssid
#define WIFI_PASSWORD your-wifi-password
const char* SCOPE_ID = your iot-central-device-scope-id;
const char* DEVICE_ID = your iot-central-device-id;
const char* DEVICE_KEY = your iot-central-device-key;
void on_event(IOTContext ctx, IOTCallbackInfo* callbackInfo);
#include "src/connection.h"
void on_event(IOTContext ctx, IOTCallbackInfo* callbackInfo)
{
// ConnectionStatus
if (strcmp(callbackInfo->eventName, "Command") == 0)
{
AzureIOT::StringBuffer buffer;
if (callbackInfo->payloadLength > 0) buffer.initialize(callbackInfo->payload, callbackInfo->payloadLength);
LOG_VERBOSE("- Command name was => %s. Command Payload => %s\r\n", callbackInfo->tag,buffer.getLength() ? *buffer : "EMPTY");
makeHttpRequest();
}
}
void makeHttpRequest()
{
WiFiClientSecure client;
client.setFingerprint("40 af 00 6b ec 90 22 41 8e a3 ad fa 1a e8 25 41 1d 1a 54 b3");
HTTPClient http;
if (http.begin(client,"https://jigsaw.w3.org/HTTP/connection.html"))
{
Serial.print("[HTTP] GET...\n");
int httpCode = http.GET();
Serial.printf("[HTTP] GET... code: %d\n", httpCode);
if (httpCode > 0)
{
if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY)
Serial.println(http.getString());
}
else Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());
http.end();
}
else Serial.printf("[HTTP} Unable to connect\n");
}
void setup()
{
Serial.begin(9600);
connect_wifi(WIFI_SSID, WIFI_PASSWORD);
makeHttpRequest();
connect_client(SCOPE_ID, DEVICE_ID, DEVICE_KEY);
makeHttpRequest();
if (context != NULL) lastTick = 0;
}
void loop()
{
if (isConnected)
{
unsigned long ms = millis();
if (ms - lastTick > 5000) //send telemetry every 5 seconds
{
char msg[128] = {0};
lastTick = ms;
int pos = snprintf(msg, sizeof(msg) - 1, "{\"Temperature\": %d, \"Humidity\": %d}",10 + (rand() % 20), 11 + (rand() % 20));
//int errorCode = iotc_send_telemetry(context, msg, pos);
msg[pos] = 0;
//if (errorCode != 0) LOG_ERROR("Sending message has failed with error code %d", errorCode);
}
iotc_do_work(context); // do background work for iotc
}
else
{
iotc_free_context(context);
context = NULL;
connect_client(SCOPE_ID, DEVICE_ID, DEVICE_KEY);
}
}