Black Lives Matter - Action and Equality. ... Adafruit is open and shipping.
0

Using a pushbutton to toggle a dashboard switch?
Moderators: adafruit_support_bill, adafruit

Forum rules
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.
Please be positive and constructive with your questions and comments.

Using a pushbutton to toggle a dashboard switch?

by BeauJayce on Mon Jul 13, 2020 5:28 am

I'm building a IO-controlled xmas tree light switch, and have run into a problem. My criteria are:

1) Toggle switch on dashboard (powerswitch feed) to tell ESP to turn a relay on or off <done>
2) Light on the dashboard (powerlight feed); value set from the ESP after the relay is switched, to show message success <done>
3) Physical momentary button to manually toggle the IO powerswitch <record scratch>

The issue I'm trying to solve with #3 is this; I turn off the lights with the IO toggle. Wife gets up in the morning and wants to turn them back on, which would be a lot easier with a physical button. So far I've gotten halfway there. :)

I'm using powerlight as a value, and want to set the powerswitch to the opposite of powerlight (toggle it on or off based on the current state). I've tried using powerswitch as a the source state, but was getting some double-replies from IO. Also tried reading the RELAY_PIN value and using that as the source. Any value would be fine, whatever I can use to say "set powerswitch to not-that."

So, if the dashboard powerlight is On, pressing the button WILL turn off powerswitch.
Serial shows:
23:26:24.600 -> Button pressed
23:26:24.847 -> IO switch is OFF

If the dashboard powerlight is Off, pressing the button will NOT turn on powerswitch.
Serial shows:
23:26:00.903 -> Button pressed
23:26:01.081 -> IO switch is OFF

CODE: SELECT ALL | TOGGLE FULL SIZE
#include "config.h"
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#define LED_PIN 2
#define RELAY_PIN 4
#define BUTTON_PIN 14

const char* ssid = "SSID";
const char* password = "PASS";
AdafruitIO_Feed *powerswitch = io.feed("powerswitch");
AdafruitIO_Feed *powerlight = io.feed("powerlight");

int button;

void setup() {
pinMode(LED_PIN, OUTPUT);
pinMode(RELAY_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT);
digitalWrite(LED_PIN, HIGH);
digitalWrite(RELAY_PIN, LOW);

Serial.begin(115200);

WiFi.begin(ssid, password);
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
delay(3000);
ESP.restart();
}

ArduinoOTA.setHostname("TreeESP");
ArduinoOTA.begin();

Serial.print("Connecting to Adafruit IO");
io.connect();

powerswitch->onMessage(handleMessage);

while(io.status() < AIO_CONNECTED) {
Serial.print(".");
delay(1000);
}
Serial.println();
Serial.println(io.statusText());
powerswitch->get();
}

void loop() {
io.run();
ArduinoOTA.handle();

button = digitalRead(BUTTON_PIN);

if (button == LOW) {
Serial.println("Button pressed");
powerlight->get();
powerswitch->save(!powerlight);
}

}

void handleMessage(AdafruitIO_Data *data) {

if(data->toPinLevel() == HIGH) {
Serial.println("IO switch is ON");
digitalWrite(RELAY_PIN, HIGH);
}
else {
Serial.println("IO switch is OFF");
digitalWrite(RELAY_PIN, LOW);
}
digitalWrite(LED_PIN, !data->toPinLevel());
powerlight->save(data->toPinLevel());
}

BeauJayce
 
Posts: 2
Joined: Mon Jul 13, 2020 5:28 am

Re: Using a pushbutton to toggle a dashboard switch?

by BeauJayce on Tue Jul 14, 2020 2:59 am

BeauJayce wrote:I'm building a IO-controlled xmas tree light switch, and have run into a problem. My criteria are:

1) Toggle switch on dashboard (powerswitch feed) to tell ESP to turn a relay on or off <done>
2) Light on the dashboard (powerlight feed); value set from the ESP after the relay is switched, to show message success <done>
3) Physical momentary button to manually toggle the IO powerswitch <record scratch>

The issue I'm trying to solve with #3 is this; I turn off the lights with the IO toggle. Wife gets up in the morning and wants to turn them back on, which would be a lot easier with a physical button. So far I've gotten halfway there. :)

I'm using powerlight as a value, and want to set the powerswitch to the opposite of powerlight (toggle it on or off based on the current state). I've tried using powerswitch as a the source state, but was getting some double-replies from IO. Also tried reading the RELAY_PIN value and using that as the source. Any value would be fine, whatever I can use to say "set powerswitch to not-that."

So, if the dashboard powerlight is On, pressing the button WILL turn off powerswitch.
Serial shows:
23:26:24.600 -> Button pressed
23:26:24.847 -> IO switch is OFF

If the dashboard powerlight is Off, pressing the button will NOT turn on powerswitch.
Serial shows:
23:26:00.903 -> Button pressed
23:26:01.081 -> IO switch is OFF

CODE: SELECT ALL | TOGGLE FULL SIZE
#include "config.h"
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#define LED_PIN 2
#define RELAY_PIN 4
#define BUTTON_PIN 14

const char* ssid = "SSID";
const char* password = "PASS";
AdafruitIO_Feed *powerswitch = io.feed("powerswitch");
AdafruitIO_Feed *powerlight = io.feed("powerlight");

int button;

void setup() {
pinMode(LED_PIN, OUTPUT);
pinMode(RELAY_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT);
digitalWrite(LED_PIN, HIGH);
digitalWrite(RELAY_PIN, LOW);

Serial.begin(115200);

WiFi.begin(ssid, password);
while (WiFi.waitForConnectResult() != WL_CONNECTED) {
delay(3000);
ESP.restart();
}

ArduinoOTA.setHostname("TreeESP");
ArduinoOTA.begin();

Serial.print("Connecting to Adafruit IO");
io.connect();

powerswitch->onMessage(handleMessage);

while(io.status() < AIO_CONNECTED) {
Serial.print(".");
delay(1000);
}
Serial.println();
Serial.println(io.statusText());
powerswitch->get();
}

void loop() {
io.run();
ArduinoOTA.handle();

button = digitalRead(BUTTON_PIN);

if (button == LOW) {
Serial.println("Button pressed");
powerlight->get();
powerswitch->save(!powerlight);
}

}

void handleMessage(AdafruitIO_Data *data) {

if(data->toPinLevel() == HIGH) {
Serial.println("IO switch is ON");
digitalWrite(RELAY_PIN, HIGH);
}
else {
Serial.println("IO switch is OFF");
digitalWrite(RELAY_PIN, LOW);192.168.0.1 192.168.l.l

}
digitalWrite(LED_PIN, !data->toPinLevel());
powerlight->save(data->toPinLevel());
}

Before the nether update, I used to be able to sleep underwater. I have a completely underwater base that I live in thanks to the conduit, but now I can't spawn in it if I die. Is this intentional? Attempting to activate a bed tells me that it is obstructed and it only works when the bed is only one level deep in water.

BeauJayce
 
Posts: 2
Joined: Mon Jul 13, 2020 5:28 am

Please be positive and constructive with your questions and comments.