【问题标题】:How to fetch Real-Time data from URL in NodeMcu using Arduino?如何使用 Arduino 从 NodeMcu 中的 URL 获取实时数据?
【发布时间】:2020-08-01 04:38:18
【问题描述】:

问题陈述 我希望我的 NodeMCU 每次循环都从 URL 获取最新的更新数据。 目前它不会更新获取的内容。

例如,链接: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);

}

【问题讨论】:

    标签: arduino esp8266 nodemcu arduino-esp8266 esp8266wifi


    【解决方案1】:

    想通了。每次获取最新记录时,链接都必须是新链接。 请遵循以下代码:

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

    【讨论】:

      猜你喜欢
      • 2017-10-14
      • 1970-01-01
      • 1970-01-01
      • 2015-03-26
      • 1970-01-01
      • 1970-01-01
      • 2017-10-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多