Wen-Liang Lin
Published © GPL3+

Outdoor plants supplement light

Monitor the amount of sunlight received by outdoor plants and use red or blue light to supplement it when it is insufficient.

BeginnerWork in progress2 hours54
Outdoor plants supplement light

Things used in this project

Hardware components

AVR IoT Mini Cellular Board
Microchip AVR IoT Mini Cellular Board
×1
Seeed Studio Grove - Red LED
×1
Seeed Studio Grove - LED Socket Kit
×1

Software apps and online services

Arduino IDE
Arduino IDE
Adafruit IO

Story

Read more

Code

plants_light_supplement_upload.ino

Arduino
// modify from Adafruit MQTT Library WINC1500 Example and AVR-IoT-Cellular Library Example mqtt_coustom_broker
// please read license in Examples
/***************************************************
  Adafruit MQTT Library WINC1500 Example

  Adafruit invests time and resources providing this open source code,
  please support Adafruit and open-source hardware by purchasing
  products from Adafruit!

  Written by Limor Fried/Ladyada for Adafruit Industries.
  MIT license, all text above must be included in any redistribution
 ****************************************************/

/* 
use SPI ver 1.1.2
use AVR-IoT-Cellular ver 1.3.9
use AVR-IoT_MCP9808 ver 1.1.5
use AVR-IoT_VEML3328 ver 1.1.2
use Wire ver 2.0.10
 */
 
#include <SPI.h>
#include <ecc608.h>
#include <mqtt_client.h>

#include <Arduino.h>
#include <led_ctrl.h>
#include <log.h>
#include <low_power.h>
#include <lte.h>
#include <mcp9808.h>
#include <veml3328.h>

#define SerialCDC Serial3
/************************* Adafruit.io Setup *********************************/
#define AIO_SERVER "io.adafruit.com"
#define AIO_SERVERPORT 1883
#define MQTT_THING_NAME    "Plant"
#define MQTT_BROKER        "io.adafruit.com" //AIO_SERVER
#define MQTT_PORT          1883 //AIO_SERVERPORT
#define MQTT_USE_TLS       false
#define MQTT_KEEPALIVE     600
#define MQTT_USE_ECC       false
#define MOSQUITTO_USERNAME "" //AIO_USERNAME
#define MOSQUITTO_PASSWORD "" //AIO_KEY

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

char sendbuffer[128];

float celsius;
int Red_sensorvalue = 0;
int Green_sensorvalue = 0;
int Blue_sensorvalue = 0;

const int LED_R_PIN = PIN_PD1;
const int LED_B_PIN = PIN_PD3;

void setup()
{
  LedCtrl.begin();
  LedCtrl.startupCycle();
  Log.begin(115200);
  Log.setLogLevel(LogLevel::INFO);

  pinConfigure(LED_R_PIN, PIN_DIR_OUTPUT);
  pinConfigure(LED_B_PIN, PIN_DIR_OUTPUT);

  pinConfigure(PIN_PD2, PIN_DIR_INPUT | PIN_PULLUP_ON);

  if (Mcp9808.begin()) {
    Log.error(F("Could not start MCP9808."));
    return false;
  }
  celsius = Mcp9808.readTempC(); // read tempC
  SerialCDC.printf("TempC: %f\r\n", celsius);

  if (Veml3328.begin())
  {
    Log.error(F("Error: could not start VEML3328 library"));
  }

  SerialCDC.printf("Device ID: %x\r\n", Veml3328.deviceID());
  Red_sensorvalue = Veml3328.getRed(); // red sensor peak 610 nm
  Green_sensorvalue = Veml3328.getGreen(); // green sensor peak 560 nm
  Blue_sensorvalue = Veml3328.getBlue(); // blue sensor peak 470 nm
  SerialCDC.printf("R,G,B: %d,%d,%d\r\n", Red_sensorvalue, Green_sensorvalue, Blue_sensorvalue);
}

uint32_t x = 0;

void loop()
{
  Red_sensorvalue = Veml3328.getRed(); // red sensor peak 610 nm
  Green_sensorvalue = Veml3328.getGreen(); // green sensor peak 560 nm
  Blue_sensorvalue = Veml3328.getBlue(); // blue sensor peak 470 nm
  SerialCDC.printf("R,G,B: %d,%d,%d\r\n", Red_sensorvalue, Green_sensorvalue, Blue_sensorvalue);

  celsius = Mcp9808.readTempC(); // read tempC
  SerialCDC.printf("TempC: %f\r\n", celsius);
  MQTT_main();
  //delay(60000);
  delay(10000);
}

/****************************** Feeds ***************************************/
#define MQTT_SUB_TOPIC_red_led "momososo/feeds/farm.red-led"
#define MQTT_SUB_TOPIC_blue_led "momososo/feeds/farm.blue-led"
void MQTT_main() {
  // Ensure the connection to the MQTT server is alive (this will make the first
  // connection and automatically reconnect when disconnected).  See the MQTT_connect
  // function definition further below.
  MQTT_connect();

  // Now we can publish stuff!
  MQTT_pub();
  MQTT_subLED();
}

// Function to connect and reconnect as necessary to the MQTT server.
// Should be called in the loop function and it will take care if connecting.
void MQTT_connect() {
  int8_t ret;

  // Check first if we are connected to the operator. We might get
  // disconnected, so for our application to continue to run, we double check
  // here
  if (!Lte.isConnected()) {

    // Attempt to connect to the operator
    if (!Lte.begin()) {
      return;
    } else {
      Log.infof(F("Connected to operator %s\r\n"),
                Lte.getOperator().c_str());
    }
  }

  if (Lte.isConnected())
  {
    // Stop if already connected.
    if (!MqttClient.isConnected())
    {
      if (!MqttClient.begin(MQTT_THING_NAME, MQTT_BROKER, MQTT_PORT, MQTT_USE_TLS, MQTT_KEEPALIVE, MQTT_USE_ECC, MOSQUITTO_USERNAME, MOSQUITTO_PASSWORD))
      {
        Log.error(F("Failed to connect to MQTT."));
      }
      else
      {
        // setting subscribes
        MqttClient.subscribe(MQTT_SUB_TOPIC_red_led);
        MqttClient.subscribe(MQTT_SUB_TOPIC_blue_led);
      }
    }
  }

}

int R_ledState = LOW;
int B_ledState = LOW;

// read subscribes and setting LED state
void MQTT_subLED() {
  String message_red = MqttClient.readMessage(MQTT_SUB_TOPIC_red_led);
  String message_blue = MqttClient.readMessage(MQTT_SUB_TOPIC_blue_led);
  String compareuse;
  // Read message will return an empty string if there were no new
  // messages, so anything other than that means that there was a new
  // message
  if (message_red != "") {
    Log.infof(F("Red: %s\r\n"), message_red.c_str());
    compareuse = message_red.c_str();
    SerialCDC.println(compareuse);
    if (compareuse == "OFF")
    {
      digitalWrite(LED_R_PIN, 0);
    }
    else if (compareuse == "ON")
    {
      digitalWrite(LED_R_PIN, 1);
    }
  }
  if (message_blue != "") {
    Log.infof(F("Blue: %s\r\n"), message_blue.c_str());
    compareuse = message_blue.c_str();
    SerialCDC.println(compareuse);
    if (compareuse == "OFF")
    {
      digitalWrite(LED_B_PIN, 0);
    }
    else if (compareuse == "ON")
    {
      digitalWrite(LED_B_PIN, 1);
    }
  }
}

/****************************** Feeds ***************************************/
#define MQTT_PUB_TOPIC_red_light "momososo/feeds/farm.red-light"
#define MQTT_PUB_TOPIC_green_light "momososo/feeds/farm.green-light"
#define MQTT_PUB_TOPIC_blue_light "momososo/feeds/farm.blue-light"
#define MQTT_PUB_TOPIC_blue_temp "momososo/feeds/farm.temperature"

// publish each sensor value and print out success or fail
void MQTT_pub() {

  bool publishedSuccessfully;
  sprintf(sendbuffer, "%d", Red_sensorvalue );
  publishedSuccessfully = MqttClient.publish(MQTT_PUB_TOPIC_red_light, sendbuffer);
  if (publishedSuccessfully) {
    Log.infof(F("Published red_light Success\r\n"));
  } else {
    Log.error(F("Failed to publish red_light"));
  }
  sprintf(sendbuffer, "%d", Green_sensorvalue );
  publishedSuccessfully = MqttClient.publish(MQTT_PUB_TOPIC_green_light, sendbuffer);
  if (publishedSuccessfully) {
    Log.infof(F("Published green_light Success\r\n"));
  } else {
    Log.error(F("Failed to publish green_light"));
  }
  sprintf(sendbuffer, "%d", Blue_sensorvalue );
  publishedSuccessfully = MqttClient.publish(MQTT_PUB_TOPIC_blue_light, sendbuffer);
  if (publishedSuccessfully) {
    Log.infof(F("Published blue_light Success\r\n"));
  } else {
    Log.error(F("Failed to publish blue_light"));
  }
  sprintf(sendbuffer, "%f", celsius );
  publishedSuccessfully = MqttClient.publish(MQTT_PUB_TOPIC_blue_temp, sendbuffer);
  if (publishedSuccessfully) {
    Log.infof(F("Published temp Success\r\n"));
  } else {
    Log.error(F("Failed to publish temp"));
  }
}

Credits

Wen-Liang Lin

Wen-Liang Lin

28 projects • 34 followers
Hi, I am momososo

Comments