Skip to content

Instantly share code, notes, and snippets.

@OlleMattsson
Created January 18, 2022 15:08
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 OlleMattsson/f199aff045fdca6c5c52712a9d85611c to your computer and use it in GitHub Desktop.
Save OlleMattsson/f199aff045fdca6c5c52712a9d85611c to your computer and use it in GitHub Desktop.
smartled
// CONFIGS
const char *ssid = "";
const char *password = "";
const float TEMP_MAX = 70;
const float TEMP_SHUTDOWN = 80;
/*
NO RESISTOR
*/
const int LED_OFF = 255;
const int LED_MIN = 230;
const int LED_MED = 165;
const int LED_MAX = 100;
/*
temps (fans on)
140: < 40
100: 45-50
50: 50-55
*/
/*
10k resistor
max power temp approaches less than 40C (no fans), less than 30C with fans on
const int LED_OFF = 255;
const int LED_MIN = 100;
const int LED_MED = 50;
const int LED_MAX = 0;
*/
// WIFI
#include <ESP8266WiFi.h>
// DS18B20
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS D1 // Data wire For Temp Probes
OneWire oneWire(ONE_WIRE_BUS);// Setup a oneWire instance to communicate with any OneWire devices
DallasTemperature sensors(&oneWire);// Pass our oneWire reference to Dallas Temperature.
int numberOfDevices; // Number of temperature devices found
DeviceAddress tempDeviceAddress;
float temperature = 0;
// FAN
#define FanPin D2
#define FAN_MIN 50
#define FAN_MIN_TRESHHOLD 22.00
#define FAN_MED 128
#define FAN_MED_TRESHHOLD 23.00
#define FAN_MAX 255
#define FAN_MAX_TRESHHOLD 25.00
int pot_pin = A0;
int c1 = 0; // declares variable c1
int c2 = 0; // declares variable c2
#define LED_PMW_PIN D5
#define LED_IO_PIN D6
String RXdata = "";
String ledState = "off";
String ledPower = "min";
void setup() {
Serial.begin(9600);
/*
// WIFI
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.println("");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
*/
// DS18B20
sensors.begin();
numberOfDevices = sensors.getDeviceCount();
// locate devices on the bus
Serial.print("Locating devices...");
Serial.print("Found ");
Serial.print(numberOfDevices, DEC);
Serial.println(" devices.");
// Loop through each device, print out address
for(int i=0;i<numberOfDevices; i++) {
// Search the wire for address
if(sensors.getAddress(tempDeviceAddress, i)) {
Serial.print("Found device ");
Serial.print(i, DEC);
Serial.print(" with address: ");
printAddress(tempDeviceAddress);
Serial.println();
} else {
Serial.print("Found ghost device at ");
Serial.print(i, DEC);
Serial.print(" but could not detect address. Check power and cabling");
}
}
/*
DeviceAddress temperatureSensorAddress;
sensors.getAddress(temperatureSensorAddress, 0);
Serial.print("Found device ");
Serial.print(i, DEC);
Serial.print(" with address: ");
Serial.println(tempDeviceAddress)
Serial.println();
printAddress(tempDeviceAddress);
Serial.println();
*/
// FAN
pinMode(FanPin, OUTPUT);
analogWrite(FanPin, 0);
analogWriteRange(255); // sets range fromn 0 - 255
analogWriteFreq(10); // eliminates fan whine
// pot for led
pinMode(pot_pin, INPUT);
// turn LED off
pinMode(LED_IO_PIN, OUTPUT);
digitalWrite(LED_IO_PIN, LOW);
// led pmw
pinMode(LED_PMW_PIN, OUTPUT);
analogWrite(LED_PMW_PIN, LED_MIN);
}
void loop() {
Serial.println("=========");
// TEMP
sensors.requestTemperatures();// Send the command to get temperatures
numberOfDevices = sensors.getDeviceCount();
for(int i=0;i<numberOfDevices; i++) {
if(sensors.getAddress(tempDeviceAddress, i)) {
temperature = sensors.getTempC(tempDeviceAddress);
Serial.print(" Temp: ");
Serial.print(temperature);
Serial.println(" C");
}
}
/*
// LED RELAY I/O
Serial.print("LED: ");
Serial.println(ledState);
*/
Serial.print("LED Power: ");
Serial.println(ledPower);
if(Serial.available() > 0){ //check if any data was received
RXdata = Serial.readString();
Serial.println(RXdata);
if (RXdata == "led_on") {
ledState = "on";
digitalWrite(LED_IO_PIN, HIGH);
}
if (RXdata == "led_off") {
ledState = "off";
digitalWrite(LED_IO_PIN, LOW);
}
if (RXdata == "led_min") {
ledPower = "min";
analogWrite(LED_PMW_PIN, LED_MIN);
}
if (RXdata == "led_med") {
ledPower = "med";
analogWrite(LED_PMW_PIN, LED_MED);
}
if (RXdata == "led_max") {
ledPower = "max";
analogWrite(LED_PMW_PIN, LED_MAX);
}
if (RXdata == "led_man") {
ledPower = "man";
}
if (RXdata == "led_off") {
ledPower = "off";
analogWrite(LED_PMW_PIN, LED_OFF);
}
}
/*
if (sensors.getAddress(tempDeviceAddress, 0)) {
temperature = sensors.getTempC(tempDeviceAddress);
Serial.print("Temp C: ");
Serial.println(temperature);
} else {
Serial.println("no temp for device: ");
printAddress(tempDeviceAddress);
}
*/
analogWrite(FanPin, FAN_MAX);
/*
// FAN
if (temperature < FAN_MIN_TRESHHOLD){
analogWrite(FanPin, 0);
Serial.println("off");
}
// 0 -> MED (MID is never actually used =)
if (temperature >= FAN_MIN_TRESHHOLD && temperature < FAN_MED_TRESHHOLD){
analogWrite(FanPin, FAN_MIN);
Serial.println("low");
}
// med < temp < max
if (temperature >= FAN_MED_TRESHHOLD && temperature < FAN_MAX_TRESHHOLD){
analogWrite(FanPin, FAN_MED);
Serial.println("med");
}
// temp > max
if (temperature >= FAN_MAX_TRESHHOLD ){
analogWrite(FanPin, FAN_MAX);
Serial.println("high");
}
*/
// LED PWM
/*
lower voltage -> higher output
Led Brigthness = 1/pwm
When ground is is disconnected, led output is maxed
Adding 10k resistor between DIM+ and GRND lowers the maximum brightness of the led
=> higher resistance between DIM+ and DIM- -> lower output
When arduino is turned off, brightness is minimized
LED_LOW: 230
LED_HIGH: 100 (reasonable max value for now. Let's check temps at this level before allowing higher levels)
*/
if (ledPower == "man") {
c2= analogRead(pot_pin);
c1= (1024-c2)/4; // subtracts c2 from 1000 ans saves the result in c1
Serial.print("POT: ");
Serial.println(c1);
analogWrite(LED_PMW_PIN, c1);
}
delay(100);
}
// function to print a device address
void printAddress(DeviceAddress deviceAddress) {
for (uint8_t i = 0; i < 8; i++) {
if (deviceAddress[i] < 16) Serial.print("0");
Serial.print(deviceAddress[i], HEX);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment