[MQTT] 아두이노 mqtt 서버 연결 실패

2022. 7. 26. 06:55·1&2학년/[2022 03 ~ 09] IoT Project

0. 서론

IoT 통신 프로토콜 중 하나인 mqtt를 공부하던 중, 아두이노가 mqtt 서버에 연결을 하지 못하고 계속 실패했다..

Attempting MQTT connection... failed, rc = - 2

  • 연결 환경 
    • 아두이노: WeMos D1 R1(esp 8266)
    • MQTT broker sever: mosquitto
    • API Library: PubSubClient

 

 

사진을 보면 와이파이 연결까지는 잘 되지만 arduino와 mqtt 통신이 잘 안 되는 것을 확인할 수 있다.

소스 코드가 복잡해 보이지만 눈여겨볼곳은 line 70에 있는 reconnect 함수이다.
연결을 하지 못하고 client.state()가 -2를 리턴하는 것을 알 수 있다.

-2를 리턴은 network connection failed라고 하네요 (Api document 참고) 

1. 해결 방법 

일주일간 고통받다가 찾아냈다 ㅠㅠ


1. 윈도우 방화벽 해제

   

2. 인바운드 규칙 추가

    인바운드 규칙으로 1883 port를 열고 (mosquitto broker의 디폴트 포트는 1883이다)

 

참고한 링크: https://www.gloriouscoding.com/8a3c4bce-bd64-48f2-8533-7f218119e52f

 

12.Arduino에 MQTT 사용하기

이전 자료

www.gloriouscoding.com

3. mosquitto 디렉토리로 이동

4. mosquitto 파일 내에 있는 mosquitto.conf를 메모장으로 실행시키고

listener 1883
allow_anonymous true

 

다음과 같이 두 줄을 추가 후 저장한다.

 

 

5. 만약 아래와 같이 저장 못한다고 뜨면

그냥 새 메모장 하나를 관리자 권한으로 열고 파일  → 열기  → mosquitto 디렉토리로 이동  

 

그러면 아마 mosquitto.conf 파일이 안보일 텐데 사진과 같이 모든 파일로 바꾸면 나온다.

 

사진 5시 방향 "모든 파일"

그 후는 2번과 동일 

 

 

 

결과

 

속이 뻥

 

success

 

 

이유..?

솔직히 잘 모른다 ㅋㅋ.. 

아마도 allow_anonymous true이게 디폴트가 false로 되어있어서 true로 바꿔줘야 연결이 되는 거 같은데.. 잘 모르겠다.

 

근데 아마 여기 읽어보면 알 수 있을 듯 전 영어 알러지가 있어서 안 읽었어요

 

읽고 누가 세줄 요약 좀 

 

 

 

2. 기타

 

나중에 알게된건데 cloudmqtt라고 mqtt 클라우드 서버를 제공해서 굳이 저런 뻘짓을 할 필요가 없었음.. 
https://www.cloudmqtt.com/

 

CloudMQTT - Hosted message broker for the Internet of Things

IoT messaging CloudMQTT is a perfect solution for "Internet of Things" messaging between low power sensors or mobile devices such as phones, embedded computers or microcontrollers like the Arduino. Hosted message broker CloudMQTT automates every part of se

www.cloudmqtt.com

 

'1&2학년 > [2022 03 ~ 09] IoT Project' 카테고리의 다른 글

[Arudino]WiFi.localIP() to char  (0) 2022.08.22
[Arduino vscode] 헤더파일  (0) 2022.08.22
[Vscode]Output path is not specified. Unable to reuse previously compiled files. Build will be slower. See README.  (0) 2022.08.08
MQTT api document  (0) 2022.08.08
[VsCode Arudino] Cannot find Arduino IDE. Please specify the "arduino.path" in the User Settings. Requires a restart after change 오류  (0) 2022.07.27
'1&2학년/[2022 03 ~ 09] IoT Project' 카테고리의 다른 글
  • [Arduino vscode] 헤더파일
  • [Vscode]Output path is not specified. Unable to reuse previously compiled files. Build will be slower. See README.
  • MQTT api document
  • [VsCode Arudino] Cannot find Arduino IDE. Please specify the "arduino.path" in the User Settings. Requires a restart after change 오류
JaewonAhn
JaewonAhn
  • JaewonAhn
    JwAhn_Embedded
    JaewonAhn
  • 전체
    오늘
    어제
    • 분류 전체보기 (136)
      • MCU (21)
        • esp32 (6)
        • stm32 (15)
      • SW (30)
        • 백준 (27)
        • C, C++ (2)
        • 자료구조 (0)
        • 파이썬 (1)
      • Tool (2)
        • KiCAD (0)
        • Git (0)
        • Visual Studio Code (1)
        • CubeIDE (1)
      • Balancing robot (28)
        • HW (11)
        • FW&SW (2)
        • 일지 (13)
        • 이론공부 (1)
      • 1&2학년 (17)
        • 1학년(2021) (2)
        • 2학년(2022) (4)
        • [2022 03 ~ 09] IoT Project (8)
        • [2022 09 ~ 12] 융합 프로젝트 (3)
      • 2025(학부 3학년) (14)
        • 신호 및 시스템 (1)
        • 전자회로 (0)
        • 제어 및 AI 입문 (3)
        • 임베디드시스템 (8)
        • 기타 (2)
      • 잡솔 (3)
      • 군대 (7)
        • 이등병 (1)
        • 일병 (4)
        • 병장 (2)
      • Driving_Assistance_Device (14)
        • Hailo (7)
        • 일지 (3)
        • 기타 (4)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    plotly
    백준
    2학년
    WiFi.local() to char
    9012
    쇠막대기
    nucelo
    이계도함수
    arudino
    10866
    library
    스택
    10799
    10828
    10845
    Arduino
    뇌절
    vscode
    headerfile
    합성함수의 미분법
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.3
JaewonAhn
[MQTT] 아두이노 mqtt 서버 연결 실패
상단으로

티스토리툴바