Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include <PubSubClient.h>
- int gomb1 = D5; //button is connected to GPIO pin D1
- int gomb2 = D2;
- // Update these with values suitable for your network.
- const char* ssid = "";//put your wifi ssid here
- const char* password = "";//put your wifi password here.
- const char* mqtt_server = "";
- WiFiClient espClient;
- PubSubClient client(espClient);
- long lastMsg = 0;
- char msg[50];
- void setup_wifi() {
- delay(100);
- // We start by connecting to a WiFi network
- Serial.print("Connecting to ");
- Serial.println(ssid);
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED)
- {
- delay(500);
- Serial.print(".");
- }
- randomSeed(micros());
- Serial.println("");
- Serial.println("WiFi connected");
- Serial.println("IP address: ");
- Serial.println(WiFi.localIP());
- }
- void callback(char* topic, byte* payload, unsigned int length)
- {
- } //end callback
- void reconnect() {
- // Loop until we're reconnected
- while (!client.connected())
- {
- Serial.print("Attempting MQTT connection...");
- // Create a random client ID
- String clientId = "ESP8266Client-";
- clientId += String(random(0xffff), HEX);
- // Attempt to connect
- //if you MQTT broker has clientID,username and password
- //please change following line to if (client.connect(clientId,userName,passWord))
- if (client.connect(clientId.c_str()))
- {
- Serial.println("connected");
- //once connected to MQTT broker, subscribe command if any
- client.subscribe("nyomogomb");
- } else {
- Serial.print("failed, rc=");
- Serial.print(client.state());
- Serial.println(" try again in 5 seconds");
- // Wait 5 seconds before retrying
- delay(5000);
- }
- }
- } //end reconnect()
- void setup() {
- Serial.begin(115200);
- setup_wifi();
- client.setServer(mqtt_server, 1883);
- client.setCallback(callback);
- pinMode(gomb1,INPUT);
- pinMode(gomb2,INPUT);
- }
- void loop() {
- if (!client.connected()) {
- reconnect();
- }
- client.loop();
- long now = millis();
- int status;
- //send message every 2 second
- if (now - lastMsg > 250) {
- lastMsg = now;
- status=digitalRead(gomb1);
- String msg="Gomb helyzete: ";
- if(status==HIGH )
- {
- msg= msg+ "Megnyomva";
- char message[58];
- msg.toCharArray(message,58);
- Serial.println(message);
- //publish sensor data to MQTT broker
- client.publish("nyomogomb1", message);
- }
- else
- {
- msg= msg+ "Nincs megnyomva";
- char message[58];
- msg.toCharArray(message,58);
- Serial.println(message);
- // ne küldje el a NEM nyomott állapotot MQTT-n
- // client.publish("nyomogomb1", message);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement