Wifi manager + RadioHead

Hi, I’m asking for help. The idea is as follows. send data from 433Mhz + Arduino and accept on ESP and send to blynk. + Send the DHT sensor data from ESP to Blynk.
When I am not connected to the blynk server, the serial line lists the received OK values. if I join the blynk does not work. Problem occurs if I use the Wifi manager sketch.


// receiver - prijmac

#include <RH_ASK.h>

 //Receiver (Pin D4)
  RH_ASK driver(2000, 2, 5, 0);

#include <SPI.h> // Not actualy used but needed to compile

struct dataStruct{
  float press_norm ; 
  float press_hg;
  float temp;  
  float input_volt; //napeti
  float pokus;
  float pokus2;
  unsigned long counter;
   
}myData;

#include <ESP8266WiFi.h>          //https://github.com/esp8266/Arduino
#include <BlynkSimpleEsp8266.h>
//needed for library
#include <ESP8266WebServer.h>
#include <DNSServer.h>
#include <WiFiManager.h>          //https://github.com/tzapu/WiFiManager
#include <SimpleTimer.h>
#include <DHT.h>
#define DHTPIN 4 //pin gpio 2 in sensor (D2)LOW PIN AP
#define DHTTYPE DHT22   // DHT 22 Change this if you have a DHT11

DHT dht(DHTPIN, DHTTYPE);


// vyberte, který pin spustí konfigurační portál, když je nastaven na hodnotu LOW
// Uživatelé ESP-01 berou na vědomí: sdílené jsou pouze dostupné kolíky (0 a 2)
// s bootloaderem, proto je při napájení vždy nastavte HIGH
#define TRIGGER_PIN 0// D3


// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "Token";  // Put your Auth Token here. (see Step 3 above)

BlynkTimer timer;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  Serial.println("\n Starting");

  // RHT-ASK
   if (!driver.init())
         Serial.println("init failed");
 

  pinMode(TRIGGER_PIN, INPUT);
  Blynk.config(auth);

// nove pridano
dht.begin();
  // Setup a function to be called every second
  timer.setInterval(5000L, sendUptime);

}


void loop() {
  // is configuration portal requested?
  if ( digitalRead(TRIGGER_PIN) == LOW ) {
    //WiFiManager
    //Local intialization. Once its business is done, there is no need to keep it around
    WiFiManager wifiManager;

    //reset settings - for testing
    //wifiManager.resetSettings();

    //sets timeout until configuration portal gets turned off
    //useful to make it all retry or go to sleep
    //in seconds
    //wifiManager.setTimeout(120);

    //it starts an access point with the specified name
    //here  "AutoConnectAP"
    //and goes into a blocking loop awaiting configuration

    //WITHOUT THIS THE AP DOES NOT SEEM TO WORK PROPERLY WITH SDK 1.5 , update to at least 1.5.1
    //WiFi.mode(WIFI_STA);
    
    if (!wifiManager.startConfigPortal("AP ESP")) {
      Serial.println("failed to connect and hit timeout");
      delay(3000);
      //reset and try again, or maybe put it to deep sleep
      ESP.reset();
      delay(5000);
    }

    //if you get here you have connected to the WiFi
    Serial.println("connected...yeey :)");
    
   
  }
 
 // put your main code here, to run repeatedly:
    Blynk.run();
    timer.run();


}


void sendUptime()

{
//RH_ASK
 uint8_t buf[RH_ASK_MAX_MESSAGE_LEN];
    uint8_t buflen = sizeof(buf);

   
 if (driver.recv(buf, &buflen)) // Non-blocking
    {
  int i;

  // Message with a good checksum received, dump it.
  driver.printBuffer("Got:", buf, buflen);
        memcpy(&myData, buf, sizeof(myData));
        Serial.println("");
        
                Serial.print("press_norm: ");
        Serial.print(myData.press_norm);
        
                      Serial.print("  press_hg: ");
        Serial.print(myData.press_hg);
        
                          Serial.print("  temp: ");
        Serial.print(myData.temp);

        Serial.print("  pokus: ");
        Serial.print(myData.pokus);

                        
        Serial.print("  napeti baterka: ");                
        Serial.print(myData.input_volt);

        Serial.print("  pokus2: ");                
        Serial.print(myData.pokus2);
                    
        
        Serial.print("  counter: ");
        Serial.println(myData.counter);
    }
  
       
   
  //Read the Temp and Humidity from DHT
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  Blynk.virtualWrite(10, t); // virtual pin 
  Blynk.virtualWrite(11, h); // virtual pin 
   }

Why on earth would you put your WiFi Manager code in the void loop?

Pete.

The wifi manager + DHT itself works OK problem occurs with part RH_ASK, I do not know why if it connects to the blynk server, the Serial monitor stops writing values.

//RH_ASK
 uint8_t buf[RH_ASK_MAX_MESSAGE_LEN];
    uint8_t buflen = sizeof(buf);

   
 if (driver.recv(buf, &buflen)) // Non-blocking
    {
  int i;

  // Message with a good checksum received, dump it.
  driver.printBuffer("Got:", buf, buflen);
        memcpy(&myData, buf, sizeof(myData));
        Serial.println("");
        
                Serial.print("press_norm: ");
        Serial.print(myData.press_norm);
        
                      Serial.print("  press_hg: ");
        Serial.print(myData.press_hg);
        
                          Serial.print("  temp: ");
        Serial.print(myData.temp);

        Serial.print("  pokus: ");
        Serial.print(myData.pokus);

                        
        Serial.print("  napeti baterka: ");                
        Serial.print(myData.input_volt);

        Serial.print("  pokus2: ");                
        Serial.print(myData.pokus2);
                    
        
        Serial.print("  counter: ");
        Serial.println(myData.counter);
    }

Because you have all that code in your void loop() :stuck_out_tongue_winking_eye: Your code is probably causing Blynk timeouts and/or disconnections, and possibly even device lockups/reboots?

It’s unlikely to overheat the server. DHT Sensor sends data to blynk OK. Only stops RH_ASK code (sending serial link)

No… just potentially cause the disconnection due to heartbeat issues from delay() commands that stop ALL processing, including Blynk.

Besides, as already stated… why do you need to call WiFi manager thousands of times a second anyhow?

Serial with Blynk does tend to get tricky to implement as Serial links need to constantly monitor or control it’s data flow. And since it usually does so in a blocking manner, it causes Blynk Server to assume the worst and disconnect. Generally the only way I can suggest is call Serial read/write processes from a fairly fast timed function (10-20ms) and adjust the BAUD rate in whatever way seems best to do the job in the time allotted. AKA trial and error.

Personally, i’d Start by asking the question “Do I need to use Wi-Fi Manager”. I played around with it for a while and came to the conclusion that although it was a clever solution, none of my devices actually need it, because my Wi-Fi SSID and password don’t change (and even if they did, re-flashing my hardware via OTA would be quicker than updating them via Wi-Fi Manager.
The only exception to this is when I want to put together a device for someone else, and want them to have the ability to make change without the need to come back to me for an update (Inhave to add that this is a non-Blynk project).

If you feel that the answer is yes, you do need to use Wi-Fi Manager then i’d suggest you search this forum for example sketches that use it successfully with Blynk.
The correct way to use Wi-Fi Manager is to either do a one-off Wi-Fi check/setup at startup (in void setup), or use a timed callback that checks if you’re connected to Wi-Fi and if it can’t connect then goes into setup (Station) mode.

You cannot run the W-Fi Manager code in the void loop, and expect Blynk to work with that arrangement.

Pete.

You’re right, I do not need a Wifi Manager. I thought it would not be such a problem, but my knowledge is not enough. This code works perfectly … Thank you all for your help

// receiver - prijmac

#include <RH_ASK.h>
// GPIO4=D2 na ESP, BPS,RX,TX ESP8266 or ESP32: do not use pin 11 or 2
   //RH_ASK driver(2000, 4, 5, 0);

 // GPIO 2=D4 na ESP
  RH_ASK driver(2000, 2, 5, 0);
//RH_ASK driver;


#include <SPI.h> // Not actualy used but needed to compile



struct dataStruct{
  float press_norm ; 
  float press_hg;
  float temp;  
  float input_volt; //napeti
  float pokus;
  float pokus2;
  unsigned long counter;
   
}myData;



#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>


// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "token";  // Put your Auth Token here. (see Step 3 above)

SimpleTimer timer;

void setup()
{
  

    Serial.begin(9600); // See the connection status in Serial Monitor
   Blynk.begin(auth, "test", "testtest"); //insert here your SSID and password

 if (!driver.init())
         Serial.println("init failed");
 
  // Setup a function to be called every second
  timer.setInterval(5000L, sendUptime);
}

void loop()
{
  Blynk.run();
  timer.run();
}


void sendUptime()
{
//RH_ASK
 uint8_t buf[RH_ASK_MAX_MESSAGE_LEN];
    uint8_t buflen = sizeof(buf);

   
 if (driver.recv(buf, &buflen)) // Non-blocking
    {
  int i;

  // Message with a good checksum received, dump it.
  driver.printBuffer("Got:", buf, buflen);
        memcpy(&myData, buf, sizeof(myData));
        Serial.println("");
        
                Serial.print("press_norm: ");
        Serial.print(myData.press_norm);
        
                      Serial.print("  press_hg: ");
        Serial.print(myData.press_hg);
        
                          Serial.print("  temp: ");
        Serial.print(myData.temp);

        Serial.print("  pokus: ");
        Serial.print(myData.pokus);

                        
        Serial.print("  napeti baterka: ");                
        Serial.print(myData.input_volt);

        Serial.print("  pokus2: ");                
        Serial.print(myData.pokus2);
                    
        
        Serial.print("  counter: ");
        Serial.println(myData.counter);
    }
  
  Blynk.virtualWrite(10, myData.counter); // virtual pin 
  Blynk.virtualWrite(11, myData.temp); //virtual pin V10


}
1 Like