#include <OneWire.h>
#include <DallasTemperature.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <ThingSpeak.h>
#include <ThingsBoard.h>
// GPIO where the DS18B20 is connected to
const int oneWireBus = 12;
const int trigpin = 25;
const int echopin = 26;
long d;
int j;
unsigned long channelNumber = 1597470;
const char* WriteAPIKey = "WIPH6LJ3R9WDPPMM";
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(oneWireBus);
// Pass our oneWire reference to Dallas Temperature sensor
DallasTemperature sensors(&oneWire);
WiFiClient client;
void setup() {
// Start the Serial Monitor
Serial.begin(115200);
Serial.print("Connecting to WiFi");
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println(" Connected!");
pinMode(trigpin, OUTPUT);
pinMode(echopin, INPUT);
// Start the DS18B20 sensor
sensors.begin();
Serial.println(WiFi.localIP());
ThingSpeak.begin(client);
}
void loop() {
digitalWrite(trigpin, LOW);
delayMicroseconds(2);
digitalWrite(trigpin, HIGH);
delayMicroseconds(10);
digitalWrite(trigpin, LOW);
d = pulseIn(echopin, HIGH);
j = d * 0.03408/2;
Serial.print(j);
Serial.println(" cm");
sensors.requestTemperatures();
float temperatureC = sensors.getTempCByIndex(0);
float temperatureF = sensors.getTempFByIndex(0);
Serial.print(temperatureC);
Serial.println("ºC");
Serial.print(temperatureF);
Serial.println("ºF");
ThingSpeak.setField(1,temperatureC);
ThingSpeak.setField(2,j);
int x = ThingSpeak.writeFields(channelNumber, WriteAPIKey);
delay(5000);
}