I am programming an ESP8266-01.
I have this code which I am using to operate an on-off switch, which is being used as an override switch for when the relay is turned on and off over the internet:
void loop() {
// Connect to the cloud
rest.handle(client);
// put your main code here, to run repeatedly:
int read3 = digitalRead(0);
Serial.print(read3);
Serial.println(" input (1 is off)");
if(read3 == 0) {
if(pressed2) {
Serial.println("in on");
digitalWrite(2, 1);
pressed = true;
pressed2 = false;
}
}
else {
Serial.println("in off");
if(pressed) {
Serial.println("in off once");
digitalWrite(2, 0);
pressed = false;
pressed2 = true;
}
}
}
The above code never starts to run (nothing gets output on the serial) and the relay turns on. The thing is, this code works:
void loop() {
// Connect to the cloud
rest.handle(client);
// put your main code here, to run repeatedly:
int read3 = digitalRead(0);
Serial.print(read3);
Serial.println(" input (1 is off)");
if(read3 == 0) {
Serial.println("in on");
digitalWrite(2, 1);
pressed = true;
}
else {
Serial.println("in off");
if(pressed) {
Serial.println("in off once");
digitalWrite(2, 0);
pressed = false;
}
}
}
Basically the way the code is set up, it is handling things so that the pin doesn't get read after the instance first instance that it is read (this is what the bool
pressed
is helping to do).
Why doesn't the first version work?
UPDATE
Whole working sketch:
/*
This a simple example of the aREST Library for the ESP8266 WiFi chip.
This example illustrate the cloud part of aREST that makes the board accessible from anywhere
See the README file for more details.
Written in 2015 by Marco Schwartz under a GPL license.
*/
// Import required libraries
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <aREST.h>
// Clients
WiFiClient espClient;
PubSubClient client(espClient);
// Create aREST instance
aREST rest = aREST(client);
// Unique ID to identify the device for cloud.arest.io
char* device_id = "ew1zard";
// WiFi parameters
const char* ssid = "MaisonBlanche";
const char* password = "7932gLBw";
bool pressed = true;
// Functions
void callback(char* topic, byte* payload, unsigned int length);
void setup(void)
{
// Set input / output pin
pinMode(0, INPUT);
pinMode(2, OUTPUT);
// Start Serial
Serial.begin(115200);
// Set callback
client.setCallback(callback);
// Give name & ID to the device (ID should be 6 characters long)
rest.set_id(device_id);
rest.set_name("esp8266");
// Connect to WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// Set output topic
char* out_topic = rest.get_topic();
}
void loop() {
// Connect to the cloud
rest.handle(client);
// put your main code here, to run repeatedly:
int read3 = digitalRead(0);
Serial.print(read3);
Serial.println(" input (1 is off)");
if(read3 == 0) {
Serial.println("in on");
digitalWrite(2, 1);
pressed = true;
}
else {
Serial.println("in off");
if(pressed) {
Serial.println("in off once");
digitalWrite(2, 0);
pressed = false;
}
}
}
// Handles message arrived on subscribed topic(s)
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("request from interwebs: ");
Serial.println(topic);
rest.handle_callback(client, topic, payload, length);
}