2017-09-24 155 views
0

任何人都可以帮助我如何只通过wifi发送文本数据到esp8266节点mcu。根据esp8266编码发送文本数据

Esp8266 Arduino的节点MCU代码至极越来越被从Android应用程序发送带有Okhttp请求 - 数据>

#include <SoftwareSerial.h> 
#define DEBUG true 

SoftwareSerial esp8266(2,3); // make RX Arduino line is pin 2, make TX Arduino line is pin 3. 
          // This means that you need to connect the TX line from the esp to the Arduino's pin 2 
          // and the RX line from the esp to the Arduino's pin 3 
#include <ESP8266WiFi.h> 
String voice; 



const char* ssid = "VISHAL J"; 
const char* password = "986713361190"; 
WiFiServer server(8000); 

void setup() 
{ 
    Serial.begin(9600); 
    esp8266.begin(9600); // your esp's baud rate might be different 



    //---- 1. Settings as Station - Connect to a WiFi network 
    Serial.println("Connecting to " + String(ssid)); 

    WiFi.mode(WIFI_STA);      // Config module as station only. 

    while (WiFi.status() != WL_CONNECTED) { 
    delay(500); 
    Serial.print("."); 
    } 
    Serial.println(""); 
    Serial.println("WiFi connected"); 
    Serial.println(WiFi.localIP()); 

    //---- 2. Settings as Access point - Create a private Wifi Network. Enable the next five lines to use module as Acces point 
    // Serial.print("Setting soft-AP ... ");     // Default IP: 192.168.4.1 
    // WiFi.mode(WIFI_AP);          // Config module as Acces point only. Set WiFi.mode(WIFI_AP_STA); to config module as Acces point and station 
    // boolean result = WiFi.softAP("NodeMCU", "12345678");  // SSID: NodeMCU Password:12345678 
    // if(result == true) Serial.println("Server Ready"); 
    // else Serial.println("Failed!"); 

    // ---- Start the server 
    server.begin(); 
    Serial.println("Server started"); 

    //---- Enter your setup code below 
    // connect a switch. On Virtuino panel add a Led to pin D5 





Serial.println(""); 
    Serial.println("WiFi connected"); 
    Serial.println(WiFi.localIP()); 

    //---- 2. Settings as Access point - Create a private Wifi Network. Enable the next five lines to use module as Acces point 
    // Serial.print("Setting soft-AP ... ");     // Default IP: 192.168.4.1 
    // WiFi.mode(WIFI_AP);          // Config module as Acces point only. Set WiFi.mode(WIFI_AP_STA); to config module as Acces point and station 
    // boolean result = WiFi.softAP("NodeMCU", "12345678");  // SSID: NodeMCU Password:12345678 
    // if(result == true) Serial.println("Server Ready"); 
    // else Serial.println("Failed!"); 

    // ---- Start the server 
    server.begin(); 
    Serial.println("Server started"); 

    } 




void loop() 
{ 
    while(esp8266.available()) // check if the esp is sending a message 
    { 


    delay(10); 
    char c= esp8266.read(); 
    if(c=='#') 
    {break; } 
    voice += c; 
    } 
    if (voice.length() > 0) { 
    Serial.println(voice);} 

    } 

这里是我的Android应用程序代码来发送文本数据的Arduino微控制器节点。但它不起作用,没有任何操作工作。在此代码首先我获得IP,引脚和端口号,然后使用Okhttp请求中的AsyncTask发送数据<> onBackground方法 - >

private class HttpRequestAsyncTask: AsyncTask<Void, Void, Void> 
    { 
     // declare variables needed 
     private var ipAddress: String? = null 
     private var portNumber: String? = null 
     private var parameter: String? = null 
     private var parameterValue: String? = null 
    constructor(parameterValue : String,ipAddress: String, portNumber: String, parameter: String){ 

     this.ipAddress = ipAddress 
     this.parameterValue = parameterValue 
     this.portNumber = portNumber 
     this.parameter = parameter 
    } 

    override fun doInBackground(vararg voids: Void): Void? 
    { 
     try { 
      val httpclient = OkHttpClient() // create an HTTP client 
      // define the URL e.g. http://myIpaddress:myport/?pin=13 (to toggle pin 13 for example) 
      val request = Request.Builder().url("http://$ipAddress:$portNumber/?$parameter=$parameterValue#").build() 
      httpclient.newCall(request).enqueue(object : Callback{ 
       override fun onFailure(call: Call?, e: IOException?) { 
        e!!.printStackTrace() 
       } 
       override fun onResponse(call: Call?, response: Response?) { 
        if(!(response!!.isSuccessful)){ 
         throw IOException("Unexpected code $response") 
        } 
       } 
      }) 
     } 
     catch (e : Exception){ 
      e.printStackTrace() 
     } 
     catch (e: IOException) { 
      // IO error 
      e.printStackTrace() 
     } catch (e: URISyntaxException) { 
      // URL syntax error 
      e.printStackTrace() 
     } 
     return null 
    } 

}

任何一个可以帮助我如何通过wifi发送文本数据到esp8266 arduino到arduino的节点mcu编码。

回答

0

您必须在那里运行Web服务器才能从Android端获取HTTP请求,以便在ESP8266端您可以选择适当的操作。例如,有一些Arduino库,如ESP8266WebServer。如果您想要回复某些移动设备,则还需要运行一个客户端库。