0

Could anyone help me with modifying this code.
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Could anyone help me with modifying this code.

by TheStarGazer on Wed Jul 24, 2019 8:50 pm

I'm trying to create a IoT project for my school project
I need to control three independent leds using a NodeMCU.

Im not good at coding, and could only find codes for controlling 1 led.

could someone modify the code and pot it
Code: Select all | TOGGLE FULL SIZE
#include <ESP8266WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"

#define WIFI_SSID "Your WiFi SSID"
#define WIFI_PASS "Your WiFi Password"

#define MQTT_SERV "io.adafruit.com"
#define MQTT_PORT 1883
#define MQTT_NAME "Your Username"
#define MQTT_PASS "Your AIO Key"

int led = D7;

WiFiClient client;
Adafruit_MQTT_Client mqtt(&client, MQTT_SERV, MQTT_PORT, MQTT_NAME, MQTT_PASS);

Adafruit_MQTT_Subscribe onoff = Adafruit_MQTT_Subscribe(&mqtt, MQTT_NAME "/f/onoff");
Adafruit_MQTT_Publish LightsStatus = Adafruit_MQTT_Publish(&mqtt, MQTT_NAME "/f/LightsStatus");


void setup()
{
  Serial.begin(9600);
  pinMode(LED_BUILTIN, OUTPUT);


  //Connect to WiFi
  Serial.print("\n\nConnecting Wifi>");
  WiFi.begin(WIFI_SSID, WIFI_PASS);
  digitalWrite(LED_BUILTIN, LOW);

  while (WiFi.status() != WL_CONNECTED)
  {
    Serial.print(">");
    delay(50);
  }

  Serial.println("OK!");

  //Subscribe to the onoff topic
  mqtt.subscribe(&onoff);

  pinMode(led, OUTPUT);
  digitalWrite(LED_BUILTIN, HIGH);
  digitalWrite(led, LOW);

}

void loop()
{
  //Connect/Reconnect to MQTT
  MQTT_connect();

  //Read from our subscription queue until we run out, or
  //wait up to 5 seconds for subscription to update
  Adafruit_MQTT_Subscribe * subscription;
  while ((subscription = mqtt.readSubscription(5000)))
  {
    //If we're in here, a subscription updated...
    if (subscription == &onoff)
    {
      //Print the new value to the serial monitor
      Serial.print("onoff: ");
      Serial.println((char*) onoff.lastread);

      //If the new value is  "ON", turn the light on.
      //Otherwise, turn it off.
      if (!strcmp((char*) onoff.lastread, "ON"))
      {
        //active low logic
        digitalWrite(led, HIGH);
        LightsStatus.publish("ON");
      }
      else if (!strcmp((char*) onoff.lastread, "OFF"))
      {
        digitalWrite(led, LOW);
        LightsStatus.publish("OFF");

      }
      else
      {
        LightsStatus.publish("ERROR");
      }
    }
    else
    {
      //LightsStatus.publish("ERROR");
    }
  }
  //  if (!mqtt.ping())
  //  {
  //    mqtt.disconnect();
  //  }
}


void MQTT_connect()
{

  //  // Stop if already connected
  if (mqtt.connected() && mqtt.ping())
  {
    //    mqtt.disconnect();
    return;
  }

  int8_t ret;

  mqtt.disconnect();

  Serial.print("Connecting to MQTT... ");
  uint8_t retries = 3;
  while ((ret = mqtt.connect()) != 0) // connect will return 0 for connected
  {
    Serial.println(mqtt.connectErrorString(ret));
    Serial.println("Retrying MQTT connection in 5 seconds...");
    mqtt.disconnect();
    delay(5000);  // wait 5 seconds
    retries--;
    if (retries == 0)
    {
      ESP.reset();
    }
  }
  Serial.println("MQTT Connected!");
}




Give the names you want for feeds, leds, etc, I'll change them if required

Thank you

TheStarGazer
 
Posts: 1
Joined: Wed Jul 24, 2019 8:44 pm

Re: Could anyone help me with modifying this code.

by brubell on Fri Jul 26, 2019 9:37 am

The code you posted could be adapted for three feeds/lights.

These two lines subscribe to an onoff feed and a lightsstatus feed. In Adafruit IO Web, create three onoff feeds named onoff1, onoff2, onoff3. Then, link them into your code by adding two extra lines:

Code: Select all | TOGGLE FULL SIZE
Adafruit_MQTT_Subscribe onoff = Adafruit_MQTT_Subscribe(&mqtt, MQTT_NAME "/f/onoff1");
Adafruit_MQTT_Subscribe onoff = Adafruit_MQTT_Subscribe(&mqtt, MQTT_NAME "/f/onoff2");
Adafruit_MQTT_Subscribe onoff = Adafruit_MQTT_Subscribe(&mqtt, MQTT_NAME "/f/onoff3");
Adafruit_MQTT_Publish LightsStatus = Adafruit_MQTT_Publish(&mqtt, MQTT_NAME "/f/LightsStatus");


Don't forget to subscribe to them in the setup() loop:
Code: Select all | TOGGLE FULL SIZE
mqtt.subscribe(&onoff1);
mqtt.subscribe(&onoff2);
mqtt.subscribe(&onoff3);


Each time the loop is run, it'll update the subscriptions. Be sure to check the subscription topics here for updates, and add code for what occurs if the topic has been updated.
Code: Select all | TOGGLE FULL SIZE
 //If we're in here, a subscription updated...
    if (subscription == &onoff1){
   //do something }
   else if (subscription == &onoff2){
   // do something else }
  ....

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

Please be positive and constructive with your questions and comments.