Aggiornamento variabile arduino IoT cloud

Buongiorno chiedo il vostro aiuto per il seguente problema.

Progetto: Visualizzare su pagina web l'altezza liquidi contenuti in vasca con widgets su dashboard.
Componenti: Arduino nano 33 IoT, modulo HC-SR04
Software: Arduino cloud + Arduino create Agent

Problema: Il monitor seriale visualizza correttamente la variabile "lettura" , ma la "things" di arduino IoT no e, di conseguenza, nemmeno i sensori grafici.
Ho provato a cambiare tipologia di variabile senza risultato.
Il nano iot 33 è ovviamente on-line

Codice sketch:

#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() {

  // Initialize serial and wait for port to open:
  Serial.begin(9600);
  // This delay gives the chance to wait for a Serial Monitor without blocking if none is found
  delay(1500);

  // Defined in thingProperties.h
  initProperties();

  // Connect to Arduino IoT Cloud
  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;
  long lettura;

  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.
  lettura = (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.println(lettura);
  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

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

Codice thingPropereties.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)


int lettura;

void initProperties(){

  ArduinoCloud.addProperty(lettura, READ, 8 * SECONDS, NULL);

}

WiFiConnectionHandler ArduinoIoTPreferredConnection(SSID, PASS);
  1. il codice, come da regolamento, devi racchiuderlo negli apposi tag (modifica il post, selezionalo tutto e usa pulsante testo preformattato < / >)

  2. Credo che dichiarando come hai fatto una (ulteriore) variabile lettura dentro al loop, hai "nascosto" la variabile lettura del IoT (già dichiarata in thingPropereties.h)

Codice corretto (scusate)

Problema risolto. Era proprio come dicevi tu !
Cosa dire, GRAZIE davvero !!! :pray: :pray: :+1: :+1: :+1:

1 Like

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