根据esp8266编码发送文本数据

时间:2017-09-24 17:04:51

标签: android arduino wifi esp8266

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

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 node mcu。但它不起作用而不是任何操作工作。在这段代码中我首先获得ip,pin和端口号,然后使用Okhttp请求在AsyncTask中发送数据&lt;&gt; onBackground方法.-&gt;

   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编码。

1 个答案:

答案 0 :(得分:0)

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