DS18B20 Temperature Sensor and NodeMCU to Thingspeak

Charlotte Dorn
3 min readApr 21, 2020

--

In this project, I assembled a very simple temperature sensor. The ultimate goal of this project is to develop a solar oven and collect data to measure its efficiency.

This instructable was very helpful in completing this project!

Materials

  • A DS18B20 Kit (or a DS18B20 and a 4.7 k resistor and connectors)
  • NodeMCU
  • A 5V solar panel or another 3.3 or 5V power source

The specific product or brand does not matter.

From the DS18B20, the red/VCC wire and black/ground should be connected to the 3V and G of the NodeMCU, respectively. The yellow/data cable should be connected to D4 (in the code this will be described as Pin 2).

Unless you have a small adapter (as seen in the picture above), you will need to connect a 4.7k resistor from the 3V to the data cable. This diagram details those connections.

Software

You can program the Node using the Arduino IDE.

After downloading this software, you will have to go to Tools>Boards>Board Manager… and find and install “ESP8266 Boards”. Once you have done so, in Tools>Boards, you should select NodeMCU 1.0 (ESP-12E Module).

The libraries you will need to run my code are ESP8266WiFi, PubSubClient, DallasTemperature, and OneWire.

  • NOTE: you must download a specific version of OneWire or you will receive the following error:
fatal error: avr/io.h: No such file or directory #include <avr/io.h> nodemcu

I would recommend installing the first three by going to Sketch>Include Library>Manage Libraries… and then installing them.

Use this link or this link to download the OneWire Library and then navigate to Sketch>Include Library>Add .Zip Library.

Code

My code should be available at this link.

After including the necessary libraries, we will first set up the OneWire input. Remember that when using the NodeMCU, the D4 is indicated as 2.

#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
OneWire ds(2);

After defining the variables for wifi connection (password, server, channelID, APIKey, and message interval), we’ll initialize the following functions:

int mqttSubscriptionCallback(char* topic, byte* payload, unsigned int length);
void mqttConnect();
void connectWifi();

In our connectWifi() function, we will use WiFi.scanNetworks() to find available networks, and then iterate through them one by one and try to connect using a password provided. In the mqttConnect() function, we try mqttClient.connect( clientID) to connect to the broker. (see code for more details) The following code is then responsible for taking the temperature value and publishing it to the ThingSpeak server:

void loop() {if (WiFi.status() != WL_CONNECTED) {
connectWifi();
}

if (!mqttClient.connected())
{
mqttConnect(); // Connect if MQTT client is not connected.
}
sensors.requestTemperatures();//Collect temperature sensor information and RSSI connection value
float tempC = sensors.getTempCByIndex(0);
float tempF = sensors.getTempFByIndex(0);
float rssi = float(WiFi.RSSI());
String dataString = "field1=" + String(tempF) + "&field2=" + String(tempC) + "&field3" + String(rssi);
Serial.println(dataString);

Serial.println(dataString);
mqttClient.loop(); // Call the loop to maintain connection to the server.

String topicString ="channels/" + String(writeChannelID) + "/publish/"+String(writeAPIKey);
mqttClient.publish( topicString.c_str(), dataString.c_str() );
Serial.println( "to channel " + String(writeChannelID ) );
delay(msg_interval);}
  • Note that in this case, I am using field1. This can be changed above.

More written in this post about the setup for an MQTT server and publisher.

And BOOM!

Once you upload this code to the NodeMCU, you should see the following (or similar output)

--

--