blnkjns wrote:Can you give the complete code? Please use code-tags on the actual code, not an image.
I am SO glad to get some help with this because I have tried to solve this for several days and still the problem persists. This evening I deleted all of my feeds, group, dashboard and recreated them. Nothing makes any sense. If I grip the DHT22 sensor with my fingers, the humidity value increases. Anyway, here is the entire sketch:
Code: Select all
/*
*
* FEATHER MO WIFI board sketch
*
*
* 11 MAY 2021 FEATHER BOARD NEEDS WIFI TO WORK **********
*
* ******* >>> NOTE! using Feather MO Wifi BOARD **********
*
* This sketch is a copy of the following sketch:
* NEW-mqtt-winc1500-11may21-V08
*
* Note that this sketch has never run the ultrasonic sensor so
* will need to update the libraries and hardware code.
*
* Note will be copying sensor code from the following sketch -
* talavera-working-v04-6may2021
*
* to add to this sketch to make the ultrasonic sensor work.
*
* 12 MAY 21 @ 1753H
* I have learned how to throttle back the temp/humidity sensor, but
* not the ultrasonic sensor. May have to save the ultrasoni sensor
* data in an array and publish it when we want to. will see.
*
*
*
*/
#include <Adafruit_SSD1306.h> // required for ultrasonic sensor
#include <splash.h> // this was added automatically with the above lib???
#include <SPI.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#include <WiFi101.h>
#include "DHT.h"; // this required for the temp/humid sensor ...........NO problem!!
#include <DHT_U.h> // NEW 9MAY21 .............
#include <Adafruit_Sensor.h> // 9MAY21 Just installed this lib per DWShop video!!
#define DHTPIN 11 // this is output pin of sensor Changed from pin#11
#define DHTTYPE DHT22
DHT_Unified dht(DHTPIN, DHTTYPE); // Initialize the sensor just added '_Unified'
uint32_t delayMS; // Used to delay the DHT22 temp/humidity sensor
float hum;
float temp;
//************************* WiFI Setup **************************************
#define WINC_CS 8
#define WINC_IRQ 7
#define WINC_RST 4
#define WINC_EN 2 // or, tie EN to VCC
// *********************** MY CREDS ****************************************
char ssid[] = "CenturyLink3132"; // your network SSID (name)
char pass[] = "a7h78dc8eh4ycc"; // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0; // your network key Index number (needed only for WEP)
// ********************* TALAVERA CREDS ***********************************
//char ssid[] = "WT1"; // your network SSID (name)
//char pass[] = "Talavera1"; // your network password (use for WPA, or use as key for WEP)
//int keyIndex = 0; // your network key Index number (needed only for WEP)
int status = WL_IDLE_STATUS;
// ************************* Adafruit.io Setup *********************************
#define AIO_SERVER "io.adafruit.com"
#define AIO_SERVERPORT 1883
#define AIO_USERNAME "_phillip"
#define AIO_KEY "aio_bqmF81UmWJqErdIcT3StKZ8kzvJA"
// ************ Global State Setup ******************************************
//Set up the wifi client
WiFiClient client;
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
// You don't need to change anything below this line!
#define halt(s) { Serial.println(F( s )); while(1); }
/****************************** Feeds ***************************************/
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
//Adafruit_MQTT_Publish photocell = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/photocell");
// Publish temp creds ............
Adafruit_MQTT_Publish temperature = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/mqtt-test.temperature");
// Publish humidty data creds ............
Adafruit_MQTT_Publish humidity = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/mqtt-test.humidity");
// Publish tank level data creds ............
Adafruit_MQTT_Publish distance = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/mqtt-test.distance");
// Setup a feed called 'onoff' for subscribing to changes.
Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/onoff");
// Note. I do not think I need the above line but data rate is too high, got warning from AIO!!!!
// Adding this line eliminated a compile error. 21MAY21 @ 1312H
#define LEDPIN 13
// Print humidity and temperature function Keep this or not ???? ....................
//
/*
void hum_temp_sensor(float hum, float temp)
{
Serial.print("Humidity: ");
Serial.print(hum);
Serial.print(" %, Temp: ");
Serial.print(temp);
Serial.println(" Celsius");
// Degrees celsius to fahrenheit
// The following is my code
float temp_F = (temp * 1.8) + 32; // where 9/5 = 1.8
Serial.print("\t\t\tTemp: ");
Serial.print(temp_F);
Serial.println(" Fahrenheit");
}
*/
void setup() {
while (!Serial); // Do not start until the serial monitor is open
Serial.begin(115200);
dht.begin(); // Activate the DHT22 sensor.
// Print temperature sensor component details. Not necessary!
sensor_t sensor;
dht.temperature().getSensor(&sensor); // NEW ...
Serial.println(F("------------------------------------"));
Serial.println(F("Temperature Sensor"));
Serial.print (F("Sensor Type: ")); Serial.println(sensor.name);
Serial.print (F("Driver Ver: ")); Serial.println(sensor.version);
Serial.print (F("Unique ID: ")); Serial.println(sensor.sensor_id);
Serial.print (F("Max Value: ")); Serial.print(sensor.max_value); Serial.println(F("°C"));
Serial.print (F("Min Value: ")); Serial.print(sensor.min_value); Serial.println(F("°C"));
Serial.print (F("Resolution: ")); Serial.print(sensor.resolution); Serial.println(F("°C"));
Serial.println(F("------------------------------------"));
// Print humidity sensor details.
dht.humidity().getSensor(&sensor);
Serial.println(F("Humidity Sensor"));
Serial.print (F("Sensor Type: ")); Serial.println(sensor.name);
Serial.print (F("Driver Ver: ")); Serial.println(sensor.version);
Serial.print (F("Unique ID: ")); Serial.println(sensor.sensor_id);
Serial.print (F("Max Value: ")); Serial.print(sensor.max_value); Serial.println(F("%"));
Serial.print (F("Min Value: ")); Serial.print(sensor.min_value); Serial.println(F("%"));
Serial.print (F("Resolution: ")); Serial.print(sensor.resolution); Serial.println(F("%"));
Serial.println(F("------------------------------------"));
delayMS = sensor.min_delay / 500; // Was 1000 ...
WiFi.setPins(WINC_CS, WINC_IRQ, WINC_RST, WINC_EN);
Serial.println(F("Adafruit MQTT demo for WINC1500"));
// Initialise the Client
Serial.print(F("\nInit the WiFi module..."));
// check for the presence of the breakout
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WINC1500 not present");
// don't continue:
while (true);
}
Serial.println("ATWINC OK!");
pinMode(LEDPIN, OUTPUT);
// mqtt.subscribe(&onoffbutton);
}
void loop() // ============================== LOOP() ==============================
{
delay(delayMS); // NEW ......................
sensors_event_t event;
// Ensure the connection to the MQTT server is alive (this will make the first
// connection and automatically reconnect when disconnected). See the MQTT_connect
// function definition further below.
MQTT_connect();
// this is our 'wait for incoming subscription packets' busy subloop
Adafruit_MQTT_Subscribe *subscription;
while ((subscription = mqtt.readSubscription(5000))) {
if (subscription == &onoffbutton) {
Serial.print(F("Got: "));
Serial.println((char *)onoffbutton.lastread);
if (0 == strcmp((char *)onoffbutton.lastread, "OFF")) {
digitalWrite(LEDPIN, LOW);
}
if (0 == strcmp((char *)onoffbutton.lastread, "ON")) {
digitalWrite(LEDPIN, HIGH);
}
}
}
// Print temp and humiidity data to serial monitor ...
// Following code does NOT WORK!!!!!!!!!!!!
// Do not need the following code .................
/*
Serial.print(F("\n Temperature value is "));
Serial.print(event.temperature);
Serial.print("...\n");
Serial.print(F("\n\t\t Humidity VALUE??? is "));
//Serial.print(event.relative_humidity); // orig code .......
Serial.print(event.temperature);
Serial.print("...\n");
*/
// Get TEMPERATURE event and print its value ...................
dht.temperature().getEvent(&event);
if (isnan(event.temperature)) {
Serial.println(F("Error reading temperature!"));
}
else {
Serial.print(F("TEMPERATURE: "));
Serial.print(event.temperature);
Serial.println(F("°C"));
}
// Get HUMIDITY event and print its value ......................
dht.humidity().getEvent(&event);
if (isnan(event.relative_humidity)) {
Serial.println(F("Error reading humidity!"));
}
else {
Serial.print(F("\t\tHUMIDITY: "));
Serial.print(event.relative_humidity);
Serial.println(F("%"));
}
// ====== end of getEvent() ====================================
// PUBLISH TEMPERATURE data ...................... PUBLISH ...
if (!temperature.publish(event.temperature)) {
Serial.println(F("Failed"));
} else {
Serial.println(F("Published Temperature. OK!\n"));
}
// PUBLISH HUMIDIITY data ...
if (!humidity.publish(event.relative_humidity) ) {
Serial.println(F("Failed"));
} else {
Serial.println(F("\tPublished Humidity. OK!\n"));
}
/*
// PUBLISH DISTANCE data ...
if (!distance.publish(event.distance) ) {
Serial.println(F("Failed"));
} else {
Serial.println(F("\tPublished Distance. OK!\n"));
}
*/
// ======= END OF NEW UNIFIED_SENSOR CODE ====================
}
// Function to connect and reconnect as necessary to the MQTT server.
// Should be called in the loop function and it will take care if connecting.
void MQTT_connect() {
int8_t ret;
// attempt to connect to Wifi network:
while (WiFi.status() != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
uint8_t timeout = 10;
while (timeout && (WiFi.status() != WL_CONNECTED)) {
timeout--;
delay(1000);
}
}
// Stop if already connected.
if (mqtt.connected()) {
return;
}
Serial.print("Connecting to MQTT... ");
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
Serial.println(mqtt.connectErrorString(ret));
Serial.println("Retrying MQTT connection in 5 seconds...");
mqtt.disconnect();
delay(5000); // wait 5 seconds
}
Serial.println("MQTT Connected!\n");
}
Here is a grab of the serial monitor's output after I had my fingers on the sensor. The humidity value went up and not the temp.
- hum-temp-data-grab.png (247.11 KiB) Viewed 434 times
In addition, the temperature feed never has worked, only the humidity feed works. I have deleted and replaced them several times and nothing seems to help.
I will remain awake in case you have any more questions. Thank you so much for your help.