Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <PID_AutoTune_v0.h>
- #include <ESP8266WiFi.h>
- #include <math.h>
- #include <PID_v1.h>
- #include <PID_AutoTune_v0.h>
- //Define analog
- #define THERMISTORPIN A0
- //Define PWM
- #define PWMPIN D1
- //Define portsselector of MUX
- #define selectorA D8
- #define selectorB D7
- #define selectorC D6
- // resistance at 25 degrees C
- #define THERMISTORNOMINAL 47000
- // temp. for nominal resistance (almost always 25 C)
- #define TEMPERATURENOMINAL 25
- // how many samples to take and average, more takes longer
- // but is more 'smooth'
- #define NUMSAMPLES 5
- // The beta coefficient of the thermistor (usually 3000-4000)
- #define BCOEFFICIENT 4450
- // the value of the 'other' resistor
- #define SERIESRESISTOR 47000
- // The port to listen for incoming TCP connections
- #define LISTEN_PORT 80
- // Create an instance of the server on Port 80
- WiFiServer server(LISTEN_PORT);
- // global Variables
- const char* ssid = "GothamCity";
- const char* password = "1888054937563387";
- unsigned long ulReqcount;
- unsigned long ulReconncount;
- int samples[NUMSAMPLES];
- double Setpoint, temperatur, Output;
- boolean tuning = false;
- double kp = 2, ki = 0.5, kd = 2;
- double aTuneStep = 50, aTuneNoise = 1, aTuneStartValue = 100;
- unsigned int aTuneLookBack = 20;
- byte ATuneModeRemember = 2;
- //Specify the links and initial tuning parameters
- PID myPID(&temperatur, &Output, &Setpoint, kp, ki, kd, DIRECT);
- PID_ATune aTune(&temperatur, &Output);
- void setup(void)
- {
- // setup globals
- ulReqcount = 0;
- ulReconncount = 0;
- digitalWrite(selectorA, 0);
- digitalWrite(selectorB, 0);
- digitalWrite(selectorC, 0);
- //Set up the PID
- myPID.SetMode(AUTOMATIC);
- if (tuning)
- {
- tuning = false;
- changeAutoTune();
- tuning = true;
- }
- // start serial
- Serial.begin(9600);
- delay(1);
- // inital connect
- WiFi.mode(WIFI_STA);
- WiFiStart();
- server.begin();
- Serial.println("HTTP server started");
- }
- void WiFiStart()
- {
- ulReconncount++;
- // Connect to WiFi network
- Serial.println();
- Serial.println();
- Serial.print("Connecting to ");
- Serial.println(ssid);
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED)
- {
- delay(1000);
- Serial.print(".");
- }
- Serial.println("");
- Serial.println("WiFi connected");
- // Start the server
- server.begin();
- Serial.println("Server started");
- // Print the IP address
- Serial.println(WiFi.localIP());
- }
- void changeAutoTune()
- {
- if (!tuning)
- {
- //Set the output to the desired starting frequency.
- Output = aTuneStartValue;
- aTune.SetNoiseBand(aTuneNoise);
- aTune.SetOutputStep(aTuneStep);
- aTune.SetLookbackSec((int)aTuneLookBack);
- AutoTuneHelper(true);
- tuning = true;
- }
- else
- { //cancel autotune
- aTune.Cancel();
- tuning = false;
- AutoTuneHelper(false);
- }
- }
- void AutoTuneHelper(boolean start)
- {
- if (start)
- ATuneModeRemember = myPID.GetMode();
- else
- myPID.SetMode(ATuneModeRemember);
- }
- float average()
- {
- uint8_t i;
- float average = 0;
- // take N samples in a row, with a slight delay
- for (i = 0; i < NUMSAMPLES; i++)
- {
- samples[i] = analogRead(THERMISTORPIN);
- delay(1000);
- }
- // average all the samples out
- for (i = 0; i < NUMSAMPLES; i++)
- {
- average += samples[i];
- }
- average /= NUMSAMPLES;
- return average;
- }
- float resistance(float resist)
- {
- // convert the value to resistance
- resist = 1023 / resist - 1;
- resist = SERIESRESISTOR / resist;
- return resist;
- }
- float steinhart(float durch)
- {
- float steinhart = 0;
- steinhart = durch / THERMISTORNOMINAL; // (R/Ro)
- steinhart = log(steinhart); // ln(R/Ro)
- steinhart /= BCOEFFICIENT; // 1/B * ln(R/Ro)
- steinhart += 1.0 / (TEMPERATURENOMINAL + 273.15); // + (1/To)
- steinhart = 1.0 / steinhart; // Invert
- steinhart -= 273.15; // convert to C
- return steinhart;
- }
- void loop()
- {
- float durchschnitt = average();
- float watt = resistance(durchschnitt);
- temperatur = steinhart(watt);
- if (tuning)
- {
- byte val = (aTune.Runtime());
- if (val != 0)
- {
- tuning = false;
- }
- if (!tuning)
- { //we're done, set the tuning parameters
- kp = aTune.GetKp();
- ki = aTune.GetKi();
- kd = aTune.GetKd();
- myPID.SetTunings(kp, ki, kd);
- AutoTuneHelper(false);
- }
- }
- else myPID.Compute();
- Serial.print("Durchschnittlicher Wert an A0: ");
- Serial.println(durchschnitt);
- Serial.print("Thermistor Widerstand: ");
- Serial.println(watt);
- Serial.print("Temperatur: ");
- Serial.print(temperatur);
- Serial.println(" *C");
- //Serial.print("t0.txt=");
- //Serial.write(temperatur);
- Serial.print("Autotune= ");
- Serial.println(tuning);
- Serial.print("PID Output: ");
- Serial.println(Output);
- delay(1000);
- // check if WLAN is connected
- if (WiFi.status() != WL_CONNECTED)
- {
- WiFiStart();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment