Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include <PubSubClient.h>
- #include <OneWire.h>
- #include <DallasTemperature.h>
- #include <ArduinoOTA.h>
- #define ONE_WIRE_BUS D5
- const char* ssid = "coskonkretnego";
- const char* password = "UhYgdFrt54de";
- const char* mqttServer = "192.168.1.105";
- const int mqttPort = 1883;
- const char* mqttUser = "dom";
- const char* mqttPassword = "dupa123";
- const char* mqttLogin = "MYROOM_STAIRS";
- const char* WiFi_hostname = "MYROOM-STAIRS";
- OneWire oneWire(ONE_WIRE_BUS);
- DallasTemperature sensors(&oneWire);
- WiFiClient espClient;
- PubSubClient client(espClient);
- void callback(char* topic, byte* payload, unsigned int length);
- void connect();
- void ledOn(int color1, int color2, int color3);
- void ledOff(int color1, int color2, int color3);
- int redLed = D2;
- int greenLed = D3;
- int blueLed = D4;
- int buttonPinLeft = D8;
- int pirPin = D7;
- char const *switchMainLight = "toggle/house/floor1/myroom/mainlight";
- char const *temperatureTopic = "sensor/temp/house/floor1/myroom/stairs";
- char const *pirTopic = "sensor/pir/house/floor1/myroom/stairs";
- char const *rgbSwitch = "switch/house/floor1/myroom/stairs/led";
- char const *rgbConfirm = "confirm/house/floor1/myroom/stairs/led";
- char const *redSwitch = "switch/house/floor1/myroom/stairs/led/red";
- char const *redConfirm = "confirm/house/floor1/myroom/stairs/led/red";
- char const *greenSwitch = "switch/house/floor1/myroom/stairs/led/green";
- char const *greenConfirm = "confirm/house/floor1/myroom/stairs/led/green";
- char const *blueSwitch = "switch/house/floor1/myroom/stairs/led/blue";
- char const *blueConfirm = "confirm/house/floor1/myroom/stairs/led/blue";
- char const *redGreenSwitch = "switch/house/floor1/myroom/stairs/led/redGreen";
- char const *redGreenConfirm = "confirm/house/floor1/myroom/stairs/led/redGreen";
- char const *redBlueSwitch = "switch/house/floor1/myroom/stairs/led/redBlue";
- char const *redBlueConfirm = "confirm/house/floor1/myroom/stairs/led/redBlue";
- char const *greenBlueSwitch = "switch/house/floor1/myroom/stairs/led/greenBlue";
- char const *greenBlueConfirm = "confirm/house/floor1/myroom/stairs/led/greenBlue";
- int brightness = 0; // how bright the LED is
- int fadeAmount = 2; // how many points to fade the LED by
- void connect() {
- WiFi.hostname(WiFi_hostname);
- WiFi.mode(WIFI_STA);
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.println("Connecting to WiFi..");
- }
- Serial.println("Connected to the WiFi network");
- client.setServer(mqttServer, mqttPort);
- client.setCallback(callback);
- while (!client.connected()) {
- Serial.println("Connecting to MQTT...");
- if (client.connect(mqttLogin, mqttUser, mqttPassword )) {
- Serial.println("connected");
- } else {
- Serial.print("failed with state ");
- Serial.print(client.state());
- delay(2000);
- }
- }
- delay(100);
- }
- long toggleMoment;
- long pastToggleMoment = 0;
- void toggleLight() {
- toggleMoment = millis();
- if(toggleMoment - pastToggleMoment > 1000) {
- pastToggleMoment = toggleMoment;
- Serial.println("buttonpushed");
- client.publish(switchMainLight, "1");
- }
- }
- void pirOn() {
- int motion = digitalRead(pirPin);
- if(motion) {
- // Serial.println("WGURE");
- client.publish(pirTopic, "1");
- } else{
- // Serial.println("WDUL");
- client.publish(pirTopic, "0");
- }
- }
- void setup() {
- Serial.begin(9600);
- ArduinoOTA.setHostname("StairsSwitch");
- ArduinoOTA.begin(); // OTA initialization
- delay(100);
- connect();
- pinMode(buttonPinLeft, INPUT);
- pinMode(pirPin, INPUT);
- pinMode(redLed, OUTPUT);
- pinMode(greenLed, OUTPUT);
- pinMode(blueLed, OUTPUT);
- // digitalWrite(0, redLed);
- // digitalWrite(0, greenLed);
- // digitalWrite(0, blueLed);
- attachInterrupt(digitalPinToInterrupt(buttonPinLeft), toggleLight,FALLING);
- attachInterrupt(digitalPinToInterrupt(pirPin), pirOn, CHANGE);
- sensors.begin();
- delay(100);
- client.subscribe(rgbSwitch);
- client.subscribe(redSwitch);
- client.subscribe(greenSwitch);
- client.subscribe(blueSwitch);
- client.subscribe(redBlueSwitch);
- client.subscribe(redGreenSwitch);
- client.subscribe(greenBlueSwitch);
- }
- int buttonState = 0;
- int lastButtonState = 0;
- int goUp;
- int motion = 0;
- long lastMsg = 0;
- float temp = 0.0;
- float tempDiff = 0.1;
- bool firstTime = true;
- bool isButtonPressed(int buttonPin, int &buttonState, int &lastButtonState) {
- bool pressed = LOW;
- buttonState = digitalRead(buttonPin);
- if (buttonState != lastButtonState) {
- if (buttonState == HIGH) {
- pressed = HIGH;
- } else {
- }
- delay(50);
- }
- lastButtonState = buttonState;
- return pressed;
- }
- // todo: add healthCheck
- void loop() {
- if (!client.connected() && WiFi.status() == 3) {
- connect();
- }
- client.loop();
- delay(10);
- // if (isButtonPressed(buttonPinLeft, buttonState, lastButtonState)) {
- // Serial.println("buttonpushed");
- // client.publish(switchMainLight, "1");
- // }
- ArduinoOTA.handle();
- long now = millis();
- float temp;
- if(firstTime ) {
- firstTime = false;
- sensors.requestTemperatures();
- temp = sensors.getTempCByIndex(0);
- // if (checkBound(newTemp, temp, tempDiff)) {
- Serial.print("New temperature:");
- Serial.println(String(temp).c_str());
- client.publish(temperatureTopic, String(temp).c_str(), true);
- }
- if (now - lastMsg > 15000) {
- lastMsg = now;
- sensors.requestTemperatures();
- // }
- temp = sensors.getTempCByIndex(0);
- // if (checkBound(newTemp, temp, tempDiff)) {
- Serial.print("New temperature:");
- Serial.println(String(temp).c_str());
- client.publish(temperatureTopic, String(temp).c_str(), true);
- // }
- // if (motion != newMotion) {
- // motion = newMotion;
- // Serial.print("New motion:");
- // Serial.println(String(motion).c_str());
- // client.publish(pirTopic, String(motion).c_str(), true);
- // }
- }
- }
- void callback(char *topic, byte *payload, unsigned int length) {
- //convert topic to string to make it easier to work with
- String topicStr = topic;
- //EJ: Note: the "topic" value gets overwritten everytime it receives confirmation (callback) message from MQTT
- //Print out some debugging info
- Serial.println("Callback update.");
- Serial.print("Topic: ");
- Serial.println(topicStr);
- char confrimation = payload[0];
- if(payload[0] == '1') {
- goUp = 1;
- }else {
- goUp = 0;
- }
- if (topicStr == rgbSwitch) {
- client.publish(rgbConfirm, &confrimation);
- goUp ? ledOn(redLed, greenLed, blueLed) : ledOff(redLed, greenLed, blueLed);
- } else if (topicStr == redGreenSwitch) {
- client.publish(redGreenConfirm, &confrimation);
- goUp ? ledOn(redLed, greenLed, greenLed) : ledOff(redLed, greenLed, greenLed);
- } else if (topicStr == redBlueSwitch) {
- client.publish(redBlueConfirm, &confrimation);
- goUp ? ledOn(redLed, blueLed, blueLed) : ledOff(redLed, blueLed, blueLed);
- }else if (topicStr == greenBlueSwitch) {
- client.publish(greenBlueConfirm, &confrimation);
- goUp ? ledOn(greenLed, blueLed, blueLed) : ledOff(greenLed, blueLed, blueLed);
- }else if (topicStr == redSwitch) {
- client.publish(redConfirm, &confrimation);
- goUp ? ledOn(redLed, redLed, redLed) : ledOff(redLed, redLed, redLed);
- }else if (topicStr == greenSwitch) {
- client.publish(greenConfirm, &confrimation);
- goUp ? ledOn(greenLed, greenLed, greenLed) : ledOff(greenLed, greenLed, greenLed);
- }else if (topicStr == blueSwitch) {
- client.publish(blueConfirm, &confrimation);
- goUp ? ledOn(blueLed, blueLed, blueLed) : ledOff(blueLed, blueLed, blueLed);
- }
- }
- bool checkBound(float newValue, float prevValue, float maxDiff) {
- return !isnan(newValue) &&
- (newValue < prevValue - maxDiff || newValue > prevValue + maxDiff);
- }
- void lightLed(int color1, int color2, int color3) {
- bool finished = false;
- // Serial.println("brightness");
- // Serial.println(brightness);
- while(!finished) {
- analogWrite(color1, brightness);
- analogWrite(color2, brightness);
- analogWrite(color3, brightness);
- brightness = brightness + fadeAmount;
- if (brightness <= 0 || brightness >= 210) {
- finished = true;
- }
- delay(30);
- }
- }
- // todo turn off builtin led (desolder or put builtin pin to LOW when not used )
- void ledOn(int color1, int color2, int color3) {
- if(brightness >= 210) {
- return;
- }
- brightness=0;
- if(fadeAmount<0) {
- fadeAmount = 1;
- }
- lightLed(color1, color2, color3);
- analogWrite(color1, 210);
- analogWrite(color2, 210);
- analogWrite(color3, 210);
- }
- void ledOff(int color1, int color2, int color3) {
- if(brightness <= 0) {
- return;
- }
- brightness=210;
- if(fadeAmount>0) {
- fadeAmount = -2;
- }
- lightLed(color1, color2, color3);
- analogWrite(color1, 0);
- analogWrite(color2, 0);
- analogWrite(color3, 0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement