Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include <PubSubClient.h>
- #include "math.h"
- #include <Stepper.h>
- #ifndef Pins_Arduino_h
- #define Pins_Arduino_h
- #include "../generic/common.h"
- #define PIN_WIRE_SDA (4)
- #define PIN_WIRE_SCL (5)
- static const uint8_t SDA = PIN_WIRE_SDA;
- static const uint8_t SCL = PIN_WIRE_SCL;
- static const uint8_t LED_BUILTIN = 2;//new ESP-12E GPIO2
- static const uint8_t BUILTIN_LED = 2;//new ESP-12E GPIO2
- static const uint8_t D0 = 3;
- static const uint8_t D1 = 1;
- static const uint8_t D2 = 16;
- static const uint8_t D3 = 5;
- static const uint8_t D4 = 4;
- static const uint8_t D5 = 14;
- static const uint8_t D6 = 12;
- static const uint8_t D7 = 13;
- static const uint8_t D8 = 0;
- static const uint8_t D9 = 2;
- static const uint8_t D10 = 15;
- static const uint8_t D11 = 13;
- static const uint8_t D12 = 12;
- static const uint8_t D13 = 14;
- static const uint8_t D14 = 4;
- static const uint8_t D15 = 5;
- #endif /* Pins_Arduino_h */
- /************************* WiFi Access Point (CHANGE THIS) **********************************/
- const char* ssid = "Wifi Network ID";
- const char* password = "Wifi Network Password";
- /************************* MQTT Setup (CHANGE THIS)****************************************/
- const char* mqtt_server = "MQTT Address";
- const char* topic_state = "/blinds/state";
- const char* topic_command = "/blinds/command";
- WiFiClient espClient;
- PubSubClient client (espClient);
- /*********************** Define Code ****************************************************/
- //Declare pin functions on Arduino
- #define stp D2
- #define dir D3
- #define MS1 D4
- #define MS2 D5
- #define MS3 D6
- #define EN D7
- //Declare variables for functions
- char user_input;
- int x;
- int y;
- int state;
- /*********************************Test Define (May Need to Remove)*************************/
- #define STEPS 200
- #define ADCwidth 12
- #define ADCsteps pow(2, ADCwidth)
- #define maxSpeed 256 //RPM (for test, using max LED value 255)
- #define MAX_ACT 2000
- uint8_t speed = 0;
- unsigned long del = 2;
- unsigned long step = 0;
- int forward = 0; //0=forward 1=backward 2=stand still
- int req = 0;
- int act = 0;
- /****************************************************************************************/
- /*********************** 1. Initialize Code *********************************************/
- /****************************************************************************************/
- void setup() {
- pinMode(stp, OUTPUT);
- pinMode(dir, OUTPUT);
- pinMode(MS1, OUTPUT);
- pinMode(MS2, OUTPUT);
- pinMode(MS3, OUTPUT);
- pinMode(EN, OUTPUT);
- resetBEDPins(); //Set step, direction, microstep and enable pins to default states
- Serial.begin(115200);
- delay(10);
- Serial.println("Blind Startup Sequence");
- setup_wifi();
- client.setServer(mqtt_server, 1883);
- client.setCallback(callback);
- }
- /****************************************************************************************/
- /************** 2. Reset Big Easy Driver Pins to Default ********************************/
- /****************************************************************************************/
- void resetBEDPins()
- {
- digitalWrite(stp, LOW);
- digitalWrite(dir, LOW);
- digitalWrite(MS1, LOW);
- digitalWrite(MS2, LOW);
- digitalWrite(MS3, LOW);
- digitalWrite(EN, HIGH);
- }
- /****************************************************************************************/
- /************************ 3. Connect to WiFI ********************************************/
- /****************************************************************************************/
- void setup_wifi() {
- delay(100);
- // We start by connecting to a WiFi network
- Serial.print("Connecting to ");
- Serial.println(ssid);
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED)
- {
- delay(500);
- Serial.print(".");
- }
- randomSeed(micros());
- Serial.println("");
- Serial.println("WiFi connected");
- Serial.println("IP address: ");
- Serial.println(WiFi.localIP());
- }
- /****************************************************************************************/
- /************************ 4. MQTT Subscription *****************************************/
- /****************************************************************************************/
- void callback(char* topic, byte* payload, unsigned int length)
- {
- Serial.print("Command from MQTT broker is :");
- int p =(char)payload[0]-'0';
- // step one revolution in one direction:
- digitalWrite(EN, LOW); //Pull enable pin low to set FETs active and allow motor control
- if(p==1)
- {
- Serial.println("Raising blinds at default step mode.");
- digitalWrite(dir, LOW); //Pull direction pin low to move "forward"
- digitalWrite(MS1, HIGH); //Pull MS1,MS2, and MS3 high to set logic to 1/16th microstep resolution
- digitalWrite(MS2, HIGH);
- digitalWrite(MS3, HIGH);
- 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
- {
- digitalWrite(stp,HIGH); //Trigger one step forward
- delay(1);
- digitalWrite(stp,LOW); //Pull step pin low so it can be triggered again
- delay(1);
- }
- }
- // step one revolution in the other direction:
- else if(p==2)
- {
- Serial.println("Lowering blinds at default step mode.");
- digitalWrite(dir, HIGH); //Pull direction pin low to move "forward"
- digitalWrite(MS1, HIGH); //Pull MS1,MS2, and MS3 high to set logic to 1/16th microstep resolution
- digitalWrite(MS2, HIGH);
- digitalWrite(MS3, HIGH);
- 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
- {
- digitalWrite(stp,HIGH); //Trigger one step
- delay(1);
- digitalWrite(stp,LOW); //Pull step pin low so it can be triggered again
- delay(1);
- }
- }
- Serial.println();
- }
- /****************************************************************************************/
- /************************ 5. MQTT Connection *******************************************/
- /****************************************************************************************/
- void mqtt_connect() {
- // Loop until we're reconnected
- while (!client.connected())
- {
- Serial.print("Attempting MQTT connection...");
- // Create a random client ID
- String clientId = "ESP8266Client-";
- clientId += String(random(0xffff), HEX);
- // Attempt to connect
- //if you MQTT broker has clientID,username and password
- //please change following line to if (client.connect(clientId,userName,passWord))
- if (client.connect(clientId.c_str()))
- {
- Serial.println("connected");
- //once connected to MQTT broker, subscribe command if any
- client.subscribe(topic_command);
- } else {
- Serial.print("failed, rc=");
- Serial.print(client.state());
- Serial.println(" try again in 5 seconds");
- // Wait 6 seconds before retrying
- delay(6000);
- }
- }
- }
- /****************************************************************************************/
- /************************ 6. Main Code Loop *********************************************/
- /****************************************************************************************/
- void loop() {
- //potVal = analogRead(potPin);
- //speed = floor(potVal/(ADCsteps/maxSpeed));
- if (!client.connected()) {
- mqtt_connect();
- }
- else{
- client.loop();
- }
- }
Add Comment
Please, Sign In to add comment