• esp8266+http (PlatformIO)


    esp8266 + http

    使用esp8266发起http请求
    
    #include <Arduino.h>
    #include <ESP8266WiFi.h>
    #include <ESP8266HTTPClient.h>
    
    #define led_1 2
    
    const char *SSID = "优美屋205";
    const char *PASSWORD = "18111549";
    const char *URL = "http://192.168.124.17:3000/users";
    
    void init_wifi();
    void esp8266Http(); // http
    
    HTTPClient httpClient;
    WiFiClient client;
    
    void setup() {
      Serial.begin(115200);
      pinMode(led_1, OUTPUT);
      init_wifi();
    }
    
    void loop() {
      digitalWrite(led_1, LOW);
      delay(500);
      digitalWrite(led_1, HIGH);
      delay(500);
      esp8266Http();
    }
    
    void init_wifi(){
      Serial.println("连接中...");
      Serial.println(SSID);
    
      WiFi.begin(SSID, PASSWORD);
      while (WiFi.status() != WL_CONNECTED)
      {
        delay(500);
        Serial.print(".");
      }
    
      Serial.print("IP:");
      Serial.println(WiFi.localIP());
      
    }
    
    void esp8266Http(){
      httpClient.begin(client, URL);
      int httpResponseCode = httpClient.GET();
    
      if(httpResponseCode > 0){
        Serial.print("HTTP Response code: ");
        Serial.println(httpResponseCode);
        String payload = httpClient.getString();
        Serial.println(payload);
      }else{
        Serial.print("Error code:");
        Serial.println(httpResponseCode);
      }
      // 结束
      httpClient.end();
    }
    
  • 相关阅读:
    KMP算法的理解和代码实现
    关于线程死锁
    PAT1018
    PAT1059
    PAT1009
    PAT1006
    PAT1005
    PAT1004
    PAT1002
    PAT
  • 原文地址:https://www.cnblogs.com/sjie/p/16307411.html
Copyright © 2020-2023  润新知