Temp - Wifi - Relay

JohnnyG
Posts: 3
Joined: Tue Apr 28, 2020 2:56 pm

Temp - Wifi - Relay

Postby JohnnyG » Tue Apr 28, 2020 5:24 pm

I am trying to combine two separate ino's on an ESP32 with the Arduino IDE. The first ino uses a temp sensor to display the temperature on the serial monitor. The second uses wifi to create a webpage with an on/off link that will turn a relay on or off. Both ino's work separately but when combined, the ESP32 continuously reboots.

Thinking that it may be a resource or timing problem, I tried to run the temp code on one core and the wifi/relay code on the other core. It still crashes. I added the core code to the separate ino's and the temp code worked as expected but the wifi/relay code still crashed. The serial monitor displays the crash info, it says "Debug exception reason: Stack canary watchpoint triggered (Task1)" Below is the wifi/relay code that crashes. Any thought on how to get this to work in it's own core?

Thanks
JohnnyG



Code: Select all

#include "Arduino.h"
#include <WiFi.h>

TaskHandle_t Task1;

const char* ssid     = "Chicago";
const char* password = "TerryKathRules";

WiFiServer server(80);

void setup()
{
      
  Serial.begin(115200);
  pinMode(23, OUTPUT);      // set the LED pin mode 
  
  xTaskCreatePinnedToCore(
      loop1,    /* Function to implement the task  Can be called anything */
      "Task1",  /* Name of the task */
      1000,     /* Stack size in words */
      NULL,     /* Task input parameter */
      0,        /* Priority of the task */
      &Task1,   /* Task handle. This is a pointer in memory that we can get to at any timne*/
      1);       /* Core where the task should run */


  delay(10);

  // We start by connecting to a WiFi network

  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());

//  server.begin();
}

int value = 0;

void loop1(void * parameter) {
  for (;;) {
   server.begin();   
    WiFiClient client = server.available();   // listen for incoming clients
    if (client) {                             // if you get a client,
      Serial.println("New Client.");           // print a message out the serial port
      String currentLine = "";                // make a String to hold incoming data from the client
      while (client.connected()) {            // loop while the client's connected
        if (client.available()) {             // if there's bytes to read from the client,
          char c = client.read();             // read a byte, then
          Serial.write(c);                    // print it out the serial monitor
          if (c == '\n') {                    // if the byte is a newline character
            // if the current line is blank, you got two newline characters in a row.
            // that's the end of the client HTTP request, so send a response:

            if (currentLine.length() == 0) {

              // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
              // and a content-type so the client knows what's coming, then a blank line:
              client.println("HTTP/1.1 200 OK");
              client.println("Content-type:text/html");
              client.println();
              client.println("<HTML>");
              client.println("<HEAD>");
              client.println("<TITLE>Arduino GET test page</TITLE>");
              client.println("</HEAD>");
              client.println("<BODY>");
              client.println("<H1>Fan Switch</H1>");
              client.print("<a href='\/L\' target='inlineframe'>ON </a>");
              client.print("<a href='\/H\' target='inlineframe'>OFF</a>");
              client.println("<IFRAME name=inlineframe style='display:none'>");
              client.println("</IFRAME>");
              client.println("</BODY>");
              client.println("</HTML>");

              // the content of the HTTP response follows the header:
              // The HTTP response ends with another blank line:

              client.println();

              // break out of the while loop:

              break;

            } else {    // if you got a newline, then clear currentLine:
              currentLine = "";
            }
          } else if (c != '\r') {  // if you got anything else but a carriage return character,
            currentLine += c;      // add it to the end of the currentLine
          }

          // Check to see if the client request was "GET /H" or "GET /L":
          if (currentLine.endsWith("GET /H")) {
            digitalWrite(23, HIGH);               // GET /H turns the LED on
          }
          if (currentLine.endsWith("GET /L")) {
            digitalWrite(23, LOW);                // GET /L turns the LED off
          }
        }
      }
      // close the connection:
      client.stop();
      Serial.println("Client Disconnected.");
    }
  }
}
    
void loop() {
  delay (1); // this is the way the guy with the swedish accent does it.
}

ESP_Sprite
Posts: 9730
Joined: Thu Nov 26, 2015 4:08 am

Re: Temp - Wifi - Relay

Postby ESP_Sprite » Tue Apr 28, 2020 6:43 pm

You're running out of stack. Increase the number where it says

Code: Select all

1000,     /* Stack size in words */
until the issue goes away.

JohnnyG
Posts: 3
Joined: Tue Apr 28, 2020 2:56 pm

Re: Temp - Wifi - Relay

Postby JohnnyG » Tue Apr 28, 2020 9:35 pm

I was just coming here to say that I increased the stack size and it fixed the problem.
Is there a way to "know" what that setting should be? Can I set it for big and somehow see how much is being used?
Thanks for the help!!!
JohnnyG

Who is online

Users browsing this forum: Baidu [Spider] and 86 guests