정보나눔

오픈소스하드웨어 프로젝트에 대한 다양한 정보를 나누는 공간입니다.

함수 호출 관련 질문입니다
gkrry6334 | 2019-10-18

한 번만 실행되어야 할 것이 있어서 idFromJava()라는 함수를 선언해주고

setup함수에서 기본 설정 코드를 작성한 뒤 제일 마지막에 idFromJava() 함수를 호출해줬는데

idFromJava()함수로 들어가 작업을 하지 않고 바로 loop()로 들어가 반복하는데

setup()에서 idFromJava() 함수를 호출했는데 왜 idFromJava() 함수에서의 작업이 완료되지 않고

loop()로 넘어가버리는지 모르겠습니다 ㅠㅠ

 

#include <ESP8266WiFi.h>
#include <WiFiClient.h> 
#include <ESP8266WebServer.h>
#include <ESP8266HTTPClient.h>

/* Set these to your desired credentials. */
const char *ssid = "MJ";
const char *password = "mj0218!!!!";
 
//Web/Server address to read/write from 
const char *host = "192.168.43.62";
WiFiServer server(80); 
WiFiClient client;

String id, ip;
int doToggle = 0;
int ledPin = 2; //16으로 바꾸기!!!!!!!!

//===========================================================
//                           setup
//===========================================================
 
void setup() {
  delay(1000);
  Serial.begin(250000);
  WiFi.mode(WIFI_OFF);        //Prevents reconnection issue (taking too long to connect)
  delay(1000);
  WiFi.mode(WIFI_STA);        //This line hides the viewing of ESP as wifi hotspot

  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, 0);
  
  WiFi.begin(ssid, password);     //Connect to your WiFi router
  Serial.println("");
 
  Serial.print("Connecting");
  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
 
  //If connection successful show IP address in serial monitor
  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());  //IP address assigned to your ESP

 

  idFromJava();
  delay(8000);
}


//===========================================================
//                           host
//===========================================================
void serv() {
  if (client.connect(host, 5179))
  {
    Serial.print("\n\nConnected to: ");
    Serial.println(host);

    /* Send "connected" to the server so it knows we are ready for data */
    client.println("deviceconnected"); //USE client.println()!!
    Serial.println("Host message: \n");
    
    /* Wait for data for 5 seconds at most before timing out */
    unsigned long timeout = millis();
    while(client.available() == 0)
    {
      if(millis() - timeout > 5000)
      {
        Serial.println("Timeout to server!");
        break;
      }
    }
   }
   else
   {
     client.stop();
   }
   delay(5000);
}


//===========================================================
//                          get id
//===========================================================
void idFromJava(){

   serv();
  
   String first;

   //======== id 저장 =========
   /* Read in the data in the stream */
   while(client.available() > 0)
   {
     first = client.readStringUntil('\n');
     id = client.readStringUntil('\n');
     Serial.println("id= " + id);
   }
       
   client.stop();
    
   delay(5000);

   if(first != NULL)
   {
     ipFromArduino();
   }
   delay(5000);
}
 

//===========================================================
//                        send ip
//===========================================================
void ipFromArduino() {
  HTTPClient http;    //Declare object of class HTTPClient
 
  String ADCData,postData;
  int adcvalue=analogRead(A0);  //Read Analog value of LDR
  
  
  http.begin("http://192.168.43.62:8652/MyArduinoSetting");              //Specify request destination
  http.addHeader("Content-Type", "application/x-www-form-urlencoded");    //Specify content-type header
  
  ip = WiFi.localIP().toString();
  
  //int httpCode = http.POST(postData);   //Send the request
  int httpCode = http.POST("equip_ip="+ip+"&equip_id="+id);
  String payload = http.getString();    //Get the response payload
  //http.writeToStream(&Serial);
  
  //Serial.println(httpCode);   //Print HTTP return code
  //Serial.println(payload);    //Print request response payload
 
  http.end();  //Close connection
  
  delay(5000);  //Post Data at every 5 seconds
}


//===========================================================
//                           loop
//===========================================================
void loop() {
//============================ led ==========================
    serv();
    String value;
    
    //======== 값 받아오기 ========
    /* Read in the data in the stream */
    while(client.available() > 0)
    {
      value = client.readStringUntil('\n');
      Serial.println("내가 받은거 " + value);
    }
    
    //======== led 제어 =========
    if (value == "1") {
        digitalWrite(ledPin, 1);
        value = "1";
    } 
    if (value == "0"){
        digitalWrite(ledPin, 0);
        value = "0";
    }
   
    client.stop();
    
    delay(5000);
}

이전글   |    기초 프로그래밍 빈칸에 알맞은 말이 무엇일까요... 2019-10-18
다음글   |    근전도 센서값으로 알 수 있는 정보 2019-10-21