bportaluri / WiFiEsp

Arduino WiFi library for ESP8266 modules
GNU General Public License v3.0
550 stars 210 forks source link

REST API Post request to microsoft power bi real time data set #211

Closed supekshala closed 4 months ago

supekshala commented 3 years ago

I need to send data to the power bi dashboard using arduinomega+esp8266 inbuilt board. I could not manage to make the post request to power bi streaming data set API. Previously I used a nodemcu board to this task and it worked flawlessly. this is the code I used in nodemcu.

#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClientSecureBearSSL.h>

#define SERVER_IP "https://api.powerbi.com/beta/d1323671-cdbe-4417-b4d4-bdb24b51316b/datasets/ce600abb-0afd-47e1-bdaf-6491f91945bd/rows?noSignUpCheck=1&key=stAvYHSNB6lDf%2BEEXS%2BU%2BsA2Kbyd3%2Bx9NKnWyZj5Uz5YPp%2Bbu7zdPUxboto4aXhls8rObnfYQXFPoIeo9ALrAA%3D%3D"

#define WI_SSID "SLT_FIBRE"
#define WI_PW  "dehff"
WiFiClient client;
void sendData(int temp){
  std::unique_ptr<BearSSL::WiFiClientSecure>client(new BearSSL::WiFiClientSecure);
  client->setInsecure();
  HTTPClient https;

  Serial.print("[HTTP] begin...\n");
  // configure traged server and url
  https.begin(client, SERVER_IP); //HTTPS
  https.addHeader("Content-Type", "application/json");

  Serial.print("[HTTP] POST...\n");
  // start connection and send HTTP header and body
  int httpCode = https.POST("[{\"Value\": " + String(temp) + "}]");

  // httpCode will be negative on error
  if (httpCode > 0) {
    // HTTP header has been send and Server response header has been handled
    Serial.printf("[HTTP] POST... code: %d\n", httpCode);

    // file found at server
    if (httpCode == HTTP_CODE_OK) {
      const String& payload = https.getString();
      Serial.println("received payload:\n<<");
      Serial.println(payload);
      Serial.println(">>");
    }
  } else {
    Serial.printf("[HTTP] POST... failed, error: %s\n", https.errorToString(httpCode).c_str());
  }

  https.end();

}

void setup() {
  Serial.begin(115200);

  Serial.println();
  Serial.println();
  Serial.println();

  WiFi.begin(WI_SSID, WI_PW);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected! IP address: ");
  Serial.println(WiFi.localIP());

}

void loop() {
  // wait for WiFi connection
  int temp = analogRead(A0);
  Serial.println(temp);
  if ((WiFi.status() == WL_CONNECTED)) {
    sendData(temp);
  }

}

This the code I tried in Arduino mega +ESP8266 board but it didn't work.

#include "WiFiEsp.h"

#ifndef HAVE_HWSERIAL3
#endif

char ssid[] = "SLT_FIBRE";            // your network SSID (name)
char pass[] = "kavindusupekshala";        // your network password
int status = WL_IDLE_STATUS;     // the Wifi radio's status

char server[] = "api.powerbi.com/beta/d1323671-cdbe-4417-b4d4-bdb24b51316b";
 String URI= "/datasets/60dfb0df-9bf8-4c05-8505-69a32ec90a95/rows?tenant=&UPN=&key=H1wfQ3c5rxxmgrDqcZkARXK%2FCypXYyN77IDXoYA13z5%2FnXN9JRAXGehl15Tb0is43ikRShQauxuQH45p%2FJV8dg%3D%3D";

// Initialize the Ethernet client object
WiFiEspClient client;

void setup()
{
  // initialize serial for debugging
  Serial.begin(115200);
  // initialize serial for ESP module
  Serial3.begin(115200);
  // initialize ESP module
  WiFi.init(&Serial3);

  // check for the presence of the shield
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    // don't continue
    while (true);
  }

  // attempt to connect to WiFi network
  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to WPA SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network
    status = WiFi.begin(ssid, pass);
  }

  // you're connected now, so print out the data
  Serial.println("You're connected to the network");

  printWifiStatus();

  Serial.println();
  Serial.println("Starting connection to server...");

  if (client.connect(server, 443)) {
    Serial.println(F("con..."));
    // send the HTTPS POST request:
    int value=100;
 String content = "{\"temperature\":\""+String(value)+"\"}";
    client.println("POST " + URI + " HTTP/1.1");
    client.println("Host: " + String(server));
    client.println("Content-Type: application/json");
    client.print("Content-Length: ");
    client.println(content.length());
    client.println();
    client.println(content);
    // note the time that the connection was made:

  } else {
    // if you couldn't make a connection:
    Serial.println(F("con failed"));
  }

}

void loop()
{
  // if there are incoming bytes available
  // from the server, read them and print them
  while (client.available()) {
    char c = client.read();
    Serial.write(c);
  }

  // if the server's disconnected, stop the client
  if (!client.connected()) {
    Serial.println();
    Serial.println("Disconnecting from server...");
    client.stop();

    // do nothing forevermore
    while (true);
  }
}

void printWifiStatus()
{
  // print the SSID of the network you're attached to
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);

  // print the received signal strength
  long rssi = WiFi.RSSI();
  Serial.print("Signal strength (RSSI):");
  Serial.print(rssi);
  Serial.println(" dBm");
}

This is the dataset I tried to connect. API = https://api.powerbi.com/beta/d1323671-cdbe-4417-b4d4-bdb24b51316b/datasets/60dfb0df-9bf8-4c05-8505-69a32ec90a95/rows?tenant=&UPN=&key=H1wfQ3c5rxxmgrDqcZkARXK%2FCypXYyN77IDXoYA13z5%2FnXN9JRAXGehl15Tb0is43ikRShQauxuQH45p%2FJV8dg%3D%3D tss [ { "temperature" :98.6 } ]

sofresh007 commented 1 year ago

im trying to do the same but on r4 wifi, cant get it to work either.