gabrielcsapo / gabrielcsapo.com

📕 code samples and content on my blog https://www.gabrielcsapo.com
https://www.gabrielcsapo.com
0 stars 0 forks source link

Post request to microsoft power bi streaming data set #7

Closed supekshala closed 2 years ago

supekshala commented 3 years ago

Hello I need to send a JSON object to the power bi streaming data set. I manage to do this with nodemcu. here is the code I used in modemcu.

#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);
  }

}

But I tried with Arduino mega esp8266 inbuilt board and I couldn't connect to power bi. here the code I tried for arduino mega esp board. I got this code from the WIFIEsp library.

#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[] = "https://api.powerbi.com/beta/d1323671-cdbe-4417-b4d4-bdb24b51316b/datasets/60dfb0df-9bf8-4c05-8505-69a32ec90a95/rows?key=H1wfQ3c5rxxmgrDqcZkARXK%2FCypXYyN77IDXoYA13z5%2FnXN9JRAXGehl15Tb0is43ikRShQauxuQH45p%2FJV8dg%3D%3D";
String URI = "/";

// Initialize the Ethernet client object
WiFiEspClient client;
int amps;
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();

  if (client.connect(server, 443)) {
    Serial.println(F("con..."));
    int value=10;
    String postData = "[{\"temperature\":\" "+String(value)+"\"}]";
    // send the HTTP GET request:
    client.println("POST " + URI + " HTTPS/1.1");
    client.println("Content-Type: application/json");
    client.println("Host: " + String(server));
    client.print("Content-Length: ");
    client.println(postData.length());
    client.println();
    client.println(postData);
    clinet.flush();
    // 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");
}
gabrielcsapo commented 2 years ago

This question is more about a specific piece of functionality rather than the articles themselves. Sorry I don't have a way to help with this.