(추가) 사진 보충설명과 ESP32에 코드만 공요합니다.  

 

PC쪽은 너무 복잡해서 공유와 설명이 도저히 불가능합니다.

 

(PC쪽에 가장 중요한 내용은 firewall에서 통신을 막으니 실험중에 firewall을 중지시키는 방법과 아니면 inbound와 outbound 룰을 사용하실 포트번호로 추가하여 여는 방법이 있습니다.)

 

 

 

 

칩에 1 써있는 모듈이 ESP32 Development Board입니다.

바로 옆에 있는 모듈은 Step-Down Variable Buck Converter로 

 9V 밧데리의 전압을 3V3으로 조정해 주는 파워모듈입니다.

그 위에 녹색 모듈이 MPU9250 (9축 센서),

그 옆에 보라색 모듈이 VL53L0X (레이저 거리측정센서) 

 

이번에는 VL53L0X는 사용을 안했고, 

  MPU9250의 기능도 딱 3D-Compass만 사용했습니다.

현재 모든 시리얼 관련코드들은 //로 막아놨는데 그냥 풀면 됩니다.

 

 

각각의 라이브러리는 

MPU9250 : https://github.com/asukiaaa/MPU9250_asukiaaa

VL53L0X : https://github.com/pololu/vl53l0x-arduino/releases

 

// Copyright : FREE for ALL. 맘껏 복사하고 고치고 필요에 맞게 쓰세요.

 

#include <WiFi.h>

#include <WiFiUdp.h>

#include <Wire.h>

#include <MPU9250_asukiaaa.h>

 

#define _ESP32_HAL_I2C_H_

 

#ifdef _ESP32_HAL_I2C_H_

#define SDA_PIN 21

#define SCL_PIN 22

#endif

 

#define BUFFER_SIZE 100

 

// WiFi network name and password:

const char * networkName = "******";  // 집이나 사무실 와이파이 아이디

const char * networkPswd = "******";  // 와이파이 연결 비밀번호

 

//IP address to send UDP data to:

// either use the ip address of the server or 

// a network broadcast address

const char * udpAddress = "192.168.1.200";   // 저의 PC의 고정주소

const int udpPort = 6767;                    // 제가 로봇용으로 주로 사용하는 포트번호

 

//The udp library class

WiFiUDP udp;

 

//Are we currently connected?

boolean connected = false;

 

// 9 DOF Gyro, Accel, Mag Sensor; here I only use 3 Axis Mag

MPU9250_asukiaaa DOF9Sensor;

 

float mDirection, mx, my, mz;

 

int r = 0; // Simple Counter to replace delay() in the main loop

 

String s; // Temporary string for general purpose

 

void setup()

{

//   while(!Serial);

//   Serial.begin(19200);

 

   //Connect to the WiFi network

   connectToWiFi(networkName, networkPswd);   

 

#ifdef _ESP32_HAL_I2C_H_ // For ESP32

   Wire.begin(SDA_PIN, SCL_PIN); // SDA, SCL

#else

   Wire.begin();

#endif

 

   //////////////// MPU9250 9-DOF SENSOR //////////////////////// 

   DOF9Sensor.setWire(&Wire);

   DOF9Sensor.beginMag();

 

   // 이 오프셋은 제 책상에서 사용하는 장비의 위치에서 나오는 기본 오차량

   DOF9Sensor.magXOffset = 66;

   DOF9Sensor.magYOffset = 18;

   DOF9Sensor.magZOffset = 41;

 

//   Serial.println("SensorId: " + String(DOF9sensorId));

 

   delay(1000);

}

 

void connectToWiFi(const char * ssid, const char * pwd){

//   Serial.println("Connecting to WiFi network: " + String(ssid));

 

   // delete old config

   WiFi.disconnect(true);

   //register event handler

   WiFi.onEvent(WiFiEvent);

  

   //Initiate connection

   WiFi.begin(ssid, pwd);

 

   connected = true;

 

//   Serial.println("Waiting for WIFI connection...");

}

 

//wifi event handler

void WiFiEvent(WiFiEvent_t event){

    switch(event) {

      case SYSTEM_EVENT_STA_GOT_IP:

          //When connected set 

//          Serial.print("WiFi connected! IP address: ");

//          Serial.println(WiFi.localIP());  

          //initializes the UDP state

          //This initializes the transfer buffer

          udp.begin(WiFi.localIP(),udpPort);

          connected = true;

          break;

      case SYSTEM_EVENT_STA_DISCONNECTED:

//          Serial.println("WiFi lost connection");

          connected = false;

          break;

    }

}

 

void print9DOF() {

   s = "<R=3Mag:4,";

   s += mx; s += ","; s += my; s += ","; s += mz; s += ",";

   s += mDirection; s += ">";

//   Serial.println(s);

 

   //only send data when connected

   if(connected){

      //Send a packet

      udp.beginPacket(udpAddress,udpPort);

      udp.print(s);

      udp.endPacket();

   }  

}

 

void loop() {

 

   r++;

   if (r == 22000){ // THIS IS DESCENT DELAY

 

      DOF9Sensor.magUpdate();

      mx = DOF9Sensor.magX();

      my = DOF9Sensor.magY();

      mz = DOF9Sensor.magZ();

      mDirection = DOF9Sensor.magHorizDirection();

      

      print9DOF();

 

      r = -22000;

   }

 

}

+ Recent posts