Advertisement
safwan092

Untitled

Dec 1st, 2018
364
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.29 KB | None | 0 0
  1. /* Virtuino example (Bluetooth)
  2. * Example name = "Read temperatures from two ds18b20 sensors "
  3. * Created by Ilias Lamprou
  4. * Updated 24 02 2017
  5. * This is the code you need to run on your arduino board to cummunicate whith VirtUino app via bluetooth
  6. * Before running this code config the settings below as the instructions on the right
  7. *
  8. * Download latest Virtuino android app from the link: https://play.google.com/store/apps/details?id=com.virtuino_automations.virtuino
  9. * Contact address for questions or comments: iliaslampr@gmail.com
  10. */
  11.  
  12. /*========= VirtuinoBluetooth Class methods
  13. * vPinMode(int pin, int state) set pin as digital OUTPUT or digital INPUT. (Insteed default pinMode method
  14. *
  15. *========= Virtuino General methods
  16. * void vDigitalMemoryWrite(int digitalMemoryIndex, int value) write a value to a Virtuino digital memory (digitalMemoryIndex=0..31, value range = 0 or 1)
  17. * int vDigitalMemoryRead(int digitalMemoryIndex) read the value of a Virtuino digital memory (digitalMemoryIndex=0..31, returned value range = 0 or 1)
  18. * void vMemoryWrite(int analogMemoryIndex, float value) write a value to Virtuino float memory (memoryIndex=0..31, value range as float value)
  19. * float vMemoryRead(int analogMemoryIndex) read the value of Virtuino analog memory (analogMemoryIndex=0..31, returned value range = 0..1023)
  20. * run() neccesary command to communicate with Virtuino android app (on start of void loop)
  21. * void vDelay(long milliseconds); Pauses the program (without block communication) for the amount of time (in miliseconds) specified as parameter
  22. * int getPinValue(int pin) read the value of a Pin. Usefull for PWM pins
  23. */
  24.  
  25. #include <DHT.h> // You have to download DHT11 library
  26. //Attention: For new DHT11 version library you will need the Adafruit_Sensor library
  27. //Download from here: https://github.com/adafruit/Adafruit_Sensor
  28. #define DHTPIN 6
  29. #define DHTTYPE DHT11
  30. DHT dht(DHTPIN, DHTTYPE);
  31.  
  32.  
  33. #include "VirtuinoBluetooth.h" // virtuino library 1.47 or highest
  34. // Code to use SoftwareSerial
  35. #include <SoftwareSerial.h> // Disable this line if you want to use hardware serial
  36. SoftwareSerial bluetoothSerial = SoftwareSerial(2,3); // arduino RX pin=2 arduino TX pin=3 connect the arduino RX pin to bluetooth module TX pin - connect the arduino TX pin to bluetooth module RX pin. Disable this line if you want to use hardware serial
  37. VirtuinoBluetooth virtuino(bluetoothSerial,9600); // Set SoftwareSerial baud rate. - Disable this line if you want to use hardware serial
  38.  
  39. // Code to use HardwareSerial
  40. // VirtuinoBluetooth virtuino(Serial1); // enable this line and disable all SoftwareSerial lines
  41. // Open VirtuinoBluetooth.h file on the virtuino library folder -> disable the line: #define BLUETOOTH_USE_SOFTWARE_SERIAL
  42. // Connect HC-05 module to Arduino (MEGA or DUE) Serial1. (pins: 18,19)
  43.  
  44.  
  45.  
  46. //======================================================================== setup
  47. //========================================================================
  48. void setup(void){
  49. Serial.begin(9600); // start monitor serial port
  50.  
  51. //Serial1.begin(9600); // start serial1 HC-05 port. Enable this line only if you use hardware serial (Mega, Due, etc)
  52.  
  53. virtuino.DEBUG=true; // set this value TRUE to enable the serial monitor status
  54.  
  55. // Start up the library Dallas Temperature IC Control Library
  56. dht.begin();
  57. Serial.println("Setup completed.");
  58. }
  59.  
  60. //======================================================================== setup
  61. void readTemperatures(){
  62. Serial.println("Read sensors' values...");
  63. float temperature = dht.readTemperature();
  64. float humidity = dht.readHumidity();
  65. if (isnan(temperature) || isnan(humidity)) {
  66. Serial.println("Failed to read from DHT");
  67. }
  68. else {
  69. Serial.println("Temp="+String(temperature)+" *C");
  70. Serial.println("Humidity="+String(humidity)+" %");
  71. virtuino.vMemoryWrite(0,temperature); // write temperature 1 to virtual pin V0. On Virtuino panel add a value display or an analog instrument to pin V0
  72. virtuino.vMemoryWrite(1,humidity); // write temperature 1 to virtual pin V1. On Virtuino panel add a value display or an analog instrument to pin V1
  73. }
  74.  
  75. }
  76.  
  77. //======================================================================== setup
  78. //========================================================================
  79. void loop(void){
  80. virtuino.run(); // neccesary command to communicate with Virtuino android app
  81.  
  82. //------ enter your loop code below here
  83. //------ avoid to use delay() function in your code. Use the command virtuino.vDelay() instead of delay()
  84. //------ your code .....
  85.  
  86.  
  87. readTemperatures(); // Dont' read the sensors values every cicle
  88. virtuino.vDelay(5000); // Add a delay at least 1 second long.
  89.  
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement