Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Using Wemos D1 mini to send MQTT messages when motion is detected through connected HF sensor.
- The HF sensor has 230V output that is transformed to 3.3V using a small 230V to 3.3V power supply.
- http://www.hobbyelektronik.nu/forum/viewtopic.php?f=13&t=37
- #include <ESP8266WiFi.h>
- #include <ESP8266mDNS.h>
- #include <WiFiUdp.h>
- #include <ArduinoOTA.h>
- #include <PubSubClient.h>
- const char* version = "HF_motion_mqtt 1.0";
- const char* date_name = "2017-06-10 Hans Palm";
- // Code for using Wemos D1 Mini with a HF motion detector
- // 3.3V input on D1 when sensor is trigged
- // MQTT messages according to outTopic* are sent when D1 input becomes active = high
- // D1 must become low before new activation can occur
- // When activated, MQTT messages similar to the following are sent:
- // Activates kitchen timer:
- //
- // mosquitto_pub -h localhost -t "KTActivate" -m "1"
- //
- // Activates Ikea TrΓ₯dfri lamp above kitchen table
- //
- // mosquitto_pub -h localhost -t "tradfri_kbordet" -m "1"
- // Wifi settings
- const char* ssid = "SSID";
- const char* password = "password";
- const char* host = "OTAHOSTNAME";
- // The follwing parameters are only required for fixed IP, otherwise uncomment, also uncomment WiFi.config(ip, gateway, subnet)
- IPAddress ip(192, 168, 0, 170);
- IPAddress gateway(192, 168, 0, 1);
- IPAddress subnet(255, 255, 255, 0);
- // MQTT server and topic settings
- const char* mqtt_server = "192.168.0.xxx";
- const char* clientID = "MotionKitchenOTA";
- const char* outTopic_KTActivate = "KTActivate";
- const char* outTopic_tradfri_kbordet = "tradfri_kbordet";
- const char* inTopic = "TBD"; // Not used in this sketch
- bool trigged = false;
- WiFiClient espClient;
- PubSubClient client(espClient);
- void setup()
- {
- Serial.begin(115200);
- delay(10);
- Serial.println("setup started ");
- pinMode(LED_BUILTIN, OUTPUT);
- pinMode(D1, INPUT);
- // Connect to WiFi network
- WiFi.config(ip, gateway, subnet); // Only required for fixed IP
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- ArduinoOTA.setHostname(host);
- ArduinoOTA.onStart([]() {
- String type;
- type = "sketch";
- Serial.println("Start updating " + type);
- });
- ArduinoOTA.onEnd([]() {
- Serial.println("\nEnd");
- });
- ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
- Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
- });
- ArduinoOTA.onError([](ota_error_t error) {
- Serial.printf("Error[%u]: ", error);
- if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
- else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
- else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
- else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
- else if (error == OTA_END_ERROR) Serial.println("End Failed");
- });
- ArduinoOTA.begin();
- Serial.println("");
- Serial.println("WiFi connected");
- Serial.println("IP address: ");
- Serial.println(WiFi.localIP());
- client.setServer(mqtt_server, 1883);
- client.setCallback(callback);
- }
- void callback(char* topic, byte* payload, unsigned int length) {
- // Check incoming message using really ugly code
- String nstring = "";
- Serial.print("Message arrived [");
- Serial.print(topic);
- Serial.print("] ");
- for (int i = 0; i < length; i++) {
- Serial.print((char)payload[i]);
- nstring = String (nstring + (char)payload[i]);
- }
- nstring = String (nstring + '\0');
- Serial.println("");
- Serial.print("nstring = ");
- Serial.println(nstring);
- int value;
- if (String(topic + '\0' == inTopic)) {
- value = nstring.toInt(); // (long) 0 is returned if unsuccessful)
- if (value > 0 ) {
- // Serial.print("TBD value change to ");
- Serial.print(value);
- Serial.println(" requested");
- // TBD
- }
- }
- }
- void reconnect() {
- // Loop until we're reconnected
- while (!client.connected()) {
- Serial.print("Attempting MQTT connection...");
- // Attempt to connect
- if (client.connect(clientID)) {
- Serial.println("connected");
- // Once connected, publish an announcement...
- // ... and resubscribe
- client.subscribe(inTopic);
- } else {
- Serial.print("failed, rc=");
- Serial.print(client.state());
- Serial.println(" try again in 5 seconds");
- // Wait 5 seconds before retrying
- delay(5000);
- }
- }
- }
- void loop()
- {
- ArduinoOTA.handle();
- // Check if a client has connected
- if (!client.connected()) {
- reconnect();
- }
- // Perform PubSubClient tasks
- client.loop();
- int input = digitalRead(D1);
- digitalWrite(LED_BUILTIN, !input);
- if (input) {
- if (!trigged) {
- trigged = true;
- char msg[50];
- snprintf (msg, 75, "1");
- client.publish(outTopic_tradfri_kbordet, msg);
- snprintf (msg, 75, "1");
- client.publish(outTopic_KTActivate, msg);
- }
- } else {
- trigged = false;
- }
- delay(10); // Probably not required
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement