Chat freely about anything...

User avatar
By ls1swap
#44735 'I found some code on random nerd tutorial that uses a esp8266-01 and a free service called" if this then that (ifttt) it works great but I would like it to do more. I am using a Nodemcu in place of the esp8266-01. I would like to add function by using more gpio available on the nodemcu. The set up I have now has a small float switch in my sump pit. When the pump should fail I get a email and text to my phone. Everything is great with that, but I have two sump pits and inplace of a second esp8266 I would like the nodemcu to send a different text depending on which sump pump has failed (I.E. which switch closes). Here is a link to tutorial and code. he is using it as a door status monitor but can be used to monitor anything with a switch or can set a pin to high or low for that matter.
http://randomnerdtutorials.com/door-sta ... e-esp8266/

I am sure the code could be modified but is beyond my skills. thanks in advance
User avatar
By Geert
#44917 Basically you can have two interrupts, and also two ways of checking if the value is changed, it is not the best way to do it (in terms of lines of code and memory used) but it will do the job, now you only have to make two IFTT sequences that listen to another URL...

Code: Select all/*
 Created by Rui Santos
 
 All the resources for this project:
 http://randomnerdtutorials.com/
 
 Based on some ESP8266 code examples
*/

#include <ESP8266WiFi.h>

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

int pin1 = 2;
int pin2 = 3;
volatile int state1 = false;
volatile int flag1 = false;
const char* door_state1 = "closed";

volatile int state2 = false;
volatile int flag2 = false;
const char* door_state2 = "closed";

unsigned long previousMillis1 = 0;
unsigned long previousMillis2 = 0;

const long interval = 3000;

void changeDoorStatus1() {

    unsigned long currentMillis1 = millis();
 
    if(currentMillis1 - previousMillis1 >= interval) {
        previousMillis1 = currentMillis1;   
   
        state1 = !state1;
        if(state1) {
            door_state1 = "opened";
        }
        else{
            door_state1 = "closed";
        }
        flag1 = true;
        Serial.println(state1);
        Serial.println(door_state1);
    }
   
}
void changeDoorStatus2() {

    unsigned long currentMillis2 = millis();
 
    if(currentMillis2 - previousMillis2 >= interval) {
        previousMillis2 = currentMillis2;   
   
        state2 = !state2;
        if(state2) {
            door_state2 = "opened";
        }
        else{
            door_state2 = "closed";
        }
        flag2 = true;
        Serial.println(state2);
        Serial.println(door_state2);
    }
   
}

void setup() {
    Serial.begin(115200);
    delay(100);
    Serial.println("Preparing the Door Status Monitor project...");
   
    pinMode(pin1, OUTPUT);
    pinMode(pin2, OUTPUT);
    attachInterrupt(digitalPinToInterrupt(pin1), changeDoorStatus1, CHANGE);
    attachInterrupt(digitalPinToInterrupt(pin2), changeDoorStatus2, 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(flag1){
          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_status1/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_state1 + "\r\n");
          flag1 = false;
      } 
      delay(10);
      if(flag2){
          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_status2/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_state2 + "\r\n");
          flag2 = false;
      } 
      delay(10);
     
}