ESP32 on Elegoo DF-...
 
Notifications
Clear all

ESP32 on Elegoo DF-ROBOT - serial communication  

  RSS

Phil_2020
(@phil_2020)
Eminent Member
Joined: 5 months ago
Posts: 26
2020-04-09 5:31 pm  

Hi,

Well, I have not had much success with my DIY robot build. So I revisited my stable of  kit-robots. The Elegoo DF-Robot was a great little kit but it is underpowered (Arduino Uno) and  lacks WIFI. Also t had a less than useful Bluetooth adapter.

Rumors on the street have it that the Bluetooth only pairs with the Elegoo phone apps.( I tried a few times to pair it to my MAC-air, but no luck. ) So anyway, I decided to see what I could do with attaching an ESP32 Lolin D32 pro. 

I went with the Lolin D32 pro because it has a battery sub-system. I can stick a 400 Mah lipo on it and it will run mobile. Otherwise I would have had to figure out how to mount a small phone charger bank  for a regular ESP-32 on the DF-Robot. Also, I didn't want to use the 5v-3.3v sources coming off the shield on top of the Arduino Uno.

Ok. So, here is what I did:

1. Yanked the bluetooth adapter.

2. Attached  jumper wires (f-m) from the molex socket labeled 'Bluetooth (UART)'  as follows:

A. +5v -> HV on my level shifter -> 3.3v on the ESP32

B. GND -> HV GND on my level shifter -> GND on the ESP32

C. RXD -> HV TXD on my level shifter -> SERIAL2 RXD on the ESP32

D. TXD -> HV RXD on my level shifter -> SERIAL2 TXD on the ESP32

I figured the +5v and GND on the molex socket were to power the Bluetooth adaptor. So I just used them as is for the level shifter.

Here is the Arduino Uno code:

 

int id=99;
#include <Servo.h> //servo library
Servo myservo; // create servo object to control servo

int Echo = A4;
int Trig = A5;
int my_servo_pin = 3;
unsigned long my_millis = 0;
unsigned long current_millis = 0;
char bfr[30];

int rightDistance = 0, leftDistance = 0, middleDistance = 0;
int testDistance = 0;

void setup() {
Serial.begin(115200);
Serial.println("Interfacfing arduino with nodemcu");

myservo.attach(my_servo_pin,700,2400); // attach servo on pin 3 to servo objec
current_millis = millis();
pinMode(Echo, INPUT);
pinMode(Trig, OUTPUT);
}

void loop() {
if (millis() - current_millis >= 500) {
//Serial.println("WRITING TO SERVO");
myservo.write(90); //setservo position according to scaled value
delay(100);
testDistance = Distance_test();
delay(100);
if (testDistance < 40) {
stop_and_look();
}
testDistance = 300;
current_millis = millis();
}
}

//Ultrasonic distance measurement Sub function
int Distance_test() {
//Serial.println("IN GET DISTANCE");
digitalWrite(Trig, LOW);
delayMicroseconds(2);
digitalWrite(Trig, HIGH);
delayMicroseconds(20);
digitalWrite(Trig, LOW);
float Fdistance = pulseIn(Echo, HIGH);
Fdistance= Fdistance / 58;
//Serial.println(Fdistance);
return (int)Fdistance;
}

void stop_and_look() {
stop();

myservo.write(30); //setservo position according to scaled value
delay(100);
rightDistance = Distance_test();
delay(100);

myservo.write(90); //setservo position according to scaled value
delay(100);
middleDistance = Distance_test();
delay(100);

myservo.write(150); //setservo position according to scaled value
delay(100);
leftDistance = Distance_test();
delay(100);

memset( bfr, '\0', sizeof(bfr));
//my_millis = millis();
sprintf(bfr,"[%3d:%3d:%3d]",rightDistance,middleDistance,leftDistance);
//Serial.print("Sending bfr to nodemcu");
Serial.println(bfr);

}
void stop() {
Serial.println("STOP");
}>

Here is the ESP32 code:

#include <WiFi.h>
#include <PubSubClient.h>


// Update these with values suitable for your network.
const char* ssid = "xxxx";
const char* password = "xxxx";
const char* mqtt_server = "x.x.x.x";
#define mqtt_port 1883

#define MQTT_SERIAL_PUBLISH_CH "topic/state/elegoo"

WiFiClient wifiClient;

PubSubClient client(wifiClient);

#define RXD2 39 // shifted things around a bit
#define TXD2 36

void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
randomSeed(micros());
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}


void reconnect() {
//classic mqtt reconnect function
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("BRIDGE")) {
Serial.println("connected");
// Subscribe
client.subscribe("topic/state/ack");
client.publish("topic/state","HELLLOOOO");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}


void setup() {
Serial.begin(115200);
Serial.setTimeout(500);// Set time out for
Serial2.begin(115200, SERIAL_8N1, RXD2, TXD2);
Serial.println("Serial Txd is on pin: "+String(TXD2));
Serial.println("Serial Rxd is on pin: "+String(RXD2));

setup_wifi();
client.setServer(mqtt_server, mqtt_port);
reconnect();
client.publish("topic/state","IN SETUP");

while (!Serial2) {
; // wait for serial port to connect. Needed for native USB port only
}

}

void publishSerialData(char *serialData){
if (!client.connected()) {
reconnect();
}
client.publish(MQTT_SERIAL_PUBLISH_CH, serialData);
}
void loop() {
client.loop();
if (Serial2.available() > 0) {
char bfr[501];
memset(bfr,0, 501);
Serial2.readBytesUntil( '\n',bfr,500);
publishSerialData(bfr);
}
}






Quote
Phil_2020
(@phil_2020)
Eminent Member
Joined: 5 months ago
Posts: 26
2020-04-11 4:11 pm  

Hi all,

Oops. The Elegoo Robot Car v3 has nothing to do with DF-Robot. 

Sorry for the confusion.

Phil


ReplyQuote