본문 바로가기
Firmware Programming

[Arduino] Arduino Wifi Module (ESP8266) 활용 윈도우 pc로 데이터 전송

by TYB 2024. 1. 31.
반응형

우선 다른 블로그의 글을 보고

#include <SoftwareSerial.h> 
#define BT_RXD 2 
#define BT_TXD 3 
SoftwareSerial ESP_wifi(BT_RXD, BT_TXD); 
void setup() { 
  Serial.begin(115200); 
  ESP_wifi.begin(115200); 
  ESP_wifi.setTimeout(5000); 
  delay(1000); 
} 
void loop() { 
if (Serial.available()){
  ESP_wifi.write(Serial.read()); 
} 
if (ESP_wifi.available()) { 
  Serial.write(ESP_wifi.read()); 
  }
}

위 코드를 업로드 한 상태에서 Serial Monitor에서 AT를 쳤을 때 OK가 나오고 와이파이에 연결까지 된 상황임을 가정하고 진행한다.

 

아래 블로그를 보고 와이파이 연결까지 완료한 후 그 아래에 있는 윈도우 pc 코드를 실행시켜 서버를 listening 상태로 만들고 아두이노에 아래 코드를 업로드 시킨다.

https://rasino.tistory.com/297

 

【 아두이노모듈#25】ESP8266 esp-01 활용하기#1 : 펌웨어 업뎃(내용보강-완료, 영상추가)

【 아두이노모듈#25】 ESP8266 ep-01 활용하기#1 : 펌웨어 업데이트 에러 해결 (내용보강-완료,영상추가) ESP8266은 wifi 통신을 이용하고 인터넷(네트웍)이나 직접 연결(무선)로 제어가 가능한 모듈이며,

rasino.tistory.com

 

 

윈도우 pc python code 

import socket

# Server settings
host = '10.10.15.58'  # Your PC's IP address
port = 12345           # Choose a port number

server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((host, port))
server.listen(1)

print(f"Listening for connections on {host}:{port}...")

while True:
    client, address = server.accept()
    print(f"Connection from {address}")

    while True:
        data = client.recv(1024).decode()
        if not data:
            break  # No more data from client, close the connection

        print(f"Received data from ESP8266: {data}")
        # Process the received data as needed

    print("Client disconnected.")
    client.close()

 

 

 

arduino code

#include <SoftwareSerial.h>

SoftwareSerial espSerial(2, 3); // RX, TX

// Function declarations
void sendCommand(String command);
void sendData(String data);
void waitForResponse();

void setup() {
  Serial.begin(115200); // Arduino serial communication setup
  espSerial.begin(115200); // ESP8266 serial communication setup
  
  // ESP8266 initialization
  sendCommand("AT+RST");
  delay(2000);
  
  // Set to single connection mode
  sendCommand("AT+CIPMUX=0");
  
  // Connect to Wi-Fi
  sendCommand("AT+CWJAP=\"aiot5\",\"aiot51234\"");
  delay(5000); // Wait for Wi-Fi connection

  // Start TCP connection
  sendCommand("AT+CIPSTART=\"TCP\",\"10.10.15.58\",12345");
}

void loop() {
  // Send data
  int x1 = analogRead(A0);
  int y1 = analogRead(A1);
  int x2 = analogRead(A2);
  int y2 = analogRead(A3);
  sendData(x1, y1, x2, y2);
  delay(10); // 5-second interval for data transmission
}

void sendCommand(String command) {
  espSerial.println(command);
  waitForResponse();
}

void sendData(int x1, int y1, int x2, int y2) {
  // Construct data string with variables
  String data = "x1(" + String(x1) + "),y1(" + String(y1) + "),x2(" + String(x2) + "),y2(" + String(y2) + ")";
  
  // Initiate data transmission command
  sendCommand("AT+CIPSEND=" + String(data.length() + 2)); // +2 to account for potential two-digit values
  
  delay(100);
  
  // Actual data transmission
  espSerial.println(data);
  //delay(100);
}

void waitForResponse() {
  delay(10);
  while (espSerial.available() > 0) {
    char c = espSerial.read();
    Serial.print(c);
  }
  Serial.println();
}

 

 

조이스틱 2개를 연결해서 그 값을 받아 wifi 모듈을 통해 윈도우 pc로 전송하는 프로젝트 진행 중..

 

반응형