Hello,
I want to use a PID control on a NodeMCU (Basically ESP8266) therefore I use the Library: Arduino PID Library by Brett Beauregard and use Blynk as an interface. Now I have the problem that it connects and disconnect all the time from Blynk, so I guess the nodeMCU crashes all the time. It is not the case if the "myPID.Compute();" is commented out.
It would nice to get it to work and thanks for your help!
My code:
#define BLYNK_PRINT Serial // Lib für die Android App
#include <max6675.h> // Lib für das Thermoelement
#include <ESP8266WiFi.h> // Lib für das Wlan Modul
#include <BlynkSimpleEsp8266.h> // Lib für die Android App
#include <Servo.h> // Lib für den Servo
//#include <WiFiClient.h> // Lib für ThingSpeak
//#include <ThingSpeak.h> // Lib für ThingSpeak
#include <PID_v1.h>
BlynkTimer timer;
Servo Servo1;
Servo Servo2;
Servo Servo3;
Servo Servo4;
WidgetLCD lcd(V0);
// Thermocouple Pinbelegung
int ktcCLK = D5;
int ktcSO = D7;
int ktcCS1 = D4; // Signalübermittlung
int ktcCS2 = D6; // Signalübermittlung
int ktcCS3 = D8; // Signalübermittlung
MAX6675 ktc1(ktcCLK, ktcCS1, ktcSO);
MAX6675 ktc2(ktcCLK, ktcCS2, ktcSO);
MAX6675 ktc3(ktcCLK, ktcCS3, ktcSO);
// Blynk sign in key
char auth[] = "...";
// WiFi Network
// char ssid[] = "...";
// char pass[] = "...";
char ssid[] = "...";
char pass[] = "...";
// ThingSpeak sign in keys
WiFiClient client;
//unsigned long myChannelNumber = ...;
//const char* server = "api.thingspeak.com";
//const char * myWriteAPIKey = "...";
// Variables
int ControlType = 0; // 0=Manuel und 1=PID
int SetServo1;
int SetServo2;
int SetServo3;
int SetServo4;
float temp1;
float temp2;
float temp3;
// PID Variable
double Setpoint=120;
double Input;
double PID_Set_Value;
double KP=2, KI=5, KD=1;
PID myPID(&Input, &PID_Set_Value, &Setpoint, KP, KI, KD, DIRECT);
//ThingSpeak Variables
//unsigned long previousMillis = 0; // Wird immer erhöht um Interval_2
//unsigned long interval_1 = 15000; // Sendet erste Thermoelement
//unsigned long interval_2 = 30000; // Sendet zweites Thermoelement
//boolean data_state = true;
void setup() {
Serial.begin(9600); // Für die Thermoelemente
Blynk.begin(auth, ssid, pass); // Starte Blynk
timer.setInterval(2000L, readTC);
// ThingSpeak.begin(client); // Starte ThingSpeak
Servo1.attach(D0);
Servo2.attach(D1);
Servo3.attach(D2);
Servo4.attach(D3);
Blynk.virtualWrite(V10, Setpoint); //Init SetTemp
Blynk.virtualWrite(V11, KP); //Init KP
Blynk.virtualWrite(V12, KI); //Init KI
Blynk.virtualWrite(V13, KD); //Init KD
//turn the PID on
myPID.SetMode(AUTOMATIC);
delay(1000);
}
// Main Loop ------------------------------------------------------------------------------------------------------------------------------------------------------
void loop()
{
Blynk.run(); // Keeping Blynk run
timer.run(); // Initiates BlynkTimer
// Servos entsprechend Blynk einstellen
// 0=Manuel und 1=PID
if (ControlType == 0) // Manuel
{
Servo1.write(SetServo1);
Servo2.write(SetServo2);
Servo3.write(SetServo3);
Servo4.write(SetServo4);
}
if(ControlType == 1) // PID
{
// PID
Input = temp1;
myPID.Compute();
//Blynk.virtualWrite(V14, KD);
Blynk.virtualWrite(V15, PID_Set_Value);
if (PID_Set_Value<=65)
{
Servo1.write(PID_Set_Value+115);
Servo2.write(117);
Servo3.write(117);
Servo4.write(117);
}
if (65<PID_Set_Value<=130)
{
Servo1.write(180);
Servo2.write(PID_Set_Value+115-65);
Servo3.write(117);
Servo4.write(117);
}
if (130<PID_Set_Value<=195)
{
Servo1.write(180);
Servo2.write(180);
Servo3.write(PID_Set_Value+115-2*65);
Servo4.write(117);
}
if (195<PID_Set_Value)
{
Servo1.write(180);
Servo2.write(180);
Servo3.write(180);
Servo4.write(PID_Set_Value+115-3*65);
}
}
// Daten an ThingSpeak übertragen
// if ((millis() - previousMillis > interval_1) && (data_state))
// {
// ThingSpeak.writeField(myChannelNumber, 1, temp1, myWriteAPIKey);
// data_state = false;
// }
// if (millis() - previousMillis > interval_2)
// {
// previousMillis = millis(); // aktuelle Zeit abspeichern
// ThingSpeak.writeField(myChannelNumber, 2, temp2, myWriteAPIKey);
// data_state = true;
//}
}
// Functions ------------------------------------------------------------------------------------------------------------------------------------------------------
void readTC()
{
temp1 = ktc1.readCelsius();
temp2 = ktc2.readCelsius();
temp3 = ktc3.readCelsius();
Blynk.virtualWrite(V7, temp1); //Schreibt temp1 auf V7 zur Ausgabe in der App
Blynk.virtualWrite(V8, temp2);
Blynk.virtualWrite(V9, temp3);
lcd.print(4, 0, (int)temp1);
lcd.print(4, 1, (int)temp2);
lcd.print(13, 0, (int)temp3);
lcd.print(0, 0, "TC1:");
lcd.print(0, 1, "TC2:");
lcd.print(9, 0, "TC3:");
}
BLYNK_WRITE(V1)
{
// Schreibt V1 als int in SetServo1
SetServo1 = param.asInt();
}
BLYNK_WRITE(V2)
{
SetServo2 = param.asInt();
}
BLYNK_WRITE(V3)
{
SetServo3 = param.asInt();
}
BLYNK_WRITE(V4)
{
SetServo4 = param.asInt();
}
BLYNK_WRITE(V5)
{
ControlType = param.asInt();
}
BLYNK_WRITE(V6)
{
Setpoint = param.asInt();
}
BLYNK_WRITE(V7)
{
KP = param.asInt();
}
BLYNK_WRITE(V8)
{
KI = param.asInt();
}
BLYNK_WRITE(V9)
{
KD = param.asInt();
}