Browse Source

Wlan an RPI sketch

master
Johannes Krug 5 years ago
parent
commit
6cb1c94f37
1 changed files with 63 additions and 0 deletions
  1. 63
    0
      arduino/wlan_an_rpi/wlan_an_rpi.ino

+ 63
- 0
arduino/wlan_an_rpi/wlan_an_rpi.ino View File

@@ -0,0 +1,63 @@
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
const char* SSID = "smartroom";
const char* PSK = "smarthome";
const char* MQTT_BROKER = "192.168.4.1";
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;
void setup() {
Serial.begin(115200);
setup_wifi();
client.setServer(MQTT_BROKER, 1883);
}
void setup_wifi() {
delay(10);
Serial.println();
Serial.print("Connecting to ");
Serial.println(SSID);
WiFi.begin(SSID, PSK);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void reconnect() {
while (!client.connected()) {
Serial.print("Reconnecting...");
if (!client.connect("ESP8266Client")) {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" retrying in 5 seconds");
delay(5000);
}
}
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
snprintf (msg, 50, "Alive since %ld milliseconds", millis());
Serial.print("Publish message: ");
Serial.println(msg);
client.publish("/home/data", "Hello World");
delay(5000);
}


Loading…
Cancel
Save