Как получить данные в реальном времени из URL-адреса в NodeMcu с помощью Arduino? - PullRequest
0 голосов
/ 01 августа 2020

Описание проблемы Я хочу, чтобы мой NodeMCU получал последние обновленные данные с URL-адреса каждый раз, когда l oop. В настоящее время он не обновляет полученный контент.

Например, Ссылка: http://abcdxyz.xyz/io.php содержит только цифры 223838. Как только NodeMCU начинает работать, и информация о ссылке обновляется до 240000, Тем не менее, последовательный принтер Arduino показывает 223838 даже после обновления ссылки на сервере.

Вот мой код:

#include "ESP8266WiFi.h"
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
# define LED D4 // Use built-in LED which connected to D4 pin or GPIO 2

String serverName = "http://abcdxyz.xyz/";
unsigned long timerDelay = 5000;
unsigned long lastTime = 0;


// WiFi parameters to be configured
const char* ssid = "XXXXXXXXX";
const char* password = "XXXXXXXXXX";


void setup() {
  pinMode(LED, OUTPUT);     // Initialize the LED as an output
  Serial.begin(9600);
  // Connect to WiFi
  WiFi.begin(ssid, password);

  // while wifi not connected yet, print '.'
  // then after it connected, get out of the loop
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  //print a new line, then print WiFi connected and the IP address
  Serial.println("");
  Serial.println("WiFi connected");
  // Print the IP address
  Serial.println(WiFi.localIP());

}

void loop() {

  //Calling An URL FOR DATA


   if ((WiFi.status() == WL_CONNECTED)) { //Check the current connection status
 
    HTTPClient http;
 
    http.begin("http://abcdxyz.xyz/io.php"); //Specify the URL
    int httpCode = http.GET();                                        //Make the request
 
    if (httpCode > 0) { //Check for the returning code
 
        String payload = http.getString();
        Serial.println(httpCode);
        Serial.println(payload);
      }
 
    else {
      Serial.println("Error on HTTP request");
    }
 
    http.end(); //Free the resources
  }
 
  delay(1000);

  //Calling URL ENDS
digitalWrite(LED, HIGH);
delay(100);
digitalWrite(LED, LOW);

}

1 Ответ

0 голосов
/ 02 августа 2020

Разобрался. Ссылка должна быть новой ссылкой каждый раз для ПОЛУЧЕНИЯ последних записей. Следуйте приведенным ниже кодам:

void loop() {

  //Calling An URL FOR DATA


   if ((WiFi.status() == WL_CONNECTED)) { //Check the current connection status
 
    HTTPClient http;
 randNumber = random(1, 99999);

 String crow = "http://abcdxyz.xyz/io.php?tag=";
 crow += randNumber;
 Serial.println(crow);


http.begin(crow); //Specify the URL

    int httpCode = http.GET();     //Make the request
 
    if (httpCode > 0) { //Check for the returning code
 
        String payload = http.getString();
        Serial.println(httpCode);
        Serial.println(payload);
      }
 
    else {
      Serial.println("Error on HTTP request");
    }
 
    http.end(); //Free the resources
  }
 
  delay(1000);

  //Calling URL ENDS
digitalWrite(LED, HIGH);
delay(100);
digitalWrite(LED, LOW);

}
...