Hello,
I have made an Android app for controlling two devices connected to an Arduino : led and fan. At first, I have tried to see if the app works using the LED 13 on the Arduino board. It worked, at my commands sent from the app, the arduino was controlling on/off the devices. The initial code was :
#include <OneWire.h>
#include <DallasTemperature.h>
#define pinSenzorLumina A0
#define pinSenzorPrezenta A1
#define ledPin 13
#define ONE_WIRE_BUS 4
//const int ledPin = 13;
// led pin
unsigned int temperatura;
unsigned int lumina;
unsigned int prezenta;
int state = 0;
// if state is 1, the LED will turn on and
// if state is 0, the LED will turn off
int flag = 0; // a flag to prevent duplicate messages
int cmd = -1;
String inputString = "";
boolean stringComplete = false;
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(115200);
digitalWrite(ledPin, HIGH);
sensors.begin();
}
void loop() {
//status LED
if (inputString.startsWith("1")) {
int ledStatus = digitalRead(13);
Serial.println(ledStatus);
//status cooler
}
else if (inputString.startsWith("2")) {
int coolerStatus = digitalRead(13);
Serial.println(coolerStatus);
//start LED
}
else if (inputString.startsWith("31")) {
digitalWrite(ledPin, HIGH);
//stop LED
}
else if (inputString.startsWith("30")) {
digitalWrite(ledPin, LOW);
//start cooler
}
else if (inputString.startsWith("41")) {
digitalWrite(ledPin, HIGH);
//stop cooler
}
else if (inputString.startsWith("40")) {
digitalWrite(ledPin, LOW);
//INTRUDER
}
else if (inputString.startsWith("8")) {
int miscare = analogRead(A0);
if (miscare > 150) {
Serial.println("1");
}
else {
Serial.println("0");
}
}
Serial.flush();
sensors.requestTemperatures();
temperatura = sensors.getTempCByIndex(0);
lumina = analogRead(pinSenzorLumina);
prezenta = analogRead(A0);
inputString = "";
stringComplete = false;
}
void serialEvent() {
while (Serial.available()) {
char inChar = (char)Serial.read();
inputString += inChar;
if (inChar == '\n' || inChar == '\r') {
stringComplete = true;
}
}
}
After that, I've written another code, setting the inputs/outputs for my sensors and output devices. I have also wanted to implement the concept of home automation, by using a photoresistor and a temperature sensor. Infortunately, after I have uploaded the new code to Arduino, the sistem doesn't work, neither the automatisation part, neither the controll by app. I'm new at this, so if you could give me an advice regarding the code or possibile problems, maybe with the Bluetooth Module.
I have tested using serial monitor and the only command processed is turning the LED on and display the status of the devices - for the fan, the system reads it as if it was ON, but the fan is not activated.
The after code is :
#include <OneWire.h>
#include <DallasTemperature.h>
#define pinSenzorLumina A2
#define pinSenzorPrezenta A1
#define pinSenzorTemperatura 4
#define pinVentilator A0
#define pinLed 13
#define ONE_WIRE_BUS 4
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
boolean stareVentilator;
int state = 0;
// if state is 1, the LED will turn on and if state is 0, the LED will turn off
int flag = 0; // a flag to prevent duplicate messages
int cmd = -1;
String inputString ="";
boolean stringComplete = false;
void setup() {
pinMode(pinLed, OUTPUT);
pinMode (pinVentilator, OUTPUT);
digitalWrite (pinVentilator, LOW);
digitalWrite (pinLed, LOW);
//Pentru comunicare prin bluetooth TREBUIE 115200
Serial.begin(115200);
// Serial.begin(9600);
sensors.begin();
}
void loop() {
//status lLED
if (inputString.startsWith("1")){
int ledStatus = digitalRead(13);
Serial.println(ledStatus);
//status cooler
} else if (inputString.startsWith("2")){
int coolerStatus = digitalRead(A0);
Serial.println(coolerStatus);
//start led
} else if (inputString.startsWith("31")) {
digitalWrite(pinLed, HIGH);
//stop led
} else if (inputString.startsWith("30")) {
digitalWrite(pinLed, LOW);
//start cooler
} else if (inputString.startsWith("41")) {
digitalWrite(pinVentilator, HIGH);
//stop cooler
} else if (inputString.startsWith("40")) {
digitalWrite(pinVentilator, LOW);
//trimitere status miscare
} else if (inputString.startsWith("8")) {
int miscare = analogRead(A0);
if (miscare > 150){
Serial.println("1");
}
else {
Serial.println("0");
}
}
Serial.flush();
sensors.requestTemperatures();
int lumina = analogRead(A2);
if (lumina < 1){
digitalWrite(pinLed, HIGH); }
else {
digitalWrite(pinLed, LOW);}
int temperatura = sensors.getTempCByIndex(0);
stareVentilator=false;
if(temperatura < 0 && stareVentilator){
digitalWrite(pinVentilator, LOW);
stareVentilator = false;
}
if(temperatura > 0 && !stareVentilator){
digitalWrite(pinVentilator, HIGH);
stareVentilator = true;
}
inputString ="";
stringComplete = false;
}
void serialEvent() {
while (Serial.available()) {
char inChar = (char)Serial.read();
inputString += inChar;
if (inChar == '\n' || inChar == '\r') {
stringComplete = true;
}
}
}
Any ideas or opinions are welcomed. Thank you!