Relay example

Relay module must be connected to controller module or with sensor module.

Relay has 3 input holes.

Needed libraries:

lib_deps = ITTIoT

The example code below shows if something is connected to the relay module

// Includes global variables and librarys that the relay shield uses
#include <Arduino.h>
#include <ittiot.h>
 
#define MODULE_TOPIC "ESP30/relay"
#define WIFI_NAME "name"
#define WIFI_PASSWORD "password"
 
#define RELAY_PIN 5 // The relay has been connected to pin 5
 
// If message received switch relay
void iot_received(String topic, String msg)
{
  Serial.print("MSG FROM USER callback, topic: ");
  Serial.print(topic);
  Serial.print(" payload: ");
  Serial.println(msg);
 
  if(topic == MODULE_TOPIC)
  {
    // If message received and it is 1, then switch relay on
    if(msg == "1")
    {
      digitalWrite(RELAY_PIN, HIGH);
    }
    // If message received and it is 0, then switch relay on
    if(msg == "0")
    {
      digitalWrite(RELAY_PIN, LOW);
    }
  }
}
 
// Function started after the connection to the server is established.
void iot_connected()
{
  Serial.println("MQTT connected callback");
  iot.subscribe(MODULE_TOPIC); // subscribe to topic relay
  iot.log("IoT relay example!");
}
 
void setup()
{
  Serial.begin(115200); // setting up serial connection parameter
  Serial.println("Booting");
 
  //iot.setConfig("wname", WIFI_NAME);
  //iot.setConfig("wpass", WIFI_PASSWORD);
  iot.printConfig(); // print json config to serial
  iot.setup(); // Initialize IoT library
 
  pinMode(RELAY_PIN, OUTPUT); // The relay pin is defined as output type
}
 
void loop()
{
  iot.handle(); // IoT behind the plan work, it should be periodically called
  delay(200); // Wait for 0.2 seconds
}
en/iot/examples/relay.txt · Last modified: 2021/03/05 01:33 by heiko.pikner
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0