Bouton poussoir et requête HTTP

Bonjour tout le monde
Je débute également avec l'ESP et souhaiterais pouvoir utiliser un ESP pour envoyer une requète HTTP a un serveur lorsque j'appuie sur un bouton poussoir.
J'ai donc utiliser la librairie simplebouton.h mais je n'arrive pas a la faire fonctionner.
Lorsque je met en contact le PIN 3 (D3) et le GND il ne se passe rien.
Voici mon code:


#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#include "simpleBouton.h"

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

const uint8_t pin_bouton = 3;//cablage pin---BP---GND

//Your Domain name with URL path or IP address with path
String serverName = "http://192.168.0.36/poussoir_bureau.php";

// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastTime = 0;
// Timer set to 10 minutes (600000)
//unsigned long timerDelay = 600000;
// Set timer to 5 seconds (5000)
unsigned long timerDelay = 5000;


boutonAction bouton(pin_bouton);

void simpleClic() {
	
Serial.println("simple clic");
  if(WiFi.status()== WL_CONNECTED){
      WiFiClient client;
      HTTPClient http;

      String serverPath = serverName + "?bouton=simple";
      
      // Your Domain name with URL path or IP address with path
      http.begin(client, serverPath.c_str());
      
      // Send HTTP GET request
      int httpResponseCode = http.GET();
      
      if (httpResponseCode>0) {
        Serial.print("HTTP Response code: ");
        Serial.println(httpResponseCode);
        String payload = http.getString();
        Serial.println(payload);
      }
      else {
        Serial.print("Error code: ");
        Serial.println(httpResponseCode);
      }
      // Free resources
      http.end();
    }
    else {
      Serial.println("WiFi Disconnected");
    }



}


void doubleClic() {
	
Serial.println("double clic");
    if(WiFi.status()== WL_CONNECTED){
      WiFiClient client;
      HTTPClient http;

      String serverPath = serverName + "?bouton=double";
      
      // Your Domain name with URL path or IP address with path
      http.begin(client, serverPath.c_str());
      
      // Send HTTP GET request
      int httpResponseCode = http.GET();
      
      if (httpResponseCode>0) {
        Serial.print("HTTP Response code: ");
        Serial.println(httpResponseCode);
        String payload = http.getString();
        Serial.println(payload);
      }
      else {
        Serial.print("Error code: ");
        Serial.println(httpResponseCode);
      }
      // Free resources
      http.end();
    }
    else {
      Serial.println("WiFi Disconnected");
    }

}



void setup() {
  Serial.begin(115200); 

  WiFi.begin(ssid, password);
  Serial.println("Connecting");
  while(WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to WiFi network with IP Address: ");
  Serial.println(WiFi.localIP());
 
  Serial.println("Timer set to 5 seconds (timerDelay variable), it will take 5 seconds before publishing the first reading.");

  bouton.attacher(simpleClic, doubleClic);

}



void loop() {
  bouton.actualiser();
}

Est-ce qu'une ame charitable sait me dire d'ou vient mon problème ?

j'ai déplacé votre post car c'est une question à part entière

est-ce que ce code fonctionne?

#include "simpleBouton.h"
const uint8_t pin_bouton = 3; //cablage pin---BP---GND
boutonAction bouton(pin_bouton);

void simpleClic() {
  Serial.println("simple clic");
}

void doubleClic() {
  Serial.println("double clic");
}

void setup() {
  Serial.begin(115200);
  bouton.attacher(simpleClic, doubleClic);
}

void loop() {
  bouton.actualiser();
}

➜ voyez vous les messages simple click et double click?

si ça ne fonctionne pas ➜ quel est exactement votre arduino? peut être il faut utiliser D3 au lieu de 3 pour le N° de pin

#include "simpleBouton.h"
const uint8_t pin_bouton = D3; //cablage pin---BP---GND
boutonAction bouton(pin_bouton);

void simpleClic() {
  Serial.println("simple clic");
}

void doubleClic() {
  Serial.println("double clic");
}

void setup() {
  Serial.begin(115200);
  bouton.attacher(simpleClic, doubleClic);
}

void loop() {
  bouton.actualiser();
}

Probablement. C'est une erreur courante.
Sur un ESP8266 :
GPIO3 = RX
GPIO0 = D3

Bonsoir !

En effet en cherchant un peu j'ai effectivement trouve que le PIN 4 n'est pas le D4 mais le PIN 2.
En corrigeant le PIN tout à fonctionné !
Par contre du coup, comment connait-on les correspondances N°PIN et D correspondant ?

Merci en tout cas pour cette idée qui a permis de faire fonctionner mon projet :wink:

utilisez le nom des pins tels qu'ils sont écrits sur la carte. S'il y a marqué D2 alors mettez D2 dans le code (sinon faut lire la doc technique ou la description de la carte choisie dans l'IDE)

Ok, merci pour l'info
Je ne savais pas que l'on pouvait faire ca :wink:

En suivant le lien fourni en #3.

ou en allant voir dans la variante du fichier de configuration pour la carte choisie

par exemple pour un NodeMCU 8266

il y a d'autres pins aussi définies dans la partie générique des variantes par exemple pour SS, MOSI, MISO et SCK

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