Skip to content

Instantly share code, notes, and snippets.

@jeffeb3
Created November 9, 2017 17:58
Show Gist options
  • Save jeffeb3/6d2b872fabf823f4922c43cfe4b1b887 to your computer and use it in GitHub Desktop.
Save jeffeb3/6d2b872fabf823f4922c43cfe4b1b887 to your computer and use it in GitHub Desktop.
Example for the ESPBackBone library I'm writing
#include <ESP32BackBone.h>
double value1(0.0);
double value2(0.0);
// Converts the voltage value to 0-100 (used in the display code)
double progress(double inValue)
{
const double MIN = 0.0;
const double MAX = 3.3;
if (inValue < MIN)
{
return 0;
}
if (inValue > MAX)
{
return 100;
}
return (inValue - MIN)*100.0/(MAX-MIN);
}
void setup() {
// "Required" before starting anything else in the backbone.
espbb::setup();
// Configure the wifi using build flags (or you can replace these flags with your wifi info, as
// strings like "ssid", "secret".
espbb::setWiFi(MY_SSID, MY_WIFI_PASSWORD);
// Configure OTA.
espbb::setOta();
// Configure Mqtt
espbb::setMqtt(IPAddress(MY_MQTT_ADDRESS), "AnalogMQTT");
// Test with:
// mosquitto_pub -h 10.0.2.50 -t /something -m "123456789012345678901234567890"
espbb::subscribe(String("/something"),
[](byte* payload, unsigned int length)
{
payload[length] = '\0';
debugPrintln("[Main]:\tMQTT Rx: " + String((char*)payload));
});
// This is a little fishy. Either you have the esp32 with an OLED display, in which case, you
// would set the DISPLAY_SSD1306 flag, and wouldn't need to check it, or you don't have the
// display, in which case, you wouldn't write this at all. For the sake of this example, I have
// written it, and put a flag around it. Sorry, bad examples :(
#ifdef DISPLAY_SSD1306
espbb::setDisplay(
[](SSD1306& display)
{
display.setTextAlignment(TEXT_ALIGN_LEFT);
display.setFont(ArialMT_Plain_16);
display.drawString(0, 0, String(value1));
display.drawProgressBar(0, 20, 127, 5, progress(value1));
display.setFont(ArialMT_Plain_16);
display.drawString(0, 27, String(value2));
display.drawProgressBar(0, 46, 127, 5, progress(value2));
});
#endif // display
}
void loop() {
// Update the data used in the display. In this case, I'm doing some analog reading.
// Keep in mind that ADC2 (pins 8+?) aren't currently readable when you have wifi turned on.
value1 = static_cast<double>(analogRead(A0)) * 3.3 / 4096.0;
value2 = static_cast<double>(analogRead(A3)) * 3.3 / 4096.0;
// Test publishing something.
// Test with:
// mosquitto_sub -h 10.0.2.50 -t /value1
espbb::publish("/value1", String(value1));
// This is how I print to the serial (when I have the BBDEBUG flag set).
//
// Keep in mind that if you have the display, then the function defined in setup will be used to
// display these values on the screen, with a progress bar.
debugPrintln("[Main]:\tSampled 0: " + String(value1) + " 3: " + String(value2));
// This will slow down the loop(), This will sleep for 10 seconds, so the loop will run a little
// slower than 10 seconds.
vTaskDelay(10 * 1000 / portTICK_PERIOD_MS); // pause this task for 10 seconds.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment