ESP8266 Coding Advice
Posted: Wed Apr 29, 2020 8:10 am
Hi. I’m new to the arduino and ESP8266 but have some limited experience coding picaxe. I’ve been working with the RCSwitch.h library which deals with mimicing RF remote controls. I have it installed as part of a sketch used to send RF signals to a remote RF socket. It basically mimics the remote control.
I find the C/C++ environment new and a bit challenging. Basically I need some help in declaring variables and running commands based on if statements. I want to run a command like mySwitch.send("100111101111101000001111"); based on the state (high or low) of an input whether analog or digital. If the input to the digital or analog pin is high I want the sketch to send the following command:
mySwitch.send("100111101111101000001111");
and if the state of the input is low I want it to send this command:
mySwitch.send("100111101111101000001110");
This is the sketch which I use to turn ON and OFF the socket for 1 second intervals. It works on my ESP8266 and arduino uno. It’s just a proof of concept and turns the plug on and off no problem at 1 second intervals.
/*
Based on the SendDemo example from the RC Switch library
https://github.com/sui77/rc-switch/
*/
#include <RCSwitch.h>
RCSwitch mySwitch = RCSwitch();
void setup() {
Serial.begin(9600);
// Transmitter is connected to Arduino Pin #10
mySwitch.enableTransmit(10);
// Optional set pulse length.
mySwitch.setPulseLength(314);
// Optional set protocol (default is 1, will work for most outlets)
mySwitch.setProtocol(1);
}
void loop() {
// Binary code - button 3
mySwitch.send("100111101111101000001111"); //turns ON switch
delay(1000);
mySwitch.send("100111101111101000001110"); //turns OFF switch
delay(1000);
}
This works fine and turns the plug ON and OFF once a second. What I want to do is to set a condition in the loop part of the code to only turn the switch on i.e. execute
mySwitch.send("100111101111101000001111");
if an input pin goes high (either analog or digital). As soon as the input goes low then the plug would be turned off by sending
mySwitch.send("100111101111101000001110");
I think I would need some sort of variable to store the state of the plug, statevar say, maybe 0 for plug off and 1 for plug on. I suppose this variable would have to be declared in the setup part of the sketch?
If I were doing this in picaxe basic it would look some thing like this;
Start:
'#picaxe 14M2
readadc C.4,b0 'read value on ADC pin C.4 and put that value into variable b0
if b0>=120 and b1=0 then goto plugON 'b1 is variable to store the state of the plug, 1 for ON and 0 for OFF
if b0<120 and b1=1 then goto plugOFF
goto Start
plugON:
mySwitch.send("100111101111101000001111");
b1=1
goto Start
plugOFF:
mySwitch.send("100111101111101000001110");
b1=0
goto Start
I would appreciate any help you could give. Thank you in advance.
Regards
Dermot
I find the C/C++ environment new and a bit challenging. Basically I need some help in declaring variables and running commands based on if statements. I want to run a command like mySwitch.send("100111101111101000001111"); based on the state (high or low) of an input whether analog or digital. If the input to the digital or analog pin is high I want the sketch to send the following command:
mySwitch.send("100111101111101000001111");
and if the state of the input is low I want it to send this command:
mySwitch.send("100111101111101000001110");
This is the sketch which I use to turn ON and OFF the socket for 1 second intervals. It works on my ESP8266 and arduino uno. It’s just a proof of concept and turns the plug on and off no problem at 1 second intervals.
/*
Based on the SendDemo example from the RC Switch library
https://github.com/sui77/rc-switch/
*/
#include <RCSwitch.h>
RCSwitch mySwitch = RCSwitch();
void setup() {
Serial.begin(9600);
// Transmitter is connected to Arduino Pin #10
mySwitch.enableTransmit(10);
// Optional set pulse length.
mySwitch.setPulseLength(314);
// Optional set protocol (default is 1, will work for most outlets)
mySwitch.setProtocol(1);
}
void loop() {
// Binary code - button 3
mySwitch.send("100111101111101000001111"); //turns ON switch
delay(1000);
mySwitch.send("100111101111101000001110"); //turns OFF switch
delay(1000);
}
This works fine and turns the plug ON and OFF once a second. What I want to do is to set a condition in the loop part of the code to only turn the switch on i.e. execute
mySwitch.send("100111101111101000001111");
if an input pin goes high (either analog or digital). As soon as the input goes low then the plug would be turned off by sending
mySwitch.send("100111101111101000001110");
I think I would need some sort of variable to store the state of the plug, statevar say, maybe 0 for plug off and 1 for plug on. I suppose this variable would have to be declared in the setup part of the sketch?
If I were doing this in picaxe basic it would look some thing like this;
Start:
'#picaxe 14M2
readadc C.4,b0 'read value on ADC pin C.4 and put that value into variable b0
if b0>=120 and b1=0 then goto plugON 'b1 is variable to store the state of the plug, 1 for ON and 0 for OFF
if b0<120 and b1=1 then goto plugOFF
goto Start
plugON:
mySwitch.send("100111101111101000001111");
b1=1
goto Start
plugOFF:
mySwitch.send("100111101111101000001110");
b1=0
goto Start
I would appreciate any help you could give. Thank you in advance.
Regards
Dermot