This is an old revision of the document!
In this scenario, you will learn how to remotely control fan speed using PWM and how to control servo angle using MQTT messages. This scenario is a practical extension to the scenario U4 where you learned, how to receive and handle MQTT messages.
Undergraduate / Bachelor / Engineering Students
We assume you already know how to:
PubSubClient libraryMQTT broker present in the internal.IOT network is also visible under public address. So whenever you subscribe to the MQTT message using VREL node that is connected to the internal.IOT network, you may publish to it using other devices connected to the internal.IOT, i.e. other VREL node or if you're physically present at SUT in the IOT laboratory room 320, then you can connect your mobile and laptop to the internal.IOT network and use “internal” IP address. However, if you're in a remote location, you can access the same broker under public IP as stated in the node description. When you publish an MQTT message using public IP, it will be delivered to the subscribers in the private internal.IOT network as well. Mind, to access MQTT broker you need to use IP, user and password (applies to both public and private IPs of the MQTT Broker). Refer to the node documentation for the latest information.
In this scenario, you will handle incoming MQTT messages (one for controlling fan, other for servo, opening and closing the flap. Parallelly you will present incoming messages on the LCD screen.
You should be able to remotely control fan rotation speed and flap angle (0..90 degrees), observe connection status and incoming MQTT messages for your device on the LCD screen.
Define some identifiers to separate and update AP's SSID and passphrase easily. To format lines for the LCD, we suggest using a char buffer of 20 characters (one full line) and some 2-3 integers for iterators and storing current PWM for servo and fan. Remember to declare the LCD control class in your code. You do not need to instantiate WiFi communication class - as you have only one interface here, it is singleton class you can refer directly using WiFi. Reading analogue input brings you an integer value.
Servo is controlled using GPIO pin D5 (GPIO14) while the fan is controlled using GPIO pin D8 (GPIO15). As servo we use is a small one, it is powered and controlled directly through the ESP8266 while in case of the FAN, it is controlled indirectly, using a transistor.
Following steps do not present full code - you need to supply missing parts on your own! We do not present here how to connect to the WiFi AP. If you're in doubt, rever to the U1 scenario. Please refer to scenario B1, if you need a recall on how to handle LCD screen. In case you're in doubt how to handle MQTT messages communication (here publishing/sending), please refer to the U4 scenario.
Include all necessary libraries. We use PubSubClient library to contact MQTT broker. The minimum set here is:
#include <Arduino.h> #include <ESP8266WiFi.h> #include <PubSubClient.h> #include <LiquidCrystal_I2C.h> #include <Servo.h> ...
Here we use Servo.h library to easily handle servomotor control for regular, modeller's servos, mostly used in RC toys.
Declare some identifiers to let you easier handle necessary modifications and keep code clear:
#define wifi_ssid "internal.IOT" #define wifi_password "IoTlab32768" #define mqtt_server "192.168.90.5" #define mqtt_user "vrel" #define mqtt_password "vrel2018" ...
Declare some identifiers, here MQTT messages' topics, MQTT client ID and payloads for the status notification (on / off).
// MQTT messages #define MQTTClientName "myVREL2clientname" #define servoTopic ...<some topic for servo>... // give it some unique topic i.e. including your name #define fanTopic ...<some topic for fan>... // give it some unique topic i.e. including your name //MQTT last will #define lastWillTopic ..<some topic for exposing state and last will>... // give it some unique topic i.e. including your name // give it some unique topic i.e. including your name #define lastWillMessage "off" #define mqttWelcomeMessage "on"
Finally, declare GPIOs pin numbers connecting to the servo and fan:
//Hardware #define PWMFanPin D8 #define servoPin D5
Declare WiFiCilent, PubSubClient, initialise, instantiate and connect to the network. If in doubt, refer to the scenario U4 on how to prepare networking code for your solution.
Additionally, instantiate a servo controlling the flap:
... Servo servo; ...
Later, in your initialisation section (possibly in setup() function) bind your servo with physical one using appropriate GPIO and set it to 0 degrees (close the flap, down). Also, initialise fan's GPIO for output, to control PWM and set it to 0 (stop fan):
... void setup() { ... // Servo and PWM fan servo.attach(servoPin); servo.write(0); //set servo down to 0 degrees pinMode(PWMFanPin,OUTPUT); analogWrite(PWMFanPin,0); //stop FAN ...
Note - here you see how to control servo and fan. In the case of the servo, we use a Servo library, so function servo.write(deg) sets servo to the desired number of degrees. While the fan is controlled using RAW values on 12bit resolution (in fact it is PWM as well) from 0 to 4095, using analogWrite(pin, value). Word “analog” may mislead you here: output on the digital pin is not a continuous analogue voltage value, but its approximation using PWM.
Describe activities done in Step n.
Provide some result validation methods, for self assesment.
This section is to be extended as new questions appear.
When using the printed version of this manual please refer to the latest online version of this document to obtain the valid and up-to-date list of the FAQ.
Provide some FAQs in the following form:
Question?: Answer.