Good day all,
I have a really odd issue and I would love some help on this odd one.
I have a ESP32 board, with a DS18B20 connected on pin D4, which I believe on the ESP references pin 7.
Once I Upload after setting the pin to 7, the DS18B20 works perfectly, until I either re-upload or plug the ESP32 in elsewhere where it is actually being used. Basically if it reboots once, it tickets. I then have to change the pin, to lets say 4, upload, then change it back to 7, upload and it works again until unplugged...
I am powering the ESP over USB C and only have 2 DS18B20's connected in serial. I have tested with only 1 DS18B20 and get the same behavior.
I am using a Gikfun DS18B20 : Gikfun DS18B20 Waterproof Digital Temperature Sensor with Adapter Module for Arduino (Pack of 3 Sets) EK1183 : Amazon.ca: Industrial & Scientific
My Code is as follows:
//Network Connectivity
#include <WiFi.h>
#include <HTTPClient.h>
#include "arduino_secrets.h"
//Temp Probe
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 7
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
//Temp Sensor DS18B20
//Black = GND
//Yellow = Data
//White = VCC
const char* password = "Password";
const char* ssid = "WIFINetwork";
int SensorID1 = 4;
float TempS1 = 0;
int SensorID2 = 5;
float TempS2 = 0;
int SensorID3 = 6;//NA
float TempS3 = 0;
// API endpoint
const char* serverName = "http://192.168.10.xxxx";
// Timing variables
unsigned long lastTime = 0;
const unsigned long interval = 30000; // 1 minute in milliseconds = 30000
void setup() {
Serial.begin(115200);
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(buttonOpen, INPUT_PULLUP);
pinMode(buttonClose, INPUT_PULLUP);
// Connect to Wi-Fi
Serial.print("Connecting to Wi-Fi");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}
Serial.println("\nConnected to Wi-Fi");
sensors.begin(); // Start up the Temperature library
TempS1 = sensors.getTempCByIndex(0);
}
void loop() {
// Check if it's time to send the PUT request
unsigned long currentTime = millis();
if (currentTime - lastTime >= interval) {
lastTime = currentTime;
ReadTemps();
if (WiFi.status() == WL_CONNECTED) {
// Prepare JSON payload
float value = TempS1;
int sensorid = SensorID1;
Serial.println("Temperature 1 : " + String(value));
PostTemps(value, sensorid);
value = TempS2;
sensorid = SensorID2;
Serial.println("Temperature 2 : " + String(value));
PostTemps(value, sensorid);
value = TempS3;
sensorid = SensorID3;
Serial.println("Temperature 3 : " + String(value));
PostTemps(value, sensorid);
} else {
Serial.println("WiFi Disconnected");
}
}
delay(100);
}
void PostTemps(float value, int sensorid) {
WiFiClient client;
HTTPClient http;
http.begin(client, serverName);
// Add headers
http.addHeader("Content-Type", "application/json; charset=utf-8");
http.addHeader("accept", "*/*");
if(value != -127)
{
String jsonPayload = "{\"value\": " + String(value) + ", \"sensorid\": " + String(sensorid) + "}";
// Send POST request
int httpResponseCode = http.POST(jsonPayload);
// Handle response
if (httpResponseCode > 0) {
String payload = http.getString(); // Get response payload
Serial.println("HTTP Response code: " + String(httpResponseCode));
Serial.println("Response: " + payload);
} else {
Serial.println("Error on HTTP request");
Serial.println("Server: " + String(serverName));
Serial.println("Payload: " + jsonPayload);
http.end(); // Free resources
}
}
}
void ReadTemps() {
sensors.requestTemperatures();
// You can have more than one IC on the same bus. 0 refers to the first IC on the wire
TempS1 = sensors.getTempCByIndex(0);
TempS2 = sensors.getTempCByIndex(1);
TempS3 = sensors.getTempCByIndex(2);
}
Apologies if I have missed anything.
Thank you
