So you're a Noob? Post your questions here until you graduate! Don't be shy.

User avatar
By alexplus100
#84709 Hi All, thanks in advance for looking and trying to help! This code's self-explanatory, I'm trying to push a physical button and do a HTTP request.

The IF/ELSE is probably not set up properly because it repeats "Button IS NOT activated" on an ongoing basis until stopping and needing to be rebooted. Also suspect my SETUP vs. LOOP aren't right, but not sure how to keep the button working repeatedly otherwise.

When the button is pushed, the serial says all the right things, but no HTTP request gets registered by Integromat Webhooks




Code: Select all#include<ESP8266WiFi.h>

const char*ssid = "MYWIFI";
const char*password = "WIFIPW";
const char*host = "https://hook.integromat.com";
const int buttonPin = 16;     // the number of the pushbutton pin
const int ledPin = 5;      // the number of the LED pin
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {

  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
 
  //WestartbyconnectingtoaWiFinetwork
    Serial.begin(115200);
  delay(100);
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(300);
    Serial.print(".");

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IPaddress:");
  Serial.println(WiFi.localIP());
    //UseWiFiClientclasstocreateTCPconnections
    WiFiClient client;
    const int httpPort = 3480;
    if (!client.connect(host, httpPort)) {
      Serial.println("connection failed");
      return;
    }
 
  }
}

void loop() {

    //readthestateofthepushbuttonvalue:
  buttonState=digitalRead(buttonPin);

  //checkifbuttonispressed
  //ifnotthenstatusisHIGH

  if (buttonState == HIGH) {
    // turn LED on:
    digitalWrite(ledPin, HIGH);
    Serial.println("Button IS activated ");
 delay(100);
Serial.print("connecting to ");
    Serial.println(host);
    //WenowcreateaURIfortherequest
    String url="https://hook.integromat.com/slj2yh1UNIQUEINTEGROMATWEBHOOKSURL6snj";
    Serial.print("RequestingURL:");
    Serial.println(url);
        WiFiClient client;

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

    Serial.println("closing connection");
   
  } else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
Serial.println("Button IS NOT activated ");
      delay(300);
  }
}