Please note: Friday June 18 is a holiday celebrating Juneteenth, please allow extra time for your order to arrive and plan accordingly.
0

Using POST to add data points to a feed
Moderators: adafruit_support_bill, adafruit

Forum rules
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.
Please be positive and constructive with your questions and comments.

Using POST to add data points to a feed

by AlexanderMe on Sat Mar 20, 2021 5:16 pm

Good evening,

I try to add data to a feed using POST.

Here is the response I have got using POSTMAN (the AIO key is supplied in a header)
Capture.PNG
Capture.PNG (21.17 KiB) Viewed 441 times


and I am at a complete loss of how to format the data appropriately.

I had to resort to POST because for some reason io.connect(); in my M5Stack Arduino code stopped connecting a few days ago (for both my code that worked for a few months and Adafruit IO examples), and I wanted to work around the issue.

Thank you for your time

AlexanderMe
 
Posts: 4
Joined: Sat Dec 26, 2020 3:31 pm

Re: Using POST to add data points to a feed

by AlexanderMe on Sun Mar 21, 2021 4:24 am

CLARIFICATION

here I included the HTTP request's headers and correct JSON string
Attachments
Capture.PNG
Request with the appropriateJSON datum
Capture.PNG (39.12 KiB) Viewed 431 times
Headers.PNG
Headers for the request
Headers.PNG (51.94 KiB) Viewed 431 times

AlexanderMe
 
Posts: 4
Joined: Sat Dec 26, 2020 3:31 pm

Re: Using POST to add data points to a feed

by AlexanderMe on Mon Mar 22, 2021 3:19 pm

SOLVED:

minimal set of headers
Headers.PNG
Headers.PNG (30.16 KiB) Viewed 414 times


JSON update info goes into the body
Body.PNG
Body.PNG (30.25 KiB) Viewed 414 times

AlexanderMe
 
Posts: 4
Joined: Sat Dec 26, 2020 3:31 pm

Re: Using POST to add data points to a feed

by AlexanderMe on Sat Apr 10, 2021 3:25 pm

It turned out that it is perfectly possible to operate the AdafruitIO from Arduino using HTTP requests only.

Here are the relevant snippets (AdafruitIO credentials are #define(d) in IO_USERNAME and IO_KEY):

reading the latest value of a manual switch from a feed (this did not work if you device got disconnected or reset with the example AdafruitIO Arduino code)

void myGET(String feed) { // get last manual switch value from the dashboard

HTTPClient http;

//String serverPath = "http://io.adafruit.com/api/v2/<my_IO_USERNAME>/feeds/<myNameOfRequiredFeed>/data/last?X-AIO-Key=<my_IO_KEY>"; // replace <...> as appropriate
String serverPath = ((String)F("http://io.adafruit.com/api/v2/") + (String)IO_USERNAME + (String) F("/feeds/") + feed + (String) F("/data/last?X-AIO-Key=") + (String) IO_KEY);

http.begin(serverPath);
//http.addHeader("Content-Type", "application/json" , "Content-Length", jsonData.length()); //Specify content-type header (not required here)

uint32_t resp = http.GET();
String payload = http.getString();

if (resp != 200) {
Serial.println(resp);
Serial.println(payload);
}

// process the returned payload
int indVal = payload.indexOf("value")+8; // the JSON value is from indVal to "
if (payload.charAt(indVal) == '1')
is_on = 1;
else
is_on =0;

http.end();

}

adding new datum to a feed

void myPOST(String JSON_value, String feed) {

HTTPClient http;

String URL;
//String tst = "{\"value\":\"101.5\"}"; // example of a valid JSON string that needs to be passed on to myPOST; use tst to check validity for you
URL = ((String)"http://io.adafruit.com/api/v2/" + (String)IO_USERNAME + (String) "/feeds/" + feed + (String) "/data?X-AIO-Key=" + (String) IO_KEY);

http.begin(URL);
http.addHeader("Content-Type", "application/json" , "Content-Length", JSON_value.length()); //Specify content-type header (required)

int httpCode = http.POST(JSON_value); //Send the request
String payload = http.getString(); //Get the response payload

if (httpCode != 200) {
Serial.println(httpCode); //Print HTTP return code
Serial.println(payload); //Print request response payload
}

http.end(); //Close connection

}

AlexanderMe
 
Posts: 4
Joined: Sat Dec 26, 2020 3:31 pm

Please be positive and constructive with your questions and comments.