Created
November 11, 2015 08:38
-
-
Save gabonator/0004a3a35a1dfa16f7cd to your computer and use it in GitHub Desktop.
DHT22 over bluetooth with timers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SoftwareSerial.h> | |
#include <TimerOne.h> | |
#include <DHT.h> | |
// Deklaracia softwaroveho serioveho portu a pinoch 2 a 3 | |
SoftwareSerial btSerial(2, 3); // RX, TX | |
// Premenne sekundoveho casovaca | |
volatile int nSecondsPassed = 0; | |
int nLastSecond = 0; | |
// Pouzity senzor je DHT22 a pripojeny je na pin 10 | |
DHT sensorDht(10, DHT22); | |
void dhtInit () | |
{ | |
// DHT senzor na pinoch 11..8. pricom: | |
// pin8: sluzi na napajanie senzora, GND | |
// pin9: nepouziva sa | |
// pin10: komunikacny pin | |
// pin11: sluzi na napajanie senzora, VCC | |
pinMode(8, OUTPUT); | |
digitalWrite(8, LOW); | |
pinMode(9, INPUT); | |
pinMode(10, INPUT); | |
pinMode(11, OUTPUT); | |
digitalWrite(11, HIGH); | |
} | |
void setup() | |
{ | |
// Inicializacia HW seriovej linky pre debugovacie ucely | |
Serial.begin(9600); | |
while (!Serial); | |
// Inicializacia SW seriovej linky pre komunikaciu cez BT | |
btSerial.begin(9600); | |
// Nakonfigurujeme casovac tak, aby sa funkcia | |
// prerusenia volala s frekevnciou 1Hz | |
int nFreq = 1; | |
Timer1.initialize(1000000UL / nFreq); | |
Timer1.attachInterrupt(onTimerIsr); | |
// Inicializacia senzora DHT22 | |
dhtInit(); | |
} | |
// Funkcia prerusenia by mala byt vykonana co najrychlejsie, | |
// aby sme zbytocne nenarusovali chod hlavnej programovej slucky | |
// loop(), iba si poznacime ze presla sekunda | |
void onTimerIsr() | |
{ | |
nSecondsPassed++; | |
} | |
// Tato funkcia je volana z hlavnej programovej slucky kazdu sekundu | |
void onTimer() | |
{ | |
// Do seriovej linky vypiseme spravu "Send..." | |
Serial.print("Send...\r\n"); | |
// Dalsie udaje posielame cez bluetooth | |
btSerial.print("Preslo"); | |
btSerial.print(nSecondsPassed); | |
btSerial.print(" sekund!"); | |
// vypis hodnot zo senzora | |
btSerial.print(" dht:"); | |
float h = sensorDht.readHumidity(); | |
float t = sensorDht.readTemperature(); | |
if ( isnan(h) || isnan(t) ) | |
{ | |
// ak sa nepodari nacitat data z DHT22 (napr. ak nie je pripojeny) | |
// vypiseme "dht:error" | |
btSerial.print("error"); | |
} else | |
{ | |
// ak sa podari nacitat data, odosleme ich vo formate: | |
// "temp=23.4 humidity=50.3" | |
btSerial.print("temp="); | |
btSerial.print(t); | |
btSerial.print(" humidity="); | |
btSerial.print(h); | |
} | |
btSerial.print("\r\n"); | |
} | |
// Hlavna programova slucka | |
void loop() | |
{ | |
// Presla uz sekunda? | |
if ( nLastSecond != nSecondsPassed ) | |
{ | |
nLastSecond = nSecondsPassed; | |
onTimer(); | |
} | |
// Presmerovanie prijatych dat z BT do debugovacieho vystupu, | |
// prijate data mozeme sledovat v Serial Console Arduina | |
while (btSerial.available()) | |
Serial.write(btSerial.read()); | |
// Presmerovanie prijatych dat zo Serial Console na bluetooth | |
if (Serial.available()) | |
btSerial.write(Serial.read()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment