Advertisement
Guest User

Untitled

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