Advertisement
Alvaroskinoski

ESP32 with DS18b20 Arduino ioT Cloud

Feb 5th, 2023
1,155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "arduino_secrets.h"
  2. // DallasTemperature(Edison) for i686 - Version: Latest
  3. #include <DallasTemperature.h>
  4. #include "BluetoothSerial.h" //código añadido
  5. #if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED) //código añadido
  6. #error Bluetooth is not enabled! Please run `make menuconfig` to and enable it //código añadido
  7. #endif //código añadido
  8.  
  9. #if !defined(CONFIG_BT_SPP_ENABLED)//código añadido
  10. #error Serial Bluetooth not available or not enabled. It is only available for the ESP32 chip.//código añadido
  11. #endif//código añadido
  12.  
  13. BluetoothSerial SerialBT;//código añadido
  14.  
  15. // OneWire(Edison) for i686 - Version: Latest
  16. #include <OneWire.h>
  17. #define ONE_WIRE_BUS 15 // Data wire is connected to ESP32 GPIO15
  18.  
  19.  
  20. OneWire oneWire(ONE_WIRE_BUS);//Setup a oneWire instance to communicate with a OneWire device
  21.  
  22. DallasTemperature sensors(&oneWire);//Pass our oneWire reference to Dallas Temperature sensor
  23.  
  24. //device addresses
  25. DeviceAddress sensor1 = { 0x28, 0x51, 0x3A, 0x76, 0xE0, 0x01, 0x3C, 0x81 };
  26. DeviceAddress sensor2 = { 0x28, 0x8F, 0x43, 0x76, 0xE0, 0x01, 0x3C, 0xC8 };
  27.  
  28. /*
  29.   Sketch generated by the Arduino IoT Cloud Thing "Untitled 2"
  30.   https://create.arduino.cc/cloud/things/7c8b6ecb-45ee-446e-9b50-b832d5a15992
  31.  
  32.   Arduino IoT Cloud Variables description
  33.  
  34.   The following variables are automatically generated and updated when changes are made to the Thing
  35.  
  36.   float temp_1;
  37.   float temp_2;
  38.  
  39.   Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
  40.   which are called when their values are changed from the Dashboard.
  41.   These functions are generated with the Thing and added at the end of this sketch.
  42. */
  43.  
  44. #include "thingProperties.h"
  45.  
  46. #include "arduino_secrets.h"
  47. // DallasTemperature(Edison) for i686 - Version: Latest
  48. #include <DallasTemperature.h>
  49. #include "BluetoothSerial.h"
  50. #if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
  51. #error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
  52. #endif
  53.  
  54. #if !defined(CONFIG_BT_SPP_ENABLED)
  55. #error Serial Bluetooth not available or not enabled. It is only available for the ESP32 chip.
  56. #endif
  57.  
  58. BluetoothSerial SerialBT;
  59.  
  60. // OneWire(Edison) for i686 - Version: Latest
  61. #include <OneWire.h>
  62. #define ONE_WIRE_BUS 15 // Data wire is connected to ESP32 GPIO15
  63.  
  64.  
  65. OneWire oneWire(ONE_WIRE_BUS);//Setup a oneWire instance to communicate with a OneWire device
  66.  
  67. DallasTemperature sensors(&oneWire);//Pass our oneWire reference to Dallas Temperature sensor
  68.  
  69. //device addresses
  70. DeviceAddress sensor1 = { 0x28, 0x51, 0x3A, 0x76, 0xE0, 0x01, 0x3C, 0x81 };
  71. DeviceAddress sensor2 = { 0x28, 0x8F, 0x43, 0x76, 0xE0, 0x01, 0x3C, 0xC8 };
  72.  
  73. /*
  74.   Sketch generated by the Arduino IoT Cloud Thing "Untitled 2"
  75.   https://create.arduino.cc/cloud/things/7c8b6ecb-45ee-446e-9b50-b832d5a15992
  76.  
  77.   Arduino IoT Cloud Variables description
  78.  
  79.   The following variables are automatically generated and updated when changes are made to the Thing
  80.  
  81.   float temp_1;
  82.   float temp_2;
  83.  
  84.   Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
  85.   which are called when their values are changed from the Dashboard.
  86.   These functions are generated with the Thing and added at the end of this sketch.
  87. */
  88.  
  89. #include "thingProperties.h"
  90.  
  91. void setup() {
  92.   // Initialize serial and wait for port to open:
  93.   Serial.begin(115200);
  94.   // This delay gives the chance to wait for a Serial Monitor without blocking if none is found
  95.   delay(1500);
  96.   SerialBT.begin("ESP32");
  97.   sensors.begin();
  98.  
  99.   // Defined in thingProperties.h
  100.   initProperties();
  101.  
  102.   // Connect to Arduino IoT Cloud
  103.   ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  104.  
  105.   /*
  106.      The following function allows you to obtain more information
  107.      related to the state of network and IoT Cloud connection and errors
  108.      the higher number the more granular information you’ll get.
  109.      The default is 0 (only errors).
  110.      Maximum is 4
  111.  */
  112.   setDebugMessageLevel(2);
  113.   ArduinoCloud.printDebugInfo();
  114. }
  115.  
  116. void loop() {
  117.   ArduinoCloud.update();
  118.   // Your code here
  119.   if (Serial.available()) {
  120.     SerialBT.write(Serial.read());
  121.   }
  122.   if (SerialBT.available()) {
  123.     Serial.write(SerialBT.read());
  124.   }
  125.   temp_1 = sensors.getTempC(sensor1);
  126.   temp_2 = sensors.getTempC(sensor2);
  127.   Serial.print(F("Requesting temperatures..."));
  128.   sensors.requestTemperatures(); // Send the command to get temperatures
  129.   Serial.println(F("DONE"));
  130.  
  131.   Serial.print(F("Sensor 1(*C): "));
  132.   Serial.print(sensors.getTempC(sensor1));
  133.   Serial.print(F(" Sensor 1(*F): "));
  134.   Serial.println(sensors.getTempF(sensor1));
  135.  
  136.   SerialBT.print(F("*T"));
  137.   SerialBT.print(sensors.getTempC(sensor1));
  138.   SerialBT.print(F("*"));
  139.   SerialBT.print(F("*S"));
  140.   SerialBT.print(sensors.getTempC(sensor2));
  141.   SerialBT.print(F("*"));
  142.  
  143.   Serial.println(sensors.getTempF(sensor1));
  144.   Serial.print(F("Sensor 2(*C): "));
  145.   Serial.print(sensors.getTempC(sensor2));
  146.   Serial.print(F(" Sensor 2(*F): "));
  147.   Serial.println(sensors.getTempF(sensor2));
  148.  
  149.   delay(2000);
  150. }
  151.  
  152.  
  153.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement