Guest User

MQTT Blind

a guest
Apr 7th, 2017
1,144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.49 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <PubSubClient.h>
  3. #include "math.h"
  4. #include <Stepper.h>
  5.  
  6. #ifndef Pins_Arduino_h
  7. #define Pins_Arduino_h
  8.  
  9. #include "../generic/common.h"
  10.  
  11. #define PIN_WIRE_SDA (4)
  12. #define PIN_WIRE_SCL (5)
  13.  
  14. static const uint8_t SDA = PIN_WIRE_SDA;
  15. static const uint8_t SCL = PIN_WIRE_SCL;
  16.  
  17. static const uint8_t LED_BUILTIN = 2;//new ESP-12E GPIO2
  18. static const uint8_t BUILTIN_LED = 2;//new ESP-12E GPIO2
  19.  
  20. static const uint8_t D0 = 3;
  21. static const uint8_t D1 = 1;
  22. static const uint8_t D2 = 16;
  23. static const uint8_t D3 = 5;
  24. static const uint8_t D4 = 4;
  25. static const uint8_t D5 = 14;
  26. static const uint8_t D6 = 12;
  27. static const uint8_t D7 = 13;
  28. static const uint8_t D8 = 0;
  29. static const uint8_t D9 = 2;
  30. static const uint8_t D10 = 15;
  31. static const uint8_t D11 = 13;
  32. static const uint8_t D12 = 12;
  33. static const uint8_t D13 = 14;
  34. static const uint8_t D14 = 4;
  35. static const uint8_t D15 = 5;
  36.  
  37. #endif /* Pins_Arduino_h */
  38.  
  39.  
  40. /************************* WiFi Access Point (CHANGE THIS) **********************************/
  41.  
  42. const char* ssid = "Wifi Network ID";
  43. const char* password = "Wifi Network Password";
  44.  
  45. /************************* MQTT Setup (CHANGE THIS)****************************************/
  46.  
  47. const char* mqtt_server = "MQTT Address";
  48. const char* topic_state = "/blinds/state";
  49. const char* topic_command = "/blinds/command";
  50. WiFiClient espClient;
  51. PubSubClient client (espClient);
  52.  
  53.  
  54. /*********************** Define Code ****************************************************/
  55.  
  56.  
  57.  
  58.  
  59. //Declare pin functions on Arduino
  60. #define stp D2
  61. #define dir D3
  62. #define MS1 D4
  63. #define MS2 D5
  64. #define MS3 D6
  65. #define EN D7
  66.  
  67.  
  68. //Declare variables for functions
  69. char user_input;
  70. int x;
  71. int y;
  72. int state;
  73.  
  74. /*********************************Test Define (May Need to Remove)*************************/
  75. #define STEPS 200
  76. #define ADCwidth 12
  77. #define ADCsteps pow(2, ADCwidth)
  78. #define maxSpeed 256 //RPM (for test, using max LED value 255)
  79. #define MAX_ACT 2000
  80.  
  81. uint8_t speed = 0;
  82.  
  83. unsigned long del = 2;
  84. unsigned long step = 0;
  85. int forward = 0; //0=forward 1=backward 2=stand still
  86. int req = 0;
  87. int act = 0;
  88.  
  89.  
  90. /****************************************************************************************/
  91. /*********************** 1. Initialize Code *********************************************/
  92. /****************************************************************************************/
  93. void setup() {
  94. pinMode(stp, OUTPUT);
  95. pinMode(dir, OUTPUT);
  96. pinMode(MS1, OUTPUT);
  97. pinMode(MS2, OUTPUT);
  98. pinMode(MS3, OUTPUT);
  99. pinMode(EN, OUTPUT);
  100. resetBEDPins(); //Set step, direction, microstep and enable pins to default states
  101. Serial.begin(115200);
  102. delay(10);
  103. Serial.println("Blind Startup Sequence");
  104. setup_wifi();
  105. client.setServer(mqtt_server, 1883);
  106. client.setCallback(callback);
  107. }
  108.  
  109. /****************************************************************************************/
  110. /************** 2. Reset Big Easy Driver Pins to Default ********************************/
  111. /****************************************************************************************/
  112.  
  113. void resetBEDPins()
  114. {
  115. digitalWrite(stp, LOW);
  116. digitalWrite(dir, LOW);
  117. digitalWrite(MS1, LOW);
  118. digitalWrite(MS2, LOW);
  119. digitalWrite(MS3, LOW);
  120. digitalWrite(EN, HIGH);
  121. }
  122.  
  123. /****************************************************************************************/
  124. /************************ 3. Connect to WiFI ********************************************/
  125. /****************************************************************************************/
  126.  
  127. void setup_wifi() {
  128. delay(100);
  129. // We start by connecting to a WiFi network
  130. Serial.print("Connecting to ");
  131. Serial.println(ssid);
  132. WiFi.begin(ssid, password);
  133. while (WiFi.status() != WL_CONNECTED)
  134. {
  135. delay(500);
  136. Serial.print(".");
  137. }
  138. randomSeed(micros());
  139. Serial.println("");
  140. Serial.println("WiFi connected");
  141. Serial.println("IP address: ");
  142. Serial.println(WiFi.localIP());
  143. }
  144.  
  145. /****************************************************************************************/
  146. /************************ 4. MQTT Subscription *****************************************/
  147. /****************************************************************************************/
  148.  
  149. void callback(char* topic, byte* payload, unsigned int length)
  150. {
  151. Serial.print("Command from MQTT broker is :");
  152. int p =(char)payload[0]-'0';
  153. // step one revolution in one direction:
  154. digitalWrite(EN, LOW); //Pull enable pin low to set FETs active and allow motor control
  155. if(p==1)
  156. {
  157. Serial.println("Raising blinds at default step mode.");
  158. digitalWrite(dir, LOW); //Pull direction pin low to move "forward"
  159. digitalWrite(MS1, HIGH); //Pull MS1,MS2, and MS3 high to set logic to 1/16th microstep resolution
  160. digitalWrite(MS2, HIGH);
  161. digitalWrite(MS3, HIGH);
  162. for(x= 1; x<75000; x++) //Loop the forward stepping enough times for motion to be visible, change x<Number Value to adjust amount of time to raise blinds
  163. {
  164. digitalWrite(stp,HIGH); //Trigger one step forward
  165. delay(1);
  166. digitalWrite(stp,LOW); //Pull step pin low so it can be triggered again
  167. delay(1);
  168. }
  169. }
  170. // step one revolution in the other direction:
  171. else if(p==2)
  172. {
  173. Serial.println("Lowering blinds at default step mode.");
  174. digitalWrite(dir, HIGH); //Pull direction pin low to move "forward"
  175. digitalWrite(MS1, HIGH); //Pull MS1,MS2, and MS3 high to set logic to 1/16th microstep resolution
  176. digitalWrite(MS2, HIGH);
  177. digitalWrite(MS3, HIGH);
  178. for(x= 1; x<75000; x++) //Loop the forward stepping enough times for motion to be visible,change x<Number Value to adjust amount of time to lower blinds
  179. {
  180. digitalWrite(stp,HIGH); //Trigger one step
  181. delay(1);
  182. digitalWrite(stp,LOW); //Pull step pin low so it can be triggered again
  183. delay(1);
  184. }
  185. }
  186. Serial.println();
  187. }
  188.  
  189.  
  190. /****************************************************************************************/
  191. /************************ 5. MQTT Connection *******************************************/
  192. /****************************************************************************************/
  193.  
  194. void mqtt_connect() {
  195. // Loop until we're reconnected
  196. while (!client.connected())
  197. {
  198. Serial.print("Attempting MQTT connection...");
  199. // Create a random client ID
  200. String clientId = "ESP8266Client-";
  201. clientId += String(random(0xffff), HEX);
  202. // Attempt to connect
  203. //if you MQTT broker has clientID,username and password
  204. //please change following line to if (client.connect(clientId,userName,passWord))
  205. if (client.connect(clientId.c_str()))
  206. {
  207. Serial.println("connected");
  208. //once connected to MQTT broker, subscribe command if any
  209. client.subscribe(topic_command);
  210. } else {
  211. Serial.print("failed, rc=");
  212. Serial.print(client.state());
  213. Serial.println(" try again in 5 seconds");
  214. // Wait 6 seconds before retrying
  215. delay(6000);
  216. }
  217. }
  218. }
  219.  
  220. /****************************************************************************************/
  221. /************************ 6. Main Code Loop *********************************************/
  222. /****************************************************************************************/
  223.  
  224. void loop() {
  225. //potVal = analogRead(potPin);
  226. //speed = floor(potVal/(ADCsteps/maxSpeed));
  227.  
  228. if (!client.connected()) {
  229. mqtt_connect();
  230. }
  231. else{
  232. client.loop();
  233. }
  234.  
  235. }
Add Comment
Please, Sign In to add comment