我想每个人都会认为这很奇怪,实际上它确实很奇怪。我一直困扰并尝试一切可能的方法来解决这个问题。问题是,如果我在薄膜键盘上按键后删除执行方法的逻辑,则会成功执行HTTP post方法。它不与服务器连接。但是一旦我按下按键并将逻辑放入流程后删除执行,它将产生一个成功的post方法 以下是代码。非常感谢任何帮助。
//Include libraries
#include <SPI.h>
#include <WiFi.h>
#include <Keypad.h>
#include <LiquidCrystal.h>
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {52, 26, 48, 46};
byte colPins[COLS] = {42, 40, 38, 36};
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
LiquidCrystal lcd(45, 43, 37, 35, 33, 31);
WiFiClient client;
// EDIT: 'Server' address to match your domain
char server[] = "https://e-medic.herokuapp.com";
#include <system.h>
void setup() {
Serial.begin(9600);
//wifi connect (I have not put the method but wifi will be connected)
connectWifi();
printWifiStatus();
//lcd start
lcd.begin(16, 2);
lcdPrint(0);
initialState();
}
void loop() {
//checking whether the client sends some data back
while (client.available()) {
char c = client.read();
Serial.write(c);
}
}
void initialState(char key) {
char key = keypad.getKey();
while (key != 'A') {
Serial.println("you havent press A");
key = keypad.getKey();
}
Serial.println("Start of Post");
// If there's a successful connection, send the HTTP POST request
value1 = "test";
//Appending the data to a variable to be posted.
String http_content = "temp=" + value1;
Serial.println("before connecting to server...");
//checking whether the client is connected
//client will not make connection with server but once I remove the
//keypad press it will connect to server
if (client.connect(server, 80)) {
Serial.println("after...");
Serial.println("connecting. now inside connected.");
client.println("POST /api/uploadPatientData HTTP/1.1");
// EDIT: 'Host' to match your domain
client.println("Host: e-medic.herokuapp.com");
client.println("User-Agent: Arduino/1.0");
client.println("Connection: close");
client.println("Content-Type: application/x-www-form-urlencoded;");
client.print("Content-Length: ");
client.println(http_content.length());
Serial.println(http_content.length());
client.println();
client.println(http_content);
delay(5000);
} else {
// If you couldn't make a connection:
Serial.println("Connection failed");
Serial.println("Disconnecting.");
client.stop();
}
char c = client.read();
Serial.write(c);
}