▶ 와이파이 통신
#include <WiFi.h>
const char* ssid = "JYPC";
const char* password = "12345678";
const char* host = "172.30.1.33";
//const char* streamId = "....................";
//const char* privateKey = "....................";
void setup()
{
Serial.begin(115200);
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
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());
}
int value = 0;
void loop()
{
delay(5000);
++value;
Serial.print("connecting to ");
Serial.println(host);
// Use WiFiClient class to create TCP connections
WiFiClient client;
const int httpPort = 8081;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
// We now create a URI for the request
String url = "/ArduinoTest/wifiTest.jsp?sensor=456";
// url += streamId;
// url += "?private_key=";
// url += privateKey;
// url += "&value=";
// url += value;
Serial.print("Requesting URL: ");
Serial.println(url);
// This will send the request to the server
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
unsigned long timeout = millis();
while (client.available() == 0) {
if (millis() - timeout > 5000) {
Serial.println(">>> Client Timeout !");
client.stop();
return;
}
}
// Read all the lines of the reply from server and print them to Serial
while(client.available()) {
String line = client.readStringUntil('\r');
Serial.print(line);
}
Serial.println();
Serial.println("closing connection");
}
▶ 와이파이 통신 변수로 따로 잡아줘서 loop문 안에만 수정해주면 됨.
#include <WiFi.h>
const char* ssid = "JYPC";
const char* password = "12345678";
const char* host = "172.30.1.33";
//const char* streamId = "....................";
//const char* privateKey = "....................";
void setup()
{
Serial.begin(115200);
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
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());
}
int value = 0;
void loop(){
int sensorValue = analogRead(39);
if(digitalRead(25) == 1){
ServerConn("abcd");
}
}
void ServerConn(String sensorData){
delay(5000);
Serial.print("connecting to ");
Serial.println(host);
// Use WiFiClient class to create TCP connections
WiFiClient client;
const int httpPort = 8081;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
// We now create a URI for the request
String url = "/ArduinoTest/wifiTest.jsp?sensor="+sensorData;
// url += streamId;
// url += "?private_key=";
// url += privateKey;
// url += "&value=";
// url += value;
Serial.print("Requesting URL: ");
Serial.println(url);
// This will send the request to the server
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
unsigned long timeout = millis();
while (client.available() == 0) {
if (millis() - timeout > 5000) {
Serial.println(">>> Client Timeout !");
client.stop();
return;
}
}
// Read all the lines of the reply from server and print them to Serial
while(client.available()) {
String line = client.readStringUntil('\r');
Serial.print(line);
}
Serial.println();
Serial.println("closing connection");
}
'아두이노' 카테고리의 다른 글
ESP32 UNO D1 R32보드 – SimpleWIFIServer (0) | 2022.05.10 |
---|---|
안드로이드 폰 블루투스 이용한 아두이노 (0) | 2022.05.10 |
▶ESP32 UNO D1 R32 보드 (0) | 2022.05.09 |
Serial 통신 (0) | 2022.05.09 |