【问题标题】:Sending text data according to esp8266 coding根据esp8266编码发送文本数据
【发布时间】:2017-09-24 17:04:50
【问题描述】:

任何人都可以帮助我如何通过 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 节点 mcu。但它不工作没有任何操作工作。在这段代码中,我首先获取 ip、pin 和端口号,然后使用 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 编码。

【问题讨论】:

    标签: android arduino wifi esp8266


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-23
      • 1970-01-01
      相关资源
      最近更新 更多