Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // James Lewis
- // PIR with MQTT Example, using millis()
- // www.baldengineer.com for more information
- #include <ESP8266WiFi.h>
- #include <PubSubClient.h>
- #define MOTION 1
- #define STILL 0
- // Update these with values suitable for your network.
- // oOoooooo Secrets!!!!!
- const char* ssid = "";
- const char* password = "";
- const char* mqtt_server = "rasppib";
- WiFiClient espClient;
- PubSubClient client(espClient);
- unsigned long lastMsg = 0;
- char msg[50];
- int value = 0;
- int redLED = 0;
- int blueLED = 2;
- int pirPin = 12;
- void setup_wifi() {
- delay(10);
- // We start by connecting to a WiFi network
- Serial.println();
- Serial.print("Connecting to ");
- Serial.println(ssid);
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println("");
- Serial.println("WiFi connected");
- Serial.println("IP address: ");
- Serial.println(WiFi.localIP());
- }
- void callback(char* topic, byte* payload, unsigned int length) {
- Serial.print("Message arrived [");
- Serial.print(topic);
- Serial.print("] ");
- for (int i = 0; i < length; i++) {
- Serial.print((char)payload[i]);
- }
- Serial.println();
- }
- void reconnect() {
- // Loop until we're reconnected
- while (!client.connected()) {
- Serial.print("Attempting MQTT connection...");
- // Attempt to connect
- if (client.connect("ESP8266Client")) {
- Serial.println("connected");
- // Once connected, publish an announcement...
- client.publish("console", "PIR 1 Reporting for duty. huhuh duty.");
- digitalWrite(redLED, HIGH);
- // ... and resubscribe
- // client.subscribe("inTopic");
- } else {
- digitalWrite(redLED, LOW);
- Serial.print("failed, rc=");
- Serial.print(client.state());
- Serial.println(" try again in 5 seconds");
- // Wait 5 seconds before retrying
- delay(5000);
- }
- }
- }
- bool pirLastStatus = LOW;
- void setup() {
- pinMode(redLED, OUTPUT);
- pinMode(blueLED, OUTPUT);
- pinMode(pirPin, INPUT);
- digitalWrite(redLED, LOW);
- digitalWrite(blueLED, LOW);
- Serial.begin(115200);
- setup_wifi();
- client.setServer(mqtt_server, 1883);
- client.setCallback(callback);
- }
- void loop() {
- if (!client.connected()) {
- reconnect();
- }
- client.loop();
- int pirStatus = digitalRead(pirPin);
- digitalWrite(blueLED, !pirStatus);
- unsigned long now = millis();
- if (pirStatus != pirLastStatus) {
- // a change occured, interrupt
- if (pirStatus == MOTION)
- sprintf(msg, "MOTION");
- else
- sprintf(msg, "STILL");
- Serial.print("Publish message: ");
- Serial.println(msg);
- client.publish("pir1Status", msg);
- pirLastStatus = pirStatus;
- lastMsg = now;
- }
- if (now - lastMsg > 5000) {
- lastMsg = now;
- //snprintf (msg, 75, "", value,);
- sprintf(msg, "IDLE");
- Serial.print("Publish message: ");
- Serial.println(msg);
- client.publish("pir1Status", msg);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement