Skip to content

Instantly share code, notes, and snippets.

@Miceuz
Created January 2, 2020 17:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Miceuz/1b54f8d1cd999e03bba54481c54fd34a to your computer and use it in GitHub Desktop.
Save Miceuz/1b54f8d1cd999e03bba54481c54fd34a to your computer and use it in GitHub Desktop.
#include <PID_v1.h>
#include <Hash.h>
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <ArduinoOTA.h>
#include <Bounce2.h>
//AP definitions
#define AP_SSID ""
#define AP_PASSWORD ""
const int channelID = 454580;
String writeAPIKey = ""; // write API key for your ThingSpeak Channel
const char* server = "api.thingspeak.com";
#define ONE_WIRE_BUS D9 // DS18B20 pin
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature DS18B20(&oneWire);
WiFiClient client;
void wifiConnect();
//void postToThingspeak(float temp1, float temp2, float temp3);
double temp1, temp2, temp3, setpoint, output;
double Kp=0.5 , Ki=0, Kd=0;
PID myPID(&temp2, &output, &setpoint, Kp, Ki, Kd, REVERSE);
#define LED_BLUE D8
#define LED_RED D7
#define SWITCH D6
#define FAN D2
Bounce sw = Bounce();
void setup() {
setpoint = 35;
pinMode(FAN, OUTPUT);
digitalWrite(FAN, LOW);
pinMode(LED_BLUE, OUTPUT);
pinMode(LED_RED, OUTPUT);
pinMode(SWITCH, INPUT);
digitalWrite(LED_BLUE, LOW);
digitalWrite(LED_RED, LOW);
digitalWrite(FAN, HIGH);
delay(1000);
digitalWrite(FAN, LOW);
digitalWrite(LED_BLUE, HIGH);
digitalWrite(LED_RED, HIGH);
Serial.begin(115200);
wifiConnect();
myPID.SetMode(AUTOMATIC);
myPID.SetSampleTime(5000);
sw.attach(SWITCH);
sw.interval(50);
}
void loop2() {
if(WiFi.status() == WL_CONNECTED) {
digitalWrite(LED_BLUE, LOW);
} else {
digitalWrite(LED_BLUE, HIGH);
}
if(LOW == digitalRead(SWITCH)) {
digitalWrite(LED_RED, LOW);
} else {
digitalWrite(LED_RED, HIGH);
}
}
uint32_t lastPostTs = 0;
uint8_t pin2[][8] = {
{0x28, 0xFF, 0x82, 0x8E, 0x81, 0x16, 0x03, 0xA1},
{0x28, 0xFF, 0xCA, 0x76, 0x81, 0x16, 0x04, 0x98},
{0x28, 0xFF, 0x76, 0x6D, 0x81, 0x16, 0x03, 0xD9},
};
uint32_t lastServoTs = 0;
uint16_t fanLevel = 64;
bool postSuccess=true;
void loop() {
myPID.Compute();
sw.update();
if(WiFi.status() == WL_CONNECTED && postSuccess) {
digitalWrite(LED_BLUE, LOW);
} else {
digitalWrite(LED_BLUE, HIGH);
ESP.reset();
}
do {
DS18B20.requestTemperatures();
temp1 = DS18B20.getTempCByIndex(0);
temp2 = DS18B20.getTempCByIndex(1);
temp3 = DS18B20.getTempCByIndex(2);
} while (temp1 == 85.0 || temp1 == (-127.0));
Serial.print("Temperature 1: ");
Serial.println(temp1);
Serial.print("Temperature 2: ");
Serial.println(temp2);
Serial.print("Temperature 3: ");
Serial.println(temp3);
if(millis() - lastPostTs > 5000) {
postSuccess = postToThingspeak(temp1, temp2, temp3);
Serial.println(output);
lastPostTs = millis();
}
if(HIGH == sw.read()){
digitalWrite(LED_RED, LOW);
if(temp2 > temp3) {
analogWrite(FAN, 64);
} else {
digitalWrite(FAN, LOW);
}
} else {
digitalWrite(LED_RED, HIGH);
digitalWrite(FAN, LOW);
}
if(sw.rose()){
digitalWrite(LED_RED, LOW);
digitalWrite(FAN, HIGH);
delay(3000);
fanLevel=64;
analogWrite(FAN, fanLevel);
}
ArduinoOTA.handle();
}
void wifiConnect() {
Serial.print("Connecting to AP");
WiFi.mode(WIFI_STA);
WiFi.begin(AP_SSID, AP_PASSWORD);
uint8_t retries = 0;
while (WiFi.status() != WL_CONNECTED && retries < 20) {
digitalWrite(LED_BLUE, LOW);
delay(500);
digitalWrite(LED_BLUE, HIGH);
delay(500);
Serial.print(".");
retries++;
}
if(retries > 19) {
ESP.reset();
}
Serial.println("");
Serial.println("WiFi connected");
// Port defaults to 8266
ArduinoOTA.setPort(8266);
// Hostname defaults to esp8266-[ChipID]
ArduinoOTA.setHostname("langas");
// No authentication by default
ArduinoOTA.setPassword("admin");
// Password can be set with it's md5 value as well
// MD5(admin) = 21232f297a57a5a743894a0e4a801fc3
// ArduinoOTA.setPasswordHash("21232f297a57a5a743894a0e4a801fc3");
ArduinoOTA.onStart([]() {
String type;
if (ArduinoOTA.getCommand() == U_FLASH)
type = "sketch";
else // U_SPIFFS
type = "filesystem";
// NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()
Serial.println("Start updating " + type);
});
ArduinoOTA.onEnd([]() {
Serial.println("\nEnd");
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
});
ArduinoOTA.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
else if (error == OTA_END_ERROR) Serial.println("End Failed");
});
ArduinoOTA.begin();
}
bool postToThingspeak(float temp1, float temp2, float temp3) {
int conn = client.connect(server, 80);
if (conn) {
// Construct API request body
String body = "field1=";
body += String(temp1);
body += "&field2=";
body += String(temp2);
body += "&field3=";
body += String(temp3);
body += "&field4=";
body += String((int) (output*1023));
client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: " + writeAPIKey + "\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(body.length());
client.print("\n\n");
client.print(body);
client.print("\n\n");
}
client.stop();
return conn != 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment