adafruit huzzah esp8266 crashing

Hi, new here, new to arduino, new to programming please try to be easy on me.

I have this crazy idea to use a servo motor to open/close/throttle a household vent or damper based on temperature. i haven’t gone too far in my sketch yet because i’m encountering a crash.

the goal of what you current see is to test if i can put the servo at a position until the temperature changes some amount but i’m already getting some sort of crash and i have zero idea why its crashing. there may be extra variables and such so just ignore those as i’ve been evolving this as i learn more and some stuff is left over.
here’s the crash info via serial:

ü!ÿL¤<1¤1óþ73 <-----------temp in F

Soft WDT reset

ctx: cont 
sp: 3ffef290 end: 3ffef470 offset: 01b0

>>>stack>>>
3ffef440:  3ffee2c8 3ffee2c0 3ffe835c 40201e00  
3ffef450:  3fffdad0 00000000 3ffee438 40202500  
3ffef460:  feefeffe feefeffe 3ffee450 40100114  
<<<stack<<<
ü!ÿL¤<1¤1óþ75 <-----------temp in F
#include <Servo.h> 
int sensorPin = A0; //reading temperature sensor on analog pin 0
int sensorValue;
int temp;
int sensorRaw;
int setpoint = 77;
int variance;
int pos = 90;
Servo myservo;  // create servo object to control a servo 

void setup() 
{ 
  Serial.begin(9600);
  myservo.attach(13);  // attaches the servo on GIO2 to the servo object 
} 
 
void loop() {
sensorValue = analogRead(sensorPin);
sensorRaw = analogRead(sensorPin);
temp = (sensorRaw-424)/6.25;
temp = (temp*1.8)+32;
delay(2000);
Serial.println(temp);
variance = (temp - setpoint);

if (variance > 0)
{
    for(pos = 90; pos <= 180; pos += 1) // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(50);                       // waits 15ms for the servo to reach the position 
  } 
  while (variance >0);
}
else if (variance < 0)
{
    for(pos = 90; pos>=0; pos-=1)     // goes from 180 degrees to 0 degrees 
  {                                
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(50);                       // waits 15ms for the servo to reach the position 
  } 
  while (variance < 0 );
}
}
if (variance > 0)
{
    for(pos = 90; pos <= 180; pos += 1) // goes from 0 degrees to 180 degrees
  {                                  // in steps of 1 degree
    myservo.write(pos);              // tell servo to go to position in variable 'pos'
    delay(50);                       // waits 15ms for the servo to reach the position
  }
  while (variance >0);
}

If the variance between the actual temperature and the set point is positive, move the servo and go into an infinite loop. Why on earth would you want to do that?

If the variance is negative, move the servo and go into an infinite loop. Why on earth would you want to do that?

Serial.println(temp);

Why are you printing this value anonymously?

Serial.print("Temperature: ");
Serial.println(temp);

conveys a LOT more information.

However, it looks like your real problem is that you are somehow causing the not-an-Arduino to crash. THAT is a hardware issue, most likely. Post a schematic.

The ESP is resetting. There are some gotchas when programming the ESP versus Arduinno. One is that your loop function can't take too long or the WDT (Watch Dog Timer) will reset the ESP (Soft WDT reset). Your for loops are probably taking too long. A solution is to not block (for loops block) while moving the servo. This page shows how. You need to keep the loop() time under 50mS, as I understand it, to avoid problems.

groundfungus:
The ESP is resetting. There are some gotchas when programming the ESP versus Arduinno. One is that your loop function can't take too long or the WDT (Watch Dog Timer) will reset the ESP (Soft WDT reset). Your for loops are probably taking too long. A solution is to not block (for loops block) while moving the servo. This page shows how. You need to keep the loop() time under 50mS, as I understand it, to avoid problems.

thanks, i did finally manage to find some info in the interim about blocking code and the esp8266 wanting to be reset happy but can't say i know well enough how to write it better yet. thank you

Like others have said, its a watchdog reset.

Mind if i ask why you have a delay(2000); in there?

For a quick/dirty fix, you could try breaking that delay into two and throw a yield in the middle (but it really is quick'n'dirty).

delay(1000);
yield();
delay(1000);

scrumfled:
Like others have said, its a watchdog reset.

Mind if i ask why you have a delay(2000); in there?

For a quick/dirty fix, you could try breaking that delay into two and throw a yield in the middle (but it really is quick'n'dirty).

delay(1000);

yield();
delay(1000);

i guess the idea was i didnt need it rerunning quite so often. temp isnt going to change drastically that often. eventually i would only have it check every few minutes. i'll just remove it completely for now and see if it changes

My bad. It is not the servo loops but the infinite while that is causing the WDT reset. I did more reading to refresh my memory and the delay calls inside the for loops should give the ESP time to do the wifi stuff that it needs to do.

https://github.com/adafruit/ESP8266-Arduino

Remember that there is a lot of code that needs to run on the chip besides the sketch when WiFi is connected. WiFi and TCP/IP libraries get a chance to handle any pending events each time the loop() function completes, OR when delay(...) is called. If you have a loop somewhere in your sketch that takes a lot of time (>50ms) without calling delay(), you might consider adding a call to delay function to keep the WiFi stack running smoothly.

There is also a yield() function which is equivalent to delay(0). The delayMicroseconds function, on the other hand, does not yield to other tasks, so using it for delays more than 20 milliseconds is not recommended.

Still should try to make the code as non blocking as possible, I think.

groundfungus:
My bad. It is not the servo loops but the infinite while that is causing the WDT reset. I did more reading to refresh my memory and the delay calls inside the for loops should give the ESP time to do the wifi stuff that it needs to do.

https://github.com/adafruit/ESP8266-Arduino

Still should try to make the code as non blocking as possible, I think.

it would be simple if i wanted to just write a value to the servo but i want it to move slightly slowly rather than all at once.

thanks, i'll see if i can figure out a better way to write this.

The link that I posted in reply #1 actually shows how to move a servo like you want.

groundfungus:
The link that I posted in reply #1 actually shows how to move a servo like you want.

oh, whoops. thanks. i had clicked on it and it was open in another tab but i couldn’t remember how i got there
:zipper_mouth_face:

Im betting its that delay(2000). If removing that doesnt fix it, try throwing a "yield()" into the middle of your while loops.

try throwing a "yield()" into the middle of your while loops.

Instead of fixing them so that they are not infinite loops? Why?

I never mentioned "instead". but if he wanted a quick diagnostic for where the problem lay......

after thinking about it for a bit i came up with this and it seems to be working significantly better.

#include <Servo.h> 
int sensorPin = A0; //reading temperature sensor on analog pin 0
int sensorValue;
int temp;
int sensorRaw;
int setpoint = 78;
int variance;
int pos = 90; //initial servo position
Servo myservo;  // create servo object to control a servo 
int moved = 2;
void setup() 
{ 
  Serial.begin(9600);
  myservo.write(pos); // sets the initial position
  myservo.attach(13);

} 

void loop() {
sensorValue = analogRead(sensorPin);
sensorRaw = analogRead(sensorPin);
temp = (sensorRaw-424)/6.25;
temp = (temp*1.8)+32;
variance = (temp - setpoint);
delay(1000);
Serial.println("Temperature");
Serial.println(temp);
Serial.println("Variance");
Serial.println(variance);
Serial.println("Move variable");
Serial.println(moved);
Serial.println("Servo Position");
Serial.println(pos);
delay(1000);
if ((variance > 0) && (moved != 0))
{
  for(pos; pos <= 170; pos += 1) // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);
  }
  moved = 0;
}
else if ( (variance < 0) && (moved != 1))
{
   for(pos; pos >= 10; pos -= 1) // goes from 0 degrees to 180 degrees 
   {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);
   }
  moved = 1;
}
}