Forum: Mikrocontroller und Digitale Elektronik ESP8266 MQTT client über access Point


von Lars (Gast)


Lesenswert?

Hallo,

ich versuchen seit Wochen einen ESP8266 als MQTT Client mit meinem MQTT 
Broker zu verbinden.

Sobald ich einen AP von TP-Link dazwischen schalte geht das nicht mehr.

Ich könnte Hilfe gebrauchen.

Biiiiiiiittttttttteeeeeeeee......


Grüße Lars

von Horst (Gast)


Lesenswert?

Schau mal auf Seite 42 der Anleitung

von TestX (Gast)


Lesenswert?

dann gib uns mal mehr infos.
wie sieht das netzwerk aus etc. hast du dir mal ein testprogramm gebaut 
?

von Lars (Gast)


Lesenswert?

Hallo,

Seite 42 welcher Anleitung?
1
#include <ESP8266WiFi.h>
2
#include <PubSubClient.h>
3
4
// Update these with values suitable for your network.
5
6
//const char* ssid = "........";
7
//const char* password = "........";
8
9
//const char* ssid = ".......";
10
//const char* password = "........"; 
11
12
//const char* ssid = "....."; //replace this with your WiFi network name
13
//const char* password = "......"; //replace this with your WiFi network password
14
15
const char* ssid = "......";
16
const char* password = "......"; 
17
18
19
const char* mqtt_server = "192.xxx.xxx.xxx";
20
21
WiFiClient espClient;
22
PubSubClient client(espClient);
23
24
long lastMsg = 0;
25
char msg[50];
26
int value = 0;
27
28
void setup_wifi() 
29
{
30
  delay(10);
31
  // We start by connecting to a WiFi network
32
  Serial.println();
33
  Serial.print("Connecting to ");
34
  Serial.println(ssid);
35
36
  WiFi.begin(ssid, password);
37
38
  while (WiFi.status() != WL_CONNECTED) 
39
  {
40
    delay(500);
41
    Serial.print(".");
42
  }
43
44
  randomSeed(micros());
45
46
  Serial.println("");
47
48
  /***********************************************/
49
  Serial.println("Connected.");
50
  Serial.print("MAC: ");
51
  Serial.println(WiFi.macAddress());
52
  Serial.print("IP:  ");
53
  Serial.println(WiFi.localIP());
54
  Serial.print("Subnet: ");
55
  Serial.println(WiFi.subnetMask());
56
  Serial.print("Gateway :");
57
  Serial.println(WiFi.gatewayIP());
58
  Serial.print("DNS: ");
59
  Serial.println(WiFi.dnsIP());
60
  Serial.print("Channel: ");
61
  Serial.println(WiFi.channel());
62
  Serial.print("Status: ");
63
  Serial.println(WiFi.status());
64
/***********************************************/ 
65
  
66
}
67
68
void callback(char* topic, byte* payload, unsigned int length) 
69
{
70
  Serial.print("Message arrived [");
71
  Serial.print(topic);
72
  Serial.print("] ");
73
  for (int i = 0; i < length; i++) 
74
  {
75
    Serial.print((char)payload[i]);
76
  }
77
  Serial.println();
78
79
  // Switch on the LED if an 1 was received as first character
80
  if ((char)payload[0] == '1') 
81
  {
82
    digitalWrite(2, LOW);   // Turn the LED on (Note that LOW is the voltage level
83
    // but actually the LED is on; this is because
84
    // it is acive low on the ESP-01)
85
  } else 
86
  {
87
    digitalWrite(2, HIGH);  // Turn the LED off by making the voltage HIGH
88
  }
89
}
90
91
void reconnect() 
92
{
93
  // Loop until we're reconnected
94
  while (!client.connected())
95
  {
96
    Serial.print("Attempting MQTT connection...");
97
    // Create a random client ID
98
    String clientId = "ESP8266Client-";
99
    clientId += String(random(0xffff), HEX);
100
    // Attempt to connect
101
    if (client.connect(clientId.c_str(),"openhabian","mqtt")) 
102
    {
103
      Serial.println("connected");
104
      // Once connected, publish an announcement...
105
      client.publish("outTopic", "hello world");
106
      // ... and resubscribe
107
      client.subscribe("inTopic");
108
    } 
109
    else 
110
    {
111
      Serial.print("failed, rc=");
112
      Serial.print(client.state());
113
      Serial.println(" try again in 5 seconds");
114
      // Wait 5 seconds before retrying
115
      delay(5000);
116
    }
117
  }
118
}
119
120
void setup() 
121
{
122
  pinMode(2, OUTPUT);     // Initialize the BUILTIN_LED pin as an output
123
  Serial.begin(115200);
124
  setup_wifi();
125
  
126
  client.setServer(mqtt_server, 1883);
127
  client.setCallback(callback);
128
}
129
130
void loop() 
131
{
132
  if (!client.connected()) 
133
  {
134
    reconnect();
135
  }
136
  client.loop();
137
138
  long now = millis();
139
  if (now - lastMsg > 2000) 
140
  {
141
    lastMsg = now;
142
    ++value;
143
    snprintf (msg, 75, "hello world #%ld", value);
144
    Serial.print("Publish message: ");
145
    Serial.println(msg);
146
    client.publish("outTopic", msg);
147
  }
148
}
1
Das Netzwerk ist so aufgebaut:
2
          ,- PC1
3
Fritzbox -|- Pc2
4
          '- TP Link Nano(als Access Point)--> ESP8266
5
          '- RaspberryPi(Mqtt Server)

Ich danke euch schon mal für den Versuch mir zu helfen.

Grüße Lars

von Stefan F. (Gast)


Lesenswert?

Dann ist dein AP entweder nicht zum ESP8266 kompatibel oder falsch 
konfiguriert.

Kannst du den MQTT Broker überhaupt über das WLAN Netz des TP-Link 
Routers erreichen? Hast du das mal mit einem anderen gerät (Laptop oder 
Smartphone) versucht?

Kann dein ESP8266 denn andere lokale Server oder das Internet erreichen?

Wird dem ESP8266 denn das richtige Gateway (in diesem Fall der TP-Link 
Router) zugewiesen?

von Rufus Τ. F. (rufus) Benutzerseite


Lesenswert?

Lars schrieb:
> Das Netzwerk ist so aufgebaut:
>           ,- PC1
> Fritzbox -|- Pc2
>           '- TP Link Nano(als Access Point)--> ESP8266
>           '- RaspberryPi(Mqtt Server)

Das bedeutet, der ESP8266 hat eine IP-Adresse aus dem gleichen Subnet 
wie die Fritzbox, die beiden PCs und der Raspberry Pi?

Wozu dient der Accesspoint, funktioniert das WLAN der Fritzbox nicht?

von Benjamin S. (recycler)


Lesenswert?

So wie es aussieht, hast du ein Routing problem.
Gib mal dem TP Link und dem ESP8266 einen anderen IP Bereich oder 
Konfiguriere den TP Link als WLAN Bridge (und mach dazu DHCP aus). dann 
gehts.

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.