Added DHT22 sensor

This commit is contained in:
Eric Teunis de Boone 2017-02-26 04:13:16 +01:00
parent ec91729896
commit b231ada9dc
12 changed files with 482 additions and 170 deletions

95
mpduino/layout.ino Normal file
View file

@ -0,0 +1,95 @@
/*
Define one function to control all of the layout.
It works per line with only one variable (tick).
This one gives a way of scrolling through text.
*/
#define FLASH_LINE_TICKS 3
void initial_layout() {
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0, 8);
display.println("Waiting..");
display.display();
}
void layout( unsigned int tick ) {
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setTextWrap(false);
if ( playing || tick % FLASH_LINE_TICKS ) {
display.setCursor(0, 0);
// First line (20)
if (volume == 100 || volume == 0){
if (volume == 100) display.print('00');
else display.print('--');
}
else display.print(volume);
display.print(F("% "));
display.print(round(temperature));
display.print(F("C "));
display.print(round(humidity));
display.print(F("% |"));
if (repeat_bool) display.print('r');
else display.print(' ');
if (random_bool) display.print('x');
else display.print(' ');
if (single_bool) display.print('s');
else display.print(' ');
if (consume_bool) display.print('c');
else display.print(' ');
if (shuffle_bool) display.print('z');
else display.print(' ');
if (updating_bool) display.print('u');
else display.print(' ');
}
//line 2
display.setCursor(0, 8);
display.print(textscroll(title,tick));
display.startscrollleft(0x06, 0x0F);
//line 3
display.setCursor(0, 16);
display.print(textscroll(artist, tick));
//line 4
display.setCursor(0, 24);
display.print(textscroll(album, tick));
display.display();
}
String textscroll(String text, unsigned int tick) {
int chars = 21;
int len = text.length();
String newstring = "";
if ( len < chars) {
for ( int i = len; i < chars -1; i+=2){
text = " "+text+" ";
}
if ( text.length() == chars - 1) text = text+" ";
return text;
}
text = text + " - ";
for ( int i = 0; i < chars; i++ )
newstring += text.charAt((2*tick + i) % (len+3));
return newstring;
}
int calcMaxTicks() {
maxticks = title.length() * artist.length() * album.length();
Serial.print("MT:");
Serial.println(maxticks);
return maxticks;
}

134
mpduino/mpduino.ino Normal file
View file

@ -0,0 +1,134 @@
/*
Project to interface a screen connected to an arduino with data from a vash script.
Used as start up for a HTPC.
*/
#define BAUD 9600
#define MAIN_DELAY 500
#define DHT_DELAY 10000
// STRING Commands
#define KEY_RESET 'R'
#define KEY_READ 'E'
#define KEY_ECHO '!'
#define KEY_SET 'S'
//MPD
#define KEY_VOLUME 'V'
#define KEY_TITLE 'T'
#define KEY_ARTIST 'A'
#define KEY_ALBUM 'a'
#define KEY_REPEAT 'r'
#define KEY_SHUFFLE 'z'
#define KEY_RANDOM 'x'
#define KEY_CONSUME 'c'
#define KEY_UPDATE 'u'
#define KEY_SINGLE 's'
#define KEY_PLAYING 'P'
//Sensors
#define KEY_DHT_HUMIDITY 'h'
#define KEY_DHT_TEMPERATURE 't'
// DHT
#include <DHT.h>
#define DHTPIN 7 // what pin we're connected to
#define DHTTYPE DHT22 // DHT 22 (AM2302)
DHT dht(DHTPIN, DHTTYPE); //// Initialize DHT sensor for normal 16mhz Arduino
// OLED thingies
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
//Functionality
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
#define NUMFLAKES 10
#define XPOS 0
#define YPOS 1
#define DELTAY 2
#define LOGO16_GLCD_HEIGHT 16
#define LOGO16_GLCD_WIDTH 16
static const unsigned char PROGMEM logo16_glcd_bmp[] =
{ B00000000, B11000000,
B00000001, B11000000,
B00000001, B11000000,
B00000011, B11100000,
B11110011, B11100000,
B11111110, B11111000,
B01111110, B11111111,
B00110011, B10011111,
B00011111, B11111100,
B00001101, B01110000,
B00011011, B10100000,
B00111111, B11100000,
B00111111, B11110000,
B01111100, B11110000,
B01110000, B01110000,
B00000000, B00110000 };
#if (SSD1306_LCDHEIGHT != 32)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif
// Vars
String inputString = ""; // a string to hold incoming data
boolean stringComplete = false; // whether the string is complete
boolean repeat_bool = false;
boolean shuffle_bool = false;
boolean consume_bool = false;
boolean random_bool = false;
boolean updating_bool = false;
boolean single_bool = false;
boolean playing = false;
int volume = 100;
String title = " << << Title >> >> ";
String artist = " << << Artist >> >> ";
String album = " << << Album >> >> ";
float humidity = 0;
float temperature = 0;
unsigned int tick = 0;
unsigned int maxticks = 0;
void setup() {
// initialise serial
Serial.begin(BAUD);
// by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3C (for the 128x32)
// init done
display.clearDisplay();
display.display();
delay(MAIN_DELAY);
initial_layout();
while(!Serial);
establishContact();
layout(tick);
calcMaxTicks();
}
void loop() {
// print the string when a newline arrives:
if (stringComplete) {
setFromSerial( inputString );
// clear the string:
inputString = "";
stringComplete = false;
}
layout(tick++);
update_sensor_readings(tick);
if ( tick > maxticks ) tick = 0;
delay(MAIN_DELAY);
}

29
mpduino/rot_encoder.ino Normal file
View file

@ -0,0 +1,29 @@
/* Functions to control playback */
void nextSong() {
}
void forward() {
}
void prevSong() {
}
void backward() {
}
void pause() {
}
void voldown() {
}
void volup() {
}

14
mpduino/sensors.ino Normal file
View file

@ -0,0 +1,14 @@
/*
Main function for sensors
*/
void update_sensor_readings( unsigned int tick ){
if ( tick % (DHT_DELAY / MAIN_DELAY) == 0)
{
humidity = dht.readHumidity();
temperature = dht.readTemperature();
writeDHT(humidity, temperature);
}
}

88
mpduino/serial.ino Normal file
View file

@ -0,0 +1,88 @@
/*
SerialEvent occurs whenever a new data comes in the
hardware serial RX. This routine is run between each
time loop() runs, so using delay inside loop can delay
response. Multiple bytes of data may be available.
*/
void serialEvent() {
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
inputString += inChar;
// if the incoming character is a newline, set a flag
// so the main loop can do something about it:
if (inChar == '\n') {
stringComplete = true;
}
}
}
void establishContact(){
while (Serial.available() <= 0) {
Serial.println(KEY_RESET); // send an initial string
delay(300);
}
Serial.println(KEY_RESET+KEY_RESET+KEY_RESET+KEY_RESET);
}
void setFromSerial (String inputString) {
//remove newline
inputString = inputString.substring(0, inputString.length() -1);
char operation1 = inputString.charAt(0);
String string = "";
if ( operation1 == KEY_READ ){
//Echo string like: EV means echo volume
switch ( inputString.charAt(1) ) {
case KEY_VOLUME: string = volume; break;
case KEY_TITLE: string = title; break;
case KEY_ARTIST: string = artist; break;
case KEY_ALBUM: string = album; break;
case KEY_REPEAT: string = repeat_bool; break;
case KEY_SHUFFLE: string = shuffle_bool; break;
case KEY_RANDOM: string = random_bool; break;
// case KEY_CONSUME: string = consume_bool; break;
// case KEY_UPDATE: string = updating_bool; break;
// case KEY_SINGLE: string = single_bool; break;
case KEY_PLAYING: string = playing; break;
case KEY_DHT_HUMIDITY: string = humidity; break;
case KEY_DHT_TEMPERATURE: string = temperature; break;
default: string = KEY_ECHO; break;
}
Serial.println(string);
}
else if ( operation1 == KEY_SET ){
bool success = true;
string = inputString.substring(2);
switch ( inputString.charAt(1) ) {
case KEY_VOLUME: volume = string.toInt(); break;
case KEY_TITLE: title = string; break;
case KEY_ARTIST: artist = string; break;
case KEY_ALBUM: album = string; break;
case KEY_REPEAT: repeat_bool = (string != '0'); break;
case KEY_SHUFFLE: shuffle_bool = (string != '0'); break;
case KEY_RANDOM: random_bool = (string != '0'); break;
// case KEY_CONSUME: consume_bool = (string != '0'); break;
// case KEY_UPDATE: updating_bool = (string != '0'); break;
// case KEY_SINGLE: single_bool = (string != '0'); break;
case KEY_PLAYING: playing = (string != '0'); break;
//Echo
case KEY_ECHO: Serial.println(string); break;
default: success = false; break;
}
calcMaxTicks();
Serial.println(int(success), DEC);
}
else Serial.println(-1, DEC);
}
// write to serial from arduino
void writeDHT(float humidity, float temperature){
Serial.println(KEY_DHT_HUMIDITY+String(humidity));
delay(100);
Serial.println(KEY_DHT_TEMPERATURE+String(temperature));
}