Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rburnett3/2545f1b2f0d2e2540aa0e10c700db7bc to your computer and use it in GitHub Desktop.
Save rburnett3/2545f1b2f0d2e2540aa0e10c700db7bc to your computer and use it in GitHub Desktop.
sends a distance value between an ultrasonic sensor and on the surface of the water detected by an MB7389 HRXL-MaxSonar-WRMT sensor. Then, the value will be managed in Ubidots to calculate the volume of free-flowing substances in the tank.
/*************************************************************************************************
* This Example sends hardcoded data to Ubidots using an ESP32-DevKitC. The code sends a distance
* value between an ultrasonic sensor and on the surface of the water detected by an MB7389
* HRXL-MaxSonar-WRMT sensor. Then, the value will be managed in Ubidots to calculate the volume
* of free-flowing substances in the tank.
*
* This example is given AS IT IS without any warranty.
*
* Made by María Carlina Hernandez.
* Nov 28, 2018.
*************************************************************************************************/
/****************************************
* Include Libraries
****************************************/
#include <WiFi.h>
#include <PubSubClient.h>
/****************************************
* Define Constants
****************************************/
namespace {
const char * WIFISSID = "xxxxx"; // Put your WifiSSID here
const char * PASSWORD = "xxxxx"; // Put your wifi password here
const char * TOKEN = "xxxxx"; // Put your Ubidots' TOKEN
const char * MQTT_CLIENT_NAME = "xxxxx"; // MQTT client Name, assign an unique 8-12 alphanumeric character ASCII string;
const char * VARIABLE_LABEL = "distance"; // Assign the variable label
const char * DEVICE_LABEL = "esp32"; // The MAC address of the device will be assigned as device label
const char * MQTT_BROKER = "industrial.api.ubidots.com";
const int ANALOG_PIN_0 = 36; // ADC1_0 GPIO36
int analog_value, distance_mm, distance_cm;
}
/* Sensor's declarations */
int distance;
/* Space to store the request */
char payload[50];
char topic[50];
/****************************************
* Auxiliar Functions
****************************************/
WiFiClient ubidots;
PubSubClient client(ubidots);
void callback(char* topic, byte* payload, unsigned int length) {
char p[length + 1];
memcpy(p, payload, length);
p[length] = NULL;
String message(p);
Serial.write(payload, length);
Serial.println(topic);
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.println("Attempting MQTT connection...");
// Attemp to connect
if (client.connect(MQTT_CLIENT_NAME, TOKEN, "")) {
Serial.println("Connected");
} else {
Serial.print("Failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 2 seconds");
// Wait 2 seconds before retrying
delay(2000);
}
}
}
/****************************************
* Sensor Functions
****************************************/
int readDistance() {
analog_value = analogRead(ANALOG_PIN_0);
distance_mm = analog_value;
distance_cm = distance_mm / 10;
return distance_cm;
}
/****************************************
* Main Functions
****************************************/
void setup() {
Serial.begin(115200);
WiFi.begin(WIFISSID, PASSWORD);
Serial.println();
Serial.print("Wait for WiFi...");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println("");
Serial.println("WiFi Connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
client.setServer(MQTT_BROKER, 1883);
client.setCallback(callback);
}
void loop() {
if (!client.connected()) {
reconnect();
}
/* call the funtion readDistance() */
distance = readDistance();
/* Building the Ubidots request */
sprintf(topic, "%s%s", "/v1.6/devices/", DEVICE_LABEL);
sprintf(payload, "%s", ""); // Cleans the payload
sprintf(payload, "{\"%s\": %d}", VARIABLE_LABEL, distance); // Adds the variable label
Serial.println(topic);
/* Print the sensor reading to the Serial Monitor */
Serial.println("Publishing values to Ubidots Cloud");
Serial.print("Distance = ");
Serial.println(distance);
/* Publish the request to Ubidots */
client.publish(topic, payload);
client.loop();
delay(1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment