Skip to content

Instantly share code, notes, and snippets.

@sven-bock
Created September 18, 2018 18:53
Show Gist options
  • Save sven-bock/6de16a9b42c88004a4a4153111364a3e to your computer and use it in GitHub Desktop.
Save sven-bock/6de16a9b42c88004a4a4153111364a3e to your computer and use it in GitHub Desktop.
Code for a Wemos D1 mini to offer a capacitive touch, show the status on the LED and also read the temperature. Can also be used with a ENC28J60 module as all required pins are still free.
/*
* Code for a Wemos D1 mini to offer a capacitive touch, show the status on the LED and also read the temperature. Can also be used with a ENC28J60 module as all required pins are still free.
*/
////Temperature sensor
#include "DHT.h"
#define DHTPIN D3 //GPIO 0
#define DHTTYPE DHT11
#define INTERVAL_PUBLISHING 5000
DHT dht(DHTPIN, DHTTYPE, 15);
////LED
#define LED_PIN 3 //RX PIN - Use a 10kOhm resistor to GND - open to upload
#define LED_COUNT 1
#define MAX_BRIGHTNESS 100
#include <Adafruit_NeoPixel.h>
#include "WS2812_Definitions.h"
byte last_value;
Adafruit_NeoPixel* leds;
//Capacitive touch sensor
#define MIN_VALUE 80
#define MAX_VALUE 300
#define CAP_SENSOR_EMITTER_PIN D1 //GPIO 5
#define CAP_SENSOR_RECEIVER_PIN D2 // GPIO 4
#include <CapacitiveSensor.h>
// 300kOhm resistor between pins D1 & D2, pin D2 is sensor pin, add a wire and or foil
CapacitiveSensor cs_4_6 = CapacitiveSensor(CAP_SENSOR_EMITTER_PIN, CAP_SENSOR_RECEIVER_PIN);
float res = 0.0;
long cur_time;
long previousDHT;
void setup() {
Serial.begin(74880);
delay(4000); //give ESP8266 a bit time to startup
dht.begin();
pinMode(LED_PIN, OUTPUT);
leds = new Adafruit_NeoPixel(LED_COUNT, LED_PIN, NEO_GRB + NEO_KHZ800);
leds->begin();
last_value = 0;
leds->setPixelColor(0, MAX_BRIGHTNESS, 0, 0);
leds->show();
delay(2000);
cur_time = previousDHT = millis();
Serial.println("led color set");
}
void loop() {
cur_time = millis();
res = (float)(4 * res / 5 + cs_4_6.capacitiveSensor(30) / 5);
float value = _max(_min(MAX_BRIGHTNESS, (res - MIN_VALUE) * MAX_BRIGHTNESS / MAX_VALUE), 0.0);
//Serial.print(res);
//Serial.print("\t");
//Serial.println(value);
if (abs((byte)value - last_value) > 5) {
leds->setPixelColor(0, MAX_BRIGHTNESS - (byte)value, (byte)value, 0);
last_value = (byte)value;
leds->show();
}
if (cur_time - previousDHT > INTERVAL_PUBLISHING) {
previousDHT = cur_time;
float t = dht.readTemperature();
Serial.println(t);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment