There is my sketch for ESP-NOW receiver :
//ESP8266_now_receiver_Nikko_en_8
//New signals for motors :
//Turn Left = TL, Turn Right = TR
//Go Forward = GF, Go Backward = GB
//Signals pro L298N dual
//Chanel A for speed of motor Forward/Backward
//Inputs IN1, IN2 for move Forward or Backward
//Chanel B for speed of motor Right/Left
//Inputs IN3, IN4 for Right or Left
//Changed from ESP8266_now_receiver_AD174_po_17_tank_en.ino
//Nikko Jeep car controlled with two joysticks
// used at first GPIO for ESP8266 NodeMCU V0.9 Esp-12
// ESP Board MAC Address (for a AD174 tank): 50:02:91:EF:E6:06 NodeMCU V1.0 po
// ESP Board MAC Address: EC:FA:BC:C9:65:17 of NodeMcu V0.9 CP2021 Nikko Jeep
/*
Thanks to Rui Santos & Sara Santos and their Random Nerd Tutorials
Complete ESP-NOW details at https://RandomNerdTutorials.com/esp-now-esp32-arduino-ide/
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files.
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
*/
// reprogrammed for my Nikko Jeep Wrangered with ESP8266 NodeMCU V0.9 by PavelOu
#include <ESP8266WiFi.h>
#include <espnow.h>
//bool serialOn = false;
String sketchName = "ESP8266_now_receiver_Nikko_en_8.ino";
// Structure example to receive data
// Must match the sender structure
typedef struct struct_message
{
int dataG;
int dataT;
int dataSW;
} struct_message;
// Create a struct_message called myData
struct_message myData;
long int timeLast = 0;
int timeOut = 1000;
bool dataOn = false;
bool joystickCalibration = false;
bool carOn = false;
bool blinkOn = false;
//definition I/O for Nikko and NodeMcu V0.9 CP2102
int enableMotorT = 4; // Enable motor in chanel B GPIO04 = D2 // PWM from 0 to 255
int enableMotorG = 5; // Enable motor in chanel A GPIO05 = D1 // PWM from 0 to 255
// Turn Left or Turn Right
int gpTL = 14; // Turn Left IN3 GPIO13 = D7 // Turn car left
int gpTR = 13; // Turn Right IN4 GPIO14 = D5 // Turn car right
//Go Forward or Go Backward
int gpGF = 15; // Go forward IN1 GPIO15 = D8 // Two wheels right forward
int gpGB = 12; // Go backwrad IN2 GPIO12 = D6 // Two wheels right backward
int carDataOnLed = 2; // GPIO2 = D4 data off LED_BUILT_IN reversed level
int carOnLed = 16; // D0 indikace car on
const byte carStop = 0;
byte speedGoMax = 220;
byte speedTurnMax = 220;
int carDirectTLR = 0;
int carDirectGFB = 0;
int carSW = 0; // = 0 = carOff , = 1 carOn
int axeG = 0; // axe G = speed of motor G
int axeGzero = 2200;
int axeGtolerance = 80;
int axeT = 0; // axe T = speed of motor T
int axeTzero = 2200;
int axeTtolerance = 80;
int axeTLR = 0; // recounted on position value speedOfmotorT
int axeGFB = 0; // - " -
// rychlosti pro motory
byte speedOfmotorG = 0;
byte speedOfmotorT = 0;
void InitMotors() //changed from AD174
{
Serial.println("Motors Initialisation");
pinMode(gpTL, OUTPUT); //Left Wheels Forward
pinMode(gpTR, OUTPUT); //Left Wheels Backward
pinMode(gpGB, OUTPUT); //Right Wheels Forward
pinMode(gpGF, OUTPUT); //Right WheelsBackward
digitalWrite(gpTL, LOW);
digitalWrite(gpTR, LOW);
digitalWrite(gpGF, LOW);
digitalWrite(gpGB, LOW);
pinMode(enableMotorG, OUTPUT);
pinMode(enableMotorT, OUTPUT);
digitalWrite(enableMotorG, 0);
digitalWrite(enableMotorT, 0);
}
void MoveCar(byte newDirect) //for variant of receiver
{
if (newDirect == 0) //stop
{
//Serial.println("Car will stop");
digitalWrite(gpTR,LOW); //set gpTR Low level
digitalWrite(gpTL,LOW); //set gpTL high level
digitalWrite(gpGB,LOW); //set gpGB Low level
digitalWrite(gpGF,LOW); //set gpGF high level
analogWrite(enableMotorT,0);
analogWrite(enableMotorG,0);
}
if (newDirect == 1) //forward
{
//Serial.println("Car will goes forward");
digitalWrite(gpGB,LOW); //set gpGB Low level
digitalWrite(gpGF,HIGH); //set gpGF high level
//digitalWrite(carBackLed,LOW);
}
if (newDirect == 2) //bacward
{
//Serial.println("Car will goes backward");
digitalWrite(gpGB,HIGH); //set gpGB High level
digitalWrite(gpGF,LOW); //set gpGF LOW level
//digitalWrite(carBackLed,HIGH);
}
if (newDirect == 3) //turn left
{
//Serial.println("Car will turn left");
digitalWrite(gpTR,HIGH); //set gpTR Higy level
digitalWrite(gpTL,LOW); //set gpTL Low level
}
if (newDirect == 4) //turn right
{
//Serial.println("Car will turn right");
digitalWrite(gpTR,LOW); //set gpTR Higy level
digitalWrite(gpTL,HIGH); //set gpTL Low level
}
}
// callback function that will be executed when data is received
void OndataGecv(const uint8_t * mac, const uint8_t *incomingData, int len)
{
memcpy(&myData, incomingData, sizeof(myData));
axeG = myData.dataG; // speedOfmotorG
axeT = myData.dataT; // speedOfmotorT
carSW = myData.dataSW;
dataOn = true;
digitalWrite(carDataOnLed,HIGH);
if (carSW == 0)
{
carOn = false;
joystickCalibration = false;
digitalWrite(carOnLed,LOW);
}
if (carSW == 1)
{
if (!carOn)
{
if (!joystickCalibration)
{
axeTzero = axeT;
axeGzero = axeG;
joystickCalibration = true;
}
carOn = true;
digitalWrite(carOnLed,HIGH);
}
}
analogWriteResolution(8);
carDirectTLR = 0;
axeTLR = axeT - axeTzero;
if (axeTLR<0) // Y < 0 = turn to the left
{
axeTLR = axeTzero - axeT;
carDirectTLR = 3;
}
else
{
carDirectTLR = 4;
}
MoveCar(carDirectTLR);
int speedintT = map(axeTLR,0,axeTzero,0,speedTurnMax);
speedOfmotorT = constrain(speedintT,0,speedTurnMax);
carDirectGFB = 0;
axeGFB = axeG - axeGzero;
if (axeGFB<0) // X < 0 = motor G / car goes backward
{
axeGFB = axeGzero - axeG;
carDirectGFB = 2;
}
else
{
carDirectGFB = 1;
}
MoveCar(carDirectGFB);
int speedintG = map(axeGFB,0,axeGzero,0,speedGoMax);
speedOfmotorG = constrain(speedintG,0,speedGoMax);
if (carOn)
{
analogWrite(enableMotorG,speedOfmotorG);
analogWrite(enableMotorT,speedOfmotorT);
}
else
{
MoveCar(0);
analogWrite(enableMotorG,0);
analogWrite(enableMotorT,0);
}
timeLast = millis();
}
void setup()
{
// Initialize Serial Monitor
Serial.begin(9600);
delay(1000);
Serial.println(sketchName);
//before using io pin, pin mode must be set first
InitMotors();
//pinMode(espnowLED, OUTPUT);
pinMode(carOnLed, OUTPUT);
pinMode(carDataOnLed, OUTPUT);
Serial.println("Test of outputs - indication:");
digitalWrite(carDataOnLed,HIGH);
delay(1000);
digitalWrite(carDataOnLed,LOW);
delay(1000);
digitalWrite(carOnLed,HIGH);
delay(1000);
digitalWrite(carOnLed,LOW);
delay(1000);
Serial.println("Set of motors");
analogWriteResolution(8);
Serial.println("Preliminary calibration of joystick ");
Serial.print("axeTzero: ");
Serial.println(axeTzero); // speed left zero
Serial.print("axeGzero: ");
Serial.println(axeGzero); // speed right zero
speedOfmotorG = 0;
speedOfmotorT = 0;
carOn = false;
joystickCalibration = false;
// Set device as a Wi-Fi Station
WiFi.mode(WIFI_STA);
// Init ESP-NOW
if (esp_now_init() != 0)
{
Serial.println("Error initializing ESP-NOW");
return;
}
else
{
Serial.println("ESP-NOW is initialized");
}
// Once ESPNow is successfully Init, we will register for recv CB to
// get recv packer info
esp_now_register_recv_cb(esp_now_recv_cb_t(OndataGecv));
digitalWrite(carDataOnLed,LOW);
digitalWrite(carOnLed,LOW);
Serial.println("There is end of serial communication for work in better real time");
delay(5000);
}
void loop()
{
long int timeActual = millis() - timeLast;
if (timeActual > timeOut)
{
// watch dog
if (!dataOn)
{
digitalWrite(carDataOnLed,LOW);
}
else
{
dataOn = false;
}
timeLast = millis();
}
}