Sending relay state from photon to ubidots


#1

Hi everyone.

I’m trying to send relay state to ubidots, which is 1 when then relay is energized, and 0 when in relax state.
Sometimes ubidots gets the reading, but most of the time nothing happens.
Particle cloud however do get the notification as expected.

#include "Particle.h"
#include <blynk.h>

#define BLYNK_PRINT Serial 

#include <Ubidots.h>

#define TOKEN "ubidots token"
Ubidots ubidots(TOKEN);

char auth[] = "auth key";

char oldRelayState = D3;

// Register the LED to virtual pin 1.
WidgetLED ledWidget(V4);


void setup()
{
  //Set the LED pin as output.
  pinMode(D7,OUTPUT);

  //Set the Button 0 as input.
  pinMode(D3,INPUT);
  
  Serial.begin(115200);
  Blynk.begin(auth);
}

void loop()
{
  Blynk.run();
 char newRelayState = digitalRead(D3);
    if (newRelayState !=oldRelayState)
        {
        int relayState;
            if (newRelayState == HIGH)
                {
                ledWidget.on();
                Particle.publish("Pump No. 1 : ", "TRIP");
                relayState = 1;
                delay(500);
                }
                else
                {
                ledWidget.off();
                Particle.publish("Pump No. 1 : ", "OKAY");
                relayState = 0;
                delay(500);    
                }
            ubidots.add("Pump no. 1", relayState);
            ubidots.sendAll();
            relayState = 0;
        }
         oldRelayState = newRelayState;
        delay(1000);
}


#2

What is the point of this line “relayState = 0;” I only ask because once you leave that if statement relayState will no longer be in scope.
I would tend to write something like this if my code was that simple and the blocking didn’t matter like this, only because I hate the idea of repeated blocks of code when unnecessary.

void loop(){
  Blynk.run();
  char newRelayState = digitalRead(D3);
  if (newRelayState != oldRelayState){
    int relayState;
    ledWidget.setValue(newRelayState?255:0);
    Particle.publish("Pump No. 1 : ", newRelayState?"TRIP":"OKAY");
    delay(500);
    ubidots.add("Pump no. 1", newRelayState);
    ubidots.sendAll();
  }
  oldRelayState = newRelayState;
  delay(1000);
}

I don’t see why this would make any difference but maybe having the ubidots and publish together will help you track it down.


#3

Hello @fariqatrash :smiley:

I just review your code and I found out that your are assigning the ubidots variable label with specials characters as spaces and that is not allowed in the Ubidots side. Please, try assigning the variable label as is shown below:

ubidots.add("pump-no-1", relayState);
ubidots.sendAll();

Instead of:

ubidots.add("Pump no. 1", relayState);
ubidots.sendAll();

If you desire after posting data to Ubidots you can modify your variable name. To get a better idea about the difference between labels and names you can reference to this article.

Let me know how everything goes!

All the best,
Maria C.


#4

Thanks @hawesg & @mariahernandez

Taking advices from both of you, here’s my latest code. Working great!

void loop()
{
  
  Blynk.run();
  relay1();
  relay2();
  relay3();

}

void relay1()
{
      char newRelayState1 = digitalRead(D3);
  if (newRelayState1 != oldRelayState1){
    int relayState1;
    led1.setValue(newRelayState1?255:0);
    Particle.publish("Pump No. 1 : ", newRelayState1?"TRIP":"OKAY");
    delay(500);
    ubidots.add("Pump-No.-1", newRelayState1);
    ubidots.sendAll();
  }
  oldRelayState1 = newRelayState1;
  delay(1000);
}

void relay2()
{
      char newRelayState2 = digitalRead(D4);
  if (newRelayState2 != oldRelayState2){
    int relayState2;
    led2.setValue(newRelayState2?255:0);
    Particle.publish("Pump No. 2 : ", newRelayState2?"TRIP":"OKAY");
    delay(500);
    ubidots.add("Pump-No.-2", newRelayState2);
    ubidots.sendAll();
  }
  oldRelayState2 = newRelayState2;
  delay(1000);
}

void relay3()
{
      char newRelayState3 = digitalRead(D5);
  if (newRelayState3 != oldRelayState3){
    int relayState3;
    led3.setValue(newRelayState3?255:0);
    Particle.publish("Pump No. 3 : ", newRelayState3?"TRIP":"OKAY");
    delay(500);
    ubidots.add("Pump-No.-3", newRelayState3);
    ubidots.sendAll();
  }
  oldRelayState3 = newRelayState3;
  delay(1000);
}

#5

That was all her I just suggested way to clean the code up a little bit. In its full context I think you would benefit from either a state machine or at least Making the delays non blocking.
What is the point of the 500 ms delay after publish and before ubidots?
Also what is the point of the 1 second delay after each one?
Seems like we have relayed three switches to our member rate as you get to the beginning of your check for relay 1 it will take 3 1/2 seconds before you update Ubidots.

Can you add each of them then do a single sendall.

It’s almost 4 here and im literally in bed but I’ll take a crack at it tomorrow.
Best
Garrett


#6

@hawesg
I’ve updated the code:

void loop()
{
  
  Blynk.run();
  relay1();
  relay2();
  relay3();
  ubidots.sendAll();

}

void relay1()
{
      char newRelayState1 = digitalRead(D3);
  if (newRelayState1 != oldRelayState1){
    int relayState1;
    led1.setValue(newRelayState1?255:0);
    Particle.publish("Pump No. 1 : ", newRelayState1?"TRIP":"OKAY");
    ubidots.add("Pump-No.-1", newRelayState1);
    }
  oldRelayState1 = newRelayState1;

}

void relay2()
{
      char newRelayState2 = digitalRead(D4);
  if (newRelayState2 != oldRelayState2){
    int relayState2;
    led2.setValue(newRelayState2?255:0);
    Particle.publish("Pump No. 2 : ", newRelayState2?"TRIP":"OKAY");
    ubidots.add("Pump-No.-2", newRelayState2);
  }
  oldRelayState2 = newRelayState2;

}

void relay3()
{
      char newRelayState3 = digitalRead(D5);
  if (newRelayState3 != oldRelayState3){
    int relayState3;
    led3.setValue(newRelayState3?255:0);
    Particle.publish("Pump No. 3 : ", newRelayState3?"TRIP":"OKAY");
    ubidots.add("Pump-No.-3", newRelayState3);
  }
  oldRelayState3 = newRelayState3;

}