Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
8 views8 pages

Iot 9

Uploaded by

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

Iot 9

Uploaded by

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

Practical: 9

AIM: Use ultrasonic sensor and monitor distance on Thingspeak cloud.

#define TRIG_PIN 2 // Trigger pin


#define ECHO_PIN 4// Echo pin

void setup() {
Serial.begin(9600); // Start serial communication
pinMode(TRIG_PIN, OUTPUT); // Set trigger pin as OUTPUT
pinMode(ECHO_PIN, INPUT); // Set echo pin as INPUT
}

void loop() {
long duration, distance;

// Clear the trigger


digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);

// Set the trigger HIGH for 10 microseconds


digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);

// Read the echo pin


duration = pulseIn(ECHO_PIN, HIGH);

// Calculate distance in cm
distance = (duration * 0.034) / 2;

// Print distance to the serial monitor


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

delay(1000); // Wait for a second before next measurement


}
Using Thingspeak:

#include <ESP8266WiFi.h>
#include <ThingSpeak.h>

const char* ssid = "Jash"; // Your WiFi SSID


const char* password = "jash1234"; // Your WiFi password
const char* apiKey = "FDBFJOHGMJX7ECJD"; // ThingSpeak Write API Key
const long channelID = 2677746; // Your ThingSpeak channel ID

#define TRIG_PIN 2 // Trigger pin


#define ECHO_PIN 4 // Echo pin

WiFiClient client;

void setup() {
Serial.begin(115200); // Start serial communication
pinMode(TRIG_PIN, OUTPUT); // Set trigger pin as OUTPUT
pinMode(ECHO_PIN, INPUT); // Set echo pin as INPUT

// Connect to WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("Connected to WiFi");
ThingSpeak.begin(client); // Initialize ThingSpeak
}

void loop() {
long duration, distance;

// Clear the trigger


digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);

// Set the trigger HIGH for 10 microseconds


digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);

// Read the echo pin


duration = pulseIn(ECHO_PIN, HIGH);

// Calculate distance in cm
distance = (duration * 0.034) / 2;

// Print distance to the serial monitor


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

// Upload distance to ThingSpeak


ThingSpeak.setField(1, distance);
int httpCode = ThingSpeak.writeFields(channelID, apiKey);

if (httpCode == 200) {
Serial.println("Data updated successfully");
} else {
Serial.print("Error updating data: ");
Serial.println(httpCode);
}

delay(2000);
}

You might also like