Lighting control

Hello,

I started a project for lighting control with arduino by means of a bluetooth modem. Center of the system is Seeeduino Mega based on Atmega 1280 with HC-07 bluetooth modem and two Betemcu relay boards (one with four and other with eight relays). I have successfully tested communication between a Android based phone (with Bluetooth SPP Pro installed on it) and Arduino. I have also been successful in sending an ASCII command interpreting it and getting the corresponding relay to react on that command.

I have written most of the code, but have not tested it yet (it is commented out) because I have not identified the pins I can use. I have identified the Bluetooth TX/RX pins and one digital out. I need 10 more digital outs and one pwm out. I hope that documentation of the board will help me identify the pins I can use.

I needed a solution that would recall last setting of the lighting and found possible solution in EEPROMex.h; there have I also found the first unsolvable problem. I have added the library folder (v9.1) to coresponding folder. I have also created three one byte variables to hold all the needed data; first byte containing states of relays on one relay board, second for data on another relay board and the third pwm value for dimmer. I have encountered the problem while trying to compile:

update(address, byte);

I will add complete code at the end, so if someone can point out an error I will appreciate it; or if someone can suggest alternative location for storing those three bytes, other then EEPROM? I am not concerned much with EEPROM number of r/w cycles (I rate the changes to max of 100 a day, on average not more then 40; with 100K at my disposal it gives me five, six years of memory use, and after that I can insert another arduino board).

I had also some other problems, one of them with Bluetooth. I found out that when I first power up a board I can make a BT connection, but the board does not understand (or does not execute) over BT sent commands. After Arduino h/w reset everything works fine. Same thing happens when still connected phone gets outside of modem reach. Same solution. I have tried to just restart the modem, but it does not help. Is there any way to reset parts of the board (registers, timers, something else) to further diagnose the problem?

Another question is also connected, but not directly. I intend to use Android phone for testing purposes only. After finalizing I intend to use a web server on Raspberry PI and a web site to control the lighting from any device using web interface. Could you suggest a web technology that can initiate bluetooth connection under linux? I have found some Java solutions , but as I do not know anything of Java I would prefer some other technology, php would be nice.

Thank you in advance,
Sasa

I have removed some parts of the code, because it was to long for the forum massage:

#include <SoftwareSerial.h>
#include <EEPROMex.h>
#include <EEPROMVar.h>

int bluetoothTx = 11;
int bluetoothRx = 12;
int Rel_01 = 3; //proveriti pin
//int Rel_02
//int Rel_03
//int Rel_04
//int Rel_11
//int Rel_12
//int Rel_13
//int Rel_14
//int Rel_15
//int Rel_16
//int Rel_17
//int Rel_18
//int PWM
int Rel3 = 8;
String btRxStr = "";
int PWMvrednost;
int EEPROMaddress = 0;

//kodne reci

String Code_Relej_01 = "LedIzvor";
String Code_Relej_02 = "Cfl120";
String Code_Relej_03 = "Cfl60";

String Code_Relej_11 = "Led01";
String Code_Relej_12 = "Led02";
String Code_Relej_13 = "Led03";
String Code_Relej_14 = "Led04";
String Code_Relej_15 = "Led05";
String Code_Relej_16 = "Led06";

String Code_pwm10plus = "pwm10plus";
String Code_pwm10minus = "pwm10minus";
String Code_pwm1plus = "pwm1plus";
String Code_pwm1minus = "pwm1minus";
String Code_pwmmax = "pwmmax";
String Code_pwmmin = "pwmmin";
String Code_pwmmedium = "pwmmedium";


//bajtovi za memorisanje

byte Rel_0 = B0;
byte Rel_1 = B0;
byte PWMbyte = B0;

SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);

void setup()
{
  //Setup usb serial connection to computer
  Serial.begin(9600);

  //Setup Bluetooth serial connection to android
  bluetooth.begin(115200);
  bluetooth.print("$$");
  delay(100);
  bluetooth.println("U,9600,N");
  bluetooth.begin(9600);
  pinMode (Rel_01, OUTPUT);
  //pinMode (Rel_02, OUTPUT);
  //pinMode (Rel_03, OUTPUT);
  //pinMode (Rel_04, OUTPUT);
  //pinMode (Rel_11, OUTPUT);
  //pinMode (Rel_12, OUTPUT);
  //pinMode (Rel_13, OUTPUT);
  //pinMode (Rel_14, OUTPUT);
  //pinMode (Rel_15, OUTPUT);
  //pinMode (Rel_16, OUTPUT);
  //pinMode (PWM, OUTPUT);
  pinMode (Rel3, OUTPUT);
}

void loop()
{
  
  
//pocetno stanje
  Rel_0 = EEPROM.read(EEPROMaddress);
  Rel_1 = EEPROM.read(EEPROMaddress + 1);
  PWMbyte = EEPROM.read(EEPROMaddress + 2);
   


  //Read from bluetooth and write to usb serial
  if(bluetooth.available())
  {
    char toSend = (char)bluetooth.read();
    Serial.print(toSend);
    btRxStr.concat(toSend);
  } 

/*
  
//Provera za Rel_01

  if (btRxStr == Code_Relej_01)
  {
    digitalWrite(Rel_01, !digitalRead(Rel_01));
    btRxStr = "";
    if (digitalRead(Rel_01) == 1)
    {
      bluetooth.print("Iskljucen LED drajver \n");
    }
    else if (digitalRead(Rel_01) == 0)
    {
      bluetooth.print("Ukljucen LED drajver \n");
    }
    bitWrite(Rel_0, 0, digitalRead(Rel_01));
    delay(10);
  }
  
//Provera za Rel_16 

  else if (btRxStr == Code_Relej_16)
  {
    digitalWrite(Rel_16, !digitalRead(Rel_16));
    btRxStr = "";
    if (digitalRead(Rel_16) == 1)
    {
      bluetooth.print("Iskljucena LED06 \n");
    }
    else if (digitalRead(Rel_16) == 0)
    {
      bluetooth.print("Ukljucena LED06 \n");
    } 
    bitWrite(Rel_1, 5, digitalRead(Rel_16)); //trajna memorija drugi bajt
    delay(10);
  }

//Provera za PWM10plus

  else if (btRxStr == Code_pwm10plus)
  {
    PWMvrednost = analogRead(PWM) / 4;
    if (PWMvrednost >= 0) && (PWMvrednost <= 225)
    {
      PWMvrednost = PWMvrednost + 30;
      analogWrite(PWM, PWMvrednost);
      bluetooth.print("Vrednost PWM " + PWMvrednost + " \n");
    }
    else if (PWMvrednost > 225)
    {
      PWMvrednost = 255;
      analogWrite(PWM, PWMvrednost);
      bluetooth.print("Maksinalna vrednost PWM " + PWMvrednost + "\n");
    }
    PWMbyte = byte(PWMvrednost);
    btRxStr = "";
    delay(10);
  }
  
//Provera za PWM1plus

  else if (btRxStr == Code_pwm1plus)
  {
    PWMvrednost = analogRead(PWM) / 4;
    if (PWMvrednost >= 0) && (PWMvrednost <= 252)
    {
      PWMvrednost = PWMvrednost + 3;
      analogWrite(PWM, PWMvrednost);
      bluetooth.print("Vrednost PWM " + PWMvrednost + " \n");
    }
    else if (PWMvrednost > 252)
    {
      PWMvrednost = 255;
      analogWrite(PWM, PWMvrednost);
      bluetooth.print("Maksinalna vrednost PWM " + PWMvrednost + " \n");
    }
    PWMbyte = byte(PWMvrednost);
    btRxStr = "";
    delay(10);
  }
  
//Provera za PWM10minus

  else if (btRxStr == Code_pwm10minus)
  {
    PWMvrednost = analogRead(PWM) / 4;
    if (PWMvrednost >= 30) && (PWMvrednost <= 255)
    {
      PWMvrednost = PWMvrednost - 30;
      analogWrite(PWM, PWMvrednost);
      bluetooth.print("Vrednost PWM " + PWMvrednost + " \n");
    }
    else if (PWMvrednost < 30)
    {
      PWMvrednost = 0;
      analogWrite(PWM, PWMvrednost);
      bluetooth.print("Minimalna vrednost PWM " + PWMvrednost + " \n");
    }
    PWMbyte = byte(PWMvrednost);
    btRxStr = "";
    delay(10);
  }

//Provera za PWM1minus

  else if (btRxStr == Code_pwm1minus)
  {
    PWMvrednost = analogRead(PWM) / 4;
    if (PWMvrednost >= 3) && (PWMvrednost <= 255)
    {
      PWMvrednost = PWMvrednost - 3;
      analogWrite(PWM, PWMvrednost);
      bluetooth.print("Vrednost PWM " + PWMvrednost + " \n");
    }
    else if (PWMvrednost < 3)
    {
      PWMvrednost = 0;
      analogWrite(PWM, PWMvrednost);
      bluetooth.print("Minimalna vrednost PWM " + PWMvrednost + "\n");
    }
    PWMbyte = byte(PWMvrednost);
    btRxStr = "";
    delay(10);
  }

//Provera za PWMmax

  else if (btRxStr == Code_pwmmax)
  {
    PWMvrednost = analogRead(PWM) / 4;
    if (PWMvrednost < 240)
    {
      PWMvrednost = 240;
      analogWrite(PWM, PWMvrednost);
      bluetooth.print("Vrednost PWM " + PWMvrednost + " \n");
    }
    PWMbyte = byte(PWMvrednost);
    btRxStr = "";
    delay(10);
  }

//Provera za PWMmin

  else if (btRxStr == Code_pwmmin)
  {
    PWMvrednost = analogRead(PWM) / 4;
    if (PWMvrednost > 15)
    {
      PWMvrednost = 15;
      analogWrite(PWM, PWMvrednost);
      bluetooth.print("Vrednost PWM " + PWMvrednost + " \n");
    }
    PWMbyte = byte(PWMvrednost);
    btRxStr = "";
    delay(10);
  }

//Provera za PWMmedium

  else if (btRxStr == Code_pwmmedium)
  {
    PWMvrednost = analogRead(PWM) / 4;
    PWMvrednost = 127;
    analogWrite(PWM, PWMvrednost);
    bluetooth.print("Vrednost PWM " + PWMvrednost + " \n");
    
    PWMbyte = byte(PWMvrednost);
    btRxStr = "";
    delay(10);
  }
  
*/
  
  if (btRxStr == "Relej3")  
  {
    digitalWrite(Rel3, !digitalRead(Rel3));
    btRxStr = "";
    if (digitalRead(Rel3) == 1)
    {
      bluetooth.print("Iskljucen relej 3 \n");
    }
    else if (digitalRead(Rel3) == 0)
    {
      bluetooth.print("ukljucen relej 3 \n");
    }
  }
  else if (btRxStr == "RelejDrajver")
  {
    digitalWrite(Rel_01, !digitalRead(Rel_01));
    btRxStr = "";
    if (digitalRead(Rel_01) == 1)
    {
      bluetooth.print("Ukljucen LED drajver \n");
    }
    else if (digitalRead(Rel_01) == 0)
    {
      bluetooth.print("Iskljucen LED drajver \n");
    }
  }
  
  //Pamcenje parametara
  
  update(EEPROMaddress, Rel_0);
  update((EEPROMaddress + 1), Rel_1);
  update((EEPROMaddress + 2), PWMbyte);    
  
  //Read from usb serial to bluetooth
  if(Serial.available())
  {
    char toSend = (char)Serial.read();
    bluetooth.print(toSend);
  }
  //digitalWrite(Rel3, !digitalRead(Rel3));
}

As an alternative to your current approach you might like to look at www.pfod.com.au in particular
http://www.forward.com.au/pfod/AndroidControllerLightsAndPower/index.html

which has a complete project. I have another one I am working on that stores the current state in EEPROM so the relays come back on it the same state when power is lost and restored. Email me if you need any help adapting the pfod project to your needs. (Another user adapted it to remote control unlocking/opening his car doors using 12 relays)
You can use the A0 to A5 pins for digital as well as the D pins
I have tried using HC bluetooth modules and gave up. I only use RN modules now, much more reliable. I know of people who successfully use HC modules, I just cannot be bothered with the getting them running.

pfod also has support for non-english character sets built in.
pfod will also connect via wifi (with 128 bit security) this mode might be easier to connect via from your computer. The app is an Android app (for wifi and bluetoot), but the message format is very simple so if you can program in php you should be able to get a simple menu system running on your computer. I am a Java programmer myself. Not much help to you for php.

matthew

Here is a great app for relay control. www.bluetoothbtmod.com

hope this helps