25. Dezember 2018 · Kommentare deaktiviert für Sonoff S20 Programm · Kategorien: Sonoff S20

Sonoff S20 mit eigenem Betriebssystem. Die Software. Programmiert wird mit PlatformIO. Die grün markierten Zeilen müssen angepasst werden.

#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <WiFiUdp.h>
#include <PubSubClient.h>
#include <EEPROM.h>

#define mqttPort 1883
#define Relais 12	// Relais
#define LED 13		// grüne LED

#ifdef ESP8266
	extern "C" {
		#include "user_interface.h"
		#include "wpa2_enterprise.h"
	}
#endif
static const char* ssid = "Deine SSID";
static const char* username = "Dein Username";
static const char* password = "Dein Passwort";

IPAddress mqttServer(192, 168, 170, 16); // Adresse MQTT-Broker
WiFiClient espClient;
PubSubClient mqttClient(mqttServer, mqttPort, espClient);
long lastMsg = 0;
char msg[50];
int value = 0;
bool RelaisStatus = LOW;

void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Nachricht empfangen [");
  Serial.print(topic);
  Serial.print("] ");
  for (int i = 0; i < length; i++) {
  Serial.print((char)payload[i]);
  }
  Serial.println();
  if ((char)payload[0] == '0') {
    digitalWrite(Relais, LOW);
		digitalWrite(LED, LOW);		// grüne LED an
    Serial.println("Relais -> Aus");
    RelaisStatus = LOW;
    EEPROM.write(0, RelaisStatus);    // Schreibe Status ins EEPROM
    EEPROM.commit();
  } else if ((char)payload[0] == '1') {
    digitalWrite(Relais, HIGH);
		digitalWrite(LED, HIGH);	// grüne LED aus
		Serial.println("Relais -> An");
    RelaisStatus = HIGH;
    EEPROM.write(0, RelaisStatus);    // Schreibe Status ins EEPROM
    EEPROM.commit();
  } else if ((char)payload[0] == '2') {
    RelaisStatus = !RelaisStatus;
    digitalWrite(Relais, RelaisStatus);
    Serial.print("Relais -> switched to ");
    Serial.println(RelaisStatus);
    EEPROM.write(0, RelaisStatus);
    EEPROM.commit();
  }

}

void reconnect() {
	int j = 0;
	while (!mqttClient.connected()) {
		Serial.println("Versuche mit MQTT-Broker zu verbinden...");
		String mqttClientId = "WLAN-ST01";
		if (mqttClient.connect(mqttClientId.c_str())) {
			Serial.println("Verbunden mit MQTT-Broker");
			mqttClient.publish("outTopic", "Hallo Welt");
			mqttClient.subscribe("inTopic");
		} else {
			Serial.print("Fehler, rc=");
			Serial.print(mqttClient.state());
			Serial.println(" Nächster Versuch in 5 sec.");
			delay(5000);
			if (++j == 5) {
				Serial.println("Reboot");				ESP.restart();
			}	
		}
	}
}

void setup(){
	Serial.begin(115200);
	pinMode(Relais, OUTPUT);	// GPIO 2 als Ausgang
	pinMode(LED, OUTPUT);		// GPIO 13 als Ausgang
	digitalWrite(Relais, LOW);// Beim Einschalten Relais aus
	digitalWrite(LED, LOW);		// grüne LED an

	WiFi.mode(WIFI_STA);
	wifi_station_disconnect();
	struct station_config wifi_config;

	memset(&wifi_config, 0, sizeof(wifi_config));
	strcpy((char*)wifi_config.ssid, ssid);
	strcpy((char*)wifi_config.password, password);
	wifi_station_set_config(&wifi_config);

  wifi_station_set_wpa2_enterprise_auth(1);
  wifi_station_set_enterprise_username((uint8*)username, strlen(username));
  wifi_station_set_enterprise_password((uint8*)password, strlen(password));
  wifi_station_connect();

	mqttClient.setServer(mqttServer, mqttPort);
  mqttClient.setCallback(callback);
}

void loop() {
	if (!mqttClient.connected()) {
		reconnect();
		mqttClient.subscribe("fhem/steckdosen/st01/#");
		digitalWrite(LED, LOW);		// grüne LED an
	}
	mqttClient.loop();
}

Kommentare geschlossen.