kozubovskyy

Untitled

May 16th, 2024
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.72 KB | None | 0 0
  1. /*
  2. ESP-NOW Demo - Receive
  3. esp-now-demo-rcv.ino
  4. Reads data from Initiator
  5.  
  6. DroneBot Workshop 2022
  7. https://dronebotworkshop.com
  8. */
  9.  
  10. // Include Libraries
  11. #include <esp_now.h>
  12. #include <WiFi.h>
  13.  
  14. // Define a data structure
  15. typedef struct struct_message {
  16. int b;
  17. } struct_message;
  18.  
  19. // Create a structured object
  20. struct_message myData;
  21.  
  22. #include <Wire.h>
  23. #include <SPI.h>
  24. #include <Adafruit_MCP4725.h>
  25.  
  26. uint32_t dac_value = 0;
  27. uint32_t dac2_value = 0;
  28.  
  29. Adafruit_MCP4725 dac;
  30. Adafruit_MCP4725 dac2;
  31.  
  32.  
  33. // Callback function executed when data is received
  34. void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
  35. memcpy(&myData, incomingData, sizeof(myData));
  36. /*Serial.print("Integer Value: ");
  37. Serial.println(myData.b);
  38. */
  39. }
  40.  
  41. void setup() {
  42. // Set up Serial Monitor
  43. Wire1.begin(1); // Activate I2C link
  44. Serial.begin(115200);
  45. dac.begin(0x60);
  46. dac2.begin(0x61);
  47.  
  48.  
  49.  
  50.  
  51. // Set ESP32 as a Wi-Fi Station
  52. WiFi.mode(WIFI_STA);
  53.  
  54. // Initilize ESP-NOW
  55. if (esp_now_init() != ESP_OK) {
  56. Serial.println("Error initializing ESP-NOW");
  57. return;
  58. }
  59.  
  60. // Register callback function
  61. esp_now_register_recv_cb(OnDataRecv);
  62. }
  63.  
  64. void loop() {
  65. Serial.print("looppato: ");
  66. Serial.println(myData.b);
  67. if (myData.b > 1000 && myData.b < 2000)
  68. {
  69. dac_value = 1700 ;
  70. dac.setVoltage(dac_value, false);
  71. }
  72. if (myData.b > 2000 && myData.b < 3000)
  73. {
  74. dac_value = 2200 ;
  75. dac.setVoltage(dac_value, false);
  76. }
  77.  
  78. if (myData.b < 1000 )
  79. {
  80. dac_value = 1600 ;
  81. dac.setVoltage(dac_value, false);
  82. }
  83.  
  84.  
  85. dac2_value = 2000 ;
  86. dac2.setVoltage(dac2_value, false);
  87.  
  88.  
  89. }
Advertisement
Add Comment
Please, Sign In to add comment