Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- ESPNOW - Basic communication - Master
- Date: 26th September 2017
- Author: Arvind Ravulavaru <https://github.com/arvindr21>
- Purpose: ESPNow Communication between a Master ESP32 and a master ESP32
- Description: This sketch consists of the code for the Master module.
- Resources: (A bit outdated)
- a. https://espressif.com/sites/default/files/documentation/esp-now_user_guide_en.pdf
- b. http://www.esploradores.com/practica-6-conexion-esp-now/
- << This Device Master >>
- Flow: Master
- Step 1 : ESPNow Init on Master and set it in STA mode
- Step 2 : Start scanning for master ESP32 (we have added a prefix of `master` to the SSID of master for an easy setup)
- Step 3 : Once found, add master as peer
- Step 4 : Register for send callback
- Step 5 : Start Transmitting data from Master to master
- Flow: master
- Step 1 : ESPNow Init on master
- Step 2 : Update the SSID of master with a prefix of `master`
- Step 3 : Set master in AP mode
- Step 4 : Register for receive callback and wait for data
- Step 5 : Once data arrives, print it in the serial monitor
- Note: Master and master have been defined to easily understand the setup.
- Based on the ESPNOW API, there is no concept of Master and master.
- Any devices can act as master or salve.
- */
- #include <esp_now.h>
- #include <WiFi.h>
- /* I2C for HDC1080 */
- #include <Wire.h>
- #include "ClosedCube_HDC1080.h"
- ClosedCube_HDC1080 hdc1080;
- RTC_DATA_ATTR int bootCount = 0;
- /* ADS1115 AD converter */
- #include <Adafruit_ADS1015.h>
- Adafruit_ADS1115 ads1115(0x48);
- #define ADS1115_CONVERSIONDELAY (10)
- //Deep Sleep
- #define uS_TO_S_FACTOR 1000000 /* Conversion factor for micro seconds to seconds */
- #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 */
- /* Analog Input & ADC*/
- int16_t adc0 = 0;
- int16_t adc1 = 1;
- /* HDC1080 variables */
- //float measuredTemperature = 0;
- //float measuredHumidity = 0;
- // Global copy of master
- esp_now_peer_info_t master;
- #define CHANNEL 1
- struct Measured_Data {
- float measuredTemperature;
- float measuredHumidity;
- float measuredBatteryLevel;
- float measuredSolarLevel;
- } measuredData;
- // Init ESP Now with fallback
- void InitESPNow() {
- WiFi.disconnect();
- if (esp_now_init() == ESP_OK) {
- //Serial.println("ESPNow Init Success");
- }
- else {
- //Serial.println("ESPNow Init Failed");
- // Retry InitESPNow, add a counte and then restart?
- // InitESPNow();
- // or Simply Restart
- ESP.restart();
- }
- }
- // Check if the master is already paired with the master.
- // If not, pair the master with master
- bool managemaster() {
- if (master.channel == CHANNEL) {
- //Serial.print("master Status: ");
- const esp_now_peer_info_t *peer = &master;
- const uint8_t *peer_addr = master.peer_addr;
- // check if the peer exists
- bool exists = esp_now_is_peer_exist(peer_addr);
- if ( exists) {
- // master already paired.
- //Serial.println("Already Paired");
- return true;
- } else {
- // master not paired, attempt pair
- esp_err_t addStatus = esp_now_add_peer(peer);
- if (addStatus == ESP_OK) {
- // Pair success
- //Serial.println("Pair success");
- return true;
- } else if (addStatus == ESP_ERR_ESPNOW_NOT_INIT) {
- // How did we get so far!!
- //Serial.println("ESPNOW Not Init");
- return false;
- } else if (addStatus == ESP_ERR_ESPNOW_ARG) {
- //Serial.println("Invalid Argument");
- return false;
- } else if (addStatus == ESP_ERR_ESPNOW_FULL) {
- //Serial.println("Peer list full");
- return false;
- } else if (addStatus == ESP_ERR_ESPNOW_NO_MEM) {
- //Serial.println("Out of memory");
- return false;
- } else if (addStatus == ESP_ERR_ESPNOW_EXIST) {
- //Serial.println("Peer Exists");
- return true;
- } else {
- //Serial.println("Not sure what happened");
- return false;
- }
- }
- } else {
- // No master found to process
- //Serial.println("No master found to process");
- return false;
- }
- }
- // send data
- void sendData() {
- uint8_t bs[sizeof(measuredData)];
- memcpy(bs, &measuredData, sizeof(measuredData));
- const uint8_t *peer_addr = master.peer_addr;
- esp_err_t result = esp_now_send(peer_addr, bs, sizeof(measuredData));
- //Serial.print("Send Status: ");
- if (result == ESP_OK) {
- //Serial.println("Success");
- } else if (result == ESP_ERR_ESPNOW_NOT_INIT) {
- // How did we get so far!!
- //Serial.println("ESPNOW not Init.");
- } else if (result == ESP_ERR_ESPNOW_ARG) {
- //Serial.println("Invalid Argument");
- } else if (result == ESP_ERR_ESPNOW_INTERNAL) {
- //Serial.println("Internal Error");
- } else if (result == ESP_ERR_ESPNOW_NO_MEM) {
- //Serial.println("ESP_ERR_ESPNOW_NO_MEM");
- } else if (result == ESP_ERR_ESPNOW_NOT_FOUND) {
- //Serial.println("Peer not found.");
- } else {
- //Serial.println("Not sure what happened");
- }
- }
- // callback when data is sent from Slave to Master
- void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
- /*char macStr[18];
- snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x",
- mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3], mac_addr[4], mac_addr[5]);
- Serial.print("Last Packet Sent to: "); Serial.println(macStr);
- Serial.print("Last Packet Send Status: "); Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
- */}
- void setup() {
- Serial.begin(115200);
- pinMode(19, OUTPUT);
- // Default settings:
- // - Heater off
- // - 14 bit Temperature and Humidity Measurement Resolutions
- // - HDC1080 address is 0x40
- hdc1080.begin(0x40);
- delay(30); //min. 15 ms needed due to power up.
- measuredData.measuredTemperature = hdc1080.readTemperature();
- measuredData.measuredHumidity = hdc1080.readHumidity();
- digitalWrite(19, HIGH); // turn the PIN19 ON (HIGH is the voltage level) for ADC measurement, turn on the mosfet switch
- ads1115.begin();
- // ADC measurement
- adc0 = ads1115.readADC_SingleEnded(0);
- measuredData.measuredBatteryLevel = (adc0 * 0.1875 * 2.013)/1000; //4.18 V max voltage of battery
- delay(50);
- adc1 = ads1115.readADC_SingleEnded(1);
- measuredData.measuredSolarLevel = (adc1 * 0.1875 * 2.889)/1000; //6 V max voltage of solar panel
- digitalWrite(19, LOW); // turn the PIN19 OFF by making the voltage LOW, ADC measurement off
- //Serial.print("AIN0: ");
- //Serial.print(adc0);
- //Serial.println("");
- //Serial.print("\tVoltage: ");
- //Serial.println(measuredData.measuredBatteryLevel, 7);
- //Serial.println();
- //Serial.print("AIN1: ");
- //Serial.print(adc1);
- //Serial.println("");
- //Serial.print("\tSolar: ");
- //Serial.println(measuredData.measuredSolarLevel, 7);
- //Serial.println();
- //Serial.println(measuredData.measuredTemperature);
- //Serial.println(measuredData.measuredHumidity);
- //Serial.println(measuredData.measuredBatteryLevel);
- //Set device in STA mode to begin with
- WiFi.mode(WIFI_STA);
- //Serial.println("ESPNow/Basic/Master Example");
- // This is the mac address of the Master in Station Mode
- //Serial.print("STA MAC: "); Serial.println(WiFi.macAddress());
- // Init ESPNow with a fallback logic
- InitESPNow();
- // Once ESPNow is successfully Init, we will register for Send CB to
- // get the status of Trasnmitted packet
- //Serial.println(WiFi.macAddress());
- esp_now_register_send_cb(OnDataSent);
- memset(&master, 0, sizeof(master));
- // Set MacAddress of Master ESP32
- /*master.peer_addr[0] = 0x30;
- master.peer_addr[1] = 0xAE;
- master.peer_addr[2] = 0xA4;
- master.peer_addr[3] = 0x1C;
- master.peer_addr[4] = 0x00;
- master.peer_addr[5] = 0x71;*/
- master.peer_addr[0] = 0x30;
- master.peer_addr[1] = 0xAE;
- master.peer_addr[2] = 0xA4;
- master.peer_addr[3] = 0x90;
- master.peer_addr[4] = 0xFD;
- master.peer_addr[5] = 0xF0;
- master.channel = CHANNEL; // pick a channel
- master.encrypt = 0; // no encryption
- // `master` is defined
- // Add master as peer if it has not been added already
- bool isPaired = managemaster();
- if (isPaired) {
- // pair success or already paired
- // Send data to device
- //Serial.println("Data sent");
- sendData();
- }
- else {
- // Check 3x if the master is already paired with the master.
- for(int i = 0; i<3; i++)
- {
- isPaired = managemaster();
- if (isPaired) {
- // pair success or already paired
- // Send data to device
- sendData();
- break;
- }
- //Wait 10 sec between the check of isPaired
- delay(5000);
- }
- }
- //Serial.print("Boot counter: ");Serial.println(++bootCount);
- esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
- //Serial.println("Setup ESP32 to sleep for every" + String(TIME_TO_SLEEP) + "Seconds");
- delay(20);
- //Serial.println("Going to sleep now");
- esp_deep_sleep_start();
- }
- void loop() {
- // wait for 3seconds to run the logic again
- delay(1000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement