2016-08-23 6 views
0

안녕하세요 메신저 내 ESP 8266에서 데이터를 가져 오는 방법이 내 아두 이노 코드후 다시 나는 내 아두 이노</p> <p>와 esp8266 연결 esp8266 에서 데이터를 전송 한 후 부착 한 안드로이드

#include <SoftwareSerial.h> 
//#include <OneWire.h> 
//#include <DallasTemperature.h> 
//#include <stdlib.h> 
//#include "Wire.h" 
//#define DS3231_I2C_ADDRESS 0x68 
//#define ONE_WIRE_BUS 5 
//WIFI 
#include <SPI.h> 
#include <WiFi.h> 
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs) 
//OneWire oneWire(ONE_WIRE_BUS); 

// Pass our oneWire reference to Dallas Temperature. 
//DallasTemperature sensors(&oneWire); 

int wifiTx = 4; 
int wifiRx = 3; 
#define DEBUG true 
char com; 
String data; 

SoftwareSerial wifi(wifiTx, wifiRx); 

//wifi 
char ssid[] = "waifu";  // your network SSID (name) 
char pass[] = "chronoangel"; // your network password 
//int status = WL_IDLE_STATUS;  // the Wifi radio's status 


void setup() { 
    // put your setup code here, to run once: 
    Wire.begin(); 
    Serial.begin(9600); 
    wifi.begin(115200); 
    sensors.begin(); 
    //sekon, menit,jam, day of week, hari, bulan taun 
    //setDS3231time(0,15,2,1,13,7,16); 
    //\start_wifi(); 
    esp(); 
} 

void loop() { 
test_wifi(); 
//loop_esp(); 

} 

void esp() 
{ 
    sendCommand("AT\r\n",2000,DEBUG); 
    sendCommand("AT+RST\r\n",2000,DEBUG); // reset module 
    sendCommand("AT+CWMODE=1\r\n",1000,DEBUG); // configure as access point 
    sendCommand("AT+CWJAP=\"waifu\",\"chronoangel\"\r\n",3000,DEBUG); 
    delay(10000); 
    //Serial.println("\nCek IP"); 
    //sendCommand("AT+CIFSR\r\n",1000,DEBUG); // get ip address 
    sendCommand("AT+CIPMUX=1\r\n",1000,DEBUG); // configure for multiple connections 
    Serial.println("\nGet PORT"); 
    sendCommand("AT+CIPSERVER=1,80\r\n",1000,DEBUG); // turn on server on port 80 
    Serial.println("\nSet IP"); 
    sendCommand("AT+CIPSTA=\"192.168.1.7\"\r\n",1000,DEBUG); // set ip address 
    sendCommand("AT+CIFSR\r\n",1000,DEBUG); // get ip address 
    Serial.println("\nServer Ready"); 
} 
void test_wifi() 
{ 
    if(wifi.available()) 
    { 
    char toSend = (char)wifi.read(); 
    Serial.print(toSend); 
    } 

    //Read from usb serial to wifi 
    if(Serial.available()) 
    { 
    char toSend = (char)Serial.read(); 
    wifi.print(toSend); 
    } 
} 

이있다 enter image description here이 내 프로그램의 내보기는

package com.example.chronoangel.myapplication; 

import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 

import java.io.ByteArrayOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.net.Socket; 
import java.net.UnknownHostException; 

import android.os.AsyncTask; 
import android.app.Activity; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 

public class MainActivity extends AppCompatActivity { 

    TextView textResponse; 
    EditText editTextAddress, editTextPort; 
    Button buttonConnect, buttonClear; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     editTextAddress = (EditText) findViewById(R.id.IP); 
     editTextPort = (EditText) findViewById(R.id.Port); 
     buttonConnect = (Button) findViewById(R.id.connect); 
     //buttonClear = (Button)findViewById(R.id.clear); 
     textResponse = (TextView) findViewById(R.id.textData); 
     buttonConnect.setOnClickListener(buttonConnectOnClickListener); 

    } 

      OnClickListener buttonConnectOnClickListener = new OnClickListener(){ 

        @Override 
        public void onClick(View arg0) { 
         MyClientTask myClientTask = new MyClientTask(editTextAddress.getText().toString(), Integer.parseInt(editTextPort.getText().toString())); 
         myClientTask.execute(); 
        }}; 


     //class to get data from esp8266 
     public class MyClientTask extends AsyncTask<Void, Void, Void> { 

      String dstAddress; 
      int dstPort; 
      String response = ""; 

      MyClientTask(String addr, int port) { 
       dstAddress = addr; 
       dstPort = port; 
      } 

      @Override 
      protected Void doInBackground(Void... arg0) { 

       Socket socket = null; 

       try { 
        socket = new Socket(dstAddress, dstPort); 

        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(1024); 
        byte[] buffer = new byte[1024]; 
        int bytesRead; 
        InputStream inputStream = socket.getInputStream(); 

    /* 
    * notice: 
    * inputStream.read() will block if no data return 
    */ 
        while ((bytesRead = inputStream.read(buffer)) != -1) { 
         byteArrayOutputStream.write(buffer, 0, bytesRead); 
         response += byteArrayOutputStream.toString("UTF-8"); 

        } 

       } catch (UnknownHostException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
        response = "UnknownHostException: " + e.toString(); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
        response = "IOException: " + e.toString(); 
       } finally { 
        if (socket != null) { 
         try { 
          socket.close(); 
         } catch (IOException e) { 
          // TODO Auto-generated catch block 
          e.printStackTrace(); 
         } 
        } 
       } 
       return null; 
      } 

      @Override 
      protected void onPostExecute(Void result) { 
       textResponse.setText(response); 
       super.onPostExecute(result); 

      } 

     } 



} 

내 안드로이드 스튜디오 코드는 0 내 문제는이 페이지에 비슷한입니다 :

http://stackoverflow.com/questions/34274774/esp8266-wifi-server-to-android-client 

하지만 난 단서가있다 didnt는 생각 중 후 데이터를 전송 다시 연결하는 방법

내가 안드로이드 응용 프로그램에 연결을 클릭 한 후 프로그램 실행

내 일련 아두 이노에 내가

AT+CIPSEND = 0,2 

그때 난 내 안드로이드로 데이터 "AB"를 보내

+ COMMAND AT 보낸 후

,

는 데이터를 보내지 만 II 경우 내 질문에 우리가 지속적인 얻을 수있다 ESP

에서 데이터를 얻기 위해 다시 연결 데이터가 전송 된

AT+CIPCLOSE=0 

+ COMMAND AT 보내,하지만 난 클릭해야되지 않았습니다 연결 후 데이터? 어떻게 arduino가 메시지를 보내고 나면 다시 연결하기 위해 안드로이드를 사용한다고 가정 해 보겠습니다.

답변

0

이것은 매번 연결을 종료하는 것 외에 다른 방법이 없다고 생각하기 때문에 대답이 아니라 조언입니다.

하지만 AT 명령을 사용하지 말고 ESP8266 용 소프트웨어를 사용하는 것이 좋습니다. 시작, 네트워크 연결 등이 훌륭합니다. 그러나 그들은 제한되어 있습니다. allduino를 이미 사용하고 있기 때문에 IDE를 사용하여 ESP 용 코드를 작성하는 것이 좋습니다. 많은 코드를 찾아서 여기에서 도움을 얻을 수 있습니다. https://create.arduino.cc/projecthub/Metavix/programming-the-esp8266-with-the-arduino-ide-in-3-simple-601c16

그리고 ESP8266WiFi.h는 사용할 수있는 많은 기능을 제공합니다.