Skip to content

13. Networking and communications

Group assignment:

  • Send a message between two projects

Individual assignment:

  • Design, build, and connect wired or wireless node(s) with network or bus addresses

Have you answered these questions?

  • Linked to the group assignment page and reflected what you learned individually of the group assignment.

  • Documented your project.

  • Documented what you have learned from implementing networking and/or communication protocols.

  • Explained the programming process/es you used.

  • Outlined problems and how you fixed them.

  • Included design files (or linked to where they are located if you are using a board you have designed and fabricated earlier) and original code.

What I’ve done this week

Group assignment:

Individual assignment:

  • Communicate with ESP32 and Raspberry pi

Communication via WiFi (ESP32)

overview

This time I used a Raspberry pi and two ESP32 (Barduino redraw board) for HTTP communication.

Entering values from the Raspberry Pi command line as shown video and interacting with the target esp32 board via Wifi.

I used the board created in week08.

Writing to the board with the Arduino IDE

When the message “Leaving… Hard resetting via RTS pin” appears, switch to run mode and press the reset button.

Wiring was done as follows

Written using FT230xs programmer

The settings for writing the program are as follows

In week08, I explained about this board

I wrote the program as follows

Code

Raspberry Pi

Make a request to the target ESP32 with the HTTP get method by entering text on the command line.

import requests as req
val = input("Enter text: ")

if (val == "esp1"):
  request = 'http://192.168.11.61'
  response = req.get(request)

if (val == "esp2"):
  request = 'http://192.168.11.62'
  response = req.get(request)

ESP32

Listens as a web server and rotates the servo motor 180 degrees when an HTTP get request comes in.

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

#define SSID "WiFI_ENV"
#define PASS "PASS_ENV"

Servo myservo;

int minUs = 500;
int maxUs = 2400;

WebServer server(80);

void setup() {
  Serial.begin(9600);
  myservo.setPeriodHertz(50);
  myservo.attach(23, minUs, maxUs);
  connectWiFi();
  server.on("/", handleRoot);
  server.begin();
  Serial.println("HTTP server started");
}

void loop() {
  server.handleClient();
}

void connectWiFi(){
  WiFi.begin(SSID, PASS);
  Serial.println();
  while(WiFi.status() != WL_CONNECTED){
  delay(500);
  Serial.print(".");
}
  Serial.println();
  Serial.println("WiFi connected");
  Serial.println(WiFi.localIP());
}

void handleRoot(){
  myservo.write(180);
  delay(2000);
  myservo.write(0);
}

First, check the IP address assigned to each ESP32.

No.1 : IP address: 192.168.11.61

No.2 : IP address: 192.168.11.62

IP address can be checked from the serial monitor.

This code display the local IP Address on the serial monitor

Serial.println(WiFi.localIP());

Run req.py (the code created this time) in python3 runtime

$ python3 req.py

Send HTTP requests to each IPAddress as shown in the video below.

Final Project Image Recognition Part (Update 2022/5/24)

Wireless networking has been incorporated in the Final Project

Recognize trash and send the recognized trash type to ESP32 by HTTP get request at the timing when the button is pressed.

  • Flow

    • 1, Put trash at the base

    • 2, Image recognition of trash type by camera

    • 3, Press the button. At the timing when the button is pressed, the garbage type (plastic, charcol, or notcharcol) displayed at that time is sent to esp32 by http get request.

    • 4, The stepping motor is made to operate according to the value received by esp32, which is listening as a web server.

    • 5, If the series of processes on the ESP32 side are successfully completed, a success response (200 ok) is sent to the Raspberry pi side. When this response is sent, image recognition is started again.

The circuit was created in week10. servo motor is connected to GPIO5 and one is used.

Code

The code is explained in detail in week11.

Raspberry Pi
import RPi.GPIO as GPIO
import time
from imutils.video.pivideostream import PiVideoStream
import tensorflow as tf
import numpy as np
import cv2
import requests as req

# Set up GPIO
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(10, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)


# Set up TFLite interpreter
interpreter = tf.lite.Interpreter(model_path="model_unquant.tflite")
interpreter.allocate_tensors()

input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()

target_height = input_details[0]["shape"][1]
target_width = input_details[0]["shape"][2]

# Load labels
f = open("labels.txt", "r")
lines = f.readlines()
f.close()
classes = {}
for line in lines:
    pair = line.strip().split(maxsplit=1)
    classes[int(pair[0])] = pair[1].strip()
print(classes)

def detect(frame):
    # Prepare input data
    resized = cv2.resize(frame, (target_width, target_height))
    input_data = np.expand_dims(resized, axis=0)
    input_data = (np.float32(input_data) - 127.5) / 127.5
    interpreter.set_tensor(input_details[0]["index"], input_data)

    interpreter.invoke()
    detection = interpreter.get_tensor(output_details[0]["index"])
    return detection

def draw_detection(frame, detection):
    for i, s in enumerate(detection[0]):
        tag = f"{classes[i]}: {s*100:.2f}%"
        cv2.putText(frame, tag, (10, 20 + 20 * i), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 1)
    return frame


def main():
    camera = PiVideoStream(resolution=(512, 400)).start()
    time.sleep(2)

    while True:
        frame = camera.read()
        detection = detect(frame)
        value = classes[detection.tolist()[0].index(max(detection.tolist()[0]))]
        drawn = draw_detection(frame, detection)
        cv2.imshow("frame", drawn)
        if GPIO.input(10) == GPIO.HIGH:  
            request = 'http://"IP Address"' + '/' + value
            response = req.get(request)
        if cv2.waitKey(1) & 0xFF == ord("q"):
            break

    camera.stop()
    cv2.destroyAllWindows()

if __name__ == "__main__":
    main()

When a button is pressed in this part of the code, the most likely trash type is sent in the http GET Request.

  if GPIO.input(10) == GPIO.HIGH:  
  request = 'http://"IP Address"' + '/' + value
  response = req.get(request)
ESP32
#include <WiFi.h>
#include <WebServer.h>
#include <ESP32Servo.h>
#include <AccelStepper.h>

#define SSID "WIFI_ENV"
#define PASS "PASS_ENV"

Servo myservo1;

int pos = 80;

#define motorInterfaceType 1

const int dirPin1 = 22;
const int stepPin1 = 23;
const int stepSpeed = 1700;
const int stepsPerRevolution = 200;

AccelStepper myStepper1(motorInterfaceType, stepPin1, dirPin1);

const int dirPin2 = 19;
const int stepPin2 = 21;

AccelStepper myStepper2(motorInterfaceType, stepPin2, dirPin2);


int minUs = 500;
int maxUs = 2400;

WebServer server(80);

void setup(){
  Serial.begin(115200);
  myservo1.setPeriodHertz(50);
  myservo1.attach(5, minUs, maxUs);
  myservo1.write(80);

  myservo2.setPeriodHertz(50);
  myservo2.attach(17, minUs, maxUs);

  myservo3.setPeriodHertz(50);
  myservo3.attach(16, minUs, maxUs);

  myservo4.setPeriodHertz(50);
  myservo4.attach(4, minUs, maxUs);

  pinMode(stepPin1, OUTPUT);
  pinMode(dirPin1, OUTPUT);

  pinMode(stepPin2, OUTPUT);
  pinMode(dirPin2, OUTPUT);

  connectWiFi();
  server.on("/", handleRoot);
  server.on("/nothing", nothing);
  server.on("/charcol", charcol);
  server.on("/notcharcol", notcharcol);
  server.on("/plastic", plastic);
  server.begin();
  Serial.println("HTTP server started");
}

void loop() {
  server.handleClient();
}

void connectWiFi(){
  WiFi.begin(SSID, PASS);
  Serial.println();
  while(WiFi.status() != WL_CONNECTED){
  delay(500);
  Serial.print(".");
}
  Serial.println();
  Serial.println("WiFi connected");
  Serial.println(WiFi.localIP());
}

void handleRoot(){
  server.send(200, "text/plain", "Hello");
}

void charcol(){
  Serial.println("charcol");

  myStepper1.setMaxSpeed(1000);
  myStepper1.setAcceleration(50);
  myStepper1.setSpeed(200);
  myStepper1.moveTo(2800);
  myStepper1.runToPosition();

  myStepper2.setMaxSpeed(1000);
  myStepper2.setAcceleration(50);
  myStepper2.setSpeed(200);
  myStepper2.moveTo(-150);
  myStepper2.runToPosition();

  delay(500);

  for (pos = 80; pos < 140; pos +=1){
    myservo1.write(pos);
    delay(50);
  }

  for (pos = 140; pos >= 80; pos -= 1){
    myservo1.write(pos);
    delay(50); 
  }

  myStepper2.setMaxSpeed(1000);
  myStepper2.setAcceleration(50);
  myStepper2.setSpeed(200);
  myStepper2.moveTo(10);
  myStepper2.runToPosition();

  myStepper1.setMaxSpeed(1000);
  myStepper1.setAcceleration(50);
  myStepper1.setSpeed(200);
  myStepper1.moveTo(0);
  myStepper1.runToPosition();

  server.send(200, "text/plain", "charcol");
}

void notcharcol(){
  Serial.println("notcharcol");

  myStepper1.setMaxSpeed(1000);
  myStepper1.setAcceleration(50);
  myStepper1.setSpeed(200);
  myStepper1.moveTo(1700);
  myStepper1.runToPosition();

  myStepper2.setMaxSpeed(1000);
  myStepper2.setAcceleration(50);
  myStepper2.setSpeed(200);
  myStepper2.moveTo(-150);
  myStepper2.runToPosition();

  for (pos = 80; pos < 140; pos +=1){
    myservo1.write(pos);
    delay(50);
  }

  for (pos = 140; pos >= 80; pos -= 1){
    myservo1.write(pos);
    delay(50); 
  }

  delay(500);

  myStepper2.setMaxSpeed(1000);
  myStepper2.setAcceleration(50);
  myStepper2.setSpeed(200);
  myStepper2.moveTo(10);
  myStepper2.runToPosition();

  myStepper1.setMaxSpeed(1000);
  myStepper1.setAcceleration(50);
  myStepper1.setSpeed(200);
  myStepper1.moveTo(0);
  myStepper1.runToPosition();

  server.send(200, "text/plain", "notcharcol");
} 

void plastic(){
  Serial.println("plastic");

  myStepper1.setMaxSpeed(1000);
  myStepper1.setAcceleration(50);
  myStepper1.setSpeed(200);
  myStepper1.moveTo(500);
  myStepper1.runToPosition();

  myStepper2.setMaxSpeed(1000);
  myStepper2.setAcceleration(50);
  myStepper2.setSpeed(200);
  myStepper2.moveTo(-150);
  myStepper2.runToPosition();

  delay(500);

  for (pos = 80; pos < 140; pos +=1){
    myservo1.write(pos);
    delay(50);
  }

  for (pos = 140; pos >= 80; pos -= 1){
    myservo1.write(pos);
    delay(50); 
  }

  myStepper2.setMaxSpeed(1000);
  myStepper2.setAcceleration(50);
  myStepper2.setSpeed(200);
  myStepper2.moveTo(10);
  myStepper2.runToPosition();

  myStepper1.setMaxSpeed(1000);
  myStepper1.setAcceleration(50);
  myStepper1.setSpeed(200);
  myStepper1.moveTo(0);
  myStepper1.runToPosition();

  server.send(200, "text/plain", "plastic");
} 

If the process is complete, return a success response in the HTTP response at the end.

void plastic(){
          :
          :
          :

    server.send(200, "text/plain", "plastic");
} 

server.send(200, “text/plain”, “plastic”); is processed and then the process is performed again from the top of the while loop on the raspberry pi side.

while True:

          :
          :
          :

What I learned

HTTP communication is a very convenient communication method. It is used in many IoT home appliances, and I would like to continue to study it.

Appendix


Last update: May 26, 2022