Please Log in or Create an account to join the conversation.
EasyIoT wrote: In program try this:
in declaration:
Esp8266EasyIoTMsg msgRelay(RELAY_1, V_DIGITAL_VALUE);
and then send status of relay based on arduino pin which is connected to relay
esp.send(msgRelay.set((uint8_t)1));
or
esp.send(msgRelay.set((uint8_t)0));
Please Log in or Create an account to join the conversation.
/*
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.
*/
/*Cdj Mod
OUT RELAY PIN 4,5,6,7
IN SIGNAL PIN 8,9,10,11
MORSETTIERA VERSO ALTO (X =NON COLLEGATO)
9-12V IN 5V OUT220 220V
|+|X|-| |4|3|2|1|X|X|4|3|2|1|L|N|
*/
#include <Esp8266EasyIoT.h>
#include <SoftwareSerial.h>
Esp8266EasyIoT esp;
SoftwareSerial serialEsp(2, 3);
#define RELAY_1 4 // 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
#define PHOTORES_1 8 //Arduino Digital pin INPUT for photoresistor (second on pin+1)
Esp8266EasyIoTMsg msgRelay1(RELAY_1, V_DIGITAL_VALUE);
Esp8266EasyIoTMsg msgRelay2(RELAY_1 + 1, V_DIGITAL_VALUE);
Esp8266EasyIoTMsg msgRelay3(RELAY_1 + 2, V_DIGITAL_VALUE);
Esp8266EasyIoTMsg msgRelay4(RELAY_1 + 3, V_DIGITAL_VALUE);
int status;
int prevstatus=0;
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);
pinMode(PHOTORES_1, INPUT);
pinMode(PHOTORES_1 + 1,INPUT);
pinMode(PHOTORES_1 + 2,INPUT);
pinMode(PHOTORES_1 + 3,INPUT);
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();
//CDJ Here i define status from photores and it works perfectly
status = digitalRead(PHOTORES_1)==HIGH ? 1 : 0;
status |= digitalRead(PHOTORES_1+1)==HIGH ? 2 : 0;
status |= digitalRead(PHOTORES_1+2)==HIGH ? 4 : 0;
status |= digitalRead(PHOTORES_1+3)==HIGH ? 8 : 0;
if (status != prevstatus) {
msgRelay1.set((uint8_t) status & 1 ? 1 : 0);
msgRelay2.set((uint8_t) status & 2 ? 1 : 0);
Serial.print("photores status: ");
Serial.println(status, HEX);
Serial.print("msgRelay1 value: ");
Serial.println(msgRelay1.getBool());
Serial.print("msgRelay2 value: ");
Serial.println(msgRelay2.getBool());
esp.send(msgRelay1);
esp.send(msgRelay2);
prevstatus = status;
}
delay(500);
}
void incomingMessage(const Esp8266EasyIoTMsg &message) {
// We only expect one type of message from controller. But we better check anyway.
//appena ricevo messaggio dalla wifi ovvero da server easyiot dico ad arduino cosa fare
Serial.println("New message");
if (message.type==V_DIGITAL_VALUE) {
// Change relay state
int st;
digitalWrite(message.sensor-1+RELAY_1, message.getBool() ? RELAY_ON : RELAY_OFF);
//CDJ here i simulate button function
delay(200);
digitalWrite(message.sensor-1+RELAY_1, RELAY_OFF);
Serial.print("Incoming change for sensor:");
Serial.print(message.sensor);
Serial.print(", New status: ");
Serial.println(message.getBool());
}
}
Please Log in or Create an account to join the conversation.
Please Log in or Create an account to join the conversation.
void loop()
{
if (esp.process())
{
if (newLevel != currLevel)
{
int dir = ( newLevel - currLevel ) < 0 ? -1 : 1;
currLevel += dir;
analogWrite( PWM_OUT, (int)(currLevel / 100. * 255) );
if (newLevel == currLevel)
{
esp.send(lightMsg.set(newLevel > 0 ? 1 : 0));
esp.send( dimmerMsg.set(newLevel) );
}
delay( FADE_DELAY );
}
}
}
Please Log in or Create an account to join the conversation.
Please Log in or Create an account to join the conversation.