S
S
seregareal22018-05-12 21:06:52
Arduino
seregareal2, 2018-05-12 21:06:52

NodeMCU is not working properly. What's wrong?

it is necessary that when the contact is closed, an SMS is sent to the phone, via the Internet
Given
NodeMCU
reed sensor, connected as a button through a resistor to 3.3 V (float)

spoiler
#include <ESP8266WiFi.h>
#include "Gsender.h"

//Теперь рассмотрим вариант, когда нам нужно сделать лишь одно действие по одному нажатию кнопки (предыдущие программы циклично повторяли действие при удержании)
//Для этого введем новую переменную flag (названия переменных могут быть какими угодно).
int flag=0;




#pragma region Globals
const char* ssid = "***";                         // WIFI network name
const char* password = "***";              // WIFI network password
uint8_t connection_state = 0;                    // Connected to WIFI or not
uint16_t reconnect_interval = 10000;             // If not connected wait time to try again
#pragma endregion Globals

uint8_t WiFiConnect(const char* nSSID = nullptr, const char* nPassword = nullptr)
{
    static uint16_t attempt = 0;
    Serial.print("Connecting to ");
    if(nSSID) {
        WiFi.begin(nSSID, nPassword);
        Serial.println(nSSID);
    } else {
        WiFi.begin(ssid, password);
        Serial.println(ssid);
    }

    uint8_t i = 0;
    while(WiFi.status()!= WL_CONNECTED && i++ < 50)
    {
        delay(200);
        Serial.print(".");
    }
    ++attempt;
    Serial.println("");
    if(i == 51) {
        Serial.print("Connection: TIMEOUT on attempt: ");
        Serial.println(attempt);
        if(attempt % 2 == 0)
            Serial.println("Check if access point available or SSID and Password\r\n");
        return false;
    }
    Serial.println("Connection: ESTABLISHED");
    Serial.print("Got IP address: ");
    Serial.println(WiFi.localIP());
    return true;
}

void Awaits()
{
    uint32_t ts = millis();
    while(!connection_state)
    {
        delay(50);
        if(millis() > (ts + reconnect_interval) && !connection_state){
            connection_state = WiFiConnect();
            ts = millis();
        }
    }
}

void setup()

{

  pinMode(2, INPUT);

    Serial.begin(115200);
    connection_state = WiFiConnect();
    if(!connection_state)  // if not connected to WIFI
        Awaits();          // constantly trying to connect

    Gsender *gsender = Gsender::Instance();    // Getting pointer to class instance
    String subject = "Subject is optional!";
    if(gsender->Subject(subject)->Send("***@sms.ru", "System check, ON")) {
        Serial.println("Message send.");
    } else {
        Serial.print("Error sending message: ");
        Serial.println(gsender->getError());
    }
}

void loop(){

  
     if(digitalRead(2)==HIGH&&flag==0)//если кнопка нажата 
     // и перемення flag равна 0 , то ...
     {
     
       flag=1;
    Gsender *gsender = Gsender::Instance();    // Getting pointer to class instance
    String subject = "Subject is optional!";
    if(gsender->Subject(subject)->Send("***@sms.ru", "Aqua potop")) {
        Serial.println("Message send.");
    } else {
        Serial.print("Error sending message: ");
        Serial.println(gsender->getError());
    }

    
     
     }
     
      if(digitalRead(2)==LOW&&flag==1)//если кнопка НЕ нажата
     //и переменная flag равна - 1 ,то ...
     {
        
        flag=0;//обнуляем переменную flag
     }

  
  }


With this code, an SMS with the text Aqua potop comes at a different position of the reed switch and only 1 SMS comes, then the controller does not react to anything,
where is the jamb?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question