Keeps reseting to void setup(), RFM69 connected to ESP32

Hello, I’m doing an internship project for greenhouses in my area, I’m somewhat new to programming and automation. My code keeps resetting to setup and I’m not sure why. Also it’s not receiving any messages from my other RFM69 connected to an Arduino Uno, but I’m assuming that’s because it keeps resetting. Any help is appreciated.

ESP32 board is Nose32s in Arduino IDE.

Setup:

Pins:
RFM69: ESP32
Vin: 5V
Mosi: GPIO23 (VPSIMOSI)
Miso: GPIO19 (VSPIMISO)
CS = GPIO5 (VSPI SS)
RST = GPI02
G0/INT = GPI15
SCK/LED = GPI18 (VSPI SCK)

Serial Monitor:

19:15:04.914 -> Feather RFM69 RX Test!
19:15:05.017 -> RFM69 radio init OK!
19:15:05.054 -> RFM69 radio @915 MHz
19:15:05.633 -> .......WiFi connected
19:15:09.739 -> test1 Feather RFM69 RX Test!
19:15:11.028 -> RFM69 radio init OK!
19:15:11.062 -> RFM69 radio @915 MHz
19:15:11.655 -> .WiFi connected
19:15:12.889 -> test1 Feather RFM69 RX Test!
19:15:14.183 -> RFM69 radio init OK!
19:15:14.183 -> RFM69 radio @915 MHz
19:15:14.799 -> .WiFi connected
19:15:15.906 -> test1 Feather RFM69 RX Test!
19:15:17.186 -> RFM69 radio init OK!
19:15:17.220 -> RFM69 radio @915 MHz
19:15:17.804 -> .WiFi connected
19:15:19.039 -> test1 Feather RFM69 RX Test!
19:15:20.334 -> RFM69 radio init OK!
19:15:20.334 -> RFM69 radio @915 MHz

Code:

// rf69 demo tx rx.pde
// -*- mode: C++ -*-
// Example sketch showing how to create a simple messageing client
// with the RH_RF69 class. RH_RF69 class does not provide for addressing or
// reliability, so you should only use RH_RF69  if you do not need the higher
// level messaging abilities.
// It is designed to work with the other example rf69_server.
// Demonstrates the use of AES encryption, setting the frequency and modem 
// configuration

#define BLYNK_TEMPLATE_ID "**************"
#define BLYNK_DEVICE_NAME "ESP32"
#define BLYNK_AUTH_TOKEN "*************************"

#include <SPI.h>
#include <RH_RF69.h>

#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <Wire.h>
#include <SPI.h>

#include <Adafruit_AHTX0.h> //for alarm 
int i = 0; //for alarm

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "***************************";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "*********";
char pass[] = "**************";


/************ Radio Setup ***************/
// Change to 434.0 or other frequency, must match RX's freq!
#define RF69_FREQ 915.0

//#if defined (Arduino Uno) //Arduino Uno input by Calder
    #define RFM69_CS      5
    #define RFM69_INT     15
    #define RFM69_RST     2
    #define LED           19

// Singleton instance of the radio driver
RH_RF69 rf69(RFM69_CS, RFM69_INT);

int16_t packetnum = 0;  // packet counter, we increment per xmission



void setup() 
{
    Serial.begin(9600);
    //while (!Serial) { delay(1); } // wait until serial console is open, remove if not tethered to computer
    
   pinMode(33, OUTPUT); //pin for LED
   pinMode(32, OUTPUT); //pin for noise maker pin

  //Serial.begin(115200);
  //while (!Serial) { delay(1); } // wait until serial console is open, remove if not tethered to computer

  pinMode(LED, OUTPUT);     
  pinMode(RFM69_RST, OUTPUT);
  digitalWrite(RFM69_RST, LOW);

  Serial.println("Feather RFM69 RX Test!");
  
  // manual reset
  digitalWrite(RFM69_RST, HIGH);
  delay(10);
  digitalWrite(RFM69_RST, LOW);
  delay(10);
  
  if (!rf69.init()) {
    Serial.println("RFM69 radio init failed");
    while (1);
  }
  Serial.println("RFM69 radio init OK!");
  
  // Defaults after init are 434.0MHz, modulation GFSK_Rb250Fd250, +13dbM (for low power module)
  // No encryption
  if (!rf69.setFrequency(RF69_FREQ)) {
    Serial.println("setFrequency failed");
  }

  // If you are using a high power RF69 eg RFM69HW, you *must* set a Tx power with the
  // ishighpowermodule flag set like this:
  rf69.setTxPower(20, true);  // range from 14-20 for power, 2nd arg must be true for 69HCW

  // The encryption key has to be the same as the one in the server
  uint8_t key[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
                    0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
  rf69.setEncryptionKey(key);
  
  pinMode(LED, OUTPUT);

  Serial.print("RFM69 radio @");  Serial.print((int)RF69_FREQ);  Serial.println(" MHz");


//Wifi code copy pasted
WiFi.begin(ssid, pass);
  int wifi_ctr = 0;
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("WiFi connected");  
  Blynk.begin(auth, ssid, pass);
}




void loop() {

  //Alarm
  Serial.print ("test1 ");
  noTone(9); //no noise for buzzer if temp normal
  
 if (rf69.available()) {
    // Should be a message for us now
    Serial.print ("test2 ");  
    uint8_t buf[RH_RF69_MAX_MESSAGE_LEN]; //Creates array using buf of the amount of units there are in the string 
    uint8_t len = sizeof(buf); //len = length of string
    
    
    if (rf69.recv(buf, &len)) {
       Serial.print ("test3 ");
        float hhtemp = *((float *)buf);
      
     // if (!len) return;
     if (len = 4); //if length is 4 (for temp) then:
     if (!len) return;
      buf[len] = 0;
      Serial.print("Received [");
      Serial.print(len); //length of message received
      Serial.print("]: ");
      Serial.println(hhtemp);
      Serial.print("RSSI: "); //RSSI is like the signal strength
      Serial.println(rf69.lastRssi(), DEC);

         //Send data to Blynk website
      Blynk.run();
      delay(200);
      Blynk.virtualWrite(V1,hhtemp);
     //////////// Blynk.virtualWrite(V2,hhhumid);


     //Siren Alarm using Buzzer
      if (hhtemp > 75) {
     for(i=700;i<800;i++){
     tone(9,i);
     digitalWrite(8, HIGH);
     delay(15);
     }
     for(i=800;i>700;i--){
     tone(9,i);
     digitalWrite(8, LOW);
     delay(15);
      }

      }
      }
      
    } else { 
      
      Serial.println("Receive failed");
      
     // set radio to use less power
     // it will wake up the next time send() or sendWithRetry() is called
    //////////// rf69.sleep(); ///removing to test if doens't work on ESP32
    }

  Serial.print ("Test4 ");
}

The radio and Wifi draw a lot of current when transmitting, and may be overloading the power supply. That buzzer might, too.

Please post a clear, hand drawing wiring diagram with all connections labeled. It is extremely difficult to decode the wiring photo.

Below is image. I fixed my setup by adding a ground from buzzer & LED, doesn't fix issue though.

The schematic is a big help. I will take a SWAG: Sorry to say you are not the only one with this problem. Try setting up this way: try entering: esp_task_wdt_reset(); SerialBT.begin(...). The way it operates is it has an internal operating system that is running and your code is just a task or function of there code. There code needs to keep operating or the system will stall, so they automatically enable the watchdog. I hope this helps.

Hi Gilshultz, thanks for the advice. I tried adding

     esp_task_wdt_reset(); //Added per forum post
     Serial.begin(115200);

to the start of void loop(), that didn't change anything so I tried instead in the void setup() before the first 'if' statement, which also didn't change anything. Am I doing this correctly? (I added #include <esp_task_wdt.h> at the start.

I think SerialBT.begin is for bluetooth correct? I just used the regular one.

Thanks

Yes, I thought it was being set up as bluetooth.

It seems to be resetting here

  Serial.print ("test1 ");
  noTone(9); //no noise for buzzer if temp normal

since no new message is received after that.
and either test2 or test4 should be received.
Still there could be a buffer that is not empty yet, you probably should add a

delay(10);

to make sure the buffer is empty,
But really the pin nr is not correct. That is the RX pin of uart0 while you

pinMode(32, OUTPUT); //pin for noise maker pin

meant to use that one.

What's on pin 9? Also you are supposed to call noTone to stop tone generated with tone, you dont have any tone to stop at the start of the loop. noTone resets timers, so I dont know if this could cause reset as no timers have been set at this point

Thanks Deva_Rishi and Killzone_kid, it was the 'noTone(9);'. I changed it to the correct pin 32 but was getting a confusing error so I just removed it (In earlier versions with Arduino Uno the buzzer kept going off after looping so that's why it was there). I added the 'delay(10);' after the 'Serial.print ("test1") line too.

My code no longer resets to void setup() after removing the 'noTone', but now the Serial Monitor stops at 'test1', and does not skip to the end where it should at-least say 'Receive Failed', or 'test4'. Any ideas?

// rf69 demo tx rx.pde
// -*- mode: C++ -*-
// Example sketch showing how to create a simple messageing client
// with the RH_RF69 class. RH_RF69 class does not provide for addressing or
// reliability, so you should only use RH_RF69  if you do not need the higher
// level messaging abilities.
// It is designed to work with the other example rf69_server.
// Demonstrates the use of AES encryption, setting the frequency and modem 
// configuration

#define BLYNK_TEMPLATE_ID "******************"
#define BLYNK_DEVICE_NAME "******"
#define BLYNK_AUTH_TOKEN "*************************"

#include <SPI.h>
#include <RH_RF69.h>

#include <esp_task_wdt.h> //from forum post, interupt 

#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <Wire.h>
#include <SPI.h>

#include <Adafruit_AHTX0.h> //for alarm 
int i = 0; //for alarm

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "*********************************";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "********";
char pass[] = "**************";


/************ Radio Setup ***************/
// Change to 434.0 or other frequency, must match RX's freq!
#define RF69_FREQ 915.0

//#if defined (Arduino Uno) //Arduino Uno input by Calder
    #define RFM69_CS      5
    #define RFM69_INT     15
    #define RFM69_RST     2
    #define LED           18

// Singleton instance of the radio driver
RH_RF69 rf69(RFM69_CS, RFM69_INT);

int16_t packetnum = 0;  // packet counter, we increment per xmission

void setup() 
{
    
    Serial.begin(9600);
    //while (!Serial) { delay(1); } // wait until serial console is open, remove if not tethered to computer
    
   pinMode(33, OUTPUT); //pin for LED
   pinMode(32, OUTPUT); //pin for noise maker pin


  pinMode(LED, OUTPUT);     
  pinMode(RFM69_RST, OUTPUT);
  digitalWrite(RFM69_RST, LOW);

  Serial.println("Feather RFM69 RX Test!");
  
  // manual reset
  digitalWrite(RFM69_RST, HIGH);
  delay(10);
  digitalWrite(RFM69_RST, LOW);
  delay(10);
  
  if (!rf69.init()) {
    Serial.println("RFM69 radio init failed");
    while (1);
  }
  Serial.println("RFM69 radio init OK!");
  
  // Defaults after init are 434.0MHz, modulation GFSK_Rb250Fd250, +13dbM (for low power module)
  // No encryption
  if (!rf69.setFrequency(RF69_FREQ)) {
    Serial.println("setFrequency failed");
  }

  // If you are using a high power RF69 eg RFM69HW, you *must* set a Tx power with the
  // ishighpowermodule flag set like this:
  rf69.setTxPower(20, true);  // range from 14-20 for power, 2nd arg must be true for 69HCW

  // The encryption key has to be the same as the one in the server
  uint8_t key[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
                    0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08};
  rf69.setEncryptionKey(key);
  
  pinMode(LED, OUTPUT);

  Serial.print("RFM69 radio @");  Serial.print((int)RF69_FREQ);  Serial.println(" MHz");


//Wifi code copy pasted
WiFi.begin(ssid, pass);
  int wifi_ctr = 0;
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("WiFi connected");  
  Blynk.begin(auth, ssid, pass);
}









void loop() {

  //Alarm
  Serial.print ("test1 ");
  delay(10);
  
  //noTone(32); //no noise for buzzer if temp normal (removed b/c was resetting to void setup(), originally added b/c was not buzzer was not shutting off after loop

 if (rf69.available()) {
    // Should be a message for us now 
    Serial.print ("test2 ");  
    uint8_t buf[RH_RF69_MAX_MESSAGE_LEN]; //Creates array using buf of the amount of units there are in the string 
    uint8_t len = sizeof(buf); //len = length of string
    
    
    if (rf69.recv(buf, &len)) {
       Serial.print ("test3 ");
        float hhtemp = *((float *)buf);
      
     // if (!len) return;
     if (len = 4); //if length is 4 (for temp) then:
     if (!len) return;
      buf[len] = 0;
      Serial.print("Received [");
      Serial.print(len); //length of message received
      Serial.print("]: ");
      Serial.println(hhtemp);
      Serial.print("RSSI: "); //RSSI is like the signal strength
      Serial.println(rf69.lastRssi(), DEC);


         //Send data to Blynk website
      Blynk.run();
      delay(200);
      Blynk.virtualWrite(V1,hhtemp);
     //////////// Blynk.virtualWrite(V2,hhhumid);


     //Siren Alarm using Buzzer
      if (hhtemp > 75) {
     for(i=700;i<800;i++){
     tone(9,i);
     digitalWrite(8, HIGH);
     delay(15);
     }
     for(i=800;i>700;i--){
     tone(9,i);
     digitalWrite(8, LOW);
     delay(15);
      }

      }
      }
      
    } else {
      
      Serial.println("Receive failed");
      
     // set radio to use less power
     // it will wake up the next time send() or sendWithRetry() is called
    //////////// rf69.sleep(); ///removing to test if doens't work on ESP32
    }

  Serial.print ("Test4 ");
}

rf69.available() could be blocking and it would wait there until there is some data available

[quote="calder1, post:9, topic:1023628"]
but now the Serial Monitor stops at 'test1', and does not skip to the end where it should at-least say 'Receive Failed', or 'test4'.
[/quote] I thought it should say 'test2' or 'receive failed' but the indent is a bit off so it is quite hard to read.
There is still some more places where you write

for(i=800;i>700;i--){
     tone(9,i);
     digitalWrite(8, LOW);
     delay(15)

still there should be response in the monitor before because of the delay after

Blynk.run();
      delay(200);

one would suspect it works the same way as with a Serial object but of course it may require some verification either way.
Look the function up in the library and see how it works.
There are actually more functions that i would like to have a look at, just to make sure that they are applied correctly.

I won't. But from OP description this is the only possible outcome.

The code and functions is mainly based an example sketch from 'radiohead' based on a feather, but it was recommended for me to use it when using the Uno, and it worked then. I guess I figured it would work now too but maybe there are incompatibilities with the library. I think that is above my expertise, I think I am going to try and find a sketch with ESP32 and an RFM69 already made.

There also may be some sort of SPI interruption going on, but looking to find something that is already solved and start from there instead of re-inventing the wheel.
"Apparently using the standard RFM69 library code on an ESP32 results in a conflict between interrupts for the RFM69 radio & SPI."
" ESP-32 SPI transactions occur as interrupt mode while the RFM69 library starts SPI transactions in interrupt routine simultaneously."

Current Library Used (I think?): RadioHead/RH_RF69.h at master · PaulStoffregen/RadioHead · GitHub

"Proof by Intuition" is a pretty weak and sketchy argument. Either back it up with the source code or don't make the assertion.

Who said anything about intuition? Go on then, what’s your take?

True, and even from the comments in RH_RF69.h not true.

 /// Starts the receiver and checks whether a received message is available.
    /// This can be called multiple times in a timeout loop
    /// \return true if a complete, valid message has been received and is able to be retrieved by
    /// recv()
    bool        available();

Actually that there is some other conflict which does cause the issue. I am not super familiar with the ESP32, i do have one and i managed to connect it to an SD-card module through SPI, but i do remember there being something about the pins, but looking at how i got it to work and what you've done it looks OK.
One thing i do notice now, and i don't know if that is relevant, is that you have connected Vcc on the RFM69 to 5v, when i am pretty sure that should be powered with 3.3v. Not only that, i actually doubt that the onboard 3.3v regulator of the ESP32 can provide the minimum current requirements that are stated here being 130mA. That is a lot !! and probably that is a peak requirement, but unlike in the tutorial where they are using a nano which itself does not draw current through the onboard 3.3v regulator, the ESP32 does.

So in short, i think it might be a power issue, and to rule out that option, well firstly connect Vcc on the RFM69 to 3.3v which is how it should be connected anyway (and that may even be the cause of the whole thing...) and then power the whole thing through a 3.3v PSU or just the RFM69 thru a dedicated 3.3v regulator.

Actually looking again at your schematic and the pinout i have, you have connected
ESP32-Vin to RFM69-Vcc
Vin on the ESP32 is exactly that, a power input, and you can not really draw power out through that when you power the ESP32 thru USB.
My bet is that that's the issue.

Firstly I didn’t connect anything, secondly the difference between ESP32 rebooting and now hanging is removal of noTone call. Yes, it is hardware problem of course.

Sorry that was meant for the OP. I know you are not the OP, but someone helping in solving this mystery.

As usual there is more than 1 thing wrong, which makes it hard to come up with a complete solution straight away.

I would think the 5V would be ok, this is an Adafruit RFM69HCW which can handle the 5V. "Each radio comes with some header, a 3.3V voltage regulator and levelshifter that can handle 3-5V DC power and logic so you can use it with 3V or 5V devices." Adafruit RFM69HCW Transceiver Radio Breakout - 868 or 915 MHz [RadioFruit] : ID 3070 : $9.95 : Adafruit Industries, Unique & fun DIY electronics and kits
Also it only uses "30mA during active radio listening", as this module I am only using to receive, not to transmit.

Eitherway though I switched it to the 3.3V pin instead of the 5V pin, but no change yet.

hmm, I'm confused about the input/output power comment as the projects I see are powered through the 3.3V or 5V Vin pins, not sure how else to power the RFM69 when hooked to the USB?

Well on an nodeMCU there is usually diodes preventing power out through the vin pin, but on an ESP32 that may not be the case.

well with any 3.3v power source and let the devices share common GND.

ah yeah, well i had been looking at barebones versions.