datasith / Ai_Tips_ESP8266

Code from my "ESP8266 Tips & Tricks" tutorial series on YouTube

Home Page:https://www.youtube.com/playlist?list=PLNFq0T6Z3JPsHwzvPQncip-kMIdWpnnip

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Web API access

Beppi4U opened this issue · comments

Hello!
I try to make the Web API access example from your YouTube Video with the esp8266.
https://www.youtube.com/watch?v=8xqgdXvn3yw&t=607s
Want to modify it to work with my OpenWeatherMap account.
Maybe you can help me with this. Thanks in Advance!
Andreas
Code is attached …. also the Json Output of the OWM

/*------------------------------------------------------------------------------
  11/01/2016
  Author: Makerbro
  Platforms: ESP8266
  Language: C++/Arduino
  File: simple_client.ino
  ------------------------------------------------------------------------------
  Description: 
  Code for YouTube video demonstrating how to run a web client on the ESP8266 to
  get Weather Data from the Weather Underground API:
  https://youtu.be/Edbxyl2BhyU
  
  Do you like my videos? You can support the channel:
  https://patreon.com/acrobotic
  https://paypal.me/acrobotic
  ------------------------------------------------------------------------------
  Please consider buying products from ACROBOTIC to help fund future
  Open-Source projects like this! We'll always put our best effort in every
  project, and release all our design files and code for you to use. 

  https://acrobotic.com/
  https://amazon.com/acrobotic
  ------------------------------------------------------------------------------
  License:
  Please see attached LICENSE.txt file for details.
------------------------------------------------------------------------------*/
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <ArduinoJson.h>

ESP8266WebServer server;
uint8_t pin_led = 16;
char* ssid = "WLAN-MYWLAN";
char* password = "MYPSWD_ABRACADABRA";

#define WU_API_KEY "MY_OPENWEATHERMAP_APIKEY"
#define WU_LOCATION "2916936"   **_// CITY ID CODE_OF MY CITY IN **GERMANY****
#define WU_URL "api.openweathermap.org"

static char respBuffer[4096];

void setup()
{
  pinMode(pin_led, OUTPUT);
  WiFi.begin(ssid,password);
  Serial.begin(115200);
  while(WiFi.status()!=WL_CONNECTED)
  {
    Serial.print(".");
    delay(500);
  }
  Serial.println("");
  Serial.print("IP Address: ");
  Serial.println(WiFi.localIP());

  server.on("/",[](){server.send(200,"text/plain","Hello simple client!");});
  server.on("/getdata",getData);
  server.begin();
}

void loop()
{
  server.handleClient();
}

WiFiClient client;

void getData()
{
  const char request[] = 
** I THINK HERE IS THE PROBLEM WITH THE WORKING WITH THE :JSON RESPOND FROM THE OWM SERVER **
    "GET /data/2.5/weather?id=" WU_LOCATION "&appid=" WU_API_KEY " HTTP/1.1\r\n"  
    "User-Agent: ESP8266/0.1\r\n"
    "Accept: */*\r\n"
    "Host: " WU_URL "\r\n"
    "Connection: close\r\n"
    "\r\n";
  Serial.println(request);
  client.connect(WU_URL, 80);
  client.print(request);
  client.flush();
  delay(1000);

  uint16_t index = 0;
  while(client.connected())
  {
    if(client.available())
    {
      respBuffer[index++] = client.read();
      delay(1);
    }
  }
  client.stop();
  char * json = strchr(respBuffer,'{');

  DynamicJsonBuffer jBuffer;
  JsonObject& root = jBuffer.parseObject(json);
  JsonObject& current = root["weather"];
  String weather = current["description"];
  JsonObject& current = root["main"];
  String temp_c = current["temp"];
  String msg = "Temp(C): "+(temp_c -273,15)+", Weather: "+weather;
  server.send(200,"text/plain", msg);
}

JSON RESPOND FROM OWM SERVER:

{
  "indent_size": "4",
  "indent_char": " ",
  "max_preserve_newlines": "5",
  "preserve_newlines": true,
  "keep_array_indentation": false,
  "break_chained_methods": false,
  "indent_scripts": "normal",
  "brace_style": "collapse",
  "space_before_conditional": true,
  "unescape_strings": false,
  "jslint_happy": false,
  "end_with_newline": false,
  "wrap_line_length": "0",
  "indent_inner_html": false,
  "comma_first": false,
  "e4x": false,
  "indent_empty_lines": false
}

Thanks for giving it a try. Everything looks good, what problems are you having?

Sorry for my late Response!
As long as i understand the COMport snippet. The ESP8266 gets Connection to the OWM API and get the result of the GET method.

Anmerkung 2020-04-03 205403

…..but when i am heading to the Website it gives me this….

Anmerkung 2020-04-03 205419

the >JSON RESPOND FROM OWM SERVER:< from my previous post is the respond when i type direct query to the browser!
Maybe you can Show me how modify the Code to fit to OWM API.
THNX for helping
Andreas

I've been away from this repo for a couple of years. One way to debug these issues is opening the debug tools on modern web browsers, and seeing what the errors on the JS side are. I'd be happy to revisit this issue if you're still having trouble. I'm closing the issue in the meantime, but anyone for anyone stumbling here by way of a search, feel free to re-open as needed.