프로그래밍/5. C#

[프로그래밍/아두이노] 아두이노 TCP 통신

핫호빵 2018. 8. 31.
반응형

안녕하세요 배당농부입니다.


이번 포스팅은 아두이노에서 와이파이망에 접속한 후 특정 서버로 TCP 프로토콜을 이용해서 서버로 메세지를 주고받는 프로그램소스 입니다.




/*

    This sketch sends data via HTTP GET requests to data.sparkfun.com service.


    You need to get streamId and privateKey at data.sparkfun.com and paste them

    below. Or just customize this script to talk to other HTTP servers.


*/


#include <ESP8266WiFi.h>


//와이파이명

const char* ssid     = "와이파이명 입력"; 

//비밀번호

const char* password = "암호입력";  


void setup() {

  Serial.begin(9600);

  delay(10);


  // We start by connecting to a WiFi network

  Serial.println();

  Serial.println();

  Serial.print("Connecting to ");

  Serial.println(ssid);


  /* Explicitly set the ESP8266 to be a WiFi-client, otherwise, it by default,

     would try to act as both a client and an access-point and could cause

     network-issues with your other WiFi-devices on your WiFi-network. */

  WiFi.mode(WIFI_STA);

  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());

  Serial.println("connection Start");


}


int value = 0;


void loop() {



  WiFiClient client;

  

  //서버의 주소와 포트번호를 적어준다.

  if (!client.connect("192.168.0.1", 7000)) {

    Serial.println("connection failed");//서버 접속에 실패

    return;

  }

  else

  {

    //서버로 보낼 정보를 구성해 보내고 받는다.

     client.write("aaa가나다라\r";);

     

     String recevbline = client.readStringUntil('\r');

     Serial.println(line);

   

  }


  delay(5000);


}


모두 즐거운 프로그래밍 되세요 ^^



반응형

댓글