๐ŸŽ„โ›„โ„๏ธ๐ŸŽ It's beginning to look a lot like ADABOX-mas! The next ADABOX from Adafruit ships soon! Get yours now, or give as a gift! ๐ŸŽ„โ›„โ„๏ธ๐ŸŽ
0

Sketch for Temperature and ultrasonic sensor
Moderators: adafruit_support_bill, adafruit

Forum rules
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.
Please be positive and constructive with your questions and comments.

Sketch for Temperature and ultrasonic sensor

by Miniot on Thu Oct 17, 2019 10:10 am

Hi,

Sorry for asking, but im not able to get my head around this.

I have a project with a NodeMCU 8266 for monitoring my garageport using a HC-SR04 and also getting the temperature and humidity from my DHT11 sensor.

I have a sketch that works for them both individualy - and im able to monitor them in my Adafruit IO dashboard.

When i try to combine the sketches - im not even able to compile it.

So please - Does anyone have a sketch /config that containes both of these?

Thank You!

Miniot
 
Posts: 4
Joined: Thu Oct 10, 2019 3:17 pm

Re: Sketch for Temperature and ultrasonic sensor

by brubell on Thu Oct 17, 2019 10:25 am

Hi Miniot,

Could you please post the full code which doesn't compile along with the compilation errors from the Arduino IDE so we can debug it together?

brubell
 
Posts: 609
Joined: Fri Jul 17, 2015 10:33 pm

Re: Sketch for Temperature and ultrasonic sensor

by Miniot on Thu Oct 17, 2019 3:32 pm

brubell wrote:Hi Miniot,

Could you please post the full code which doesn't compile along with the compilation errors from the Arduino IDE so we can debug it together?



Thank you for that! really appriciate it!

Now the sketch compiles and uploads as it should and gives me readings in serial monitor.

I would appriciate help with how to get the distance (HC-SR04) to publish to adafruit io.

Adding the code and also a "snip" from the serial monitor.
Code: Select all | TOGGLE FULL SIZE
// Libraries
#include <ESP8266WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#include "DHT.h"

// DHT 11 sensor
#define DHTPIN 2
#define DHTTYPE DHT11

// defines pins numbers
const int trigPin = 4;  //D2
const int echoPin = 0;  //D3

// defines variables
long duration;
int distance;

// WiFi parameters
#define WLAN_SSID       "xxxxxxxxxxxxxxx"
#define WLAN_PASS       "abcd123456789abcd"

// Adafruit IO
#define AIO_SERVER      "io.adafruit.com"
#define AIO_SERVERPORT  1883
#define AIO_USERNAME    "Miniot"
#define AIO_KEY         "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"  // Obtained from account info on io.adafruit.com

// DHT sensor
DHT dht(DHTPIN, DHTTYPE, 15);

// Create an ESP8266 WiFiClient class to connect to the MQTT server.
WiFiClient client;

// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);

// Setup feeds for temperature & humidity
Adafruit_MQTT_Publish temperature = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/temperaturebaat");
Adafruit_MQTT_Publish humidity = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/humiditybaat");

/*************************** Sketch Code ************************************/

void setup() {

  // Init sensor
  dht.begin();

  Serial.begin(115200);
  Serial.println(F("Adafruit IO Example"));

  // Connect to WiFi access point.
  Serial.println(); Serial.println();
  delay(10);
  Serial.print(F("Connecting to "));
  Serial.println(WLAN_SSID);
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin, INPUT); // Sets the echoPin as an Input
  Serial.begin(9600); // Starts the serial communication

  WiFi.begin(WLAN_SSID, WLAN_PASS);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(F("."));
  }
  Serial.println();

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

  // connect to adafruit io
  connect();

}

void loop() {
  // Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);

// Calculating the distance
distance= duration*0.034/2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
delay(2000);

  // ping adafruit io a few times to make sure we remain connected
  if(! mqtt.ping(3)) {
    // reconnect to adafruit io
    if(! mqtt.connected())
      connect();
  }

  // Grab the current state of the sensor
  int humidity_data = (int)dht.readHumidity();
  int temperature_data = (int)dht.readTemperature();

  // By default, the temperature report is in Celsius, for Fahrenheit uncomment
  //    following line.
  // temperature_data = temperature_data*(9.0/5.0) + 32.0;

  // Publish data
  if (! temperature.publish(temperature_data))
    Serial.println(F("Failed to publish temperature"));
  else
    Serial.println(F("Temperature published!"));

  if (! humidity.publish(humidity_data))
    Serial.println(F("Failed to publish humidity"));
  else
    Serial.println(F("Humidity published!"));

  // Repeat every 10 seconds
  delay(10000);

}

// connect to adafruit io via MQTT
void connect() {

  Serial.print(F("Connecting to Adafruit IO... "));

  int8_t ret;

  while ((ret = mqtt.connect()) != 0) {

    switch (ret) {
      case 1: Serial.println(F("Wrong protocol")); break;
      case 2: Serial.println(F("ID rejected")); break;
      case 3: Serial.println(F("Server unavail")); break;
      case 4: Serial.println(F("Bad user/pass")); break;
      case 5: Serial.println(F("Not authed")); break;
      case 6: Serial.println(F("Failed to subscribe")); break;
      default: Serial.println(F("Connection failed")); break;
    }

    if(ret >= 0)
      mqtt.disconnect();

    Serial.println(F("Retrying connection..."));
    delay(5000);

  }

  Serial.println(F("Adafruit IO Connected!"));

}




From Serial Monitor:


Distance: 2264
Temperature published!
Humidity published!
Distance: 20
Temperature published!
Humidity published!
Distance: 22
Temperature published!
Humidity published!
Distance: 2264
Temperature published!
Humidity published!
Distance: 43
Temperature published!
Humidity published!



Thanks!

Miniot
 
Posts: 4
Joined: Thu Oct 10, 2019 3:17 pm

Re: Sketch for Temperature and ultrasonic sensor

by brubell on Thu Oct 17, 2019 3:39 pm

I would appriciate help with how to get the distance (HC-SR04) to publish to adafruit io.


Ok, so we are first going to declare a feed for the distance sensor, named distance. Make sure this feed exists in your Adafruit IO feed listing (on Adafruit IO Web).
Code: Select all | TOGGLE FULL SIZE
Adafruit_MQTT_Publish distanceFeed = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/distance");


^ also, place this underneath your existing MQTT feed setup (your code references these as // Setup feeds for temperature & humidity)

In your loop, underneath the // Publish data, add an ioClient.publish() call specifically for the distanceFeed you made above:
Code: Select all | TOGGLE FULL SIZE
  // Publish data
  if (! distanceFeed.publish(distance))
    Serial.println(F("Failed to publish distance value"));
  else
    Serial.println(F("Distance published!"));


Let me know how it goes, or if you get another error please the output from your serial monitor.

brubell
 
Posts: 609
Joined: Fri Jul 17, 2015 10:33 pm

Re: Sketch for Temperature and ultrasonic sensor

by Miniot on Thu Oct 17, 2019 4:41 pm

Brubell - Thank you!!!

I do not know what i owe you - but i owe you something!

Again - Thank you!

Miniot
 
Posts: 4
Joined: Thu Oct 10, 2019 3:17 pm

Re: Sketch for Temperature and ultrasonic sensor

by brubell on Thu Oct 17, 2019 4:46 pm

Miniot wrote:Brubell - Thank you!!!

I do not know what i owe you - but i owe you something!

Again - Thank you!


I work for Adafruit on Adafruit IO, you don't owe me anything. That's what we're here for. (although..IO+ is a great way to support this service)

Feel free to ask me any other questions you may have related to this project in this thread. If you encounter other issues - please open a new thread and we'll take it from there!

brubell
 
Posts: 609
Joined: Fri Jul 17, 2015 10:33 pm

Re: Sketch for Temperature and ultrasonic sensor

by Miniot on Thu Oct 17, 2019 5:13 pm

:-) already have IO+

Miniot
 
Posts: 4
Joined: Thu Oct 10, 2019 3:17 pm

Please be positive and constructive with your questions and comments.