Voting resources, early voting, and poll worker information - VOTE. ... Adafruit is open and shipping.
0

Long Distance Lamp On Off Feed
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.

Long Distance Lamp On Off Feed

by rmorrill on Sun Dec 15, 2019 3:23 pm

have two DIY long-distance lamps powered and lit up with a Wemos mini D1 that I'd like to connect using Adafruit. Basically I want the two lamps to be subscribed to the topic, and if one turns on or off so will the other one. One of the easiest methods I could think of to accomplish this was to send a data value. This might not actually be the simplest, I'm extremely new to MQTT brokers and coding with Arduino. In my mind, the lamps would check for the data value and also send it. So basically while off the feed is at some predetermined value, let's say 0, both lamps are off. As soon as one of the lamps is pushed on, the data value is changed to 1, meaning on. The other lamp needs to see this change and also turn on. Then if either one of the lamps is pushed off again, it sends the 0 value and both turn off. I've not fully coded this, mostly cause I'm stuck and not exactly sure how to do this with my code. The code I have thus far is as follows
Code: Select all | TOGGLE FULL SIZE
#include <bitswap.h>
#include <chipsets.h>
#include <color.h>
#include <colorpalettes.h>
#include <colorutils.h>
#include <controller.h>
#include <cpp_compat.h>
#include <dmx.h>
#include <FastLED.h>
#include <fastled_config.h>
#include <fastled_delay.h>
#include <fastled_progmem.h>
#include <fastpin.h>
#include <fastspi.h>
#include <fastspi_bitbang.h>
#include <fastspi_dma.h>
#include <fastspi_nop.h>
#include <fastspi_ref.h>
#include <fastspi_types.h>
#include <hsv2rgb.h>
#include <led_sysdefs.h>
#include <lib8tion.h>
#include <noise.h>
#include <pixelset.h>
#include <pixeltypes.h>
#include <platforms.h>
#include <power_mgt.h>
#include <OneButton.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include "AdafruitIO_WiFi.h"
#define IO_SERVER      "io.adafruit.com"
#define IO_SERVERPORT  1883
#define IO_USERNAME  "myusername"
#define IO_KEY       "KEY is here"
#define LED_PIN     15
#define NUM_LEDS    48
#define BUTTON_PIN 1
char ssid[] = "WIFISSID"; //SSID of your Wi-Fi router
char pass[] = ""; //Password of your Wi-Fi router

// Connect to Wi-Fi and Adafruit IO handel

AdafruitIO_WiFi io(IO_USERNAME, IO_KEY, ssid, pass);
 
// Create a feed object that allows us to send data to
AdafruitIO_Feed *lamponoff = io.feed("lamponoff");

bool showLights = true;
CRGB leds[NUM_LEDS];
void doLights() {
   fill_solid( leds,12, CRGB(255,140,0));
   fill_solid( leds+12,12, CRGB(255,80,0));
   fill_solid( leds+24,4, CRGB(255,80,0));
   fill_solid( leds+28,4, CRGB(255,69,80));
   fill_solid( leds+32,4, CRGB(255,80,0));
   fill_solid( leds+36,4, CRGB(255,80,0));
   fill_solid( leds+40,4, CRGB(255,69,80));
   fill_solid( leds+44,4, CRGB(255,80,0));
  FastLED.show();
}

void noLights() {
  fill_solid( leds,NUM_LEDS , CRGB(0,0,0));
    FastLED.show();
  }

void buttonClick() {
  showLights = !showLights;
}

OneButton button(BUTTON_PIN, true);

void setup() {
  FastLED.addLeds<WS2812B, LED_PIN, GRB>(leds, NUM_LEDS);
  FastLED.setBrightness(200);
  button.attachClick(buttonClick);
  Serial.begin(115200);
  delay(10);
  // Connect to Wi-Fi network
  Serial.println();
  Serial.println();
  Serial.print("Connecting to...");
  Serial.println(ssid);

  WiFi.begin(ssid, pass);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("Wi-Fi connected successfully");
 
  // Connect to Adafruit IO
  io.connect();
 
  // wait for a connection
  while(io.status() < AIO_CONNECTED)
  {
    Serial.print(".");
    delay(500);
  }
 
  }
 
 

void loop() {
  button.tick();
  if (showLights) {
    doLights();
   
     // Always keep this at the top of your main loop
  // While not confirmed, this implies that the Adafruit IO library is not event-driven
  // This means you should refrain from using infinite loops
  io.run();
 
  // Send 10 to our Workshop Temperature Feed
  lamponoff->save(1);
 
  // This is so we can see some response on the computer
  Serial.print("Data sent");
 
  // DONT SEND A BILLION DATA POINTS! This slows down the program so that it sends readings once every 5 seconds
  delay(5000);
   
  } else {
    noLights();
  }  }

As I'm sure anyone would notice I did try and send the value of 1 during the loop for the lights being one. The code was taken from this guide https://www.digikey.com/en/maker/blogs/ ... rduino-ide. However, the problem with this is it continually sends a data value every 5 seconds. I believe this interferes with my button input as to when I press it now, it doesn't always turn off the lights, actually, it almost never turns off the lights while this code runs. I think if the value is only sent once and is not is a loop the button will work again. I'm not completely sure though. Thank you to anyone who may be able to help

rmorrill
 
Posts: 1
Joined: Mon Dec 09, 2019 2:33 pm

Re: Long Distance Lamp On Off Feed

by brubell on Wed Dec 18, 2019 5:09 pm

For a better code reference, you may want to look at our guide on sending Digital Inputs (like your button) to Adafruit IO: https://learn.adafruit.com/adafruit-io- ... ital-input

For your lamp, you may want to reference our Adafruit IO Basics: Digital Output guide: https://learn.adafruit.com/adafruit-io- ... tal-output

Both of these guides have more robust example code, designed to handle incoming messages.

brubell
 
Posts: 1043
Joined: Fri Jul 17, 2015 10:33 pm

Please be positive and constructive with your questions and comments.