Sonoff Issues

Hi all,

So I do know my question isn't related to an arduino exactly, but it does still use the arduino IDE and code syntax so hoping I can get some good feedback here. If not sorry for wasting your time. I have a sonoff that I flashed with the code below. After flashing it, I unplug it, plug it into the wall and expect to be able to control it using the mqtt broker but I'm not getting anything, no LED flashes. Nothing. What am I missing? When uploading to the IDE is said it uploaded 100%.

  • I have replaced the ssid and password with "....." This is not what it is in the real code, just don't wanna share my personal wifi info with the internet. :slight_smile:
#include <ESP8266WiFi.h>
#include <PubSubClient.h>

const byte ledPin = 13;

// Update these with values suitable for your network.

const char* ssid = ".......";
const char* password = "......";
const char* mqtt_server = "10.0.0.116";

WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;

byte relayPin = 12;

void setup() {
  pinMode(relayPin, OUTPUT);
  pinMode(ledPin, OUTPUT); // Initialize the BUILTIN_LED pin as an output
  Serial.begin(115200);
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);
}

void setup_wifi() {

  delay(10);
  // We start by connecting to a WiFi network
  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 callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  for (int i = 0; i < length; i++) {
    Serial.print((char)payload[i]);
  }
  Serial.println();

  // Switch on the LED if an 1 was received as first character
  if ((char)payload[0] == '1') {
    digitalWrite(relayPin, HIGH);
    digitalWrite(ledPin, LOW);
    // but actually the LED is on; this is because
    // it is acive low on the ESP-01)
  } else {
    digitalWrite(relayPin, LOW);  // Turn the LED off by making the voltage HIGH
    digitalWrite(ledPin, HIGH);
  }

}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect("ESP8266Client")) {
      Serial.println("connected");
      // Once connected, publish an announcement...
      client.publish("HomeConsole", "SO1 connected");
      // ... and resubscribe
      client.subscribe("HomeMain/SO1");
    } else {
      digitalWrite(ledPin, LOW);
      delay(100);
      digitalWrite(ledPin, HIGH);
      delay(1000);
      digitalWrite(ledPin, LOW);
      delay(100);
      digitalWrite(ledPin, HIGH);
      
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}
void loop() {

  if (!client.connected()) {
    reconnect();
  }
  client.loop();
}

You have to make sure all the other parts work. Writing code, uploading it, installing the device and expecting it works usually means it doesn't. Especially when there's a long chain of connecting devices.

  1. does it connect to the WiFi correctly?
  2. if yes: does it get messages from the MQTT server?
  3. If not: does the MQTT server send out any messages?
  4. if not: does the MQTT server even work?
  5. does your other device, whatever it is, successfully communicate with the MQTT server?

You must verify all those steps for starters.

  1. Not 100% sure, I will verify this as soon as I get back to my desk.
  2. ^
  3. Yes, I did also try an adafruit huzzah and that did receive messages from the MQTT service.
  4. Yes.
  5. Yes.

I will double check 1 and 2 as soon as I can, in the meantime, the reason I asked is, Is there any other "procedures" required for uploading code to a sonoff? I have seen a handful of tutorials but none of them go into great detail on the upload process.

Thanks,

  • Matt

I don't expect there are - those modules are built around an ESP8266 processor.

They do have some on-board LED. Why don't you start with Blink, get that to work, and then upgrade to the MQTT version of Blink (forgot the name - there's an MQTT example out there that switches on and off an LED through an MQTT server).