|
/* |
|
by Matthew Bordignon Feb 2016 (@bordignon on twitter) |
|
|
|
Quick demo for displaying temperature on oled screen. |
|
|
|
- connects to wifi |
|
- connects to an MQTT server |
|
- subscribes to topic |
|
- displays temperature (needs to be in xx.xx format) |
|
|
|
Based on the built-in examples from ESP8266 MQTT lib and the SPI OLED ESP8266 lib from somhi. |
|
*/ |
|
|
|
// MQTT and wifi libs |
|
#include <ESP8266WiFi.h> |
|
#include <PubSubClient.h> // using v1.65. v2 from the esp8266 arduino library didn't work for me for some reason. |
|
|
|
// OLED libs |
|
#include <ESP_SSD1306.h> // Modification of Adafruit_SSD1306 for ESP8266 compatibility -> https://github.com/somhi/ESP_SSD1306 |
|
#include <Adafruit_GFX.h> // Needs a little change in original Adafruit library (See README.txt file) --> https://github.com/adafruit/Adafruit-GFX-Library |
|
#include <SPI.h> // For SPI comm (needed for not getting compile error) |
|
#include <Wire.h> // For I2C comm, but needed for not getting compile error |
|
|
|
/*HardWare ESP8266 SPI pins --> https://raw.githubusercontent.com/nodemcu/nodemcu-devkit-v1.0/master/Documents/NODEMCU_DEVKIT_V1.0_PINMAP.png |
|
Fixed.. |
|
D5 (GPIO14) -> D0 pin OLED display (CLK) |
|
D7 (GPIO13) -> D1 pin OLED display (MOSI) |
|
*/ |
|
|
|
// Pin definitions for OLED spi |
|
#define OLED_CS 15 // D8 (GPIO15) -> CS (Chip select) pin OLED display |
|
#define OLED_DC 2 // D4 (GPIO2) -> DC (Digital signal) pin OLED display |
|
#define OLED_RESET 16 // D0 (GPIO16) -> RESET pin OLED display |
|
|
|
ESP_SSD1306 display(OLED_DC, OLED_RESET, OLED_CS); // FOR SPI |
|
|
|
|
|
// WIFI setup |
|
const char *ssid = "xxxxxxxxxx"; // cannot be longer than 32 characters! |
|
const char *pass = "xxxxxxxxxx"; // |
|
|
|
// MQTT broker |
|
IPAddress server(192, 168, 4, 24); |
|
|
|
|
|
#define BUFFER_SIZE 100 |
|
|
|
void callback(const MQTT::Publish& pub) { |
|
Serial.print(pub.topic()); |
|
Serial.print(" => "); |
|
if (pub.has_stream()) { |
|
// probably don't need this, just using it because it is part of the example |
|
uint8_t buf[BUFFER_SIZE]; |
|
int read; |
|
while (read = pub.payload_stream()->read(buf, BUFFER_SIZE)) { |
|
Serial.write(buf, read); |
|
} |
|
pub.payload_stream()->stop(); |
|
Serial.println(""); |
|
} else |
|
Serial.println(pub.payload_string()); |
|
display.clearDisplay(); |
|
display.setTextSize(3); |
|
display.setTextColor(WHITE); |
|
display.setCursor(20, 10); |
|
display.println(pub.payload_string()); |
|
display.display(); |
|
delay(2000); |
|
} |
|
|
|
WiFiClient wclient; |
|
PubSubClient client(wclient, server); |
|
|
|
void setup() { |
|
// Setup console |
|
Serial.begin(115200); |
|
delay(10); |
|
Serial.println(); |
|
Serial.println(); |
|
|
|
// SSD1306 Init |
|
display.begin(SSD1306_SWITCHCAPVCC); // Switch OLED |
|
// Clear the buffer. |
|
display.clearDisplay(); |
|
// draw a single pixel |
|
display.drawPixel(10, 10, WHITE); |
|
// Show the display buffer on the hardware. |
|
// NOTE: You _must_ call display after making any drawing commands |
|
// to make them visible on the display hardware! |
|
display.display(); |
|
delay(2000); |
|
display.clearDisplay(); |
|
} |
|
|
|
void loop() { |
|
if (WiFi.status() != WL_CONNECTED) { |
|
Serial.print("Connecting to "); |
|
Serial.print(ssid); |
|
Serial.println("..."); |
|
WiFi.begin(ssid, pass); |
|
|
|
if (WiFi.waitForConnectResult() != WL_CONNECTED) |
|
return; |
|
Serial.println("WiFi connected"); |
|
display.clearDisplay(); |
|
display.setTextSize(1); |
|
display.setTextColor(WHITE); |
|
display.setCursor(0, 0); |
|
display.println("WiFi connected"); |
|
display.display(); |
|
delay(2000); |
|
} |
|
|
|
if (WiFi.status() == WL_CONNECTED) { |
|
if (!client.connected()) { |
|
if (client.connect("esp_temp")) { |
|
client.set_callback(callback); |
|
client.subscribe("house/outside/temp/current"); // expecting a decimal, eg 25.54 degrees |
|
display.setCursor(0, 10); |
|
display.println("MQTT Broker connected"); |
|
display.display(); |
|
delay(2000); |
|
} |
|
} |
|
|
|
if (client.connected()) |
|
client.loop(); |
|
} |
|
} |