I now have it looping, until the mbrVint changes values. It completes the servo move, then freezes. I'm SOOOO close I can smell it!!!
// 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;
const unsigned long SampleInterval = 500;
// 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 = "*************";
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() {
static unsigned long lastSampleTime = 0;
unsigned long currentTime = millis();
if (currentTime - lastSampleTime >= SampleInterval) {
lastSampleTime = currentTime;
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);
}
}
}