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

ThingSpeak Updated Code

This document contains code to send ultrasonic sensor distance readings from an Arduino to ThingSpeak every 15 seconds. It initializes the ultrasonic sensor pins, Ethernet connection, and ThingSpeak client. In the loop, it takes ultrasonic readings, prints them to serial, and if 15 seconds have passed, sends the data to the ThingSpeak channel using the sendDataToThingSpeak function.
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)
22 views2 pages

ThingSpeak Updated Code

This document contains code to send ultrasonic sensor distance readings from an Arduino to ThingSpeak every 15 seconds. It initializes the ultrasonic sensor pins, Ethernet connection, and ThingSpeak client. In the loop, it takes ultrasonic readings, prints them to serial, and if 15 seconds have passed, sends the data to the ThingSpeak channel using the sendDataToThingSpeak function.
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

#include <SPI.

h>
#include <Ethernet.h>
#include "ThingSpeak.h"

// Define your MAC address and ThingSpeak credentials


byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
unsigned long channelNumber = 2305557;
const char* channelKey = "OVNP0GYB5Y531589";

// Define your EthernetClient


EthernetClient client;

// Define the Ultrasonic Sensor pins


const int trigPin = 7; // Trigger Pin of Ultrasonic Sensor
const int echoPin = 6; // Echo Pin of Ultrasonic Sensor

// Define a timer to track the last update time


unsigned long lastUpdateTime = 0;
const unsigned long updateInterval = 15000; // 15 seconds

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

// Initialize Ultrasonic Sensor pins


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

// Initialize Ethernet
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
} else {
Serial.println("Configured Ethernet using DHCP");
IPAddress myIPAddress = Ethernet.localIP();
Serial.print("My IP address: ");
Serial.println(String(myIPAddress));
}
delay(500);

// Initialize ThingSpeak
ThingSpeak.begin(client);
}

void loop() {
// Variables to store Ultrasonic Sensor data
long duration;

// Send a trigger signal to the Ultrasonic Sensor


digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Measure the echo duration


duration = pulseIn(echoPin, HIGH);
float cm = (duration/2)/29.1;

// Additional information in Serial Monitor


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

// Check if it's time to send data to ThingSpeak


if (millis() - lastUpdateTime >= updateInterval) {
if (sendDataToThingSpeak(duration)) {
Serial.println("Data sent to ThingSpeak CLOUD");
lastUpdateTime = millis(); // Update the last update time
} else {
Serial.println("Failed to send data to ThingSpeak. Check your setup.");
}
}

// Wait for a short time before the next reading


delay(500);
}

bool sendDataToThingSpeak(long data) {


// Send the data to ThingSpeak
int x = ThingSpeak.writeField(channelNumber, 1, String(data), channelKey);

// Check if the channel update was successful


if (x == 200) {
return true;
} else if (x == 401) {
Serial.println("Error 401: Authorization Required. Check your ThingSpeak API
key.");
} else {
Serial.println("Problem updating channel. HTTP error code " + String(x));
}
return false;
}

You might also like