Skip to content

Instantly share code, notes, and snippets.

@masihtehrani
Created August 19, 2020 12:42
Show Gist options
  • Save masihtehrani/7520735cc46d30d2a3586986a61c899f to your computer and use it in GitHub Desktop.
Save masihtehrani/7520735cc46d30d2a3586986a61c899f to your computer and use it in GitHub Desktop.
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
// use onboard LED for convenience
#define LED (2)
// maximum received message length
#define MAX_MSG_LEN (128)
const char *ssid = "";
const char *password = "";
const char *mqttServer = "broker.hivemq.com";
const int mqttPort = 1883;
const char *mqttUser = "";
const char *mqttPassword = "";
const char *topic = "terminal/1002";
WiFiClient espClient;
PubSubClient client(espClient);
void connectWifi();
void connectMQTT();
void callback(char *msgTopic, uint8_t *msgPayload, unsigned int msgLength);
void setLedState(boolean state);
void setup()
{
// LED pin as output
pinMode(LED, OUTPUT);
digitalWrite(LED, HIGH);
// Configure serial port for debugging
Serial.begin(9600);
// Initialise wifi connection - this will wait until connected
connectWifi();
// connect to MQTT server
client.setServer(mqttServer, mqttPort);
client.setCallback(callback);
}
// connect to wifi
void connectWifi()
{
delay(10);
// Connecting to a WiFi network
Serial.printf("\nConnecting to %s\n", ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(250);
Serial.print(".");
}
Serial.println("");
Serial.print("WiFi connected on IP address ");
Serial.println(WiFi.localIP());
}
// connect to MQTT server
void connectMQTT()
{
// Wait until we're connected
while (!client.connected())
{
// Create a random client ID
String clientId = "ESP8266-";
clientId += String(random(0xffff), HEX);
Serial.printf("MQTT connecting as client %s...\n", clientId.c_str());
// Attempt to connect
if (client.connect(clientId.c_str()))
{
Serial.println("MQTT connected");
// Once connected, publish an announcement...
client.publish(topic, "Hi id pay I'm one of the IdpayQ with mac address: 1002");
// ... and resubscribe
client.subscribe(topic);
}
else
{
Serial.printf("MQTT failed, state %s, retrying...\n", client.state());
// Wait before retrying
delay(2500);
}
}
}
void callback(char *msgTopic, uint8_t *msgPayload, unsigned int msgLength)
{
// copy payload to a static string
static char message[MAX_MSG_LEN + 1];
if (msgLength > MAX_MSG_LEN)
{
msgLength = MAX_MSG_LEN;
}
strncpy(message, (char *)msgPayload, msgLength);
message[msgLength] = '\0';
Serial.printf("topic %s, message received: %s\n", topic, message);
// decode message
if (strcmp(message, "off") == 0)
{
setLedState(false);
}
else if (strcmp(message, "on") == 0)
{
setLedState(true);
}
}
void setLedState(boolean state)
{
// LED logic is inverted, low means on
digitalWrite(LED, !state);
}
void loop()
{
if (!client.connected())
{
connectMQTT();
}
// this is ESSENTIAL!
client.loop();
// idle
delay(500);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment