Arduino cloud code not working / push notifications

Hi, I'm trying to code a laser sensor/receiver to turn a LED on and send a push notification to my phone . I'm having trouble with my code and I'm unsure what could be wrong. The laser sensor is not even turning on but the led is. I know one issue I'm probably having is the network not connecting. But I'm sure another issue is my code is incorrect. We are using a Arduino uno R4. Any help is greatly appreciated. Here is my current code:

#include "thingProperties.h"
 int laser = 4;
 int receiver = 12;
 int LED = 8;


void setup() {
  Serial.begin(9600);
  delay(1500); 

  // Defined in thingProperties.h
  initProperties();

  // Connect to Arduino Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  
  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();
  
  pinMode(laser, OUTPUT);
  pinMode(receiver, INPUT);
  pinMode(LED, OUTPUT);
}

void loop() {
  ArduinoCloud.update();
  
  int value = digitalRead(receiver);
  
  if (value == 0) {
    digitalWrite(LED, HIGH);
    onDeerChange();
  } 
  else {
    digitalWrite(8, LOW);
  }
}
/*
  Since Deer is READ_WRITE variable, onDeerChange() is
  executed every time a new value is received from IoT Cloud.
*/
 void onDeerChange()  {
   int deer = true;
 }
  
  // Add your code here to act upon Deer change} 
// Code generated by Arduino IoT Cloud, DO NOT EDIT.

#include <ArduinoIoTCloud.h>
#include <Arduino_ConnectionHandler.h>

const char SSID[]     = SECRET_SSID;    // Network SSID (name)
const char PASS[]     = SECRET_OPTIONAL_PASS;    // Network password (use for WPA, or use as key for WEP)

void onDeerChange();

bool deer;

void initProperties(){

  ArduinoCloud.addProperty(deer, READWRITE, ON_CHANGE, onDeerChange);

}

WiFiConnectionHandler ArduinoIoTPreferredConnection(SSID, PASS);

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum

In my experience the easiest way to tidy up the code and add the code tags is as follows

Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.

Hi @aewibble.

With this code, I can tell you are intended to set the value of the deer Cloud Variable to true. However, it is not doing that. If you look in the thingProperties.h tab of the sketch, you will see the declaration of the Cloud Variable.

This means that you can use those variables in the .ino file of the sketch without declaring them.

So if you want to set the value of the deer Cloud Variable to true, you must do this:

deer = true;

What you are doing instead in the .ino file is this:

int deer = true;

This declares a new variable and sets the value of that new variable rather than the value of the variable of the same name that was declared at global scope. That means a line like this in your sketch will never affect the Cloud Variable.

This is known as "variable shadowing" and is the source of many a confusing bug.


I will also point out that, although this code design is not the cause of the problem you are experiencing, it does indicate that you don't understand how the Cloud variable callback functions work:

As the comment explains, the onDeerChange function is automatically called when you manually change the value of the deer cloud variable via the Arduino Cloud dashboard. This feature is useful for when you want to trigger your program to do something when the value of the variable is changed from the dashboard.

But you aren't using it for that purpose. You instead (attempting to) use it to change the value of the variable from the sketch program. That doesn't make sense and can also cause problems because it means if you set the variable to false via the dashboard then the the onDeerChange function will be called and the sketch program will simply set it right back to true again.

If you want to set the value of a Cloud variable from your sketch, just do it directly:

  if (value == 0) {
    digitalWrite(LED, HIGH);
    deer = true;
  } 

There is no requirement to do so in the callback function.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.