Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
en:iot-open:remotelab:sut:generalpurpose2:u7 [2019/08/11 14:44] pczekalskien:iot-open:remotelab:sut:generalpurpose2:u7 [2020/07/20 09:00] (current) – external edit 127.0.0.1
Line 60: Line 60:
 // MQTT messages // MQTT messages
 #define MQTTClientName "myVREL2clientname" #define MQTTClientName "myVREL2clientname"
-#define servoTopic ...<some topic for servo>... // give it some unique topic i.e. including your name +#define servoTopic ...<some topic for servo>... // give it some unique topic  
-#define fanTopic   ...<some topic for fan>... // give it some unique topic i.e. including your name+                                                // i.e. including your name 
 +#define fanTopic   ...<some topic for fan>...   // give it some unique topic  
 +                                                // i.e. including your name
  
 //MQTT last will //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 lastWillTopic ..<some topic for exposing state and last will>...    
 +                                                // give it some unique topic  
 +                                                // i.e. including your name
 #define lastWillMessage "off" #define lastWillMessage "off"
 #define mqttWelcomeMessage "on" #define mqttWelcomeMessage "on"
Line 98: Line 102:
 </code> </code>
  
-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.+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 anyway, it works perfectly for the fan and many other devices.
  
 == Step 4 == == Step 4 ==
- +Implement MQTT callback - a function that is being called, whenever there comes an MQTT message. You should have subscribed only to the selected MQTT messages (one for the fan, other for the servo) so only those messages may trigger your callback function. Anyway, it is a matter to distinguish, which one message comes. Your function may look like this: 
- +<code c> 
-== Step n == +void mqttCallback(char* topic, byte* payload, unsigned int length) { 
-//Describe activities done in Step n.//+  lcd.setCursor(0,2); 
 +  lcd.print("Servo:"); 
 +  lcd.setCursor(0,3); 
 +  lcd.print("Fan:"); 
 +  String sTopic(topic); 
 +  if(sTopic.startsWith(servoTopic)) 
 +  { 
 +    //Handle servo message 
 +    for(int i=0; i< length; i++) 
 +      { 
 +        buffer[i] (char)payload[i]; 
 +      } 
 +    buffer[length]='\0'; 
 +    srv atoi(buffer); 
 +    servo.write(srv); 
 +    lcd.setCursor(9,2); 
 +    sprintf(buffer,"%2d deg",srv); 
 +    lcd.print(buffer); 
 +  } 
 +  if(sTopic.startsWith(fanTopic)) 
 +  { 
 +    //Handle fan message 
 +    for(int i=0; i< length; i++) 
 +      { 
 +        buffer[i] = (char)payload[i]; 
 +      } 
 +      buffer[length]='\0'; 
 +      fan = atoi(buffer); 
 +      analogWrite(PWMFanPin,fan); 
 +      lcd.setCursor(7,3); 
 +      sprintf(buffer,"%4d PWM",fan); 
 +      lcd.print(buffer); 
 +  }  // give it some unique topic i.e. including your name 
 +
 +</code> 
 +As you see, we do not only receive the value but also drive LCD display to present it. Still you may inject some validation code, particularly for the servo, i.e. check if incoming MQTT payload for the servo message is more than 90 degres then truncate it not to break the device physically. 
 +== Step 5 == 
 +Remember to bind your MQTT callback function to the MQTT PubSubClient. Perhaps you will do it in the ''setup()'' or in the ''reconnect()'' functions: 
 +<code c> 
 +... 
 +client.setCallback(mqttCallback); 
 +... 
 +</code>
  
 === Result validation === === Result validation ===
-//Provide some result validation methods, for self assesment.//+Observe flap moving out and in. As the fan is mounted perpendicular to the video camerayou cannot observe rotation directly (video stream is too slow to present it on the other hand). You can observe value on the analogue gauge to the right but also indirectly through the flap mounted in the corresponding air receiving node (RX). Those are VREL1 and VREL3 for sending nodes VREL2 and VREL4 respectively. You will see the air stream pushing the flap thus you can monitor the airflow.
  
 === FAQ === === FAQ ===
-This section is to be extended as new questions appear. \\ +**What is the valid range for controlling the fan?**: The fan is connected to the digital pin and controlled via PWM (12-bit resolution). Thus valid range is from 0 (min) to 4095 (max). Note rotation speed and airflow do not have linear characteristic vs PWM controlling value, So issuing 2047 on the GPIO pin controlling the fan won't make your fan to rotate 50% nor the airflow will be 50% of the maximum oneYou need to experiment individually!\\ 
-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. +**What is the valid range for controlling the servo?**: The valid range is from 0 to 90 degreesExceeding 90 degrees can break the construction! Never go beyond 90 degrees!
-//Provide some FAQs in the following form:\\ +
-**Question?**: Answer. +
-// +
en/iot-open/remotelab/sut/generalpurpose2/u7.1565534682.txt.gz · Last modified: 2020/07/20 09:00 (external edit)
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0