Cdj ESP EasyIOT 4 Relay Mod

9 years 9 months ago - 9 years 6 months ago #262 by cdj
Hello everybody. Here's the code for arduino to find 4 esp8266 node that pilot 4 relay...
software serial pin 2,3
relay pin 8,9,10,11

Rock'n'roll !
/*
 V1.0 - first version
 V1.1 - adopt to new library 
 
 Created by Igor Jarc <igor.jarc1@gmail.com>
 See http://iot-playground.com for details
 
 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 version 2 as published by the Free Software Foundation.
 */
#include <Esp8266EasyIoT.h>
#include <SoftwareSerial.h> 

Esp8266EasyIoT esp; 

SoftwareSerial serialEsp(2, 3);



#define RELAY_1  8  // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
#define RELAY_ON 1  // GPIO value to write to turn on attached relay
#define RELAY_OFF 0 // GPIO value to write to turn off attached relay


void setup()
{
  serialEsp.begin(9600);
  Serial.begin(115200);  

  Serial.println("EasyIoTEsp init");

  esp.begin(incomingMessage, 3, &serialEsp, &Serial);
  //esp.begin(incomingMessage, 3, &serialEsp);

  pinMode(RELAY_1, OUTPUT);
  pinMode(RELAY_1 + 1, OUTPUT);
  pinMode(RELAY_1 + 2, OUTPUT);
  pinMode(RELAY_1 + 3, OUTPUT);
 
  esp.present(1, S_DIGITAL_OUTPUT);
  esp.present(2, S_DIGITAL_OUTPUT);
  esp.present(3, S_DIGITAL_OUTPUT);
  esp.present(4, S_DIGITAL_OUTPUT);
  
}

void loop()
{
  esp.process();
}

void incomingMessage(const Esp8266EasyIoTMsg &message) {
  // We only expect one type of message from controller. But we better check anyway.

  Serial.println("New message");
  if (message.type==V_DIGITAL_VALUE) {
    // Change relay state
    digitalWrite(message.sensor-1+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF);

    Serial.print("Incoming change for sensor:");
    Serial.print(message.sensor);
    Serial.print(", New status: ");
    Serial.println(message.getBool());
  } 
}
The following user(s) said Thank You: EasyIoT, eggster

Please Log in or Create an account to join the conversation.

9 years 9 months ago #267 by EasyIoT
Replied by EasyIoT on topic Cdj ESP EasyIOT 4 Relay Mod
nice :)

Please Log in or Create an account to join the conversation.

9 years 9 months ago #274 by redice2k
Very nice project, what model of relay use ?

Please Log in or Create an account to join the conversation.

9 years 9 months ago #278 by cdj
Replied by cdj on topic Cdj ESP EasyIOT 4 Relay Mod
SONGLE SRD-05VDC-SD-C

But i suppose that you can use all 5v relay....
Dario

Please Log in or Create an account to join the conversation.

9 years 9 months ago #287 by skywatch
Nice!

Songle relays are good, but be aware that there are a lot of fake Songle relays coming out of China that are not so good - I know from experience and have both real and fake ones here!

Skywatch.

Please Log in or Create an account to join the conversation.

9 years 6 months ago #1399 by eggster
Replied by eggster on topic Cdj ESP EasyIOT 4 Relay Mod
@Dario

Thank you for sharing the code.

I have a somewhat stupid question: Can the 4 relays be operated independently from the EasyIoT server or do they all switch simultaneously?

Regards

Please Log in or Create an account to join the conversation.

Time to create page: 0.231 seconds

Forum latest

  • No posts to display.