Ubidots api events using

hey how can i use events as button 1 is on when temperature is 30 celcius i can’t set variables. i have 2 nodemcu. 1 is controlling relays other is for sensors. i want my relays on/off with sensors data. can i do that with events thanks.

-Erge

Hello @ergesama, I hope you are well.

I understand that what you want to do is to trigger button 1 in nodemcu #2, when a variable called “temperature” reaches 30 °C in the nodemcu #1. If I’m wrong, please correct me.

Now, it is possible to set a variable when an event triggers, this way when your sensors send to Ubidots a value equal or higher to 30 to the device “nodemcu #1”, the event will set a variable called, for example, “button 1” to 1 in the device “nodemcu #2”. You can also set a 0 when the value goes lower than 30.

Then you could receive in your physical nodemcu #2 this value by subscribing to the button 1 variable. You can read the following article to have a better Idea of how to subscribe to your variable: Connect an ESP32-DevKitC to Ubidots over MQTT | Ubidots Help Center

When you receive your “Button 1” data correctly in your nodemcu connected to your relays, it would need a logic to turn them On when the value received from “Button 1” is 1, and a logic to turn Off when the value received is 0.

I hope this helps, and if you have any other doubts just let us know.
–Camilo

Hello @CamiloDavila, i hope you are well.

Yeah you understand right. Can i do that only in events or do i need add some codes for my code block? And i have only nodemcu esp8266 i don’t have devkitC

-Ergesama

Hi @ergesama,

Yes, you would have to implement code in your NodeMCU device, to subscribe to the variable set by the events. This way, when the value received from Ubidots in your ESP8266 is 1, it turns On the relay for example, and when it receives a 0, it turns it Off.

You can look at the following guides to have a better understanding on how to connect your ESP8266 to Ubidots, and how to retrieve data via MQTT or HTTP from Ubidots:

On Ubidots, you would have to create the event linked to the temperature sensor variable, so when the value reaches 30 it triggers. Check this page to have a better idea on how to do this:

You can try it out, and let me know how it goes. Or, if you still have doubts, don’t be afraid to ask.

Best regards,
–Camilo

Hey Camilo

I understood i get values from nodemcu it is no problem. I ask just can i do 2 nodemcu communication in events i mean i have 1 dashboard 2 sources. I want communicate each other with events.

sensor_update.ino (1.9 KB)
reciving_code.ino (3.7 KB)
i have these codes i want they communicate each other on ubidots’ events can i do that

Hello @ergesama. The short answer is yes, you can control them using events.

I checked your account, and you are receiving the values correctly, as you mentioned, with this part done you can create the event that is going to monitor the temperature value sent by the sensor. To do this you can go to events section in the platform, once there you add a new event.


In the triggers, you need to set up the variable that receives the temperature value, in your case the one label “temp”, and according to what you’ve mentioned, you set the condition to “greater than or equal to”, the trigger value to 30, and the “trigger after” to 0 minutes, in order to activate the event instantly when the temperature is 30 or higher.

Then you configure the “Actions” of the event. In your case, you want to “Set a variable” when the event is triggered.


The variable or variables you need to select are the target variable, in this case, the variable(s) you will use to control the relays. I suppose the variables you would want to set according to the temperature are the ones labeled “fan1” and “fan2” in your “control” device.

We can establish that when the “fan” variables are set to 1, this means On, and 0 means Off. Why this is important? Because we are setting a “Back to normal” payload with the value sent being “0”, this way, when the trigger variable goes back to normal conditions (under 30) the “fan” variables will receive a 0 automatically.

Finally, you set the event name and the “Active windows”, which is basically the days and hours the event will be able to trigger. When you are done with that, just click save and your event is ready.

Now, on the device you are using to control the relays, which I assume is the one using “receiving_code”, you must use the following two lines in the setup():

float fan1=0;
float fan2=0;

We are going to use these variables to store the value of the variable we receive from Ubidots. Then in the loop() function, you can use the following lines:

fan1= ubidots.get(DEVICE_LABEL, VARIABLE_LABEL_FAN1);
fan2= ubidots.get(DEVICE_LABEL, VARIABLE_LABEL_FAN2);
 
 // Evaluates the results obtained
  if (fan1!= ERROR_VALUE) {
    Serial.print("Fan1:");
    Serial.println(fan1);
  }
  if (fan1!= ERROR_VALUE) {
    Serial.print("Fan2:");
    Serial.println(fan2);
  }
  delay(5000);

I noticed you are using the Ubidots.h library, with HTTP, so to stay in the same line, we use HTTP to retrieve the value. The delay is to wait 5 seconds before retrieving the values again, since it’s not going to change that quick. The labels of the variables you can find in the variable view:
imagen

Now you can play with the values you store in fan1 and fan2, which is a 1 or a 0, and this way you can create the rest of the logic to control the relays accordingly.
–Camilo

hey camilo
Thanks so much. I did what u say and it worked. Thanks a lot have a good day. You are helpful guy. I finished my project thanks to you.

  • Erge

Hello Erge, I hope you are well.

You’re welcome, I’m glad it worked and that you were able to finish your project!

Best regards,
–Camilo

1 Like