[ESP32] Move the robot arm from LINE bot / Robot arm edition

Overview

Roughly speaking, we have jointly developed with @fauntleroy that when an instruction is sent from outside the house with a LINE bot, a robot arm under another network such as home moves according to the instruction and presses the remote control.

I was in charge of the robot arm side (client), and @fauntleroy was in charge of the LINE bot side (server side).

The image is as shown in the figure below.

https---qiita-image-store.s3.ap-northeast-1.amazonaws.com-0-288721-51bce6b7-43e8-2147-3e0b-9a1e789efee2.jpeg The robot arm always sends a GET request to the server, and when the instruction is thrown from the LINE bot, the instruction content such as state = 1 is passed to the robot arm side.

For the LINE side, see this article by @fauntleroy. [ESP32] Move the robot arm from LINE bot / Robot arm edition (The above image was borrowed from Mr. @fauntleroy.)

What was used

Environmental setting

In order for the ESP32 to be recognized by the Mac IDE for Arduino, an additional driver is required in addition to the normal board manager. See the site around here L Chika of ESP32 on Mac (Arduino IDE version)

--Simply write the policy. First, search for ESP32 from Tools-> Board-> Board Manager and enter it. --After that here. -Install the driver for here.

Implementation method

Various initialization

I wrote the initialization code separately for server communication and robot arm control.


#include <ESP32Servo.h>
#include <WiFi.h>
#include <string> 

//Declaration for robots
Servo servo1,servo2,servo3;

int servo3Pin = 25;
int servo2Pin = 2;
int servo1Pin = 15;

int minUs = 500;
int maxUs = 2400;
int pos = 0;   

//Declaration for server
WiFiClient client;
//wifi settings
const char* ssid     = "Write the SSID here";
const char* password = "Write your WIFI password here";

//Destination server
const char* server     = "hogehoge.com";
//Port number is appropriate. This time it was number 80.
const int httpPort = 80; 

int fsr_array[10];
int count = 0;

void setup() void setup () is the part that is read only once at the beginning when the code is executed. Originally, initialization is often written here.


void setup() {

  Serial.begin(9600);
  delay(5000);
  
  //Initialization of servo port, etc.
  servo1.attach(servo1Pin, minUs, maxUs);
  servo2.attach(servo2Pin, minUs, maxUs);
  servo3.attach(servo3Pin, minUs, maxUs);
  //Fixed orientation
  servo1.write(0);
  servo2.write(45);
  servo3.write(180);

  
  Serial.println("===Robot setting completed===");

  //wifi connection
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
 
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());


  //Server connection
  if (client.connect(server,httpPort)) {
    Serial.println("Client Connected");
  } else {
    Serial.println("No Connection desu");
  }

}


void loop()

This is basically called after setup () is called. First, the definition of the function used in the loop. This time, we implemented a function that moves the robot arm at an appropriate speed to press a button when instructed.


void ButtunPush(Servo servo2){

   //Bring the robot arm closer to the remote control
   for (pos = 45; pos >= 30; pos -= 1) { //Reduce by 1 degree from 45 degrees to 30 degrees
    servo2.write(pos);
    delay(50);  //0 for each reduction.Wait 5 seconds.
  }

   //Press the button on the remote control
   for (pos = 30; pos >= 0; pos -= 1) { //Reduce by 1 degree from 30 degrees to 0 degrees
    servo2.write(pos);
    delay(15);             //0 for each reduction.Wait 15 seconds.
  }

  //Return the remote control to the initial position
  for (pos = 0; pos <= 45; pos += 1) { //Increase from 0 degrees to 45 degrees.
    servo2.write(pos);
    delay(50);
  }
}

Finally, implement inside the loop.


void loop() {

  if (client.connect(server,httpPort)) {
    Serial.println("Client Connected on loop"+(String)count+"Second loop");
  } else {
    Serial.println("No Connection on loop");
  }
  count++;

  //Create request URI
  //Where to write the place where you want to make a GET request.
  String url = "/ToI"; 

  Serial.print("Requesting URL: ");
  Serial.println(url);

  //Send a request to the server here
  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + server + "\r\n" + 
               "Connection: close\r\n\r\n");
  //Every second
   delay(1000);

  Serial.println("Respond:");
  int i = 0;
  String line;
  while(client.available()){
    line = client.readStringUntil('\r');//From the serial buffer
  }
  
    line.toCharArray(number,line.length()+1);
    Serial.println("motion_number is");
    Serial.println(number[1]);

    motion_number = number[1];
    
  Serial.println();
  Serial.println("closing connection");

  //Change depending on the value received
  //1 is dehumidifying, 2 is cooling
  if  (motion_number.equals("2")) {
      for (pos = 0; pos <= 5; pos += 1) {
          servo1.write(pos);
          delay(50);
  }
  ButtunPush(servo2);

   }else if (motion_number.equals("1")) { //Dehumidification
          for (pos = 5; pos >= 0; pos -= 1) {degrees
        // in steps of 1 degree
        servo1.write(pos);
        delay(50);
      }
      ButtunPush(servo2);    
   }else {
    for (pos = 0; pos <= 5; pos += 1) {
          servo1.write(pos);
          delay(50);
      }
    }
}

code

Finally, the code on the robot arm side is as follows.


#include <ESP32Servo.h>
#include <WiFi.h>
#include <string> 



//Declaration for robots
Servo servo1,servo2,servo3; 

int servo3Pin = 25;
int servo2Pin = 2;
int servo1Pin = 15;

int minUs = 500;
int maxUs = 2400;
int pos = 0;

//Declaration for server
WiFiClient client;
const char* ssid     = "Write the SSID here";
const char* password = "Write your WIFI password here";
const char* server     = "hogehoge.com";
//Port number is appropriate. This time it was number 80.
const int httpPort = 80; 

int fsr_array[10];
int count = 0;
String motion_number;
char number[1000];
char buff[256];
unsigned char a='A';


void setup() {

  Serial.begin(9600);
  delay(5000);
  
  //Initialization of servo port, etc.
  servo1.attach(servo1Pin, minUs, maxUs);
  servo2.attach(servo2Pin, minUs, maxUs);
  servo3.attach(servo3Pin, minUs, maxUs);
  //Fixed orientation
  servo1.write(0);
  servo2.write(45);
  servo3.write(180);

  
  Serial.println("===Robot setting completed===");

  //wifi connection
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
 
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());


  //Server connection
  if (client.connect(server,httpPort)) {
    Serial.println("Client Connected");
  } else {
    Serial.println("No Connection desu");
  }

}


void ButtunPush(Servo servo2){

   //Bring the robot arm closer to the remote control
   for (pos = 45; pos >= 30; pos -= 1) { //Reduce by 1 degree from 45 degrees to 30 degrees
    servo2.write(pos);
    delay(50);  //0 for each reduction.Wait 5 seconds.
  }

   //Press the button on the remote control
   for (pos = 30; pos >= 0; pos -= 1) { //Reduce by 1 degree from 30 degrees to 0 degrees
    servo2.write(pos);
    delay(15);             //0 for each reduction.Wait 15 seconds.
  }

  //Return the remote control to the initial position
  for (pos = 0; pos <= 45; pos += 1) { //Increase from 0 degrees to 45 degrees.
    servo2.write(pos);
    delay(50);
  }
}




void loop() {

  if (client.connect(server,httpPort)) {
    Serial.println("Client Connected on loop"+(String)count+"Second loop");
  } else {
    Serial.println("No Connection on loop");
  }
  count++;

  //Create request URI
  String url = "/ToI";

  Serial.print("Requesting URL: ");
  Serial.println(url);

  //Send request to server
  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + server + "\r\n" + 
               "Connection: close\r\n\r\n");
  //Every second
   delay(1000);

  Serial.println("Respond:");
  int i = 0;
  String line;
  while(client.available()){
    line = client.readStringUntil('\r');//From the serial buffer
  }
  
    line.toCharArray(number,line.length()+1);
    Serial.println("motion_number is");
    Serial.println(number[1]);

    motion_number = number[1];
    
  Serial.println();
  Serial.println("closing connection");

  //Change depending on the value received
  //1 is dehumidifying, 2 is cooling
  if  (motion_number.equals("2")) {
      for (pos = 0; pos <= 5; pos += 1) {
          servo1.write(pos);
          delay(50);
  }
  ButtunPush(servo2);

   }else if (motion_number.equals("1")) { //Dehumidification
          for (pos = 5; pos >= 0; pos -= 1) {
        servo1.write(pos);
        delay(50);
      }
      ButtunPush(servo2);    
   }else {
    for (pos = 0; pos <= 5; pos += 1) {
          servo1.write(pos);
          delay(50);
      }
    }
}

Recommended Posts

[ESP32] Move the robot arm from LINE bot / Robot arm edition
Upload photos from LINE Bot to Dropbox
How to measure line speed from the terminal