Can anyone see the problem?

Can someone more experienced than myself stop what I’ve done wrong with this…

all I get from the serial monitor is “connecting to SKY79424…” over and over. SKY79424 is ssid.

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <ESP_SSD1306.h>
#include <Adafruit_GFX.h>
#include <SPI.h>
#include <Wire.h>

#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

const char *ssid = “SKY79424”;
const char *pass = “FFVYYFWX”;

IPAddress server(192, 168, 0, 6);

#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(“ha/_temperature1”); // 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();
}
}

Sorry for the typo, first line should read:

Can someone more experienced than myself spot what I've done wrong with this....

Please enclose your code in code tags (edit, select the code, click </>).

The problem can reside in your code or outside it (wiring, server…).

Have you already tried some simpler example, not using the OLED or other additional components?

You're missing the entire part where it actually connects to wifi, so . . . . steal code better, and use code tags?

Dead giveaway is where you define what ssid is, and, well, it's never used other than to print it.

I guess you based your wifi web client code on a published example. Can you post a link to it ?
This part will definitely loop as you described, if WiFi.waitForConnectResult() does not return the expected result.

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;
    . . . 
    . . .

Hi thanks to everyone for the comments and suggestions.

the link to the original post as requested is: Display temperature on a SPI OLED screen running off a ESP8266 board. Pic; https://twitter.com/bordignon/status/698727496271863808 · GitHub

I pointed out the problem.

Compare it to this and see if you can figure it out.

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Thanks.. Tom.. :slight_smile:

Tom, do you permanently have that copied to your clipboard, or do you have a txt file just sitting on your desktop, or do you type that out repeatedly?

INTP:
Tom, do you permanently have that copied to your clipboard, or do you have a txt file just sitting on your desktop, or do you type that out repeatedly?

I have a file in notebook++ that has a whole collection of stuff to cut and paste.
Tom... :slight_smile:

INTP:
I pointed out the problem.

Compare it to this and see if you can figure it out.

PubSubClient sample for ESP8266 Arduino · GitHub

Thank you for trying to help. I'm close to giving up on this. I looked at that link and tried to compare the two lots of code. I don't understand how to read it , but from an observational point of view, the code i'm questioning appears mixed up and i'm not sure but "void loop() {" may be in the wrong place.

The placement of your loop function is fine.

There are surely other problems, but you won't get much help until you follow the guidelines that TomGeorge pointed to above. Your code, spread out over multiple scrolls, and possibly mangled by the forum, is difficult to read. using code tags ([code] ... [/code]) will fix this, and the rest of the How to use... post will be just as useful.

You will get very good help here, but you must meet your helpers at least halfway.

OK, so what happens when you do not immediately get connected after the WiFi.begin()? You get out of loop(), next back into loop() and try again. And so on and so on.

I don't have a WiFi shield, but maybe a delay (for testing purposes) after the WiFi.begin(); e.g. delay(5000) for a 5 second delay.

void loop() {
  if (WiFi.status() != WL_CONNECTED) {
    Serial.print("Connecting to ");
    Serial.print(ssid);
    Serial.println("...");
    WiFi.begin(ssid, pass);


    delay(5000);


    if (WiFi.waitForConnectResult() != WL_CONNECTED)
      return;

  ...
  ...
}

Play with the delay a bit, it's only for testing. Have a look at the link provided by INTP for a different approach.

What have you got installed on IPAddress server(192, 168, 0, 6); ? An MQTT Broker ? It looks from the IP address to be something in your local network.

If you don’t get anywhere with the code sample you found, another approach would be to find a basic MQTT which works for you then add the code to handle the display and other peripherals later.

Edit.
Here is a very basic sample which should test your connectivity with the Wireless Lan (which is the stage you are currently failing at). It is based on something I currently use, but stripped down.

#include <ESP8266WiFi.h>

const char* ssid     = "mySSID";
const char* password = "myPassword";


void setup() {
  Serial.begin(115200);
  delay(100);

  // We start by connecting to a WiFi network

  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}
void loop() {
  // put your main code here, to run repeatedly:

}