无法连接 ESP8266 到服务器

问题描述 投票:0回答:0

我想从网页切换 ESP8266 的 GPIO 2。

**这是我的 Arduino 代码: **

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#include <Arduino_JSON.h>


const char* ssid ="****";
const char* password ="******";

// Your IP address or domain name with URL path
const char* serverName = "ppcmicro.gigfa.com/esp-outputs-action.php?action=outputs_state&board=1";

// Update interval time set to 5 seconds
const long interval = 10000;
unsigned long previousMillis = 0;

String outputsState;

void setup() {
  Serial.begin(115200);
  
  WiFi.mode(WIFI_STA);
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("Connected to WiFi");
}

void loop() {
  unsigned long currentMillis = millis();
  
  if(currentMillis - previousMillis >= interval) {
     // Check WiFi connection status
    if ((WiFiMulti.run() == WL_CONNECTED)) {
      outputsState = httpGETRequest(serverName);
      Serial.println(outputsState);
      JSONVar myObject = JSON.parse(outputsState);
  
      // JSON.typeof(jsonVar) can be used to get the type of the var
      if (JSON.typeof(myObject) == "undefined") {
        Serial.println("Parsing input failed!");
        return;
      }
    
      Serial.print("JSON object = ");
      Serial.println(myObject);
    
      // myObject.keys() can be used to get an array of all the keys in the object
      JSONVar keys = myObject.keys();
    
      for (int i = 0; i < keys.length(); i++) {
        JSONVar value = myObject[keys[i]];
        Serial.print("GPIO: ");
        Serial.print(keys[i]);
        Serial.print(" - SET to: ");
        Serial.println(value);
        pinMode(atoi(keys[i]), OUTPUT);
        digitalWrite(atoi(keys[i]), atoi(value));
      }
      // save the last HTTP GET Request
      previousMillis = currentMillis;
    }
    else {
      Serial.println("WiFi Disconnected");
    }
  }
}

String httpGETRequest(const char* serverName) {
  
  WiFiClient client;
  HTTPClient http;
    
  // Your IP address with path or Domain name with URL path 
  http.begin(client, serverName);
  
  // Send HTTP POST request
  int httpResponseCode = http.GET();
  
  String payload = "{}"; 
  
  if (httpResponseCode>0) {
    Serial.print("HTTP Response code: ");
    Serial.println(httpResponseCode);
    payload = http.getString();
  }
  else {
    Serial.print("Error code: ");
    Serial.println(httpResponseCode);
  }
  // Free resources
  http.end();

  return payload;
}

这是完整的错误:

连接到*** .......已连接到WiFi 错误代码:-1 {} JSON 对象 = {}

---------------- 在这里剪切异常解码器 ----------------

例外(29): epc1=0x402022a3 epc2=0x00000000 epc3=0x00000000 excvaddr=0x00000004 depc=0x00000000

堆栈>>>

ctx:续 sp:3ffffd70 结束:3fffffd0 偏移量:0190 3fffff00: 000005c0 000000b8 3ffe85f4 40202203 3fffff10: 3ffef65e fffffffc 3fffff40 40202259 3fffff20: 3ffe8946 fffffffc 00000000 40100a14 3fffff30: 40205930 3ffe8944 3ffee72c 3fffff50 3fffff40: 3ffee69c 3fffff84 3fffff78 40202b13 3fffff50: 00002710 3ffee72c 00000000 4020285e 3fffff60: 00000000 00007463 00000000 402074b5 3fffff70: 4023df8a 3ffee72c 3ffee6c8 40208408 3fffff80: 00000000 4020a50c 3ffef8bc 00000000 3fffff90: 3ffee6b0 3ffe8944 3ffee72c 3ffee7d4 3fffffa0: 3fffdad0 00000000 3ffee7a8 3ffee7d4 3ffffb0: 3fffdad0 00000000 3ffee7a8 40207564 3fffffc0: feefeffe feefeffe 3fffdab0 40100dbd <<

---------------- 在这里剪切异常解码器 ----------------

ets 2013 年 1 月 8 日,第一个原因:2,启动模式:(3,7)

load 0x4010f000,len 3424,房间 16 尾部 0 校验和 0x2e 加载 0x3fff20b8,长度 40,房间 8 尾部 0 校验和 0x2b csum 0x2b v0004a4d0 ~ld

Http Request 好像不行。我该如何解决? 如果您认为有帮助,我也可以发送我的 PHP 代码。

esp8266 arduino-esp8266
© www.soinside.com 2019 - 2024. All rights reserved.