0% found this document useful (0 votes)
12 views2 pages

Iot Ultra

Node mcu IOT cloud distance measurement

Uploaded by

vijayprasathme
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views2 pages

Iot Ultra

Node mcu IOT cloud distance measurement

Uploaded by

vijayprasathme
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

/*

Sketch generated by the Arduino IoT Cloud Thing "Ultrasonic Sensor"


https://create.arduino.cc/cloud/things/8e8ccfc2-b282-471b-9ecb-5c7e306824f5

Arduino IoT Cloud Variables description

The following variables are automatically generated and updated when changes are
made to the Thing

float distance;

Variables which are marked as READ/WRITE in the Cloud Thing will also have
functions
which are called when their values are changed from the Dashboard.
These functions are generated with the Thing and added at the end of this sketch.
*/

#include "thingProperties.h"

// Pin definitions
const int trigPin = D5;
const int echoPin = D6;

// Function to measure distance


long measureDistance() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

long duration = pulseIn(echoPin, HIGH);


long distance = duration * 0.034 / 2;

return distance;
}

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

// Set up the ultrasonic sensor pins


pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);

// Defined in thingProperties.h
initProperties();

// Connect to Arduino IoT Cloud


ArduinoCloud.begin(ArduinoIoTPreferredConnection);

/*
The following function allows you to obtain more information
related to the state of network and IoT Cloud connection and errors
the higher number the more granular information you’ll get.
The default is 0 (only errors).
Maximum is 4
*/
setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();
}

void loop() {
ArduinoCloud.update();

// Measure distance
long distanceValue = measureDistance();
distance = distanceValue;

// Print the value to the Serial Monitor


Serial.print("Distance: ");
Serial.print(distanceValue);
Serial.println(" cm");

// Wait for 2 seconds before taking the next reading


delay(2000);
}

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

You might also like