2017-09-24 14 views
0

Quelqu'un peut-il m'aider dans Comment envoyer des données de texte à esp8266 nœud mcu sur wifi.Envoyer des données de texte selon esp8266 codage

Esp8266 noeud arduino wich code mcu reçoit des données qui sont envoyées à partir de l'application Android avec Okhttp request->

#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);} 

    } 

Voici mon code Android app pour envoyer des données texte au noeud arduino mcu. Mais ça ne marche pas, aucun travail d'opération. dans ce premier code je reçois ip, la broche et le numéro de port, puis utiliser Okhttp demande pour envoyer des données dans AsyncTask <> Méthode 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 
    } 

}

quelqu'un peut me aider à comment envoyer des données de texte sur wifi à esp8266 arduino au noeud codage mcu de Arduino.

Répondre

0

Vous devez y exécuter un serveur Web afin d'obtenir des requêtes HTTP du côté Android, de sorte que du côté de l'ESP8266 vous pouvez choisir l'action appropriée. Il existe des bibliothèques pour Arduino comme l'ESP8266WebServer par exemple. Si vous souhaitez répondre à un mobile, vous devez également exécuter une bibliothèque client.