-->
Page 1 of 1

reed switch sensor working backwards with nodemcu lua ifttt

PostPosted: Sat May 06, 2017 8:15 pm
by danluanodemcu
greetings,

i found a tutorial online on how to make a door sensor that sends a sms message to my cell through the ifttt service. it's supposed to text me when the door gets opened but for some reason it's working backwards and is only texting me when the door gets closed. ideally, i'd like to get a message when the door changes to any state, closed to open, open to closed.

things involved:
NodeMCU 0.9.6 build 20150704 powered by Lua 5.1.4 (tried both integer and floating firmwares)
-MC-38 door sensor that has continuity when the magnet is near the reed
-lua loader v0.91
IFTTT push service

the instructions state to connect the reed sensor to d2 and GND.

i have attached a zip file containing the lua files uploaded to the nodemcu.

Re: reed switch sensor working backwards with nodemcu lua if

PostPosted: Thu May 11, 2017 5:58 am
by danluanodemcu
for anyone wondering, i gave up with the lua loader and switched to the arduino ide.

i used the following sketch and got the effect i wanted. every time the door changes state, i get a text message either "door open" or "door closed". only issue is if the nodemcu is powered up and the magnet is away from the reed sensor, the notifications are reversed. door open is really door closed and vice versa.

Code: Select all#include <ESP8266WiFi.h>

const char* ssid = "********";
const char* password = "********";
const char* host = "maker.ifttt.com";
const char* apiKey = "********";

int pin = 2;
volatile int state = false;
volatile int flag = false;
const char* door_state = "closed";

unsigned long previousMillis = 0;
const long interval = 3000;

void changeDoorStatus() {

    unsigned long currentMillis = millis();
 
    if(currentMillis - previousMillis >= interval) {
        previousMillis = currentMillis;   
   
        state = !state;
        if(state) {
            door_state = "opened";
        }
        else{
            door_state = "closed";
        }
        flag = true;
        Serial.println(state);
        Serial.println(door_state);
    }
   
}


void setup() {
    Serial.begin(115200);
    delay(100);
    Serial.println("Preparing the Door Status Monitor project...");
   
    pinMode(pin, OUTPUT);
    attachInterrupt(digitalPinToInterrupt(pin), changeDoorStatus, CHANGE);

    Serial.println();
    Serial.println();
    Serial.print("Connecting to ");
    Serial.println(ssid);
   
    WiFi.begin(ssid, password);
   
    while (WiFi.status() != WL_CONNECTED) {
      delay(500);
      Serial.print(".");
    }
 
    Serial.println("");
    Serial.println("WiFi connected"); 
    Serial.println("IP address: ");
    Serial.println(WiFi.localIP()); 
}

void loop() {

      if(flag){
          Serial.print("connecting to ");
          Serial.println(host);
         
          WiFiClient client;
          const int httpPort = 80;
          if (!client.connect(host, httpPort)) {
            Serial.println("connection failed");
            return;
          }
   
          String url = "/trigger/door_status/with/key/";
          url += apiKey;
         
          Serial.print("Requesting URL: ");
          Serial.println(url);
          client.print(String("POST ") + url + " HTTP/1.1\r\n" +
                       "Host: " + host + "\r\n" +
                       "Content-Type: application/x-www-form-urlencoded\r\n" +
                       "Content-Length: 13\r\n\r\n" +
                       "value1=" + door_state + "\r\n");
          flag = false;
      } 
      delay(10);
}