Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include <PubSubClient.h>
- const char *ssid = "_SSID_"; // cannot be longer than 32 characters!
- const char *pass = "_Wifi_password_"; //
- const char *mqtt_server = "_IP_Of_MQTT_Server";
- const int mqtt_port = 1883;
- const char* connection_id = "wemo_button";
- const char* client_name = "_USERNAME_";
- const char* client_password = "_PASSWORD_";
- const char* topic = "wemobutton/state";
- char somebigthing[100];
- const int buttonPin = D3;
- const int ledPin = BUILTIN_LED;
- WiFiClient espClient;
- PubSubClient client(espClient);
- // the current state of the LED and button
- int ledState = LOW;
- int buttonState = LOW;
- // the current and previous readings from the input pin
- int thisButtonState = LOW;
- int lastButtonState = LOW;
- // time is measured in milliseconds and will quickly exceed limitations of an integer, so we use a long for these two
- unsigned long lastDebounceTime = 0; // the time the button state last switched
- unsigned long debounceDelay = 50; // the state must remain the same for this many millis to register the button press
- void setup() {
- Serial.begin(9600);
- pinMode(buttonPin, INPUT);
- client.setServer(mqtt_server, mqtt_port); // MQTT!!!
- }
- void loop() {
- Serial.println("Hello world1");
- WiFi.begin(ssid, pass);
- client.connect(connection_id, client_name, client_password);
- Serial.println("Hello world2");
- // the buttonPin is read multiple times and the value must remain the same for debounceDelay millis to toggle the LED
- // read button state, HIGH when pressed, LOW when not
- thisButtonState = digitalRead(buttonPin);
- // if the current state does not match the previous state
- // the button was just pressed/released, or is transition noise
- if (thisButtonState != lastButtonState) {
- // reset the timer
- lastDebounceTime = millis();
- }
- // once delay millis have elapsed, if the state remains the same, register the press
- if ((millis() - lastDebounceTime) > debounceDelay) {
- // if the button state has changed
- if (thisButtonState != buttonState) {
- buttonState = thisButtonState;
- // only toggle the LED if the buttonState has switched from LOW to HIGH
- if (buttonState == HIGH) {
- ledState = !ledState;
- // toggle the LED
- if (ledState == HIGH) {
- SendOn();
- }
- else {
- SendOff();
- }
- }
- }
- }
- // persist for next loop iteration
- lastButtonState = thisButtonState;
- }
- void SendOn(){
- strcpy(somebigthing, "on");
- client.publish(topic, somebigthing);
- }
- void SendOff(){
- strcpy(somebigthing, "off");
- client.publish(topic, somebigthing);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement