本帖最后由 ilychw 于 2019-3-26 19:36 编辑
本人用W5500来上传温湿度数据到服务器,刚开始是可以上传数据的,但不定期(有时几个小时,有时一天或两天)就会出现用Ethernet.localIP()获取到的IP显示是0.0.0.0,然后上传不了数据,或者IP显示是正常的的,也可以PING通这个IP,但就是上传不了数据,重新初始化也是不行,LCD显示屏会出现花屏等现象。以为计算机的USB口电压不够,就用了手机的充电插头,也是一样的情况。真的没办法了。[mw_shl_code=arduino,true]
#include
#include
#include
#include
#include
#include
#include //定时器库的头文件
#define dht11pin 8 //DHT11的DATA脚
#define W5500CS 10 //W5500的CS脚
bool showtime = false; //是否显示时间
LiquidCrystal lcd(4, 5, A0, A1, A2, A3); //对应LCD1602的RS, E, D4, D5, D6, D7
DS3231 Clock; //DS3231的SDA接A4,SCL接A5
bool h12, PM, Century = false;
int p = 6; //LCD1602的V0脚
dht11 DHT11;
char inString[27];
int stringPos = 0;
boolean startRead = false; //是否开始读取服务器返回的数据
float temp = 0; //保存温度
float hum = 0; //保存湿度
float jg = 20; //发送内容的间隔
String sp = " ";
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress ip(192, 168, 0, 109);
IPAddress myDns(192, 168, 0, 1);
IPAddress gateway(192, 168, 0, 1);
IPAddress subnet(255, 255, 255, 0);
char server[] = "192.168.0.2"; //服务器地址
EthernetClient client;
void(* resetFunc)(void) = 0; //制造重启命令
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Wire.begin();
pinMode(p, OUTPUT);
analogWrite(p, 110);
lcd.begin(16, 2);
lcd.clear();
Ethernet.init(W5500CS); //初始化W5500的CS脚对应的D10
Ethernet.begin(mac, ip, myDns, gateway, subnet); //W5500的CS接D10,SI接D11,SO接D12,SCK接D13
delay(1000);
setDateTime();
getTempHum();
showData(temp, hum);
sendData();
//client.flush();
MsTimer2::set(jg * 1000 * 60, sendData); //中断设置函数,每jg分钟进入一次中断
MsTimer2::start(); //开始计时
}
void loop() {
// put your main code here, to run repeatedly:
if(Ethernet.localIP()[0] == '0'){
Ethernet.init(W5500CS);
Ethernet.begin(mac, ip, myDns, gateway, subnet);
delay(500);
}
getTempHum();
showData(temp, hum);
}
void setDateTime(){
Serial.println("connecting...");
if(client.connect(server, 80)){
Serial.println("connected");
client.println("GET /tempHum/getTime.aspx HTTP/1.0");
client.println();
}
else{
Serial.println("connection failed");
}
Serial.println(Ethernet.localIP());
lcd.setCursor(0, 0);
lcd.print(Ethernet.localIP());
lcd.setCursor(0, 1);
lcd.print("Initialization.");
Serial.println(readPage());
/*for(int i = 3; i < readPage().length() - 4; i ++){
Serial.print(readPage());
}*/
Serial.println();
String year = (String)readPage()[3] + (String)readPage()[4] + (String)readPage()[5] + (String)readPage()[6];
String month = (String)readPage()[8] + (String)readPage()[9];
String day = ((String)readPage()[11] + (String)readPage()[12]);
String hour = ((String)readPage()[14] + (String)readPage()[15]);
String minute = ((String)readPage()[17] + (String)readPage()[18]);
String second = ((String)readPage()[20] + (String)readPage()[21]);
Clock.setYear(year.toInt() - 2000);
Clock.setMonth(month.toInt());
Clock.setDate(day.toInt());
Clock.setHour(hour.toInt());
Clock.setMinute(minute.toInt());
Clock.setSecond(second.toInt());
client.stop();
client.flush();
}
String readPage(){
while(true){
if(client.available()){
char c = client.read();
if(c == 'YYYY-MM-DD HH:mm:SS
startRead = true; //可以读取数据
}
if(startRead){
inString[stringPos] = c; //将读取到的字节保存
stringPos++; //记数加1
}
}
else{
return inString;
}
}
}
void getTempHum(){
int chk = DHT11.read(dht11pin); //读取DHT11的数据
temp = (float)DHT11.temperature; //保存读取的温度
hum = (float)DHT11.humidity; //保存读取的湿度
}
void showData(float temp, float hum){
int hour = Clock.getHour(h12, PM); //以24小时制显示小时
int minute = Clock.getMinute();
int second = Clock.getSecond();
int n = second % 10;
int m = second / 10;
if(showtime)
{
lcd.setCursor(0, 0);
lcd.print("20");
lcd.print(Clock.getYear());
lcd.print("-");
//if(Clock.getMonth(Century) < 10){
// print.print("0");
//}
lcd.print(Clock.getMonth(Century));
lcd.print("-");
lcd.print(Clock.getDate());
if((Clock.getMonth(Century) < 10) && (Clock.getDate() < 10)){
sp = " ";
}
else if((Clock.getMonth(Century) > 9) && (Clock.getDate() > 9)){
sp = " ";
}
else{
sp = " ";
}
lcd.print(sp);
lcd.setCursor(11, 0);
lcd.print(temp);
lcd.setCursor(15, 0);
lcd.print("C");
lcd.setCursor(0, 1);
lcd.print(Clock.getHour(h12, PM));
lcd.print(":");
if(Clock.getMinute() < 10){
lcd.print("0");
}
lcd.print(Clock.getMinute());
lcd.print(":");
if(Clock.getSecond() < 10){
lcd.print("0");
}
lcd.print(Clock.getSecond());
lcd.print(" ");
lcd.setCursor(11, 1);
lcd.print(hum);
lcd.setCursor(15, 1);
lcd.print("%");
}
else{
lcd.setCursor(0, 0);
lcd.print(Ethernet.localIP());
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print(temp);
lcd.setCursor(4, 1);
lcd.print("C");
lcd.print(" ");
lcd.setCursor(11, 1);
lcd.print(hum);
lcd.setCursor(15, 1);
lcd.print("%");
}
if((m % 2 == 0) && n == 0){ //秒数是否被2整除
showtime = false;
}
else if((m % 2 != 0) && n == 0){
showtime = true;
}
if((minute == 0) && (second == 0)){
setDateTime();
delay(1000);
}
}
void sendData(){
String url = "GET /tempHum/Add.aspx?ip="; //拼接url
url.concat(Ethernet.localIP()[0]);
url.concat(".");
url.concat(Ethernet.localIP()[1]);
url.concat(".");
url.concat(Ethernet.localIP()[2]);
url.concat(".");
url.concat(Ethernet.localIP()[3]);
url.concat("&mac=");
String strMac = String(mac[0], HEX);
strMac.concat(String(mac[1], HEX));
strMac.concat("-");
strMac.concat(String(mac[2], HEX));
strMac.concat(String(mac[3], HEX));
strMac.concat("-");
strMac.concat(String(mac[4], HEX));
strMac.concat(String(mac[5], HEX));
url.concat(strMac);
url.concat("&temp=");
url.concat(temp);
url.concat("&hum=");
url.concat(hum);
url.concat(" HTTP/1.0");
if(client.connect(server, 80)){
Serial.println(url);
client.println(url);
client.println();
}
else{
Serial.println("connection failed");
Ethernet.init(W5500CS);
delay(1000);
}
client.stop();
client.flush();
}
[/mw_shl_code]