Sending a POST Request from inside IoT cloud

Hey there,

One way I've been triggering Alexa in custom ways is Virtual Buttons, which allows you to trigger an Alexa routine using a simple POST rtequest. I've gotten this concept to work using a POST example without using IoT, and I've managed to get the device recognized and working on IoT, but I can't figure out how to send a POST request to a different server while I'm connected to the IoT cloud. Is there a way to do this?

I'm using a MKR 1010.

Thanks for any and all help!

I have similar challenge - I try to use a GET http request to get current weather from OpenWeather and as the same time have a Arduino Cloud connection. It works if I don't use the cloud, but with ArduninoCloud connected, when I use #include <WiFiNINA.h> I can establish a connection, but when trying to read with line = client.readStringUntil('\n'); I never get any response. Seems to be a conflict between WiFiNINA and Arduino_ConnectionHandler.

I guess that I should use getClient() from arduino-libraries/Arduino_ConnectionHandler (github.com) to get a client I can use to make my GET. But I can't find how to use it. Maybe it is too obvious...

1 Like

Worst case scenario I could use another MKR reading from the first via Serial in order to have one connected to cloud, and one able to do POST requests, but that's such a waste of resources. Maybe we need to actually disconnect from the cloud in the main loop to grab that info and then reconnect? I haven't tried that yet, Arduino really isn't my sweet spot, but I'm learning!

Disconnecting to make the query and then connecting again was a good idea, I'll try that!

No, sorry, it did not work. Using the connection handler, GET works fine
#include <Arduino_ConnectionHandler.h>

but as soon as you include
#include <ArduinoIoTCloud.h>
(not even staring the cloud with begin)

it stops getting the reply from the webserver.

Here is the code... Uncomment #include <ArduinoIoTCloud.h> and it will stop working

/J.

#include <Arduino_MKRIoTCarrier.h>
// #include "thingProperties.h"
// #include <ArduinoIoTCloud.h>
#include <Arduino_ConnectionHandler.h>

MKRIoTCarrier carrier;
WiFiClient client;

char ssid[] = "xxxx"; //  your network SSID (name)
char pass[] = "xxxx";//  your network PASSWORD ()
String apiKey = "xxxx"; // Open weather map api key

int status = WL_IDLE_STATUS;
char server[] = "api.openweathermap.org";
char cstr[32];

void setup() {
  
  int numberOfTries = 0, maxTries = 5;
    
  CARRIER_CASE = true;
  carrier.begin();
  
  carrier.leds.setPixelColor(0, 16); carrier.leds.show();         

  //Initialize serial and wait for port to open:
  Serial.begin(9600);
  while (numberOfTries++ <maxTries) {
    if (!Serial) {
      delay(1000);
    } 
    else {
      Serial.println("Connected to serial");
      carrier.leds.setPixelColor(1, 16); carrier.leds.show(); 
      break;        
    }
  }

  // attempt to connect to Wifi network:
  WiFiConnectionHandler ArduinoIoTPreferredConnection(ssid, pass);
  numberOfTries = 0;
  while (numberOfTries++ <maxTries) {
    if(status != WL_CONNECTED) {
      Serial.print("Attempting to connect to SSID: ");
      Serial.println(ssid);
      status = WiFi.begin(ssid, pass);
      delay(1000);
    }
    else {
      carrier.leds.setPixelColor(2, 16); carrier.leds.show();         
      Serial.println("Connected to wifi");
      break;
   }
 }

//  carrier.leds.setPixelColor(3, 16); // 'On' pixel at head
//  carrier.leds.show();         

}

void loop() {
  Serial.println("Setup ready");

  String callAPI;
  String line = "";
  int i;

  Serial.println("\nStarting connection to server...");

  client.setTimeout(5000);

  if (client.connect(server, 80)) {
    Serial.println("connected to server");
    // Make a HTTP request:
    
    callAPI = "GET /data/2.5/onecall?lat=62.59951&lon=18.03113&cnt=3&units=metric&exclude=hourly,daily,alerts&appid=" + apiKey;

    Serial.println(callAPI);
    client.println(callAPI);
    client.println("Host: api.openweathermap.org");
    client.println("Connection: close");
    client.println();
  } else {
    Serial.println("Unable to connect");
  }

  if (client.connected()) {
    Serial.println("Reading from host...");
    line = client.readStringUntil('\n');
    Serial.println("Finished reading.");
    Serial.println(line);
    delay(2000);
  }
}

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.