Sie sind auf Seite 1von 5

#include <SoftwareSerial.

h> //Software Serial Port


#define RxD 7
#define TxD 6
#define KEY 5
SoftwareSerial SoftSerial(RxD,TxD);
#define debugline Serial // Port for debugging
#define dataline SoftSerial // Port for communication with BT
//#define debugline SoftSerial // Port for debugging
//#define dataline Serial // Port for communication with BT

#include "dht11.h"
#define DHT11PIN 2
dht11 DHT11;
void setup()
{
debugline.begin(9600);
dataline.begin(38400); //38400 for commands
pinMode(RxD, INPUT);
pinMode(TxD, OUTPUT);
pinMode(KEY, OUTPUT);

delay(2000);

// inquire_HC05();
// configure_HC05();
debugline.println("Setup finished... Key LOW"); //For debugging, Comme
nt this line if not required
digitalWrite(KEY, LOW); // Disable commands
dataline.println("R!"); //For debugging, Comment this line if not requ
ired

}
int read_sensor(){
int chk = DHT11.read(DHT11PIN);
int error = 0;
debugline.print("Read sensor: ");
switch (chk)
{
case DHTLIB_OK:
debugline.println("OK");
break;
case DHTLIB_ERROR_CHECKSUM:
debugline.println("Checksum error");
error = 1;
break;
case DHTLIB_ERROR_TIMEOUT:
debugline.println("Time out error");
error = 2;
break;
default:
debugline.println("Unknown error");
error = 3;
break;
}
debugline.print("Humidity (%): ");
debugline.println((float)DHT11.humidity, 2);
debugline.print("Temperature (C): ");
debugline.println((float)DHT11.temperature, 2);
dataline.print("Humidity (%): ");
dataline.println((float)DHT11.humidity, 2);
dataline.print("Temperature (C): ");
dataline.println((float)DHT11.temperature, 2);
dataline.println ("OK");
return error;
}
void echo_dataline()
{
byte item;
if (dataline.available()) {
item = dataline.read();
// if (item == 255) {
// return
// }
dataline.write(item);
debugline.print(item);
}
}
String readline()
{
String output;
byte key = 0;
while(key!=13)
{
if (dataline.available()){
key = dataline.read();
if (key != 13) {
debugline.print(key);
output += (char)key;
}
}
}
debugline.println(output);
return output;
}
int split (String input, char delim, String &first, String &rest){
int pos = input.indexOf(delim);
if (pos == -1) {
first = input;
return 1;
}
first = input.substring(0,pos);
rest = input.substring(pos+1);
return 0;
}
void accept_commands(){
String comm,params;
String line = readline();
split(line,' ',comm,params);
debugline.println ("Command: "+comm+" Params: "+params);
if (comm == "TEMP") {
//dataline.println ("Temperature: 23 C");
read_sensor();
return;
}
if (comm == "PING") {
dataline.println ("PONG");
dataline.println ("OK");
return;
}

debugline.println("ERROR(0): Line not recognized: "+line);
dataline.println("ERROR(0)");
}
void loop()
{
// echo_dataline();
accept_commands();
}

void inquire_HC05()
{
debugline.println("Setting up Bluetooth Command mode. Key HIGH, wait 10 sec.
.."); //For debugging, Comment this line if not required
digitalWrite(KEY, HIGH); // Disable commands
//TODO Here it would be nice to do a reset
delay(10000);
debugline.println("Querying BT..."); //For debugging, Comment this lin
e if not required
sendBlueToothCommand("at");
sendBlueToothCommand("at");
sendBlueToothCommand("AT+VERSION?");
sendBlueToothCommand("AT+ADDR?");
sendBlueToothCommand("AT+NAME?");
sendBlueToothCommand("AT+ROLE?");
sendBlueToothCommand("AT+CLASS?");
sendBlueToothCommand("AT+UART?");
sendBlueToothCommand("AT+MRAD?");
sendBlueToothCommand("AT+STATE?");
sendBlueToothCommand("AT+PSWD?");
}
void configure_HC05()
{
debugline.println("Configuring BT..."); //For debugging, Comment this
line if not required
sendBlueToothCommand("AT+NAME=HG");
sendBlueToothCommand("AT+UART=38400,0,0");
sendBlueToothCommand("AT+PSWD=7707");
delay(2000); // This delay is required.
debugline.println("Setup complete, resetting BT...");
digitalWrite(KEY, LOW); // Disable commands
sendBlueToothCommand("AT+RESET");
}

void sendBlueToothCommand(char command[])
{
char buffer[50];
debugline.println(command);
dataline.print(command);
dataline.print("\r\n");
delay(1000);

// Display output
int i=0;
while(dataline.available())
{
buffer[i] = (char) dataline.read();
i++;
}
buffer[i]=0; //eol
debugline.println(buffer);
}
/// OLD STUFF
void echo_dataline_old()
{
char buffer[50];
int i=0;
while(dataline.available())
{
buffer[i] = dataline.read();
i++;
}
buffer[i]=0; //eol
dataline.print(buffer);
debugline.print(buffer);
}
char * readline_old()
{
char buffer[100];
byte key = 0;
int i=0;
while(key!=13)
{
if (dataline.available()){
key = dataline.read();
debugline.print(key);
buffer[i]=(char)key;
i++;
}
}
buffer[i]=0; //eol
debugline.println(buffer);
return buffer;
}
Status API Training Shop Blog About 2014 GitHub, Inc. Terms Privacy Security Con
tact

Das könnte Ihnen auch gefallen