0

Hi there I want to send a request to the server with get method with URL parameters, when a key is pressed with nodemcu. But I Can't Do This Work, I Got the Below Error From Serial Monitor:

I Get This Error From Serial Monitor

 ets Jan  8 2013,rst cause:4, boot mode:(3,7)

wdt reset
load 0x4010f000, len 1384, room 16 
tail 8
chksum 0x2d
csum 0x2d
v951aeffa
~ld

My Main Code

#include <ESP8266WiFi.h> 
const char* ssid = "MobinNet1365"; 
//replace with your own wifi ssid 
const char* password = "G12229M1Q64"; //replace with your own //wifi ssid password 
const char* host = "webhook.site";

const int button = 8;
int temp = 0;

void setup() { 
  
  Serial.begin(115200); 
  pinMode(button, INPUT);
  delay(10);
  Serial.println(); 
  Serial.print("Connecting to ");
  Serial.println(ssid);
  
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  
  while (WiFi.status() != WL_CONNECTED){
    delay(500);
    Serial.print(".");
  }
  
  Serial.println("");
  Serial.println("WiFi connected"); 
  Serial.println("IP address: "); 
  Serial.println(WiFi.localIP()); 
} 


void loop() {
  
  temp = digitalRead(button);  
  if (temp == HIGH) {       
    Serial.println(host);
    WiFiClient client;
    const int httpPort = 80;
    
    String url = "/df35c8cd-0398-4c92-b55f-b9e36629b309";
    url += "?switche=";
    url += "1";  
       
    client.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n");      
  }
}

my request sending code is below:

#include <ESP8266WiFi.h> 
const char* ssid = "your-ssid";
const char* password = "your-password";
const char* host = "";

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

    Serial.println("");
    Serial.println("WiFi connected"); 
    Serial.println("IP address: "); 
    Serial.println(WiFi.localIP()); 
} 
int value = 0; 
void loop() { 
    delay(5000);
    ++value;
    Serial.print("connecting to ");
    Serial.println(host);
    WiFiClient client;
    const int httpPort = 80;
    if (!client.connect(host, httpPort)) {
        Serial.println("connection failed");
        return;
    }



    String url = "/";
    /* url += "?param1=";
    url += param1;
    url += "?param2=";
    url += param2;
    */
    Serial.print("Requesting URL: ");
    Serial.println(url);
    client.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n");
    unsigned long timeout = millis();
    while (client.available() == 0) {
        if (millis() - timeout > 5000)
        {
            Serial.println(">>> Client Timeout !");
            client.stop(); 
            return;
        } 
    }

    while (client.available())
    {
        String line = client.readStringUntil('\r'); Serial.print(line);
    }
    
    Serial.println();
    Serial.println("closing connection"); 
}

And My Key Press Code Detector Is below:

const int button = 8;
int temp = 0;

void setup() {
  Serial.begin(9600);
  pinMode(button, INPUT);
}

void loop() {
  temp = digitalRead(button);
     
   if (temp == HIGH) {
      Serial.println("LED Turned ON");
      delay(1500);
   }
   else {
      Serial.println("LED Turned OFF");
      delay(1500);
   }
}

And My Circuit Is:

NodeMcu => MicroSwitch

VIN => C

D0 => NO

G => 10k => NO

How Can I Make It When That Key Pressed It Send Request?

4
  • I don't quite understand--what is ypur actual code? One chunk of code never initializes "host", another does. What is the actual code that causes the error? Are you making a request between two NodeMCUs? – Dave Newton May 31 at 16:59
  • No its just for sending a request to a server when a key pressed but i get "ets Jan 8 2013,rst cause:4, boot mode:(3,7) wdt reset load 0x4010f000, len 1384, room 16 tail 8 chksum 0x2d csum 0x2d v951aeffa ~ld" error and that code has header named main code is my actual code – Alirezaaraby May 31 at 21:21
  • Please include only the relevant code. – Dave Newton Jun 1 at 12:03
  • why do you believe that the ets Jan 8 2013,rst cause:4, boot mode:(3,7) .... message is an error message? – jsotola Jun 1 at 15:43
1

IMHO, some consideration :

  1. const int button = 8; const int button = 5; // CHANGE TO Pin "D1 GPIO5"

GPIO8 is used to connect the flash chip, you may change to Pin "D1" which is GPIO 5.

  • Check with your board's pinout.
  1. to prevent non-stop keep calling within in the loop, you may use millis();

for example :

void GetUrl() {
    temp = digitalRead(button);  
  if (temp == HIGH) {       
    Serial.println(host);
    WiFiClient client;
    const int httpPort = 80;
    
    String url = "/df35c8cd-0398-4c92-b55f-b9e36629b309";
    url += "?switche=";
    url += "1";  

    String link = String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n";
//    Serial.println( "link : " + link );
    client.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n");      
  }
}


unsigned long previousMillis = 0;
const long interval = 1000 * 0.5; // 500ms (0.5 sec)

void loop() {
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
    GetUrl();
  }
}

Reference :

https://www.arduino.cc/en/Tutorial/BuiltInExamples/BlinkWithoutDelay

https://iotbyhvm.ooo/gpio-pins-esp8266/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.