diff --git a/src/receiver.cpp b/src/receiver.cpp index baa3344..3b5f014 100644 --- a/src/receiver.cpp +++ b/src/receiver.cpp @@ -4,18 +4,39 @@ #include "SSD1306.h" #include #include +#include +#include + +#include "mac_config.h" #define esc1pin 15 #define esc2pin 13 #define fallbackpin 36 #define failsafeValue 127 +#define ONE_WIRE_BUS 14 +#define fanRelais 16 +#define DELETEBEFOREPAIR 0 Servo esc1; Servo esc2; SSD1306 display(0x3c, 5, 4); +OneWire oneWire(ONE_WIRE_BUS); +DallasTemperature sensors(&oneWire); // one instance for all sensrs + +esp_now_peer_info_t remote; + +float temperature = 0; +float voltage = 0; +float speed = 0; + +uint8_t sendTemperature = 0; +uint8_t sendTemperatureDecimals = 0; +uint8_t sendVoltage = 0; +uint8_t sendSpeed = 0; + //#define pairingMode #define CONNECTION_TIMEOUT 300 #define CHANNEL 1 @@ -39,7 +60,7 @@ void writeServos(uint8_t firstServo, uint8_t secondServo) { esc2.write(secondServo); } -// callback when data is recv from Master +// callback when data is recv from remote void OnDataRecv(const uint8_t *mac_addr, const uint8_t *data, int data_len) { char macStr[18]; snprintf(macStr, sizeof(macStr), "%02x:%02x:%02x:%02x:%02x:%02x", @@ -49,6 +70,31 @@ void OnDataRecv(const uint8_t *mac_addr, const uint8_t *data, int data_len) { memcpy(recData, data, data_len); Serial.print("Last Packet Recv Data: "); Serial.println(recData[0]); Serial.print(" "); Serial.print(recData[1]); Serial.print(" len:"); Serial.println(data_len); +// Answer with response + const uint8_t respData[] = { sendTemperature, sendTemperatureDecimals, sendVoltage }; // sendSpeed + Serial.print("Sending RESPONSE.... "); + esp_err_t result = esp_now_send(mac_addr, respData, sizeof(data)); + 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 if (result == ESP_ERR_ESPNOW_IF) { + Serial.println("ESP_ERR_ESPNOW_IF"); + } else { + Serial.println("Not sure what happened"); + } + + + lastPacket = millis(); isConnected = true; // Could check mac here for some security @@ -61,8 +107,90 @@ void OnDataRecv(const uint8_t *mac_addr, const uint8_t *data, int data_len) { display.display(); } +void deletePeer() { + const esp_now_peer_info_t *peer = &remote; + const uint8_t *peer_addr = remote.peer_addr; + esp_err_t delStatus = esp_now_del_peer(peer_addr); + Serial.print("Slave Delete Status: "); + if (delStatus == ESP_OK) { + // Delete success + Serial.println("Success"); + } else if (delStatus == ESP_ERR_ESPNOW_NOT_INIT) { + // How did we get so far!! + Serial.println("ESPNOW Not Init"); + } else if (delStatus == ESP_ERR_ESPNOW_ARG) { + Serial.println("Invalid Argument"); + } else if (delStatus == ESP_ERR_ESPNOW_NOT_FOUND) { + Serial.println("Peer not found."); + } else { + Serial.println("Not sure what happened"); + } +} + +bool manageRemote() { + if (remote.channel == CHANNEL) { + if (DELETEBEFOREPAIR) { + deletePeer(); + } + + Serial.print("Remote Status: "); + const esp_now_peer_info_t *peer = &remote; + const uint8_t *peer_addr = remote.peer_addr; + // check if the peer exists + bool exists = esp_now_is_peer_exist(peer_addr); + if ( exists) { + // Slave already paired. + Serial.println("Already Paired"); + return true; + } else { + // Slave 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 slave found to process + Serial.println("No Slave found to process"); + return false; + } +} + // end ESPNOW functions + +void checkTemperature() { + sensors.requestTemperatures(); // Send the command to get temperatures + temperature = sensors.getTempCByIndex(0); + Serial.print("Temp: "); + Serial.println(temperature); + if(temperature < 35) digitalWrite(fanRelais, LOW); + if(temperature > 40) digitalWrite(fanRelais, HIGH); + sendTemperature = (uint16_t) temperature; + sendTemperatureDecimals = (uint16_t)(temperature - sendTemperature) *100; +} + void setup() { Serial.begin(115200); Serial.println("ESPNowSkate Receiver"); @@ -76,6 +204,10 @@ void setup() { esc1.attach(esc1pin, 0, 0, 255, 1100, 1900); esc2.attach(esc2pin, 1, 0, 255, 1100, 1900); + sensors.begin(); + + pinMode(fanRelais, OUTPUT); + //Set device in AP mode to begin with WiFi.mode(WIFI_AP); // configure device AP mode @@ -95,9 +227,19 @@ void setup() { Serial.println("ESPNow Init Failed"); ESP.restart(); } + // Once ESPNow is successfully Init, we will register for recv CB to // get recv packer info. esp_now_register_recv_cb(OnDataRecv); + + for (int i = 0; i < 6; ++i ) { + remote.peer_addr[i] = (uint8_t) mac_remote[i]; + } + + remote.channel = CHANNEL; // pick a channel + remote.encrypt = 0; // no encryption + remote.ifidx = ESP_IF_WIFI_AP; + manageRemote(); } @@ -113,4 +255,6 @@ void loop() { display.drawString(2, 0, buf); display.display(); } + + checkTemperature(); } diff --git a/src/remote.cpp b/src/remote.cpp index 0f7d379..5a7e24c 100644 --- a/src/remote.cpp +++ b/src/remote.cpp @@ -9,6 +9,8 @@ #include "mac_config.h" #include "graphics.h" +#include "accel.h" + // Defining variables for OLED display U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R2, /* clock=*/ 15, /* data=*/ 4, /* reset=*/ 16); char displayBuffer[20]; @@ -29,7 +31,7 @@ bool settingsChangeValueFlag = false; short hallMeasurement; int throttle = 127; int sendThrottle = 127; -byte hallCenterMargin = 4; +byte hallCenterMargin = 5; byte currentSetting = 0; const byte numOfSettings = 11; @@ -39,14 +41,14 @@ const float maxVoltage = 4.1; const float refVoltage = 3.3; -// Global copy of slave -esp_now_peer_info_t slave; -#define CHANNEL 3 +// Global copy of board +esp_now_peer_info_t board; +#define CHANNEL 1 #define PRINTSCANRESULTS 0 #define DELETEBEFOREPAIR 0 #define HAL_MIN 1390 -#define HAL_MAX 2260 -#define HAL_CENTER 1890 +#define HAL_MAX 2230 +#define HAL_CENTER 1880 #define TRIM_LOW 180 #define TRIM_HIGH 0 @@ -57,13 +59,23 @@ esp_now_peer_info_t slave; // ESPNOW functions ############################## -// Scan for slaves in AP mode +// Scan for boards in AP mode + +void configDeviceAP(bool hidden) { + bool result = WiFi.softAP("ESK8Remote", "ESK8_Password+vD8z2YAvoDBW?Zx", CHANNEL, hidden); + if (!result) { + Serial.println("AP Config failed."); + } else { + Serial.println("AP Config Success. Broadcasting with AP: " + String("ESK8")); + } +} + #ifdef pairingMode -void ScanForSlave() { +void ScanForboard() { int8_t scanResults = WiFi.scanNetworks(); // reset on each scan - bool slaveFound = 0; - memset(&slave, 0, sizeof(slave)); + bool boardFound = 0; + memset(&board, 0, sizeof(board)); Serial.println(""); if (scanResults == 0) { @@ -85,35 +97,36 @@ void ScanForSlave() { Serial.print(")"); Serial.println(""); } + delay(10); - // Check if the current device starts with `Slave` + // Check if the current device starts with `board` if (SSID.indexOf("ESK8") == 0) { // SSID of interest - Serial.println("Found a Slave."); + Serial.println("Found a board."); Serial.print(i + 1); Serial.print(": "); Serial.print(SSID); Serial.print(" ["); Serial.print(BSSIDstr); Serial.print("]"); Serial.print(" ("); Serial.print(RSSI); Serial.print(")"); Serial.println(""); - // Get BSSID => Mac Address of the Slave + // Get BSSID => Mac Address of the board int mac[6]; if ( 6 == sscanf(BSSIDstr.c_str(), "%x:%x:%x:%x:%x:%x%c", &mac[0], &mac[1], &mac[2], &mac[3], &mac[4], &mac[5] ) ) { for (int ii = 0; ii < 6; ++ii ) { - slave.peer_addr[ii] = (uint8_t) mac[ii]; + board.peer_addr[ii] = (uint8_t) mac[ii]; } } - slave.channel = CHANNEL; // pick a channel - slave.encrypt = 0; // no encryption + board.channel = CHANNEL; // pick a channel + board.encrypt = 0; // no encryption - slaveFound = 1; - // we are planning to have only one slave in this example; + boardFound = 1; + // we are planning to have only one board in this example; // Hence, break after we find one, to be a bit efficient break; } } } - if (slaveFound) { - Serial.println("Slave Found, processing.."); + if (boardFound) { + Serial.println("board Found, processing.."); } else { - Serial.println("Slave Not Found, trying again."); + Serial.println("board Not Found, trying again."); } // clean up ram @@ -122,10 +135,10 @@ void ScanForSlave() { #endif void deletePeer() { - const esp_now_peer_info_t *peer = &slave; - const uint8_t *peer_addr = slave.peer_addr; + const esp_now_peer_info_t *peer = &board; + const uint8_t *peer_addr = board.peer_addr; esp_err_t delStatus = esp_now_del_peer(peer_addr); - Serial.print("Slave Delete Status: "); + Serial.print("board Delete Status: "); if (delStatus == ESP_OK) { // Delete success Serial.println("Success"); @@ -141,25 +154,25 @@ void deletePeer() { } } -// Check if the slave is already paired with the master. -// If not, pair the slave with master -bool manageSlave() { - if (slave.channel == CHANNEL) { +// Check if the board is already paired with the master. +// If not, pair the board with master +bool manageBoard() { + if (board.channel == CHANNEL) { if (DELETEBEFOREPAIR) { deletePeer(); } - Serial.print("Slave Status: "); - const esp_now_peer_info_t *peer = &slave; - const uint8_t *peer_addr = slave.peer_addr; + Serial.print("board Status: "); + const esp_now_peer_info_t *peer = &board; + const uint8_t *peer_addr = board.peer_addr; // check if the peer exists bool exists = esp_now_is_peer_exist(peer_addr); if ( exists) { - // Slave already paired. + // board already paired. Serial.println("Already Paired"); return true; } else { - // Slave not paired, attempt pair + // board not paired, attempt pair esp_err_t addStatus = esp_now_add_peer(peer); if (addStatus == ESP_OK) { // Pair success @@ -187,8 +200,8 @@ bool manageSlave() { } } } else { - // No slave found to process - Serial.println("No Slave found to process"); + // No board found to process + Serial.println("No board found to process"); return false; } } @@ -200,7 +213,7 @@ void sendData() { const uint8_t data[] = { esc1, esc2 }; // no mixture for the normal mode - const uint8_t *peer_addr = slave.peer_addr; + const uint8_t *peer_addr = board.peer_addr; Serial.print("Sending: "); Serial.println(esc1); esp_err_t result = esp_now_send(peer_addr, data, sizeof(data)); Serial.print("Send Status: "); @@ -222,7 +235,7 @@ void sendData() { } } -// callback when data is sent from Master to Slave +// callback when data is sent from Master to board 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", @@ -230,10 +243,21 @@ void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) { 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"); } + +// callback when data is recv from board +void OnDataRecv(const uint8_t *mac_addr, const uint8_t *data, int data_len) { + 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 Response Recv from: "); Serial.println(macStr); + uint8_t recData[3]; + memcpy(recData, data, data_len); + Serial.print("Last Response Recv Data: "); Serial.println(recData[0]); Serial.print(" "); Serial.print(recData[1]); Serial.print(" len:"); Serial.println(data_len); +} + //############ End ESP Now //############ Hardware Helpers -//############ // Check if an integer is within a min and max value bool inRange(int val, int minimum, int maximum) { return ((minimum <= val) && (val <= maximum)); @@ -342,14 +366,14 @@ void drawThrottle() { u8g2.drawHLine(x, y + 10, 5); u8g2.drawHLine(x + 52 - 4, y + 10, 5); - if (sendThrottle >= 127) { - int width = map(sendThrottle, 127, 255, 0, 49); + if (throttle >= 127) { + int width = map(throttle, 127, 255, 0, 49); for (int i = 0; i < width; i++) { u8g2.drawVLine(x + i + 2, y + 2, 7); } } else { - int width = map(sendThrottle, 0, 126, 49, 0); + int width = map(throttle, 0, 126, 49, 0); for (int i = 0; i < width; i++) { u8g2.drawVLine(x + 50 - i, y + 2, 7); } @@ -523,6 +547,8 @@ void setup() { // setup other pins pinMode(triggerPin, INPUT_PULLUP); + initAccel(); + Serial.println("ESPNowSkate Sender"); u8g2.begin(); drawStartScreen(); @@ -543,18 +569,24 @@ void setup() { ESP.restart(); } + //configDeviceAP(true); + // Once ESPNow is successfully Init, we will register for Send CB to // get the status of Trasnmitted packet esp_now_register_send_cb(OnDataSent); - //ScanForSlave(); + //ScanForboard(); + + // Once ESPNow is successfully Init, we will register for recv CB to + // get recv packer info. + esp_now_register_recv_cb(OnDataRecv); - // Retrieve Slave from config: + // Retrieve board from config: for (int i = 0; i < 6; ++i ) { - slave.peer_addr[i] = (uint8_t) mac_receiver[i]; + board.peer_addr[i] = (uint8_t) mac_receiver[i]; } - slave.channel = CHANNEL; // pick a channel - slave.encrypt = 0; // no encryption + board.channel = CHANNEL; // pick a channel + board.encrypt = 0; // no encryption } void loop() { @@ -575,12 +607,12 @@ void loop() { sendThrottle = 127; } - // If Slave is found, it would be populate in `slave` variable - // We will check if `slave` is defined and then we proceed further - if (slave.channel == CHANNEL) { // check if slave channel is defined - // `slave` is defined - // Add slave as peer if it has not been added already - bool isPaired = manageSlave(); + // If board is found, it would be populate in `board` variable + // We will check if `board` is defined and then we proceed further + if (board.channel == CHANNEL) { // check if board channel is defined + // `board` is defined + // Add board as peer if it has not been added already + bool isPaired = manageBoard(); if (isPaired) { // pair success or already paired // Send data to device @@ -596,15 +628,20 @@ void loop() { } while ( u8g2.nextPage() ); */ } else { - // slave pair failed + // board pair failed - Serial.println("Slave not found / paired!"); + Serial.println("board not found / paired!"); } } else { - // No slave found to process + // No board found to process } delay(20); } + + readAccel(); + Serial.print(AcX); + Serial.print(" "); + Serial.println(GyX); }