Hi,
I've been trying to figure out what's wrong with this code for a few weeks now and it's time I admit defeat. I can't code. If someone was able to help me with this I'd gladly make payment via PayPal.
Essentially it's a ESP8266 what connects to Wifi and MQTT broker. Subscribes to topic and publishes the value on a oled display.
I have no problems editing the user specific information such as wifi and broker credentials.
When I upload this using Arduino and check the serial monitor I see "connecting to SKY11223..." every 10 seconds but it never actually connects.
(SKY11223 is the name of my ssid)
My MQTT broker is already set up and is publishing the values I hope to view on the oled every 30 second.
I'm using a nodemcu dev board but it's basically just an ESP12. I have two displays, one is an SPI and the other is I2C depending which is easier to implement.
I'm serious about getting this working. I've spend a lot of time and money on this only to fail and I'll be more than happy to pay someone if they can help me.
Code is below (unedited original from GitHub)
/*
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();
}
}
I had some issues with the pubsubclient.h which prevented it from compiling. I found it would do so if I used this version:
Many thanks
Andrew