Guest User

Untitled

a guest
Sep 4th, 2018
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.26 KB | None | 0 0
  1. //#define BLYNK_DEBUG
  2. #define BLYNK_PRINT Serial
  3.  
  4. #include <ArduinoOTA.h>
  5. #include <ESP8266WiFi.h>
  6. #include <BlynkSimpleEsp8266.h>
  7. #include <TimeLib.h>
  8. #include <WidgetRTC.h>
  9.  
  10. #include <ESP_SSD1306.h> // Modification of Adafruit_SSD1306 for ESP8266 compatibility -> https://github.com/somhi/ESP_SSD1306
  11. #include <Adafruit_GFX.h> // Needs a little change in original Adafruit library (See README.txt file) --> https://github.com/adafruit/Adafruit-GFX-Library
  12. #include <SPI.h> // For SPI comm (needed for not getting compile error)
  13.  
  14.  
  15. #include "settings_base.h"
  16. #include "globals.h"
  17. //#include "functions.h"
  18. //#include "blynk_writes.h"
  19.  
  20.  
  21.  
  22. //Pin connected to ST_CP of 74HC595
  23. const int latchPin = 15; //D8
  24. //Pin connected to SH_CP of 74HC595
  25. const int clockPin = 14; //D5
  26. //Pin connected to DS of 74HC595
  27. const int dataPin = 13; // D7
  28.  
  29.  
  30. int totalRelays = 2; // Number of relays
  31. int relayState = 0; // default state = off
  32. int relayArr[] = {12, 16}; // Pins on the Wemos
  33. char vbutton1 = V1; char vbutton2 = V2;
  34. char relayVButtons[] = {V1,V2};
  35. int zone = 0;
  36. int relayVButton = 0;
  37. byte data;
  38. byte dataArray[10];
  39.  
  40.  
  41. /*
  42. SETUP
  43. */
  44. BLYNK_WRITE(V1)
  45. {
  46. // Get the state of the VButton
  47. relayVButton = param.asInt();
  48. relayControl(relayVButton, 12, 49); // Test led on Wemos pin
  49. //int bitToSet = 49;
  50.  
  51. Serial.println("V1 pressed");
  52. }
  53.  
  54. BLYNK_WRITE(V2)
  55. {
  56. // Get the state of the VButton
  57. relayVButton = param.asInt();
  58. relayControl(relayVButton, 16, 50); // Test led on Wemos pin
  59. Serial.println("V2 pressed");
  60. }
  61.  
  62. void setup() {
  63.  
  64. pinMode(dataPin, OUTPUT); // All 3 pins are output
  65. pinMode(clockPin, OUTPUT);
  66. pinMode(latchPin, OUTPUT);
  67.  
  68. // Do some loop through all the relays and buttons for pinMode
  69. for (int i=0; i<=totalRelays; i++){
  70. pinMode(relayArr[i], OUTPUT);
  71. digitalWrite(relayArr[i], 0);
  72. pinMode(relayVButtons[i], INPUT);
  73. }
  74.  
  75. // COMMUNICATIONS
  76. Serial.begin(115200);
  77. WiFi.mode(WIFI_STA);
  78. // CONNECT TO BLYNK
  79. Blynk.begin(AUTH, WIFI_SSID, WIFI_PASS);
  80. while (Blynk.connect() == false) {}
  81. // OVER THE AIR UPDATES
  82. ArduinoOTA.setHostname(OTA_HOSTNAME);
  83. ArduinoOTA.begin();
  84.  
  85. Serial.println(F("Blynk v" BLYNK_VERSION ": Device started"));
  86. }
  87. /*
  88. LOOP
  89. */
  90. void loop() {
  91. Blynk.run();
  92. ArduinoOTA.handle();
  93. timer.run();
  94. }
  95.  
  96. void relayControl(int onoff, int relayNumber, int bitToSet){
  97. if(onoff == 1) // Virtual button set to on
  98. {
  99. digitalWrite(relayNumber, HIGH); // set Wemos pin on HIGH (for test)
  100. registerWrite(bitToSet, HIGH);
  101. relayState = 1;
  102. }
  103. else
  104. {
  105. digitalWrite(relayNumber, LOW); // set Wemos pin on HIGH (for test)
  106. registerWrite(bitToSet, LOW);
  107. relayState = 0;
  108. }
  109. }
  110. void registerWrite(int whichPin, int whichState) {
  111. // the bits you want to send
  112. byte bitsToSend = 0;
  113.  
  114. // turn off the output so the pins don't light up
  115. // while you're shifting bits:
  116. digitalWrite(latchPin, LOW);
  117.  
  118. // turn on the next highest bit in bitsToSend:
  119. bitWrite(bitsToSend, whichPin, whichState);
  120.  
  121. // shift the bits out:
  122. shiftOut(dataPin, clockPin, MSBFIRST, bitsToSend);
  123.  
  124. // turn on the output so the LEDs can light up:
  125. digitalWrite(latchPin, HIGH);
  126.  
  127. }
  128.  
  129. //#define BLYNK_DEBUG
  130. #define BLYNK_PRINT Serial
  131.  
  132. #include <ArduinoOTA.h>
  133. #include <ESP8266WiFi.h>
  134. #include <BlynkSimpleEsp8266.h>
  135. #include <TimeLib.h>
  136. #include <WidgetRTC.h>
  137.  
  138. #include <ESP_SSD1306.h> // Modification of Adafruit_SSD1306 for ESP8266 compatibility -> https://github.com/somhi/ESP_SSD1306
  139. #include <Adafruit_GFX.h> // Needs a little change in original Adafruit library (See README.txt file) --> https://github.com/adafruit/Adafruit-GFX-Library
  140. #include <SPI.h> // For SPI comm (needed for not getting compile error)
  141.  
  142.  
  143. #include "settings_base.h"
  144. #include "globals.h"
  145. //#include "functions.h"
  146. //#include "blynk_writes.h"
  147.  
  148.  
  149.  
  150. //Pin connected to ST_CP of 74HC595
  151. const int latchPin = 15; //D8
  152. //Pin connected to SH_CP of 74HC595
  153. const int clockPin = 14; //D5
  154. //Pin connected to DS of 74HC595
  155. const int dataPin = 13; // D7
  156.  
  157.  
  158. int totalRelays = 2; // Number of relays
  159. int relayState = 0; // default state = off
  160. int relayArr[] = {12, 16}; // Pins on the Wemos
  161. char vbutton1 = V1; char vbutton2 = V2;
  162. char relayVButtons[] = {V1,V2};
  163. int zone = 0;
  164. int relayVButton = 0;
  165. byte data;
  166. byte dataArray[10];
  167.  
  168.  
  169. /*
  170. SETUP
  171. */
  172. BLYNK_WRITE(V1)
  173. {
  174. // Get the state of the VButton
  175. relayVButton = param.asInt();
  176. relayControl(relayVButton, 12, 49); // Test led on Wemos pin
  177. //int bitToSet = 49;
  178.  
  179. Serial.println("V1 pressed");
  180. }
  181.  
  182. BLYNK_WRITE(V2)
  183. {
  184. // Get the state of the VButton
  185. relayVButton = param.asInt();
  186. relayControl(relayVButton, 16, 50); // Test led on Wemos pin
  187. Serial.println("V2 pressed");
  188. }
  189.  
  190. void setup() {
  191.  
  192. pinMode(dataPin, OUTPUT); // All 3 pins are output
  193. pinMode(clockPin, OUTPUT);
  194. pinMode(latchPin, OUTPUT);
  195.  
  196. // Do some loop through all the relays and buttons for pinMode
  197. for (int i=0; i<=totalRelays; i++){
  198. pinMode(relayArr[i], OUTPUT);
  199. digitalWrite(relayArr[i], 0);
  200. pinMode(relayVButtons[i], INPUT);
  201. }
  202.  
  203. // COMMUNICATIONS
  204. Serial.begin(115200);
  205. WiFi.mode(WIFI_STA);
  206. // CONNECT TO BLYNK
  207. Blynk.begin(AUTH, WIFI_SSID, WIFI_PASS);
  208. while (Blynk.connect() == false) {}
  209. // OVER THE AIR UPDATES
  210. ArduinoOTA.setHostname(OTA_HOSTNAME);
  211. ArduinoOTA.begin();
  212.  
  213. Serial.println(F("Blynk v" BLYNK_VERSION ": Device started"));
  214. }
  215. /*
  216. LOOP
  217. */
  218. void loop() {
  219. Blynk.run();
  220. ArduinoOTA.handle();
  221. timer.run();
  222. }
  223.  
  224. void relayControl(int onoff, int relayNumber, int bitToSet){
  225. if(onoff == 1) // Virtual button set to on
  226. {
  227. digitalWrite(relayNumber, HIGH); // set Wemos pin on HIGH (for test)
  228. registerWrite(bitToSet, HIGH);
  229. relayState = 1;
  230. }
  231. else
  232. {
  233. digitalWrite(relayNumber, LOW); // set Wemos pin on HIGH (for test)
  234. registerWrite(bitToSet, LOW);
  235. relayState = 0;
  236. }
  237. }
  238. void registerWrite(int whichPin, int whichState) {
  239. // the bits you want to send
  240. byte bitsToSend = 0;
  241.  
  242. // turn off the output so the pins don't light up
  243. // while you're shifting bits:
  244. digitalWrite(latchPin, LOW);
  245.  
  246. // turn on the next highest bit in bitsToSend:
  247. bitWrite(bitsToSend, whichPin, whichState);
  248.  
  249. // shift the bits out:
  250. shiftOut(dataPin, clockPin, MSBFIRST, bitsToSend);
  251.  
  252. // turn on the output so the LEDs can light up:
  253. digitalWrite(latchPin, HIGH);
  254.  
  255. }
Add Comment
Please, Sign In to add comment