Hello, I'm using a Heltec Wifi Kit 8 and a HC-SR04 to monitor the level of the water in a container. I'd like it to sense the level and display it as a percentage of full on the OLED display. Simultaneously, I'd like it to send the percentage to Adafruit.io and graph it.
When I attach the HC-SR04 (VCC--> 5V, GND-->GND, Trig --> D7 and Echo --> D6), I am only able get a readout of 100% on the OLED and AIO. I have it working if i comment out the "Read signal from sensor" lines and set duration to 1300. It can convert this to a percentage and sends it to AIO.
I'm pretty sure I have some unnecessary lines in there somewhere, as well as not being able to get a true readout. Can anyone tell me where I've gone wrong?
Thanks in advance
//WiFi
#define WLAN_SSID "XXXXXXXXXXX"
#define WLAN_PASS "XXXXXXXXXXX"
#include <ESP8266WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"#define AIO_SERVER "io.adafruit.com"
#define AIO_SERVERPORT 1883 // use 8883 for SSL
#define AIO_USERNAME "XXXXXXXXXXX"
#define AIO_KEY "XXXXXXXXXXX"WiFiClient client;
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
Adafruit_MQTT_Publish reservoir = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/reservoir");//Sensor
#include <Arduino.h>
#include <U8g2lib.h>
#include <hcsr04.h>#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endifU8G2_SSD1306_128X32_UNIVISION_F_HW_I2C u8g2(U8G2_R0, /* reset=/ 16, / clock=/ 5, / data=*/ 4);
#define trigPin 12 //Trigger D1 Pin on ESP8266
#define echoPin 13 //Echo Tx Pin on ESP8266
long duration; //Length of time between pulse and return
float distance; //Distance to water
const float maxheight = 23.50; //Distance between bottom of reservoir and sensor
float howfull1; //Water in reservoir
float howfull2; //Reservoir fill as decimal
uint32_t howfull3 = 0; //Reservoir fill as percentage (not required)
long wait = 3000;void MQTT_connect();
void setup() {
u8g2.begin();
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);WiFi.begin(WLAN_SSID, WLAN_PASS);
Serial.begin(115200);
delay(10);}
void loop() {
MQTT_connect();
//Send Ultrasonic Pulse
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);//Read Signal From Sensor
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);// Convert the time into a distance to water surface
distance = (duration/2.0) / 29.1;//Convert distance to into a percentage of full
howfull1 = maxheight - distance;
howfull2 = howfull1 / maxheight;
howfull3 = howfull2 * 100.0;Serial.print(F("\nSending photocell val "));
Serial.print(howfull3);
Serial.print("...");
if (! reservoir.publish(howfull3)) {
Serial.println(F("Failed"));
} else {
Serial.println(F("OK!"));
}u8g2.setFont(u8g2_font_fub20_tr);
u8g2.firstPage();
do {
u8g2.setCursor(0, 30);
u8g2.print(howfull3);
u8g2.print("%");} while ( u8g2.nextPage() );
delay(wait);}
void MQTT_connect() {
int8_t ret;// Stop if already connected.
if (mqtt.connected()) {
return;
}Serial.print("Connecting to MQTT... ");
uint8_t retries = 3;
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
retries--;
if (retries == 0) {
// basically die and wait for WDT to reset me
while (1);
}
}
Serial.println("MQTT Connected!");
}