Hc-SRc04 value read

Good morning.
By connecting the HC-SR04 sensor to the cloud with code that perfectly reports the values ​​of the variable in the serial monitor, nothing is displayed in the Value of the Cloud Variables and consequently on the dashboard. I have associated variable "Aftstand_Sensor" Length - Read only and periodic every 2 seconds. No value read in cloud.

Cloud broadcast code in thingProperties.h is:
ArduinoCloud.addProperty(Afstand_sensor, READWRITE, 2 * SECONDS, onAfstandSensorChange);

Which?

Sorry write error to copy here.
Aftstand_Sensor to all

n.b. Free Plan for now

here's code:

Sketch "Livelli_dec21a.ino"

#include <SPI.h>
#include <WiFiNINA.h>

#define ULTRASONIC_TRIG_PIN 11 // pin TRIG is D11
#define ULTRASONIC_ECHO_PIN 12 //pin ECHO is D12
#include "thingProperties.h"

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

initProperties();

ArduinoCloud.begin(ArduinoIoTPreferredConnection);

setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();

pinMode(ULTRASONIC_TRIG_PIN, OUTPUT); // The trigger is defined as output
pinMode(ULTRASONIC_ECHO_PIN, INPUT); // The Echo is defined as input
}

void loop() {
ArduinoCloud.update();

long duration, Afstand_Sensor;
digitalWrite(ULTRASONIC_TRIG_PIN, LOW);
delayMicroseconds(2);

digitalWrite(ULTRASONIC_TRIG_PIN, HIGH); // With this we send an ultrasonic sound
delayMicroseconds(10); // We will send for 10 milliseconds

digitalWrite(ULTRASONIC_TRIG_PIN, LOW); // Now we stop transmitting
duration = pulseIn(ULTRASONIC_ECHO_PIN, HIGH); // Here we measure how long it takes before we get the ultrasound back.
Afstand_Sensor = (duration / 2) / 29.1; // With this we ensure that the values ​​in cm are displayed in the IoT cloud
Serial.print("distanza "); // To be able to check whether everything is working without an IoT Cloud connection, we also display it in the Monitor
Serial.print(Afstand_Sensor);
Serial.println(" cm");
}

/*
Since AfstandSensor is READ_WRITE variable, onAfstandSensorChange() is
executed every time a new value is received from IoT Cloud.
*/
void onAfstandSensorChange() {
// Add your code here to act upon AfstandSensor change

}

thingProperties.h
// 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)

CloudLength Afstand_Sensor;

void initProperties(){

ArduinoCloud.addProperty(Afstand_Sensor, READ, ON_CHANGE, NULL, 2);

}

WiFiConnectionHandler ArduinoIoTPreferredConnection(SSID, PASS);


Serial monitor show correctly distance. Dashboard widgets, associated with correct variable, not show.

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