I Can't Send Request With NODEMCU When A Key Press

Hi there I want to send a request to the server as a get method with URL parameters when a key pressed with nodemcu. But I Cant Do This Work.

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?

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

Problem:

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

the 'main code' sketch doesn't connect to server, has an incomplete url, doesn't read the response...

You should post code by using code-tags
There is an automatic function for doing this in the Arduino-IDE
just three steps

  1. press Ctrl-T for autoformatting your code
  2. do a rightclick with the mouse and choose "copy for forum"
  3. paste clipboard into write-window of a posting

best regards Stefan

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.