1 | #include <ESP8266WiFi.h>
|
2 | #include <PubSubClient.h>
|
3 |
|
4 | // Update these with values suitable for your network.
|
5 |
|
6 | const char* ssid = "ssid";
|
7 | const char* password = "wlanpasswort";
|
8 | const char* mqtt_server = "192.168.xxx.xxx";
|
9 |
|
10 | WiFiClient espClient;
|
11 | PubSubClient client(espClient);
|
12 | long lastMsg = 0;
|
13 | char msg[50];
|
14 | int value = 0;
|
15 |
|
16 | void setup_wifi() {
|
17 |
|
18 | delay(10);
|
19 | // We start by connecting to a WiFi network
|
20 | Serial.println();
|
21 | Serial.print("Connecting to ");
|
22 | Serial.println(ssid);
|
23 |
|
24 | WiFi.begin(ssid, password);
|
25 |
|
26 | while (WiFi.status() != WL_CONNECTED) {
|
27 | for(int i = 0; i<500; i++){
|
28 | delay(1);
|
29 | }
|
30 | Serial.print(".");
|
31 | }
|
32 |
|
33 | Serial.println("");
|
34 | Serial.println("WiFi connected");
|
35 | Serial.println("IP address: ");
|
36 | Serial.println(WiFi.localIP());
|
37 | }
|
38 |
|
39 | void callback(char* topic, byte* payload, unsigned int length) {
|
40 | Serial.print("Message arrived [");
|
41 | Serial.print(topic);
|
42 | Serial.print("] ");
|
43 | for (int i = 0; i < length; i++) {
|
44 | Serial.print((char)payload[i]);
|
45 | }
|
46 | Serial.println();
|
47 | // Switch on the LED if an 1 was received as first character
|
48 | if ((char)payload[0] == '1') {
|
49 | digitalWrite(BUILTIN_LED, LOW); // Turn the LED on (Note that LOW is the voltage level
|
50 | // but actually the LED is on; this is because
|
51 | // it is acive low on the ESP-01)
|
52 | } else {
|
53 | digitalWrite(BUILTIN_LED, HIGH); // Turn the LED off by making the voltage HIGH
|
54 |
|
55 | }
|
56 |
|
57 | }
|
58 |
|
59 | void reconnect() {
|
60 | // Loop until we're reconnected
|
61 | while (!client.connected()) {
|
62 | Serial.print("Attempting MQTT connection...");
|
63 | // Attempt to connect
|
64 | if (client.connect("ESP8266_Taster")) {
|
65 | Serial.println("connected");
|
66 | // Once connected, publish an announcement...
|
67 | client.publish("(outTopic)debug","ESP8266 subscriber connected.");
|
68 | // ... and resubscribe
|
69 | client.subscribe("(inTopic)Variable der Led");
|
70 | }
|
71 | else {
|
72 | Serial.print("failed, rc=");
|
73 | Serial.print(client.state());
|
74 | Serial.println(" versuche nochmal in 5 seconds");
|
75 | // Wait 5 seconds before retrying
|
76 | for(int i = 0; i<5000; i++){
|
77 | delay(1);
|
78 | }
|
79 | }
|
80 | }
|
81 | }
|
82 |
|
83 |
|
84 | void setup() {
|
85 | pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output
|
86 | Serial.begin(115200);
|
87 | setup_wifi();
|
88 | client.setServer(mqtt_server, 1883);
|
89 | client.setCallback(callback);
|
90 | }
|
91 | void loop() {
|
92 |
|
93 | if (!client.connected()) {
|
94 | reconnect();
|
95 | }
|
96 | client.loop();
|
97 | long now = millis();
|
98 | if (now - lastMsg > 2000) {
|
99 | lastMsg = now;
|
100 | ++value;
|
101 | snprintf (msg, 75, "hello world#%ld", value);
|
102 | //Serial.print("Publish message: ");
|
103 | // Serial.println(msg);
|
104 | //client.publish("Led ist an", msg);
|
105 | }
|
106 |
|
107 | }
|