//Parts that contains wifi connection codes, is found at the Course website. //////////// Initiering /////////// //Including librabries #include #include #include #include #include #include //Defining variables for the stepper motor steering: int mode = 0; int input = 3; int val = 0; #define STEPS 2038 // the number of steps in one revolution of the motor (28BYJ-48) Stepper stepper(STEPS, D5, D6, D7, D8); //defining ouputpins from the NodeMCu 1.0 (ESP-12 E module) // Defining ID and Password for the internet that the MCU is using const char* ssid = "The Internet 2000"; const char* password = "Havenfrb"; // Defining the specific mqtt server const char *mqtt_server = "hairdresser.cloudmqtt.com"; //server name const int mqtt_port = 17315; // Port const char *mqtt_user = "mrplxbqp"; // user const char *mqtt_pass = "2rYuRBNJtHg-"; // user password // String payload; // Defining the variable "payload" as a string. int ledPin_G = D1; // defines outputpins from the MCU that connects to respectively red and green LED´s int ledPin_R = D2; // // // // // // /////// Function defining //////// //Creates a placeholder for the callback-function for use later on. The actual function can be seen further down void callback(char* byteArraytopic, byte* byteArrayPayload, unsigned int length); //Creates a client that can connect to a specific internet IP adress WiFiClient espClient; //Creates a connection to the mqtt client PubSubClient client(mqtt_server, mqtt_port, callback, espClient); //Initializes the libary to make it possible to recieve and send messages to mqtt. It retrieves from the defined mqtt server and port. It retrieves from topic and the message payload. /////// The function setup end ///////// // // // // // // ///////// CALLBACK FUNCTION //////// //Defines the callback function that recieve messages from mqtt. //OBS: this function is activated each time the MCU receive a messagr via mqtt. void callback(char* byteArraytopic, byte* byteArrayPayload, unsigned int length) { //Converts incoming messages (topic) to a string. String topic; topic = String(byteArraytopic); Serial.print("Message arrived ["); Serial.print(topic); Serial.println("] "); //Converts the incoming message (payload) from a array to a string if (topic == "Test") //tests if the topic is acceptable. it should be calle "Test" payload = ""; //Reset the payload variable so the for looped wont append to an already excisting payload. for (int i = 0; i < length; i++) { payload += (char)byteArrayPayload[i]; //If the payload variable is saved as "unlocked" 8and mode eqauls one) it will turn the servomotor 90 degrees counterclockwise, turn the green LED on and the red OFF //the mode is then set to 0, which indicates that the locker is already open. if the customer is trying to open the locker when its already open, nothing will happen, because the mode is not= 1 if (payload == "unlocked" && mode==1) { Serial.println("Test: unlocked"); //Sets the speed of the motor stepper.setSpeed(60); // 60 rpm stepper.step((-2038)/4); // do 2038/ steps -- corresponds to one quarter revolution Serial.println("Locker is open"); //LED green light delay(300); digitalWrite(D1, HIGH); digitalWrite(D2, LOW); //sets mode variable to 0 mode=0; //Send to nodered: delay(1000); client.publish("mathiashovmark@gmail.com/TilNodered", "LED is ON"); } //basically says the opposite of the first if statement. the motor will turn 90 degrees clockwise, the red LED will light up, and the green will be turned off. //The mode will be changed to 1, so its not possible to open it more, when its already open if (payload == "locked" && mode==0) { Serial.println("Test: locked"); //Moter locks: stepper.setSpeed(60); // 60 rpm stepper.step(2038/4); // do 2038/4 steps -- corresponds to one quarter revolution in one minute //LED light delay(300); digitalWrite(D1, LOW); digitalWrite(D2, HIGH); Serial.println("Locker is locked"); mode=1; //Send to nodered: delay(1000); client.publish("mathiashovmark@gmail.com/TilNodered", "LED is ON"); } } Serial.println(payload); } } ///////// CALLBACK END ///////// // // // // // // /////// WIFI CONNECTION SETUP /////////// //Creates a connection to the WiFi void setup_wifi() { //Connects to a WiFi network Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); } //establishes contact to the mqtt server. this is repeated if the connection is unsuccesfull. void reconnect() { // continues until contact is established while (!client.connected()) { Serial.print("Forsøger at oprette MQTT forbindelse..."); if (client.connect("GroupNamexMCU", mqtt_user, mqtt_pass)) { //connects to client with user and password Serial.println("connected"); //subscribes to the topic Test client.subscribe("Test"); //if connection fails it wait 5 second and try again } else { Serial.print("failed, rc="); Serial.print(client.state()); Serial.println(" try again in 5 seconds"); delay(5000); } } } ///////// OPSÆTNING AF WIFI SLUT ///////// // // // // // // ///////// SETUP /////////// void setup() { Serial.begin(9600); //The serial port is opened with baud rate 9600 delay(1000); //Output for the LED lights pinMode(D1,OUTPUT); pinMode(D2,OUTPUT); //The green LED light is turned on as this is the starting point for our locker, before it gets occupied digitalWrite(ledPin_G, HIGH); digitalWrite(ledPin_R, LOW); setup_wifi(); // runs wifi loop client.setServer(mqtt_server, mqtt_port); // connects to mqtt server client.setCallback(callback); //starts the defined callbackfuction every time a new message is recieved. } //////// SETUP END //////// // // // // // // /////// LOOP ///////// void loop() { //if problems with the connection occurs, it tries to reach connection again by running the client loop. if (!client.connected()) { reconnect(); } client.loop(); delay(1000); } //////// Loop slut ////////