DHT22 + Maxim DB18b20 Temperature sensor NodeMCU

5 years 5 months ago #3244 by blitz
Hello!

As the topic suggests, I'm trying to relay the data from a DHT 22 plus a Maxim BD18b20 sensor at the same time onto the same graph on easyIOT.

For some reason, the DHT22 is sent wihtout any hiccups, but not that from my maxim sensor. My code is as Below. Any help will be sincerely appreciated.
#include <ESP8266WiFi.h>
#include "DHT.h"
#include <OneWire.h> 
#include <DallasTemperature.h> 

 
//AP definitions - update this
#define AP_SSID     "Windows Phone6885"
#define AP_PASSWORD "yoohoooo"

// EasyIoT Cloud definitions - change EIOT_CLOUD_TEMP_INSTANCE_PARAM_ID and EIOT_CLOUD_HUM_INSTANCE_PARAM_ID
#define EIOT_CLOUD_TEMP_INSTANCE_PARAM_ID    "5719dfa4c943a0182f6c0b4b/ajL9nyaYbuZBu7eF"
#define EIOT_CLOUD_HUM_INSTANCE_PARAM_ID     "5719dfa4c943a0182f6c0b4b/WcGJGNP45vVHiwto"
#define EIOT_CLOUD_MAX_INSTANCE_PARAM_ID      "5719dfa4c943a0182f6c0b4b/8pB7wSIGBZWZEC0J"
#define REPORT_INTERVAL 60 // in sec


#define EIOT_CLOUD_ADDRESS     "cloud.iot-playground.com"
#define EIOT_CLOUD_PORT        40404



#define DHT22_PIN 4  // DHT22 pin
#define ONE_WIRE_BUS 2 // one wire bus pin

OneWire oneWire(ONE_WIRE_BUS); 
DallasTemperature DS18B20(&oneWire); 
DallasTemperature sensors(&oneWire);


 

float oldTemp;
float oldHum;

DHT dht;


void setup() {
  Serial.begin(115200);
  
  wifiConnect();

  Serial.println();
  Serial.println("Status\tHumidity (%)\tTemperature (C)\t(F) \t Maxim Sensor Temperature (C)");

  dht.setup(DHT22_PIN); // data pin 2
  DeviceAddress Probe02 = { 0x28, 0x8B, 0x77, 0x3E, 0x07, 0x00, 0x00, 0x17 };
  
  oldTemp = -1;
  oldHum = -1;
}

void loop() {


  
  delay(500);
  
  float hum = dht.getHumidity();
  float temp = dht.getTemperature();
  float maximtemp;
  
  DS18B20.requestTemperatures();  
  maximtemp = DS18B20.getTempCByIndex(0);
  Serial.print("The maxim temperature sensor reading is: ");
  Serial.print(maximtemp);
   
  Serial.print(dht.getStatusString());
  Serial.print("\t");
  Serial.print(hum, 1);
  Serial.print("\t\t");
  Serial.print(temp, 1);
  Serial.print("\t\t");
  Serial.println(dht.toFahrenheit(temp), 1);

  /*if (temp != oldTemp)
  {
    sendTeperature(temp);
    oldTemp = temp;
  }*/

  if (hum != oldHum)
  {
    sendHumidity(hum);
    oldHum = hum;
  }

  int cnt = REPORT_INTERVAL;
  
  while(cnt--)
    delay(1000);
}

void wifiConnect()
{
    Serial.print("Connecting to AP");
    WiFi.begin(AP_SSID, AP_PASSWORD);
    while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.print(".");
  }
  
  Serial.println("");
  Serial.println("WiFi connected");  
}

void sendTeperature(float temp)
{  
   WiFiClient client;
   
   while(!client.connect(EIOT_CLOUD_ADDRESS, EIOT_CLOUD_PORT)) {
    Serial.println("connection failed");
    wifiConnect(); 
  }

  String url = "";
  // URL: /RestApi/SetParameter/[instance id]/[parameter id]/[value]
  url += "/RestApi/SetParameter/"+ String(EIOT_CLOUD_TEMP_INSTANCE_PARAM_ID) + "/"+String(temp); // generate EasIoT cloud update parameter URL

  Serial.print("POST data to URL: ");
  Serial.println(url);
  
  client.print(String("POST ") + url + " HTTP/1.1\r\n" +
               "Host: " + String(EIOT_CLOUD_ADDRESS) + "\r\n" + 
               "Connection: close\r\n" + 
               "Content-Length: 0\r\n" + 
               "\r\n");

  delay(100);
    while(client.available()){
    String line = client.readStringUntil('\r');
    Serial.print(line);
  }
  
  Serial.println();
  Serial.println("Connection closed");
}

void sendTemperature(float maximtemp)
{  
   WiFiClient client;
   
   while(!client.connect(EIOT_CLOUD_ADDRESS, EIOT_CLOUD_PORT)) {
    Serial.println("connection failed");
    wifiConnect(); 
  }

  Serial.print("now posting maxim sensor data");
  String url = "";
  // URL: /RestApi/SetParameter/[instance id]/[parameter id]/[value]
  url += "/RestApi/SetParameter/"+ String(EIOT_CLOUD_MAX_INSTANCE_PARAM_ID) + "/"+String(maximtemp); // generate EasIoT cloud update parameter URL

  Serial.print("POST data to URL: ");
  Serial.println(url);
  
  client.print(String("POST ") + url + " HTTP/1.1\r\n" +
               "Host: " + String(EIOT_CLOUD_ADDRESS) + "\r\n" + 
               "Connection: close\r\n" + 
               "Content-Length: 0\r\n" + 
               "\r\n");

  delay(100);
    while(client.available()){
    String line = client.readStringUntil('\r');
    Serial.print(line);
  }
  
  Serial.println();
  Serial.println("Connection closed");
}


void sendHumidity(float hum)
{  
   WiFiClient client;
   
   while(!client.connect(EIOT_CLOUD_ADDRESS, EIOT_CLOUD_PORT)) {
    Serial.println("connection failed");
    wifiConnect(); 
  }

  String url = "";
  // URL: /RestApi/SetParameter/[instance id]/[parameter id]/[value]
  url += "/RestApi/SetParameter/"+ String(EIOT_CLOUD_HUM_INSTANCE_PARAM_ID) + "/"+String(hum); // generate EasIoT cloud update parameter URL

  Serial.print("POST data to URL: ");
  Serial.println(url);
  
  client.print(String("POST ") + url + " HTTP/1.1\r\n" +
               "Host: " + String(EIOT_CLOUD_ADDRESS) + "\r\n" + 
               "Connection: close\r\n" + 
               "Content-Length: 0\r\n" + 
               "\r\n");

  delay(100);
    while(client.available()){
    String line = client.readStringUntil('\r');
    Serial.print(line);
  }
  
  Serial.println();
  Serial.println("Connection closed");
}

Please Log in or Create an account to join the conversation.

5 years 5 months ago - 5 years 5 months ago #3246 by NightOne
I suppose I could be more helpfull Here is the corrected code for you to try


File Attachment:

File Name: Correctedsketch.txt
File Size:5 KB
Attachments:

Please Log in or Create an account to join the conversation.

5 years 5 months ago #3247 by NightOne
Trying code snippet again....my firewall seems to strip it out :(
#include <ESP8266WiFi.h>
#include "DHT.h"
#include <OneWire.h> 
#include <DallasTemperature.h> 

 
//AP definitions - update this
#define AP_SSID     "Windows Phone6885"
#define AP_PASSWORD "yoohoooo"

// EasyIoT Cloud definitions - change EIOT_CLOUD_TEMP_INSTANCE_PARAM_ID and EIOT_CLOUD_HUM_INSTANCE_PARAM_ID
#define EIOT_CLOUD_TEMP_INSTANCE_PARAM_ID    "5719dfa4c943a0182f6c0b4b/ajL9nyaYbuZBu7eF"
#define EIOT_CLOUD_HUM_INSTANCE_PARAM_ID     "5719dfa4c943a0182f6c0b4b/WcGJGNP45vVHiwto"
#define EIOT_CLOUD_MAX_INSTANCE_PARAM_ID      "5719dfa4c943a0182f6c0b4b/8pB7wSIGBZWZEC0J"
#define REPORT_INTERVAL 60 // in sec


#define EIOT_CLOUD_ADDRESS     "cloud.iot-playground.com"
#define EIOT_CLOUD_PORT        40404



#define DHT22_PIN 4  // DHT22 pin
#define ONE_WIRE_BUS 2 // one wire bus pin

OneWire oneWire(ONE_WIRE_BUS); 
DallasTemperature DS18B20(&oneWire); 
DallasTemperature sensors(&oneWire);


 

float oldTemp;
float oldHum;
float oldMax;

DHT dht;


void setup() {
  Serial.begin(115200);
  
  wifiConnect();

  Serial.println();
  Serial.println("Status\tHumidity (%)\tTemperature (C)\t(F) \t Maxim Sensor Temperature (C)");

  dht.setup(DHT22_PIN); // data pin 2
  DeviceAddress Probe02 = { 0x28, 0x8B, 0x77, 0x3E, 0x07, 0x00, 0x00, 0x17 };
  
  oldTemp = -1;
  oldHum = -1;
  oldMax = -1;
}

void loop() {


  
  delay(500);
  
  float hum = dht.getHumidity();
  float temp = dht.getTemperature();
  float maximtemp;
  
  DS18B20.requestTemperatures();  
  maximtemp = DS18B20.getTempCByIndex(0);
  Serial.print("The maxim temperature sensor reading is: ");
  Serial.print(maximtemp);
   
  Serial.print(dht.getStatusString());
  Serial.print("\t");
  Serial.print(hum, 1);
  Serial.print("\t\t");
  Serial.print(temp, 1);
  Serial.print("\t\t");
  Serial.println(dht.toFahrenheit(temp), 1);

  if (temp != oldTemp)
  {
    sendDHTTemp(temp);
    oldTemp = temp;
  }

  if (hum != oldHum)
  {
    sendDHTHumidity(hum);
    oldHum = hum;
  }
  
  if (maximtemp != oldMax)
  {
    sendMAXItemp(maximtemp);
    oldMax = maximtemp;
  }
  

  int cnt = REPORT_INTERVAL;
  
  while(cnt--)
    delay(1000);
}

void wifiConnect()
{
    Serial.print("Connecting to AP");
    WiFi.begin(AP_SSID, AP_PASSWORD);
    while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.print(".");
  }
  
  Serial.println("");
  Serial.println("WiFi connected");  
}

void sendDHTTemp(float temp)
{  
   WiFiClient client;
   
   while(!client.connect(EIOT_CLOUD_ADDRESS, EIOT_CLOUD_PORT)) {
    Serial.println("connection failed");
    wifiConnect(); 
  }

  String url = "";
  // URL: /RestApi/SetParameter/[instance id]/[parameter id]/[value]
  url += "/RestApi/SetParameter/"+ String(EIOT_CLOUD_TEMP_INSTANCE_PARAM_ID) + "/"+String(temp); // generate EasIoT cloud update parameter URL

  Serial.print("POST data to URL: ");
  Serial.println(url);
  
  client.print(String("POST ") + url + " HTTP/1.1\r\n" +
               "Host: " + String(EIOT_CLOUD_ADDRESS) + "\r\n" + 
               "Connection: close\r\n" + 
               "Content-Length: 0\r\n" + 
               "\r\n");

  delay(100);
    while(client.available()){
    String line = client.readStringUntil('\r');
    Serial.print(line);
  }
  
  Serial.println();
  Serial.println("Connection closed");
}

void sendMAXItemp(float maximtemp)
{  
   WiFiClient client;
   
   while(!client.connect(EIOT_CLOUD_ADDRESS, EIOT_CLOUD_PORT)) {
    Serial.println("connection failed");
    wifiConnect(); 
  }

  Serial.print("now posting maxim sensor data");
  String url = "";
  // URL: /RestApi/SetParameter/[instance id]/[parameter id]/[value]
  url += "/RestApi/SetParameter/"+ String(EIOT_CLOUD_MAX_INSTANCE_PARAM_ID) + "/"+String(maximtemp); // generate EasIoT cloud update parameter URL

  Serial.print("POST data to URL: ");
  Serial.println(url);
  
  client.print(String("POST ") + url + " HTTP/1.1\r\n" +
               "Host: " + String(EIOT_CLOUD_ADDRESS) + "\r\n" + 
               "Connection: close\r\n" + 
               "Content-Length: 0\r\n" + 
               "\r\n");

  delay(100);
    while(client.available()){
    String line = client.readStringUntil('\r');
    Serial.print(line);
  }
  
  Serial.println();
  Serial.println("Connection closed");
}


void sendDHTHumidity(float hum)
{  
   WiFiClient client;
   
   while(!client.connect(EIOT_CLOUD_ADDRESS, EIOT_CLOUD_PORT)) {
    Serial.println("connection failed");
    wifiConnect(); 
  }

  String url = "";
  // URL: /RestApi/SetParameter/[instance id]/[parameter id]/[value]
  url += "/RestApi/SetParameter/"+ String(EIOT_CLOUD_HUM_INSTANCE_PARAM_ID) + "/"+String(hum); // generate EasIoT cloud update parameter URL

  Serial.print("POST data to URL: ");
  Serial.println(url);
  
  client.print(String("POST ") + url + " HTTP/1.1\r\n" +
               "Host: " + String(EIOT_CLOUD_ADDRESS) + "\r\n" + 
               "Connection: close\r\n" + 
               "Content-Length: 0\r\n" + 
               "\r\n");

  delay(100);
    while(client.available()){
    String line = client.readStringUntil('\r');
    Serial.print(line);
  }
  
  Serial.println();
  Serial.println("Connection closed");
}
The following user(s) said Thank You: blitz

Please Log in or Create an account to join the conversation.

5 years 5 months ago #3251 by blitz
Thankyou so much!

Works like a charm :)

Please Log in or Create an account to join the conversation.

Time to create page: 0.235 seconds

Forum latest

  • No posts to display.