发送可靠的HTTP请求与Arduino的WiFi客户端

问题描述:

当我的Arduino MKR1000传感器被触发时,我发送一封电子邮件:发送可靠的HTTP请求与Arduino的WiFi客户端

//.... 
WiFiClient client; 
char server[] = "www.myserver.com"; 
//.... 

void setup() { 
//... Connect to wifi .... 
} 

void loop() { 
    // when sensor triggered => 
    if (client.connect(server, 80)) { 
     Serial.println("Sending email"); 
     client.print("GET /WaterOff/sendGridCall.php"); 
     client.print("?valve="); 
     client.print(valve); 
     client.print("&position="); 
     client.print(position); 
     client.println(" HTTP/1.1"); 
     client.print("Host: "); 
     client.println(server); 
     client.println("User-Agent: Arduino"); 
     client.println("Connection: close"); 
     client.println(); 
    } 

    delay(6000); 
} 

然而,它不仅工程首几次(“发送电子邮件”印)之后,它不会执行请求(“发送电子邮件”不会被打印)。我应该采取一些额外的步骤来使其可靠。

+0

也许你需要关闭与'client.stop()'的连接? –

+0

似乎没有帮助。 – Kashif

+0

你有没有尝试做另一个func()并从loop()调用这个func? –

您从不关闭连接。但阻止等待电子邮件确认可能会永远持续下去。你应该考虑一个小型的状态机来让你的网络连接起作用。

void loop() 
{ 
    // when sensor triggered => 
    if (client.connect(server, 80)) {  // You connect to client, but it's already 
              // connected the second time around 
     // send ... 
     // client() should wait for response, and client.disconnect() 
    } 

    // ... 
    delay(6000);  // tip: avoid blocking the main loop, this blocks your ability to 
         // read your sensor while sending emails. 
} 

为您的应用状态机看起来是这样的:

enum EmailState { 
    emIdle, emSendMail, emCheckMailSent, emEmailOK, emEmailError, 
}; 

EmailState email_state; 
unsigned long long mail_timer;  // can be shared between states. 

// small state handlers that do a bit at a time, 
// check for an event, wait on some delay (use millis()) 
// avoid blocking the instruction flow as much as possible. 

static void on_emIdle() 
{ 
    // read sensor, check timer? 
    email_state = (triggered) ? emSendMail : emIdle; 
} 

static void on_sendMail() 
{ 
    if (client.connect()) 
    { 
     // ... prints 
     email_state = emCheckMailSent; 
     return; 
    } 
    email_state = emEmailError; 
} 

static void on_emCheckMailSent() 
{ 
    // receive HTTP response... can have a long timeout without blocking. 
    email_state = emCheckMailSent; 
    // or emEmailOK or emEmailError or retry emSendMail 
} 

static void on_emEmailOK() 
{ 
    if (/*client is connected?*/) 
     client.disconnect(); 

    if (!mail_timer) 
     mail_timer = millis(); 

    if (millis() - mail_timer < 6000) // stay in this state for 6 seconds... 
    { 
     email_state = emEmailOK; 
     return; 
    } 

    mail_timer = 0;  // always reset the timer so it can be used by other states 
    email_state = emIdle; 
} 

static void on_emEmailError() 
{ 
    if (/*client is connected?*/) 
     client.disconnect(); 
    // keep a log ? 
    email_state = emIdle; // or another timer 
} 

//... 
void loop() 
{ 
    // update readings, manage other things... 
    // ... 
    switch(email_state) 
    { 
    default: 
    case emIdle:   on_emIdle();   break; 
    case emSendMail:  on_emSendMail();  break; 
    case emCheckMailSent: on_emCheckMailSent(); break; 
    case emEmailOK:  on_emEmailOK();  break; 
    case emEmailError:  on_emEmailError(); break; 
    } 
}