Here is an example ESP8266 Arduino Code:
Note that this example also flashes a light on a house map that shows which sensor sent data (used for PIR
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <RCSwitch.h>
// Initialize RF
RCSwitch mySwitch = RCSwitch();
// These must be unique:
const char* mqtt_client = "RF1";
// Wifi Connection details
const char* ssid = "xxxxxxxxxx";
const char* password = "xxxxxxxxxxxxx";
// MQTT Server address
const char* mqtt_server = "192.168.0.xxx";
// Initialize MQTT
WiFiClient espClient;
PubSubClient client(espClient);
void setup() {
Serial.begin(9600);
pinMode(16, OUTPUT);
pinMode(5, OUTPUT);
pinMode(4, OUTPUT);
pinMode(14, OUTPUT);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
pinMode(3, OUTPUT);
pinMode(1, OUTPUT);
mySwitch.enableReceive(2); // Receive on GPIO 2
setup_wifi();
client.setServer(mqtt_server, 1883);
}
// Connect to Wifi
void setup_wifi() {
delay(1);
// connect to a WiFi network
WiFi.begin(ssid, password);
Serial.print("[*] Connecting to Access Point: ");
Serial.println(ssid);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("[*] WiFi connected");
Serial.println("[* ]IP address: ");
Serial.println(WiFi.localIP());
}
// Reconnect to MQTT Server
void reconnect() {
// Loop until we're reconnected
int state;
while (!client.connected()) {
Serial.print("[*] Attempting MQTT connection...");
// Attempt to connect
if (client.connect(mqtt_client)) {
mqtt_state();
} else {
mqtt_state();
Serial.println("[*] try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
// Main functions
void loop() {
if (mySwitch.available()) {
char str[32];
Serial.println("-------------------");
Serial.println("[*] Received RF Message from:");
dtostrf(mySwitch.getReceivedValue(), 0, 1, str);
Serial.println(str);
mySwitch.resetAvailable();
// Send data to MQTT
if (!client.connected()) {
reconnect();
}
//client.loop();
if (strcmp (str,"9114120.0") == 0) {
beep_led(16);
}
if (strcmp (str,"9970184.0") == 0) {
beep_led(5);
}
if (strcmp (str,"9021960.0") == 0) {
beep_led(4);
}
if (strcmp (str,"2382428.0") == 0) {
beep_led(14);
}
if (strcmp (str,"15848796.0") == 0) {
beep_led(12);
}
if (strcmp (str,"15848796.0") == 0) {
beep_led(13);
}
if (strcmp (str,"39560110.0") == 0) {
beep_led(3);
}
if (strcmp (str,"3952060.0") == 0) {
beep_led(1);
}
client.publish("/rf", str);
delay(5000);
}
}
// Display LEDs
void beep_led(int P){
Serial.println("[*] Beep LED");
digitalWrite(P, HIGH);
delay(2000);
digitalWrite(P, LOW);
}
// GET MQTT State
void mqtt_state() {
int state;
state = client.state();
Serial.println();
if (state == -4) {
Serial.print("The server didn't respond within the keepalive time");
}
if (state == -3) {
Serial.print("The network connection was broken");
}
if (state == -2) {
Serial.print("The network connection failed");
}
if (state == -1) {
Serial.print("The client is disconnected cleanly");
}
if (state == 0) {
Serial.print("The cient is connected");
}
if (state == 1) {
Serial.print("The server doesn't support the requested version of MQTT");
}
if (state == 2) {
Serial.print("The server rejected the client identifier");
}
if (state == 3) {
Serial.print("The server was unable to accept the connection");
}
if (state == 4) {
Serial.print("The username/password were rejected");
}
if (state == 5) {
Serial.print("The client was not authorized to connect");
}
Serial.println();
}