Been a while since I played with Arduino, need some help with DHTs

Hey all, I am blowing the dust of a project. I have a Wemos D1mini with a DHT22 sensor

my goal is to use the DHT22 as a refrigerator temp monitor, and pin D6/Grd with a mag switch to see if the door is open or closed

I have everything soldered up, but Im having some issues with my Arduino IDE libraries… I believe I have downloaded and installed them correctly… Please see rough code below

#include <b64.h>
#include <HttpClient.h>
#include <WiFi.h>
#include "DHT.h"

#define DHTPIN 4     // Digital pin connected to the DHT sensor
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);

//-------- User Settings -----------
const char* ssid = "Dooryard Guest";
const char* password = "";
const char* accesskey = "INITIAL STATE ACCESS KEY";
const char* bucketkey = "INITIAL STATE BUCKET KEY";
const char* signalname = "temperature";
//----------------------------------

HTTPClient ask;
int counter = 0;  
static bool hasWifi = false;

//////////////
// Network //
////////////

static void InitWifi()
{
  Serial.println("Connecting...");
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  hasWifi = true;
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void setup()
{
  Serial.begin(115200);
  Serial.println("ESP32 Device");
  Serial.println("Initializing...");
  
  dht.begin();

  // Initialize the WiFi module
  Serial.println(" > WiFi");
  hasWifi = false;
  InitWifi();
  if (!hasWifi)
  {
    return;
  }
}

//////////////
//Send Data//
////////////

void loop(){
  if (WiFi.status() == WL_CONNECTED) { //if we are connected
    counter = 0; //reset counter
    Serial.println("Wifi is still connected with IP: "); 
    Serial.println(WiFi.localIP());   //inform user about his IP address
  }else if (WiFi.status() != WL_CONNECTED) { //if we lost connection, retry
    WiFi.begin(ssid);      
  }
  while (WiFi.status() != WL_CONNECTED) { //during lost connection, print dots
    delay(500);
    Serial.print(".");
    counter++;
    if(counter>=60){ //30 seconds timeout - reset board
    ESP.restart();
    }
  }

  // Reading temperature or humidity
  float t = dht.readTemperature(true);

  // Create a URL for the request
  String url = "https://groker.init.st/api/events?accessKey=";
  url += accesskey;
  url += "&bucketKey=";
  url += bucketkey;
  url += "&";
  url += signalname;
  url += "=";
  url += t;

  Serial.print("*** requesting URL");
 
  // ask.begin;
  ask.begin(url); //Specify the URL
  
  //Check for the returning code
  int httpCode = ask.GET();       

  Serial.print(httpCode);
   
  if (httpCode > 0) { 
      String payload = ask.getString();
  } else {
      Serial.println("Error on HTTP request");
  }
  ask.end(); //End 
  Serial.println("*** End ");
  delay(10000);    // delay
}

Here is the error message Im getting

DHT22_Test_walk-in:18:1: error: 'HTTPClient' does not name a type
 HTTPClient ask;
 ^
/Users/jason/Documents/Arduino/DHT22_Test_walk-in/DHT22_Test_walk-in.ino: In function 'void InitWifi()':
DHT22_Test_walk-in:29:28: error: invalid conversion from 'const char*' to 'char*' [-fpermissive]
   WiFi.begin(ssid, password);
                            ^
In file included from /Users/jason/Documents/Arduino/DHT22_Test_walk-in/DHT22_Test_walk-in.ino:3:0:
/Applications/Arduino.app/Contents/Java/libraries/WiFi/src/WiFi.h:79:9: error:   initializing argument 1 of 'int WiFiClass::begin(char*, const char*)' [-fpermissive]
     int begin(char* ssid, const char *passphrase);
         ^
/Users/jason/Documents/Arduino/DHT22_Test_walk-in/DHT22_Test_walk-in.ino: In function 'void loop()':
DHT22_Test_walk-in:68:20: error: invalid conversion from 'const char*' to 'char*' [-fpermissive]
     WiFi.begin(ssid);      
                    ^
In file included from /Users/jason/Documents/Arduino/DHT22_Test_walk-in/DHT22_Test_walk-in.ino:3:0:
/Applications/Arduino.app/Contents/Java/libraries/WiFi/src/WiFi.h:60:9: error:   initializing argument 1 of 'int WiFiClass::begin(char*)' [-fpermissive]
     int begin(char* ssid);
         ^
DHT22_Test_walk-in:95:3: error: 'ask' was not declared in this scope
   ask.begin(url); //Specify the URL
   ^
Multiple libraries were found for "b64.h"
 Used: /Users/jason/Documents/Arduino/libraries/ArduinoHttpClient
 Not used: /Users/jason/Documents/Arduino/libraries/HttpClient-master
Multiple libraries were found for "DHT.h"
 Used: /Users/jason/Documents/Arduino/libraries/DHT_sensor_library
 Not used: /Users/jason/Documents/Arduino/libraries/arduino_430164
exit status 1
'HTTPClient' does not name a type

Any insight would be appreciated

HTTPClient ask;

Is this spelled correctly? is it HTTP or Http?

It failed with the caps… I opted to try using #include <ESP8266WiFi.h> instead

still get this error → ‘HTTPClient’ does not name a type

#include <ESP8266WiFi.h>
#include <HttpClient.h>
#include "DHT.h"

#define DHTPIN 4     // Digital pin connected to the DHT sensor
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);

#ifndef STASSID
#define STASSID "Dooryard Guest"
#define STAPSK  ""
#endif

//-------- User Settings -----------
const char* ssid     = STASSID;
const char* password = STAPSK;
const char* accesskey = "INITIAL STATE ACCESS KEY";
const char* bucketkey = "INITIAL STATE BUCKET KEY";
const char* signalname = "temperature";
//----------------------------------

HTTPClient ask;
int counter = 0;  
static bool hasWifi = false;

static void InitWifi()
{
  Serial.println("Connecting to...");
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  hasWifi = true;
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}


void setup() {
  Serial.begin(115200);
  Serial.println("ESP32 Device");
  Serial.println("Initializing...");

  // We start by connecting to a WiFi network

 // Initialize the WiFi module
  Serial.println(" > WiFi");
  hasWifi = false;
  InitWifi();
  if (!hasWifi)
  {
    return;
  }

  /* Explicitly set the ESP8266 to be a WiFi-client, otherwise, it by default,
     would try to act as both a client and an access-point and could cause
     network-issues with your other WiFi-devices on your WiFi-network. */
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void loop() {
  if (WiFi.status() == WL_CONNECTED) { //if we are connected
    counter = 0; //reset counter
    Serial.println("Wifi is still connected with IP: "); 
    Serial.println(WiFi.localIP());   //inform user about his IP address
  }else if (WiFi.status() != WL_CONNECTED) { //if we lost connection, retry
    WiFi.begin(ssid);      
  }
  while (WiFi.status() != WL_CONNECTED) { //during lost connection, print dots
    delay(500);
    Serial.print(".");
    counter++;
    if(counter>=60){ //30 seconds timeout - reset board
    ESP.restart();
    }
  }  
  
  // Reading temperature or humidity
  float t = dht.readTemperature(true);

// Create a URL for the request
  String url = "https://groker.init.st/api/events?accessKey=";
  url += accesskey;
  url += "&bucketKey=";
  url += bucketkey;
  url += "&";
  url += signalname;
  url += "=";
  url += t;

  Serial.print("*** requesting URL");
 
  // ask.begin;
  ask.begin(url); //Specify the URL
  
  //Check for the returning code
  int httpCode = ask.GET();       

  Serial.print(httpCode);
   
  if (httpCode > 0) { 
      String payload = ask.getString();
  } else {
      Serial.println("Error on HTTP request");
  }
  ask.end(); //End 
  Serial.println("*** End ");
  delay(10000);    // delay
}

So I changed gears and went with a BME sensor… I have the hardware working and got the code to load. I am still having issues trying to get the data to post to Initial State. can anyone take a look at my code and help me out…

#include <ESP8266WiFi.h>        // Include the Wi-Fi library
#include <Wire.h>
#include <ArduinoHttpClient.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>

#define SEALEVELPRESSURE_HPA (1013.25)



// API Endpoint URL
// https://groker.init.st/api/events?accessKey=ist_qGMBKJ6Z6xAMJIU0JZZitncLDKNoq3GM&bucketKey=V7NMMX9QFAX3
// Dashboard viewer
// https://go.init.st/fpyw3h7

float t = 0;
float p = 0;
float h = 0;
String url = "";

Adafruit_BME280 bme;

const char* ssid     = "Dooryard Guest";         // The SSID (name) of the Wi-Fi network you want to connect to
const char* password = "";     // The password of the Wi-Fi network
const char* accesskey = "ist_qGMBKJ6Z6xAMJIU0JZZitncLDKNoq3GM";
const char* bucketkey = "V7NMMX9QFAX3";
const char* signalname = "temperature";
char serverAddress[] = "groker.init.st/api/events";  // server address
int port = 443;

// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastTime = 0;
// Timer set to 10 minutes (600000)
//unsigned long timerDelay = 600000;
// Set timer to 5 seconds (5000)
unsigned long timerDelay = 5000;

WiFiClient wifi;
HttpClient client = HttpClient(wifi, serverAddress, port);

void setup() {
  Serial.begin(115200);         // Start the Serial communication to send messages to the computer
  delay(1000);
    if (!bme.begin(0x76)) {
      Serial.println("Could not find a valid BME280 sensor, check wiring!");
      while (1);
    Serial.println('\n');
    }
  
  WiFi.begin(ssid, password);             // Connect to the network
  Serial.print("Connecting to ");
  Serial.print(ssid); Serial.println(" ...");

  int i = 0;
  while (WiFi.status() != WL_CONNECTED) { // Wait for the Wi-Fi to connect
    delay(1000);
    Serial.print(++i); Serial.print(' ');
  }
  Serial.println('\n');
  Serial.println("Connection established!");  
  Serial.print("IP address:\t");
  Serial.println(WiFi.localIP());         // Send the IP address of the ESP8266 to the computer
 }


void loop() { 
getData();
//sendData();
SendToWeb();
printData();
  delay(300000);
  }



void getData() {
 t = bme.readTemperature();  //read temp and assign it to float 't'
 t = (t*1.8)+32;  //convert t to fahrenheit
 p = (bme.readPressure() / 100.0F);  //read pressure and assign to float 'p'
 h = bme.readHumidity();  //read humidity and assign to float 'h'
}

void BuildURL() {
  url = "groker.init.st/api/events?accessKey=";
  url += accesskey;
  url += "&bucketKey="; 
  url += bucketkey;
  url += "&";
  url += signalname;
  url += "=";
  url += t;
}

void SendToWeb() {
  Serial.println("making POST request");
  client.begin(url)
}

void printData() {
 Serial.println();
 Serial.println(t);
 Serial.println(p);
 Serial.print(h);
 Serial.print("%");
 //Serial.println();
 //Serial.println(url);
 Serial.println();
}

https?

Then start with the IDE Example

ESP8266HTTPClient / BasicHTTPSClient.ino

leave away the sensor in the first step.
just care about the client itself.

I tried that but the URL http://groker.init.st/ shows Not Secure in Chrome…

-Jason

Got it working with this code: its not elegant but it works :slight_smile:

#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClientSecureBearSSL.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>

// Fingerprint if failing check https://www.grc.com/fingerprints.htm  using groker.init.st
const uint8_t fingerprint[20] = {0x26, 0x79, 0x30, 0x97, 0xD3, 0xDF, 0x5A, 0x60, 0x55, 0xC2, 0xFC, 0x29, 0xB4, 0x12, 0x8B, 0x56, 0x28, 0xB4, 0xE6, 0x05};

float t = 0;
float p = 0;
float h = 0;
String url = "";

Adafruit_BME280 bme;

const char* accesskey = "xxxxxxxxxxxxxxxxxxx";
const char* bucketkey = "xxxxxxxxxxxxx";
const char* signalname1 = "Temperature";
const char* signalname2 = "Humidity";
 

// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastTime = 0;
// Timer set to 1 minute (60000)
unsigned long timerDelay = 60000;


ESP8266WiFiMulti WiFiMulti;

void setup() {

  Serial.begin(115200);
    delay(1000);
    if (!bme.begin(0x76)) {
      Serial.println("Could not find a valid BME280 sensor, check wiring!");
      while (1);
    Serial.println('\n');
    }


  for (uint8_t t = 4; t > 0; t--) {
    Serial.printf("[SETUP] WAIT %d...\n", t);
    Serial.flush();
    delay(1000);
  }

  WiFi.mode(WIFI_STA);
  WiFiMulti.addAP("Dooryard Guest", "");
}

void loop() {
  if ((millis() - lastTime) > timerDelay) {
    getData();
    buildURL();
    connectSend();
    lastTime = millis();
  }
}

void connectSend() {
if ((WiFiMulti.run() == WL_CONNECTED)) {

    Serial.println("connected to WiFi!");

    std::unique_ptr<BearSSL::WiFiClientSecure>client(new BearSSL::WiFiClientSecure);

    client->setFingerprint(fingerprint);

    HTTPClient https;
    
    getData();
    buildURL();
    
    Serial.print("[HTTPS] begin...\n");
    if (https.begin(*client, url)) {  // HTTPS

      Serial.print("[HTTPS] GET...\n");
      // start connection and send HTTP header
      int httpCode = https.GET();

      // httpCode will be negative on error
      if (httpCode > 0) {
        // HTTP header has been send and Server response header has been handled
        Serial.printf("[HTTPS] GET... code: %d\n", httpCode);

        // file found at server
        if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
          String payload = https.getString();
          Serial.println(payload);
        }
      } else {
        Serial.printf("[HTTPS] GET... failed, error: %s\n", https.errorToString(httpCode).c_str());
      }

      https.end();
    } else {
      Serial.printf("[HTTPS] Unable to connect\n");
    }
  }
  Serial.print("url:");
  Serial.println(url);
}

void getData() {
 t = bme.readTemperature();  //read temp and assign it to float 't'
 t = (t*1.8)+32;  //convert t to fahrenheit
 p = (bme.readPressure() / 100.0F);  //read pressure and assign to float 'p'
 h = bme.readHumidity();  //read humidity and assign to float 'h'
}

void buildURL() {
  url = "https://groker.init.st/api/events?accessKey=";
  url += accesskey;
  url += "&bucketKey="; 
  url += bucketkey;
  url += "&";
  url += signalname1;
  url += "=";
  url += t;
  url += "&";
  url += signalname2;
  url += "=";
  url += h;
}