gabrielcsapo / gabrielcsapo.com

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

Arduino mega + ESP8266 board #5

Closed supekshala closed 3 years ago

supekshala commented 3 years ago

I am using Arduino Mega 2560 R3 built-in ESP8266 dev board for a project. my project basically reading few analog data and sends to the power bi dashboard using streaming dataset API. I have no idea yet how to program this particular board for this purpose. Previously I did this with node MCU board and it's worked. Here's the code I wrote for Node MCU.


#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"

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

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

}```
gabrielcsapo commented 3 years ago

Hey @supekshala !

Can you give info about the board (eg. version, etc) and what errors you are receiving as a result of it?

supekshala commented 3 years ago

Iam using Mega 2560 R3 built-in ESP8266 board. I try to follow this https://www.gabrielcsapo.com/arduino-web-server-mega-2560-r3-built-in-esp8266/? but I am using a windows computer.

gabrielcsapo commented 3 years ago

Were you able to flash the firmware as instructed?

image

Are you having issues with something particular about the post or asking to do something specific that the post doesn't cover?

supekshala commented 3 years ago

No I think I messed up with that. First problem I have is to how to write a sketch to connect to internet using WiFi, read analog pins and POST data. In the first glance this is what came to my stupid mind “IF it is WiFi, upload a sketch to ESP, so I uploaded a sketch to ESP by setting the DIP switches 5,6,7 NO only” it could establish connection and POST data , but it couldn’t communicate with the analog pins of Arduino as expected. Yes, I’m wrong.
what Iam trying to do is, Read analog pins of Arduino mega >>>> send those data to the API.

gabrielcsapo commented 3 years ago

@supekshala it is important that when running the verify command you get the version output correctly. I can update the article to include a part about reading analog pins to show how to communicate that information into the server code.

supekshala commented 3 years ago

That would be a huge help. If you could please update the article about reading analog values from the mega+esp8266 board and sending those data into the Power BI streaming dataset. that would be great. THANK YOU VERY MUCH!!

supekshala commented 3 years ago

I successfully installed the AT firmware and It's responding to AT commands. 199830745_233308388294963_7149238890501587581_n

supekshala commented 3 years ago

Any update : )

gabrielcsapo commented 3 years ago

@supekshala I have posted a new section in https://www.gabrielcsapo.com/arduino-web-server-mega-2560-r3-built-in-esp8266/ that contains the interfacing with a sensor and showing that sensor data in the web server response.