Advertisement
Guest User

Untitled

a guest
Dec 4th, 2018
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.50 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <PubSubClient.h>
  3. #include <OneWire.h>
  4. #include <DallasTemperature.h>
  5. #include <ArduinoOTA.h>
  6.  
  7. #define ONE_WIRE_BUS D5
  8. const char* ssid = "coskonkretnego";
  9. const char* password = "UhYgdFrt54de";
  10. const char* mqttServer = "192.168.1.105";
  11. const int mqttPort = 1883;
  12. const char* mqttUser = "dom";
  13. const char* mqttPassword = "dupa123";
  14. const char* mqttLogin = "MYROOM_STAIRS";
  15. const char* WiFi_hostname = "MYROOM-STAIRS";
  16.  
  17.  
  18.  
  19. OneWire oneWire(ONE_WIRE_BUS);
  20. DallasTemperature sensors(&oneWire);
  21.  
  22. WiFiClient espClient;
  23. PubSubClient client(espClient);
  24.  
  25. void callback(char* topic, byte* payload, unsigned int length);
  26. void connect();
  27.  
  28. void ledOn(int color1, int color2, int color3);
  29. void ledOff(int color1, int color2, int color3);
  30.  
  31. int redLed = D2;
  32. int greenLed = D3;
  33. int blueLed = D4;
  34. int buttonPinLeft = D8;
  35. int pirPin = D7;
  36.  
  37. char const *switchMainLight = "toggle/house/floor1/myroom/mainlight";
  38. char const *temperatureTopic = "sensor/temp/house/floor1/myroom/stairs";
  39. char const *pirTopic = "sensor/pir/house/floor1/myroom/stairs";
  40.  
  41. char const *rgbSwitch = "switch/house/floor1/myroom/stairs/led";
  42. char const *rgbConfirm = "confirm/house/floor1/myroom/stairs/led";
  43.  
  44. char const *redSwitch = "switch/house/floor1/myroom/stairs/led/red";
  45. char const *redConfirm = "confirm/house/floor1/myroom/stairs/led/red";
  46.  
  47. char const *greenSwitch = "switch/house/floor1/myroom/stairs/led/green";
  48. char const *greenConfirm = "confirm/house/floor1/myroom/stairs/led/green";
  49.  
  50. char const *blueSwitch = "switch/house/floor1/myroom/stairs/led/blue";
  51. char const *blueConfirm = "confirm/house/floor1/myroom/stairs/led/blue";
  52.  
  53. char const *redGreenSwitch = "switch/house/floor1/myroom/stairs/led/redGreen";
  54. char const *redGreenConfirm = "confirm/house/floor1/myroom/stairs/led/redGreen";
  55.  
  56. char const *redBlueSwitch = "switch/house/floor1/myroom/stairs/led/redBlue";
  57. char const *redBlueConfirm = "confirm/house/floor1/myroom/stairs/led/redBlue";
  58.  
  59. char const *greenBlueSwitch = "switch/house/floor1/myroom/stairs/led/greenBlue";
  60. char const *greenBlueConfirm = "confirm/house/floor1/myroom/stairs/led/greenBlue";
  61.  
  62.  
  63. int brightness = 0; // how bright the LED is
  64. int fadeAmount = 2; // how many points to fade the LED by
  65.  
  66. void connect() {
  67. WiFi.hostname(WiFi_hostname);
  68. WiFi.mode(WIFI_STA);
  69. WiFi.begin(ssid, password);
  70.  
  71. while (WiFi.status() != WL_CONNECTED) {
  72. delay(500);
  73. Serial.println("Connecting to WiFi..");
  74. }
  75. Serial.println("Connected to the WiFi network");
  76.  
  77. client.setServer(mqttServer, mqttPort);
  78. client.setCallback(callback);
  79.  
  80. while (!client.connected()) {
  81. Serial.println("Connecting to MQTT...");
  82.  
  83. if (client.connect(mqttLogin, mqttUser, mqttPassword )) {
  84.  
  85. Serial.println("connected");
  86.  
  87. } else {
  88.  
  89. Serial.print("failed with state ");
  90. Serial.print(client.state());
  91. delay(2000);
  92.  
  93. }
  94. }
  95.  
  96. delay(100);
  97. }
  98. long toggleMoment;
  99. long pastToggleMoment = 0;
  100. void toggleLight() {
  101. toggleMoment = millis();
  102. if(toggleMoment - pastToggleMoment > 1000) {
  103. pastToggleMoment = toggleMoment;
  104. Serial.println("buttonpushed");
  105. client.publish(switchMainLight, "1");
  106. }
  107.  
  108. }
  109.  
  110. void pirOn() {
  111. int motion = digitalRead(pirPin);
  112. if(motion) {
  113. // Serial.println("WGURE");
  114. client.publish(pirTopic, "1");
  115. } else{
  116. // Serial.println("WDUL");
  117. client.publish(pirTopic, "0");
  118. }
  119. }
  120.  
  121.  
  122. void setup() {
  123. Serial.begin(9600);
  124. ArduinoOTA.setHostname("StairsSwitch");
  125. ArduinoOTA.begin(); // OTA initialization
  126. delay(100);
  127.  
  128. connect();
  129. pinMode(buttonPinLeft, INPUT);
  130. pinMode(pirPin, INPUT);
  131. pinMode(redLed, OUTPUT);
  132. pinMode(greenLed, OUTPUT);
  133. pinMode(blueLed, OUTPUT);
  134.  
  135. // digitalWrite(0, redLed);
  136. // digitalWrite(0, greenLed);
  137. // digitalWrite(0, blueLed);
  138.  
  139. attachInterrupt(digitalPinToInterrupt(buttonPinLeft), toggleLight,FALLING);
  140. attachInterrupt(digitalPinToInterrupt(pirPin), pirOn, CHANGE);
  141.  
  142. sensors.begin();
  143. delay(100);
  144.  
  145. client.subscribe(rgbSwitch);
  146.  
  147. client.subscribe(redSwitch);
  148. client.subscribe(greenSwitch);
  149. client.subscribe(blueSwitch);
  150.  
  151. client.subscribe(redBlueSwitch);
  152. client.subscribe(redGreenSwitch);
  153.  
  154. client.subscribe(greenBlueSwitch);
  155.  
  156. }
  157.  
  158. int buttonState = 0;
  159. int lastButtonState = 0;
  160.  
  161. int goUp;
  162.  
  163. int motion = 0;
  164. long lastMsg = 0;
  165. float temp = 0.0;
  166. float tempDiff = 0.1;
  167. bool firstTime = true;
  168.  
  169.  
  170. bool isButtonPressed(int buttonPin, int &buttonState, int &lastButtonState) {
  171. bool pressed = LOW;
  172.  
  173. buttonState = digitalRead(buttonPin);
  174.  
  175. if (buttonState != lastButtonState) {
  176. if (buttonState == HIGH) {
  177. pressed = HIGH;
  178. } else {
  179. }
  180. delay(50);
  181. }
  182. lastButtonState = buttonState;
  183. return pressed;
  184.  
  185. }
  186. // todo: add healthCheck
  187. void loop() {
  188.  
  189. if (!client.connected() && WiFi.status() == 3) {
  190. connect();
  191. }
  192.  
  193. client.loop();
  194. delay(10);
  195. // if (isButtonPressed(buttonPinLeft, buttonState, lastButtonState)) {
  196. // Serial.println("buttonpushed");
  197. // client.publish(switchMainLight, "1");
  198. // }
  199.  
  200. ArduinoOTA.handle();
  201.  
  202. long now = millis();
  203. float temp;
  204. if(firstTime ) {
  205. firstTime = false;
  206. sensors.requestTemperatures();
  207.  
  208. temp = sensors.getTempCByIndex(0);
  209.  
  210. // if (checkBound(newTemp, temp, tempDiff)) {
  211. Serial.print("New temperature:");
  212. Serial.println(String(temp).c_str());
  213. client.publish(temperatureTopic, String(temp).c_str(), true);
  214. }
  215.  
  216. if (now - lastMsg > 15000) {
  217. lastMsg = now;
  218. sensors.requestTemperatures();
  219. // }
  220.  
  221. temp = sensors.getTempCByIndex(0);
  222.  
  223. // if (checkBound(newTemp, temp, tempDiff)) {
  224. Serial.print("New temperature:");
  225. Serial.println(String(temp).c_str());
  226. client.publish(temperatureTopic, String(temp).c_str(), true);
  227. // }
  228.  
  229. // if (motion != newMotion) {
  230. // motion = newMotion;
  231. // Serial.print("New motion:");
  232. // Serial.println(String(motion).c_str());
  233. // client.publish(pirTopic, String(motion).c_str(), true);
  234. // }
  235. }
  236. }
  237. void callback(char *topic, byte *payload, unsigned int length) {
  238. //convert topic to string to make it easier to work with
  239. String topicStr = topic;
  240. //EJ: Note: the "topic" value gets overwritten everytime it receives confirmation (callback) message from MQTT
  241.  
  242. //Print out some debugging info
  243. Serial.println("Callback update.");
  244. Serial.print("Topic: ");
  245. Serial.println(topicStr);
  246.  
  247. char confrimation = payload[0];
  248.  
  249. if(payload[0] == '1') {
  250. goUp = 1;
  251. }else {
  252. goUp = 0;
  253. }
  254.  
  255. if (topicStr == rgbSwitch) {
  256. client.publish(rgbConfirm, &confrimation);
  257.  
  258. goUp ? ledOn(redLed, greenLed, blueLed) : ledOff(redLed, greenLed, blueLed);
  259.  
  260. } else if (topicStr == redGreenSwitch) {
  261. client.publish(redGreenConfirm, &confrimation);
  262.  
  263. goUp ? ledOn(redLed, greenLed, greenLed) : ledOff(redLed, greenLed, greenLed);
  264.  
  265. } else if (topicStr == redBlueSwitch) {
  266. client.publish(redBlueConfirm, &confrimation);
  267.  
  268. goUp ? ledOn(redLed, blueLed, blueLed) : ledOff(redLed, blueLed, blueLed);
  269.  
  270. }else if (topicStr == greenBlueSwitch) {
  271. client.publish(greenBlueConfirm, &confrimation);
  272.  
  273. goUp ? ledOn(greenLed, blueLed, blueLed) : ledOff(greenLed, blueLed, blueLed);
  274.  
  275. }else if (topicStr == redSwitch) {
  276. client.publish(redConfirm, &confrimation);
  277. goUp ? ledOn(redLed, redLed, redLed) : ledOff(redLed, redLed, redLed);
  278.  
  279. }else if (topicStr == greenSwitch) {
  280. client.publish(greenConfirm, &confrimation);
  281. goUp ? ledOn(greenLed, greenLed, greenLed) : ledOff(greenLed, greenLed, greenLed);
  282.  
  283. }else if (topicStr == blueSwitch) {
  284. client.publish(blueConfirm, &confrimation);
  285. goUp ? ledOn(blueLed, blueLed, blueLed) : ledOff(blueLed, blueLed, blueLed);
  286.  
  287. }
  288.  
  289.  
  290. }
  291.  
  292. bool checkBound(float newValue, float prevValue, float maxDiff) {
  293. return !isnan(newValue) &&
  294. (newValue < prevValue - maxDiff || newValue > prevValue + maxDiff);
  295. }
  296.  
  297.  
  298. void lightLed(int color1, int color2, int color3) {
  299. bool finished = false;
  300. // Serial.println("brightness");
  301. // Serial.println(brightness);
  302.  
  303. while(!finished) {
  304.  
  305. analogWrite(color1, brightness);
  306. analogWrite(color2, brightness);
  307. analogWrite(color3, brightness);
  308. brightness = brightness + fadeAmount;
  309. if (brightness <= 0 || brightness >= 210) {
  310. finished = true;
  311. }
  312. delay(30);
  313. }
  314.  
  315. }
  316. // todo turn off builtin led (desolder or put builtin pin to LOW when not used )
  317. void ledOn(int color1, int color2, int color3) {
  318. if(brightness >= 210) {
  319. return;
  320. }
  321. brightness=0;
  322. if(fadeAmount<0) {
  323. fadeAmount = 1;
  324. }
  325.  
  326. lightLed(color1, color2, color3);
  327.  
  328. analogWrite(color1, 210);
  329. analogWrite(color2, 210);
  330. analogWrite(color3, 210);
  331. }
  332.  
  333.  
  334. void ledOff(int color1, int color2, int color3) {
  335. if(brightness <= 0) {
  336. return;
  337. }
  338. brightness=210;
  339.  
  340. if(fadeAmount>0) {
  341. fadeAmount = -2;
  342. }
  343.  
  344. lightLed(color1, color2, color3);
  345. analogWrite(color1, 0);
  346. analogWrite(color2, 0);
  347. analogWrite(color3, 0);
  348.  
  349. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement