2017-05-02 2 views
0

Je ne suis pas familier avec arduino et esp8266. J'avais développé une ASP.NET WebAPI qui reçoit des données de urlEnvoyer une demande Post à ASP.NET Restful Web API à partir d'Arduino en utilisant esp8266

http://iotworkshop.somee.com/api/Data/PostData?Temperature=67 

j'avais testé mon API de Fiddler, tout fonctionne bien. Maintenant, je dois envoyer une requête http à mon API de arduino et esp8266 module wifi. J'avais cherché beaucoup de choses mais rien ne m'aide, la plupart du temps les gens envoient des demandes de HTTP aux pages de php plus je ne veux pas programmer mon esp8266 directement. Je veux envoyer une requête POST de telle sorte que arduino envoie les commandes AT à esp8266.

Après la recherche de quelques jours, j'écrit le code suivant

`#include <SoftwareSerial.h> 

#define DEBUG true 

SoftwareSerial esp8266(3,2); 
void setup() 
{ 
Serial.begin(9600); 
esp8266.begin(115200); // your esp's baud rate might be different 
sendData("AT+RST\r\n",2000,DEBUG); // reset module 
sendData("AT+CWMODE=1\r\n",1000,DEBUG); 
sendData("AT+CWJAP=\"Wifi\",\"blackday\"\r\n",9000,DEBUG); 
sendData("AT+CIPSTART=\"TCP\",\"iotworkshop.somee.com\",80\r\n", 
5000,DEBUG); 
String toSend = "POST /api/Data/PostData?Temperature=45 HTTP/1.1\r\n"; 
toSend += "Host: iotworkshop.somee.com\r\n" ; 
toSend += "User-Agent:Arduino\r\n"; 
toSend += "Accept-Version: ~0\r\n"; 
toSend += "Content-Type: application/json\r\n"; 

sendData("AT+CIPSEND="+toSend.length(),3000,DEBUG); 

sendData(toSend,3000,DEBUG); 
sendData("AT+CIPCLOSE", 1000, DEBUG); 
} 

void loop() 
{ 

} 


    String sendData(String command, const int timeout, boolean debug) 
{ 
    String response = ""; 

esp8266.print(command); // send the read character to the esp8266 

long int time = millis(); 

while((time+timeout) > millis()) 
{ 
    while(esp8266.available()) 
    { 

    // The esp has data so display its output to the serial window 
    char c = esp8266.read(); // read the next character. 
    response+=c; 
    } 
} 

if(debug) 
{ 
    Serial.print(response); 
} 

return response; 
}` 

Mais je reçois ci-dessous eteint.

AT+RST 


    OK 
    WIFI DISCONNECT 

    ets Jan 8 2013,rst c`use:2, boot mode:(3,7) 

load 0x40100000, len 1396, room 16 
t`il 4 
    chkstm 0x89 
loaf 0x3ffe8000, len 776, room tail 4 
    cmf0t`il 
so1ez p2r 
    ⸮⸮(j5⸮! 
    -⸮1%1⸮ 

    j 
    ⸮sl⸮⸮o⸮ 
    @i-Thinker Tdchnology Co/ Ltd. 

    invalhd 
    AT+CW⸮ODQOLCC⸮C⸮j⸮H⸮AT+CWJAP="Wifi","blackday" 

WIFI CONNDCTED 
WIFI GOT IP 

OK 
    AT+AIPSTART="TCP","iotworkshop.somee.com",80 

    CONOECT 

    OK 

    ⸮jE⸮)⸮⸮UW^Y⸮zure=44 PQA⸮⸮(Q⸮H⸮.]'Z]K⸮..⸮⸮V⸮⸮⸮HH׮Dɩ⸮ծYKPY]⸮P 
    ol 
    AT+CIPCLOSD 

Une aide ou un lien complet à cet égard?

Répondre

1

Enfin, après quelques jours, je communiquais avec succès arduino asp.net api reposant

#include "SoftwareSerial.h" 
String ssid ="PTCL-BB"; 

String password="1122334455"; 

SoftwareSerial esp(3,2);// RX, TX 

String data; 

String server = "myservername"; // www.example.com 

String uri = "/api/Data/PostData?Temperature=70"; 
void setup() { 


esp.begin(115200); 

Serial.begin(9600); 

reset(); 

    connectWifi(); 

} 
void reset() { 

esp.println("AT+RST"); 

delay(1000); 

if(esp.find("OK")) Serial.println("Module Reset"); 

} 
void connectWifi() { 

String cmd = "AT+CWJAP=\"" +ssid+"\",\"" + password + "\""; 

    esp.println(cmd); 

    delay(4000); 

if(esp.find("OK")) { 

    Serial.println("Connected!"); 

    } 

    else { 

        connectWifi(); 

    Serial.println("Cannot connect to wifi"); } 

     } 
    void loop() 
    { 
     senddata(); 
     delay(3000); 

     } 

     void senddata() 
    { 
    data = "temperature= 34"; 
     esp.println("AT+CIPSTART=\"TCP\",\"" + server + "\",80");//start a 
     TCP connection. 

    if(esp.find("OK")) { 

     Serial.println("TCP connection ready"); 

     } delay(1000); 

     String postRequest = 

     "POST " + uri + " HTTP/1.0\r\n" + 

     "Host: " + server + "\r\n" + 

     "Accept: *" + "/" + "*\r\n" + 

     "Content-Length: " + data.length() + "\r\n" + 

     "Content-Type: application/x-www-form-urlencoded\r\n" + 

     "\r\n" + data; 

     String sendCmd = "AT+CIPSEND=";//determine the number of c  
     aracters to be sent. 

     esp.print(sendCmd); 

     esp.println(postRequest.length()); 

     delay(500); 

     if(esp.find(">")) { Serial.println("Sending.."); 
    esp.print(postRequest); 

    if(esp.find("SEND OK")) { Serial.println("Packet sent"); 

     while (esp.available()) { 

     String tmpResp = esp.readString(); 

     Serial.println(tmpResp); 

     } 

     // close the connection 

     esp.println("AT+CIPCLOSE"); 

      } 

      } 

      }