2 DS18B20 on NodeMCU | ThingSpeak Projects | Forum

Avatar

Please consider registering
Guest

Search

— Forum Scope —






— Match —





— Forum Options —





Minimum search word length is 3 characters - maximum search word length is 84 characters

Register Lost password?
sp_Feed sp_TopicIcon
2 DS18B20 on NodeMCU
Avatar
eddie84
New Member
Forum Posts: 2
sp_UserOfflineSmall Offline
1
May 12, 2017 - 5:25 pm
sp_Permalink sp_Print sp_EditHistory

Hi I wrote this code to run two DS18B20 probes on the NodeMCU, with a probe the system works perfectly but with two probes I have difficulty. You can help me?

#include <ESP8266WiFi.h>
#include <OneWire.h>
#include <DallasTemperature.h>

//Def
#define myPeriodic 15 //in secondi | Thingspeak pubblica in 15sec
#define ONE_WIRE_BUS 2 // DS18B20 SONDA N° 1 su arduino pin2 corrisponde a D4 su nodeMCU
#define ONE_WIRE_BUS2 14 // DS18B20 SONDA N° 2 su arduino pin2 corrisponde a D5 su nodeMCU

OneWire oneWire(ONE_WIRE_BUS);
OneWire oneWire2(ONE_WIRE_BUS2);
DallasTemperature DS18B20(&oneWire);

float prevTemp = 0;
float prevTemp2 = 0;
const char* server = "api.thingspeak.com";
String apiKey ="xxxxxxxxxxxxxxxx"; // apikey thingspeak.com
const char* MY_SSID = "xxxxxxxxxxxx"; // Nome SSID Wifi
const char* MY_PWD = "xxxxxxxxxxxxx"; // Password Wifi
int sent = 0;
void setup() {
Serial.begin(115200);
connectWifi();
}

void loop() {
float temp;
//char buffer[10];
DS18B20.requestTemperatures();
temp = DS18B20.getTempCByIndex(0);
//String tempC = dtostrf(temp, 4, 1, buffer);//gestito in sendTemp()
Serial.print(String(sent)+" Temperatura Sonda Alta Puffer: ");
Serial.println(temp);

float temp2;
//char buffer[10];
DS18B20.requestTemperatures();
temp2= DS18B20.getTempCByIndex(1);
//String tempC = dtostrf(temp, 4, 1, buffer);//gestito in sendTemp()
Serial.print(String(sent)+" Temperatura Sonda Bassa Puffer: ");
Serial.println(temp2);

//if (temp != prevTemp)
//{
//sendTeperatureTS(temp);
//prevTemp = temp;
//}

sendTeperatureTS(temp);
sendTeperatureTS(temp2);
int count = myPeriodic;
while(count--)
delay(1000);

}

void connectWifi()
{
Serial.print("Connecting to "+*MY_SSID);
WiFi.begin(MY_SSID, MY_PWD);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.print(".");
}

Serial.println("");
Serial.println("Connesso");
Serial.println("");
}//fine connessione

void sendTeperatureTS(float temp)
void sendTeperatureTS(float temp2)
{
WiFiClient client;

if (client.connect(server, 80)) { // usa ip 184.106.153.149 oppure api.thingspeak.com
Serial.println("WiFi Client connected ");

String postStr = apiKey;
postStr += "&field1=";
postStr += String(temp);
postStr += "

";

String postStr = apiKey;
postStr += "&field2=";
postStr += String(temp2);
postStr += "

";

client.print("POST /update HTTP/1.1
");
client.print("Host: api.thingspeak.com
");
client.print("Connection: close
");
client.print("X-THINGSPEAKAPIKEY: " + apiKey + "
");
client.print("Content-Type: application/x-www-form-urlencoded
");
client.print("Content-Length: ");
client.print(postStr.length());
client.print("

");
client.print(postStr);
delay(1000);

}//fine if
sent++;
client.stop();
}//fine invio

Avatar
rw950431

Forum Posts: 250
sp_UserOfflineSmall Offline
2
May 14, 2017 - 8:34 am
sp_Permalink sp_Print

What difficulty are you having?  Wont compile?  Runs but doesnt send anything?  Sends wrong data? 

Your code looks OK  (I dont think you need OneWire oneWire2(ONE_WIRE_BUS2); but its not used) up until

 

void sendTeperatureTS(float temp)
void sendTeperatureTS(float temp2)
{
WiFiClient client;

Try deleting the line "void sendTeperatureTS(float temp2)".  

 

Otherwise how are you connecting the one-wire sensors- 3 wire mode or 2 wire mode?  Its best to use 3 wire mode. Do you have the pullup resistor connected (see http://playground.arduino.cc/Learning/OneWire)?

Avatar
eddie84
New Member
Forum Posts: 2
sp_UserOfflineSmall Offline
3
May 15, 2017 - 10:01 am
sp_Permalink sp_Print

The code above is completely wrong. I wrote another and it works correctly.

Now I wanted to know if there is the ability to send notifications by telegram or email when a temp == value

rw950431 said
What difficulty are you having?  Wont compile?  Runs but doesnt send anything?  Sends wrong data? 

Your code looks OK  (I dont think you need OneWire oneWire2(ONE_WIRE_BUS2); but its not used) up until

 

void sendTeperatureTS(float temp)
void sendTeperatureTS(float temp2)
{
WiFiClient client;

Try deleting the line "void sendTeperatureTS(float temp2)".  

 

Otherwise how are you connecting the one-wire sensors- 3 wire mode or 2 wire mode?  Its best to use 3 wire mode. Do you have the pullup resistor connected (see http://playground.arduino.cc/Learning/OneWire)?  

Avatar
rw950431

Forum Posts: 250
sp_UserOfflineSmall Offline
4
May 16, 2017 - 2:48 am
sp_Permalink sp_Print

Check out the React thinkspeak app to do that kind of thing on the thingspeak side- there is a built-in twitter interface.

I dont know about telegram but I wrote a little piece about my experiences interfacing to Slack which might give you a starting point http://community.thingspeak.com/forum/thingspeak-apps/thinghttp-slack-incoming-webhook/#p3842.

You can also use the IFTTT Maker channel but I found there were often long delays in forwarding messages.

Forum Timezone: America/New_York

Most Users Ever Online: 114

Currently Online:
16 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

rw950431: 250

Vinod: 154

cstapels: 83

piajola: 75

vespapierre: 63

Adarsh_Murthy: 59

Newest Members:

webone

KMVp

aamir786

sugengprayogi

vickysam@gmail.com

farmerkjs

Forum Stats:

Groups: 4

Forums: 17

Topics: 1289

Posts: 4481

 

Member Stats:

Guest Posters: 1

Members: 5652

Moderators: 0

Admins: 2

Administrators: Hans, lee