Advertisement
Guest User

espnow

a guest
Aug 10th, 2021
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.07 KB | None | 0 0
  1. /**
  2. ESPNOW - Basic communication - Master
  3. Date: 26th September 2017
  4. Author: Arvind Ravulavaru <https://github.com/arvindr21>
  5. Purpose: ESPNow Communication between a Master ESP32 and a master ESP32
  6. Description: This sketch consists of the code for the Master module.
  7. Resources: (A bit outdated)
  8. a. https://espressif.com/sites/default/files/documentation/esp-now_user_guide_en.pdf
  9. b. http://www.esploradores.com/practica-6-conexion-esp-now/
  10.  
  11. << This Device Master >>
  12.  
  13. Flow: Master
  14. Step 1 : ESPNow Init on Master and set it in STA mode
  15. Step 2 : Start scanning for master ESP32 (we have added a prefix of `master` to the SSID of master for an easy setup)
  16. Step 3 : Once found, add master as peer
  17. Step 4 : Register for send callback
  18. Step 5 : Start Transmitting data from Master to master
  19.  
  20. Flow: master
  21. Step 1 : ESPNow Init on master
  22. Step 2 : Update the SSID of master with a prefix of `master`
  23. Step 3 : Set master in AP mode
  24. Step 4 : Register for receive callback and wait for data
  25. Step 5 : Once data arrives, print it in the serial monitor
  26.  
  27. Note: Master and master have been defined to easily understand the setup.
  28. Based on the ESPNOW API, there is no concept of Master and master.
  29. Any devices can act as master or salve.
  30. */
  31.  
  32. #include <esp_now.h>
  33. #include <WiFi.h>
  34.  
  35. /* I2C for HDC1080 */
  36. #include <Wire.h>
  37. #include "ClosedCube_HDC1080.h"
  38.  
  39. ClosedCube_HDC1080 hdc1080;
  40. RTC_DATA_ATTR int bootCount = 0;
  41.  
  42. /* ADS1115 AD converter */
  43. #include <Adafruit_ADS1015.h>
  44.  
  45. Adafruit_ADS1115 ads1115(0x48);
  46. #define ADS1115_CONVERSIONDELAY (10)
  47.  
  48. //Deep Sleep
  49.  
  50. #define uS_TO_S_FACTOR 1000000 /* Conversion factor for micro seconds to seconds */
  51. #define TIME_TO_SLEEP 10 * 60 /* Time ESP32 will go to sleep and wake up in every 10 min (in seconds), 10*60sec = 10 min */
  52.  
  53. /* Analog Input & ADC*/
  54. int16_t adc0 = 0;
  55. int16_t adc1 = 1;
  56.  
  57. /* HDC1080 variables */
  58. //float measuredTemperature = 0;
  59. //float measuredHumidity = 0;
  60.  
  61. // Global copy of master
  62. esp_now_peer_info_t master;
  63.  
  64. #define CHANNEL 1
  65.  
  66. struct Measured_Data {
  67. float measuredTemperature;
  68. float measuredHumidity;
  69. float measuredBatteryLevel;
  70. float measuredSolarLevel;
  71. } measuredData;
  72.  
  73. // Init ESP Now with fallback
  74. void InitESPNow() {
  75. WiFi.disconnect();
  76. if (esp_now_init() == ESP_OK) {
  77. //Serial.println("ESPNow Init Success");
  78. }
  79. else {
  80. //Serial.println("ESPNow Init Failed");
  81. // Retry InitESPNow, add a counte and then restart?
  82. // InitESPNow();
  83. // or Simply Restart
  84. ESP.restart();
  85. }
  86. }
  87.  
  88. // Check if the master is already paired with the master.
  89. // If not, pair the master with master
  90. bool managemaster() {
  91. if (master.channel == CHANNEL) {
  92. //Serial.print("master Status: ");
  93. const esp_now_peer_info_t *peer = &master;
  94. const uint8_t *peer_addr = master.peer_addr;
  95. // check if the peer exists
  96. bool exists = esp_now_is_peer_exist(peer_addr);
  97. if ( exists) {
  98. // master already paired.
  99. //Serial.println("Already Paired");
  100. return true;
  101. } else {
  102. // master not paired, attempt pair
  103. esp_err_t addStatus = esp_now_add_peer(peer);
  104. if (addStatus == ESP_OK) {
  105. // Pair success
  106. //Serial.println("Pair success");
  107. return true;
  108. } else if (addStatus == ESP_ERR_ESPNOW_NOT_INIT) {
  109. // How did we get so far!!
  110. //Serial.println("ESPNOW Not Init");
  111. return false;
  112. } else if (addStatus == ESP_ERR_ESPNOW_ARG) {
  113. //Serial.println("Invalid Argument");
  114. return false;
  115. } else if (addStatus == ESP_ERR_ESPNOW_FULL) {
  116. //Serial.println("Peer list full");
  117. return false;
  118. } else if (addStatus == ESP_ERR_ESPNOW_NO_MEM) {
  119. //Serial.println("Out of memory");
  120. return false;
  121. } else if (addStatus == ESP_ERR_ESPNOW_EXIST) {
  122. //Serial.println("Peer Exists");
  123. return true;
  124. } else {
  125. //Serial.println("Not sure what happened");
  126. return false;
  127. }
  128. }
  129. } else {
  130. // No master found to process
  131. //Serial.println("No master found to process");
  132. return false;
  133. }
  134. }
  135.  
  136. // send data
  137. void sendData() {
  138. uint8_t bs[sizeof(measuredData)];
  139. memcpy(bs, &measuredData, sizeof(measuredData));
  140. const uint8_t *peer_addr = master.peer_addr;
  141. esp_err_t result = esp_now_send(peer_addr, bs, sizeof(measuredData));
  142. //Serial.print("Send Status: ");
  143. if (result == ESP_OK) {
  144. //Serial.println("Success");
  145. } else if (result == ESP_ERR_ESPNOW_NOT_INIT) {
  146. // How did we get so far!!
  147. //Serial.println("ESPNOW not Init.");
  148. } else if (result == ESP_ERR_ESPNOW_ARG) {
  149. //Serial.println("Invalid Argument");
  150. } else if (result == ESP_ERR_ESPNOW_INTERNAL) {
  151. //Serial.println("Internal Error");
  152. } else if (result == ESP_ERR_ESPNOW_NO_MEM) {
  153. //Serial.println("ESP_ERR_ESPNOW_NO_MEM");
  154. } else if (result == ESP_ERR_ESPNOW_NOT_FOUND) {
  155. //Serial.println("Peer not found.");
  156. } else {
  157. //Serial.println("Not sure what happened");
  158. }
  159. }
  160.  
  161. // callback when data is sent from Slave to Master
  162. void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
  163. /*char macStr[18];
  164. snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x",
  165. mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
  166. Serial.print("Last Packet Sent to: "); Serial.println(macStr);
  167. Serial.print("Last Packet Send Status: "); Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
  168. */}
  169.  
  170. void setup() {
  171. Serial.begin(115200);
  172. pinMode(19, OUTPUT);
  173.  
  174. // Default settings:
  175. // - Heater off
  176. // - 14 bit Temperature and Humidity Measurement Resolutions
  177. // - HDC1080 address is 0x40
  178. hdc1080.begin(0x40);
  179. delay(30); //min. 15 ms needed due to power up.
  180. measuredData.measuredTemperature = hdc1080.readTemperature();
  181. measuredData.measuredHumidity = hdc1080.readHumidity();
  182.  
  183. digitalWrite(19, HIGH); // turn the PIN19 ON (HIGH is the voltage level) for ADC measurement, turn on the mosfet switch
  184. ads1115.begin();
  185. // ADC measurement
  186. adc0 = ads1115.readADC_SingleEnded(0);
  187. measuredData.measuredBatteryLevel = (adc0 * 0.1875 * 2.013)/1000; //4.18 V max voltage of battery
  188. delay(50);
  189. adc1 = ads1115.readADC_SingleEnded(1);
  190. measuredData.measuredSolarLevel = (adc1 * 0.1875 * 2.889)/1000; //6 V max voltage of solar panel
  191. digitalWrite(19, LOW); // turn the PIN19 OFF by making the voltage LOW, ADC measurement off
  192.  
  193. //Serial.print("AIN0: ");
  194. //Serial.print(adc0);
  195. //Serial.println("");
  196. //Serial.print("\tVoltage: ");
  197. //Serial.println(measuredData.measuredBatteryLevel, 7);
  198. //Serial.println();
  199.  
  200. //Serial.print("AIN1: ");
  201. //Serial.print(adc1);
  202. //Serial.println("");
  203. //Serial.print("\tSolar: ");
  204. //Serial.println(measuredData.measuredSolarLevel, 7);
  205. //Serial.println();
  206.  
  207. //Serial.println(measuredData.measuredTemperature);
  208. //Serial.println(measuredData.measuredHumidity);
  209. //Serial.println(measuredData.measuredBatteryLevel);
  210.  
  211. //Set device in STA mode to begin with
  212. WiFi.mode(WIFI_STA);
  213. //Serial.println("ESPNow/Basic/Master Example");
  214. // This is the mac address of the Master in Station Mode
  215. //Serial.print("STA MAC: "); Serial.println(WiFi.macAddress());
  216. // Init ESPNow with a fallback logic
  217. InitESPNow();
  218. // Once ESPNow is successfully Init, we will register for Send CB to
  219. // get the status of Trasnmitted packet
  220. //Serial.println(WiFi.macAddress());
  221. esp_now_register_send_cb(OnDataSent);
  222.  
  223. memset(&master, 0, sizeof(master));
  224.  
  225. // Set MacAddress of Master ESP32
  226. /*master.peer_addr[0] = 0x30;
  227. master.peer_addr[1] = 0xAE;
  228. master.peer_addr[2] = 0xA4;
  229. master.peer_addr[3] = 0x1C;
  230. master.peer_addr[4] = 0x00;
  231. master.peer_addr[5] = 0x71;*/
  232.  
  233. master.peer_addr[0] = 0x30;
  234. master.peer_addr[1] = 0xAE;
  235. master.peer_addr[2] = 0xA4;
  236. master.peer_addr[3] = 0x90;
  237. master.peer_addr[4] = 0xFD;
  238. master.peer_addr[5] = 0xF0;
  239.  
  240. master.channel = CHANNEL; // pick a channel
  241. master.encrypt = 0; // no encryption
  242.  
  243. // `master` is defined
  244. // Add master as peer if it has not been added already
  245. bool isPaired = managemaster();
  246.  
  247. if (isPaired) {
  248. // pair success or already paired
  249. // Send data to device
  250. //Serial.println("Data sent");
  251. sendData();
  252. }
  253. else {
  254. // Check 3x if the master is already paired with the master.
  255. for(int i = 0; i<3; i++)
  256. {
  257. isPaired = managemaster();
  258. if (isPaired) {
  259. // pair success or already paired
  260. // Send data to device
  261. sendData();
  262. break;
  263. }
  264. //Wait 10 sec between the check of isPaired
  265. delay(5000);
  266. }
  267. }
  268. //Serial.print("Boot counter: ");Serial.println(++bootCount);
  269.  
  270. esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
  271. //Serial.println("Setup ESP32 to sleep for every" + String(TIME_TO_SLEEP) + "Seconds");
  272.  
  273. delay(20);
  274. //Serial.println("Going to sleep now");
  275. esp_deep_sleep_start();
  276. }
  277.  
  278. void loop() {
  279. // wait for 3seconds to run the logic again
  280. delay(1000);
  281. }
  282.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement