0
w6012358

您好,我練習著改掉Example中的WiFiWebClient,將上傳目標網址改成dweet.io,但固定會在第16筆上傳的時候就會出現ERROR opening socket,想請問這是哪方面的問題,謝謝

以下是我的程式碼:

#include <MP3Player_KT403A.h>

#include <WiFi.h>
#include <WiFiClient.h>
#include <WiFiServer.h>
#include <WiFiSSLClient.h>
#include <WiFiUdp.h>

#include <I2CIO.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <SoftwareSerial.h>
#include <SPI.h>

char ssid[] = "SSID";
char pass[] = "pasword";for WEP)
int keyIndex = 0;
int status = WL_IDLE_STATUS;
WiFiClient client;
char server[] = "www.dweet.io";
String IP = "52.3.188.112";
const uint16_t port = 80;
const char * host = "52.3.188.112";

void WiFi_INI(){
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
// don't continue:
while (true);
}

// attempt to connect to Wifi network:
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);

// wait 1 seconds for connection:
delay(1000);
}
Serial.println("Connected to wifi");
printWifiStatus();
}
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
WiFi_INI();
}

int n = 0;
void loop() {

Serial.print("n = ");
Serial.println(n);
if (client.connect(server, 80)) {

Serial.println("connected to server");
String s = "POST /dweet/for/amebatest?data=";
s.concat(n);
String s1 = s;
s1.concat(" HTTP/1.1");
Serial.println(s1);
client.println(s1);

client.println("Host: www.dweet.io");
client.println("Connection: close");
client.println();
n+=10;
delay(1000);
}
}

void printWifiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());

// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);

}

sean 回答了