The servo portion of this sketch worked fine before all the ESP8266 added in. There was a thermistor getting a temp reading, that is now replaced by a 0 or 1 value being read from ThingSpeak.
If I comment out line 59 and 63 the sketch loops fine. It pulls the data from thingspeak just fine.
Obviously moving that servo is the whole point of the sketch. I can't figure out why it isn't looping.
The point of the MoveTo is to detach the servo, and only attach it if the mbrVint value changes state.
This part worked fine on another sketch that had alot of other code in it doing a thermistor temp reading and averaging.
When the sketch starts, the servo does attach, moves to the MBRclosePos (mbrVint is 0), stays attached for 1000ms, then detaches. However the sketch doesn't loop after that as it should. Serial output stops.
ESP8266 Code below that is freezing. I've stripped out as much as possible of unnecessary stuff and still able to replicate the problem.
// D0=16, D1=5, D2=4, D3=0, D4=2, D5=14, D6=12, D7=13, D8=15
#include <ThingSpeak.h>
#include <ESP8266WiFi.h>
#include <Servo.h>
const char* ssid = "NOTYOURS";
const char* password = "12345678";
WiFiClient client;
// Master Bedroom Stuff
Servo servo1;
const int servo1Pin = 13; //Master Bedroom - D1
const int MBRopenPos = 0;
const int MBRclosePos = 180;
unsigned long myChannelNumber = 123456;
const char * myReadAPIKey = "APIKEYHERE";
String mbrV = "";
void MoveTo(int position) {
static int lastPosition = -1;
if (position != lastPosition) { // Position changing
servo1.attach(servo1Pin, 785, 2180); // Set limits of pulse width in microseconds
servo1.write(position);
delay(1000); // Give the servo time to move to the new position
servo1.detach(); // Disconnect servo to reduce currrent load and servo life
lastPosition = position;
}
}
void setup() {
Serial.begin(115200);
delay(10);
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
int wifi_ctr = 0;
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("WiFi connected");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP());
ThingSpeak.begin(client);
}
void loop() {
mbrV = ThingSpeak.readFloatField(myChannelNumber, 4, myReadAPIKey);
int mbrVint = mbrV.toInt();
Serial.print("Master Bedroom Valve Status: ");
if (mbrVint >= 1) {
Serial.println("Open");
MoveTo(MBRopenPos);
}
if (mbrVint <= 0) {
Serial.println("Closed");
MoveTo(MBRclosePos);
}
}
Here is the serial output:
Connecting to NOTYOURS
...WiFi connected
IP Address: 192.168.0.30
Master Bedroom Valve Status: Closed