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編碼。