Smart Sockets with Android App

I am working on Smart Socket Project.

Idea is to control this extension with spark core. Replace push buttons with Momentary Touch buttons to ON or OFF manually. Every Single socket is connect with 5v Relay.

With this we can control extension manually and control from any where in the work and get status is any socket is ON or OFF.

For Status when Android app start it get status of sockets with “digitalread”. In this case we only get status update at start up. but after that we are not be able to get update if any one change status any socket manually.

in this case we can use “spark.publish” to get real time socket status either ON or OFF.

Code I use for Core is


int tinkerDigitalRead(String pin);
const int buttonPin1 = D0; 
const int buttonPin2 = D1; 
const int buttonPin3 = D2; 
const int ledPin1 = D5;
const int ledPin2 = D6;
const int ledPin3 = D7;

static bool pressed1 = false;
static bool pressed2 = false;
static bool pressed3 = false;

int ledState1 = HIGH; 
int lastButtonState1 = LOW;

int ledState2 = HIGH; 
int lastButtonState2 = LOW;

int ledState3 = HIGH; 
int lastButtonState3 = LOW;

unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;

void setup() 
{
  pinMode(buttonPin1, INPUT_PULLUP);
  pinMode(buttonPin2, INPUT_PULLUP);
  pinMode(buttonPin3, INPUT_PULLUP);
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  digitalWrite(ledPin1, ledState1);
  digitalWrite(ledPin2, ledState2);
  digitalWrite(ledPin3, ledState3);
  Spark.function("digitalread", tinkerDigitalRead);
  Spark.function("toggleLed1", toggleLed1);
  Spark.function("toggleLed2", toggleLed2);
  Spark.function("toggleLed3", toggleLed3);
}

void loop() 
{
    
  int button_state1 = digitalRead(A0);
     if(button_state1 == HIGH && pressed1 == false)
    {
        Spark.publish("bs1", "ON1");
        pressed1 = true;
        delay(500);
    }
    else if(button_state1 == LOW && pressed1 == true)
    {
        Spark.publish("bs1", "OFF1");
        pressed1 = false;
    }
 
int button_state2 = digitalRead(A1);
     if(button_state2 == HIGH && pressed2 == false)
    {
        Spark.publish("bs2", "ON2");
        pressed2 = true;
        delay(500);
    }
    else if(button_state2 == LOW && pressed2 == true)
    {
        Spark.publish("bs2", "OFF2");
        pressed2 = false;
    }
 int button_state3 = digitalRead(A2);
     if(button_state3 == HIGH && pressed3 == false)
    {
        Spark.publish("bs3", "ON3");
        pressed3 = true;
        delay(500);
    }
    else if(button_state3 == LOW && pressed3 == true)
    {
        Spark.publish("bs3", "OFF3");
        pressed3 = false;
    }  
  // this block is your run-of-the-mill pin state change with a debounce
  int buttonState1 = digitalRead(buttonPin1);
  if ((buttonState1 != lastButtonState1) && (millis() - lastDebounceTime > debounceDelay))
  {
    if (buttonState1 == LOW)
    {
      Serial.println("Pressed!!!");
      ledState1 = !ledState1; // Toggle the led wth the button press
      lastDebounceTime = millis();
    }
  }
  lastButtonState1 = buttonState1;
  // you can leave this here.... 
  digitalWrite(ledPin1, ledState1);
  
  
  int buttonState2 = digitalRead(buttonPin2);
  if ((buttonState2 != lastButtonState2) && (millis() - lastDebounceTime > debounceDelay))
  {
    if (buttonState2 == LOW)
    {
      Serial.println("Pressed!!!");
      ledState2 = !ledState2; // Toggle the led wth the button press
      lastDebounceTime = millis();
    }
  }
  lastButtonState2 = buttonState2;
  // you can leave this here.... 
  digitalWrite(ledPin2, ledState2);
  
  
  int buttonState3 = digitalRead(buttonPin3);
  if ((buttonState3 != lastButtonState3) && (millis() - lastDebounceTime > debounceDelay))
  {
    if (buttonState3 == LOW)
    {
      Serial.println("Pressed!!!");
      ledState3 = !ledState3; // Toggle the led wth the button press
      lastDebounceTime = millis();
    }
  }
  lastButtonState3 = buttonState3;
  // you can leave this here.... 
  digitalWrite(ledPin3, ledState3);
}
// add this block to control via curl
// * To turn on/off
// * curl -k https://api.spark.io/v1/devices/<yourSparkID>/toggleLed -d access_token=<yourAccessToken> -d params=<0 or 1>  // or send a '2' and Toggle the led
//
int toggleLed1(String command)
{
  int value = command.charAt(0) - '0';
  if (value == 0 || value == 1)
  {
      ledState1 = value;
      return value;
  }
  else if (value == 2)
  {
    ledState1 = !ledState1;
    return ledState1;
  }
  else return -1;
}


int toggleLed2(String command)
{
  int value = command.charAt(0) - '0';
  if (value == 0 || value == 1)
  {
      ledState2 = value;
      return value;
  }
  else if (value == 2)
  {
    ledState2 = !ledState2;
    return ledState2;
  }
  else return -1;
}

int toggleLed3(String command)
{
  int value = command.charAt(0) - '0';
  if (value == 0 || value == 1)
  {
      ledState3 = value;
      return value;
  }
  else if (value == 2)
  {
    ledState3 = !ledState3;
    return ledState3;
  }
  else return -1;
}

int tinkerDigitalRead(String pin)
{
	int pinNumber = pin.charAt(1) - '0';
	if (pinNumber< 0 || pinNumber >7) return -1;
	if(pin.startsWith("D"))
	{
		pinMode(pinNumber, INPUT_PULLDOWN);
		return digitalRead(pinNumber);
	}
	else if (pin.startsWith("A"))
	{
		pinMode(pinNumber+10, INPUT_PULLDOWN);
		return digitalRead(pinNumber+10);
	}
	return -2;
}

Kindly help me to improve this code, I am not a programmer that’s why code looks like this but this code is working fine.

I want to make 4 to 5 extension like this and control all of them with android app.

One thing that can be improved definetly, is to put all the functionality that only differs in the respective socket/LED out into a function that takes the socket/LED number as a parameter rather than having multiple copies of virtually the same code.

This improves scalability, readability and reduces maintenance work. It also frees up some Spark.function slots.

For this I’d also add a button to the mobile app to trigger “digitalread” manually.

ScruffR brother can you modify code for me it will help me a lot

Although @ScruffR is really nice, he shouldn’t be writing your code for you. You’ve asked for help, and he helped you in the form of suggestions. Try to implement those yourself. If you can’t figure it out, let us know and we’ll see what we can do to help you out.
I’m not saying this because we’re lazy or anything. We’re doing this in our own spare time, and if we had to fix everyone’s code, there’d be no time left for other things. Simply writing the code for you is even worse, since you won’t learn anything, and will be back in no time with new ideas. You’d be much better of learning new things step by step, than you are by skipping it and having someone do it for you. You can accomplish one project by having someone write it for you. You can accomplish an infinite number of project by learning how to do it yourself :wink: It also gives much more satisfaction knowing something you made yourself actually works.

So try to implement @ScruffR’s ideas, and let us know if you get stuck (with specific questions, not:“fix my code” ;))

1 Like

Moors7 you are right brother :). I can try to fix code otherwise I do let you know.

2 Likes

Are you able to address this issue?? If yes how are you reading the status if it is changed manually??

BTW its a nice project.

You can use Spark.publish() to publish a state change. You can subscribe to those in your app to receive updates. That’ll probably only work as long as your app is active.
If your app is not active, you can initiate a digitalRead() upon start, and/or manually with a button.

1 Like

That is the BIG Question in Home Automation!!!
Man say: yes gimme that gadget,
Women say, allright but… I don’t want any changes to MY normal life.
They are right. Think of a Philips HUE bulb, don’t work if she turns the wall switch off…
Unless we completely rethink the way we build our electricity net in our home, total home automation is not possible. Says the gray old expert…

You are absolutely right. I was trying to control a bulb from spark by using a relay. I also wanted it to be controlled from a physical switch. So the solution I came up with was to use a 3 way switch along with a SPDT relay.

But the problem was if its turned off from the switch there is no way to know the state.

Is there any solution for this problem??

Hi @bijay

You could of course sense the current on the hot side of the relay (the pole) with a small current transformer. Then you would know if there was current flowing or not.

1 Like

Have you actually read @Moors7 's or my own reply further up?

If you place the Core “directly” (without additional circuit breakers in between) at the load (e.g. your light bulb) - as the OP project suggests - that “problem” will be defused slightly, and the above suggestions might become feasable again.


And as for the issue of the wall switch, the easy solution is to replace the “toggle” switch with an N/C momentary switch and things are settled again :wink:
One push to the switch will reset the home auto devices and in a fews secs you’re ready to go again.
You can even preset the initial state (after power loss/regain) in software.

##CAUTION: Maintenance of electric devices will call for extra measures to off the power and prevent accidental on-switching!##

1 Like

If there was an easy solution @bijay , this world would be a much easier place to be :grinning:

Solution: disconnect all wiring to the switch. connect the switch between a digital io pin of the Spark and to ground. Read the pin to see in what position the switch is and act accordingly.
make shure to do a digitalWrite(pin,HIGH); to the input pin, see Spark Docs for explanation.

Or connect a light depending resistor to the spark, then it can see if the light is on :wink:

1 Like

bijay

We use publish for manual state change if PIN goes high or low spark publish its state and we get the real time position either ON or OFF.

1 Like

@marcus, I’d be interested in the reason for this suggestion.
If it’s meant to attach a pullup to the input pin, than this is the way it would be done on an AVR Arduino, but on the Core it’s achieved via pinMode(pin, INPUT_PULLUP) and the Core even provides a pinMode(pin, INPUT_PULLDOWN).

You are right; a typo, sorry guys, the inheritance of too many years with Arduino :wink:

1 Like