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

RE-PURPOSING OF LAMP SKETCH
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.

RE-PURPOSING OF LAMP SKETCH

by 94566rik on Wed Sep 23, 2015 11:14 am

I changed the Lamp Sketch "ESP8266_lamp_module" from the example given in the instructions.

I have switched out all the "LAMP" references and replaced with "RELAY"

The sketch will compile just fine.

I created a RELAY feed just like the LAMP feed and toggle button to be used on the dashboard.

When I load the Lamp Module using the FTDI Friend it loads fine and works great.

When I load the RELAY changed "ESP8266_relay_module" the top LED light on the "GREEN" side next to the USB connector flashes continuously and does not stop flashing like the "ESP8266_lamp_module" upload does.

So the ESP8266_relay_module does not sync with the Relay Toggle Button and will not turn the lamp on or off.

I compared each line of code in the sketches and cannot see any difference, so do not understand why the "RELAY TOGGLE BUTTON" will not turn my lamp on and off in my adjusted sketch?

Here is the sketch as used for the change to "RELAY" instead of "LAMP"

Code: Select all | TOGGLE FULL SIZE
/***************************************************
  Change to RELAY instead of Lamp
  Adafruit ESP8266 Lamp Controller Module
 
  Must use ESP8266 Arduino from:
    https://github.com/esp8266/Arduino
  Works great with Adafruit's Huzzah ESP board:
  ----> https://www.adafruit.com/product/2471
  Adafruit invests time and resources providing this open source code,
  please support Adafruit and open-source hardware by purchasing
  products from Adafruit!
  Written by Tony DiCola for Adafruit Industries.
  Adafruit IO example additions by Todd Treece.
  MIT license, all text above must be included in any redistribution
 ****************************************************/

// Libraries
#include <ESP8266WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"

// Relay pin
const int relay_pin = 5;

// WiFi parameters
#define WLAN_SSID       "Attic_2.4G"
#define WLAN_PASS       "WLAN_PASS"

// Adafruit IO
#define AIO_SERVER      "io.adafruit.com"
#define AIO_SERVERPORT  1883
#define AIO_USERNAME    "94566rik"
#define AIO_KEY         "AIO_KEY"  -- I have the KEY matching the Lamp one

// Create an ESP8266 WiFiClient class to connect to the MQTT server.
WiFiClient client;

// Store the MQTT server, client ID, username, and password in flash memory.
// This is required for using the Adafruit MQTT library.
const char MQTT_SERVER[] PROGMEM    = AIO_SERVER;
// Set a unique MQTT client ID using the AIO key + the date and time the sketch
// was compiled (so this should be unique across multiple devices for a user,
// alternatively you can manually set this to a GUID or other random value).
const char MQTT_CLIENTID[] PROGMEM  = AIO_KEY __DATE__ __TIME__;
const char MQTT_USERNAME[] PROGMEM  = AIO_USERNAME;
const char MQTT_PASSWORD[] PROGMEM  = AIO_KEY;

// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
Adafruit_MQTT_Client mqtt(&client, MQTT_SERVER, AIO_SERVERPORT, MQTT_CLIENTID, MQTT_USERNAME, MQTT_PASSWORD);

/****************************** Feeds ***************************************/

// Setup a feed called 'relay' for subscribing to changes.
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
const char RELAY_FEED[] PROGMEM = AIO_USERNAME "/feeds/Relay";
Adafruit_MQTT_Subscribe relay = Adafruit_MQTT_Subscribe(&mqtt, RELAY_FEED);

/*************************** Sketch Code ************************************/

void setup() {

  // Set RELAY pin to output
  pinMode(relay_pin, OUTPUT);

  Serial.begin(115200);

  Serial.println(F("Adafruit IO Example"));

  // Connect to WiFi access point.
  Serial.println(); Serial.println();
  delay(10);
  Serial.print(F("Connecting to "));
  Serial.println(WLAN_SSID);

  WiFi.begin(WLAN_SSID, WLAN_PASS);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(F("."));
  }
  Serial.println();

  Serial.println(F("WiFi connected"));
  Serial.println(F("IP address: "));
  Serial.println(WiFi.localIP());

  // listen for events on the relay feed
  mqtt.subscribe(&relay);

  // connect to adafruit io
  connect();

}

void loop() {

  Adafruit_MQTT_Subscribe *subscription;

  // ping adafruit io a few times to make sure we remain connected
  if(! mqtt.ping(3)) {
    // reconnect to adafruit io
    if(! mqtt.connected())
      connect();
  }

  // this is our 'wait for incoming subscription packets' busy subloop
  while (subscription = mqtt.readSubscription(1000)) {

    // we only care about the relay events
    if (subscription == &relay) {

      // convert mqtt ascii payload to int
      char *value = (char *)relay.lastread;
      Serial.print(F("Received: "));
      Serial.println(value);

      // Apply message to relay
      String message = String(value);
      message.trim();
      if (message == "ON") {digitalWrite(relay_pin, HIGH);}
      if (message == "OFF") {digitalWrite(relay_pin, LOW);}

    }

  }

}

// connect to adafruit io via MQTT
void connect() {

  Serial.print(F("Connecting to Adafruit IO... "));

  int8_t ret;

  while ((ret = mqtt.connect()) != 0) {

    switch (ret) {
      case 1: Serial.println(F("Wrong protocol")); break;
      case 2: Serial.println(F("ID rejected")); break;
      case 3: Serial.println(F("Server unavail")); break;
      case 4: Serial.println(F("Bad user/pass")); break;
      case 5: Serial.println(F("Not authed")); break;
      case 6: Serial.println(F("Failed to subscribe")); break;
      default: Serial.println(F("Connection failed")); break;
    }

    if(ret >= 0)
      mqtt.disconnect();

    Serial.println(F("Retrying connection..."));
    delay(5000);

  }

  Serial.println(F("Adafruit IO Connected!"));

}

94566rik
 
Posts: 15
Joined: Fri Sep 11, 2015 5:40 pm

Re: RE-PURPOSING OF LAMP SKETCH

by Rpvanpatt on Sun Jan 31, 2016 4:14 pm

I'm using a CC3000, but had the same issue. I got some help on reddit, this worked for me...

Code: Select all | TOGGLE FULL SIZE
  while ((subscription = mqtt.readSubscription(5000))) {
    // Check if its the onoff button feed
    if (subscription == &lamp) {
      Serial.print(F("Lamp: "));
      Serial.println((char *)lamp.lastread);
     
      if (strcmp((char *)lamp.lastread, "ON") == 0) {
        digitalWrite(LAMP, LOW);
      }
      if (strcmp((char *)lamp.lastread, "OFF") == 0) {
        digitalWrite(LAMP, HIGH);
      }


Now, the issue I have is when I reopen IO, the switches are all reset to 'off'. I just have to switch them on then off to get it actually matching the state of the relay. However, do you know if there's a way to check the state of the relay and have IO corrected?

Rpvanpatt
 
Posts: 20
Joined: Mon Dec 14, 2015 10:48 am

Please be positive and constructive with your questions and comments.