|
各位前輩好我目前正在學習撰寫Arduino程式
利用DHT22偵測溫溼度
搭配使用pushingbox做通知
在W5100的連線上遇到問題
還請各位協助
最下面是我的部份程式
及搭配pushingbox的範例
目前遇到有問題的是在連線至pushingbox的部份
因為我是以一個實體固定IP讓Arduino直接連線
所以需要DNS解析pushingbox的IP
但是如果我將程式碼DNS的部分拿掉
在serial monitor裡面還是會看到出現下面的訊息
-----------------------------------------------------
connecting...
connected
sending request
-----------------------------------------------------
但是因IP無法解析訊息並不會被送出
為甚麼還是會出現connected的訊息
而不是connection failed
謝謝各位前輩
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xFE, 0xEF, 0xED };
byte ip[] = {
122, AAA, BBB, CCC };
byte gateway[] = {
122, AAA, BBB, 254 };
byte subnet[] = {
255, 255, 255, 0 };
IPAddress dnServer(8,8,8,8);
char DEVID1[] = "v0123456ABCDEFGH";
uint8_t pinDevid1 = 3
// Debug mode
boolean DEBUG = true;
char serverName[] = "api.pushingbox.com";
boolean pinDevid1State = false; // Save the last state of the Pin for DEVID1
boolean lastConnected = false; // State of the connection last time through the main loop
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;
void setup() {
Serial.begin(9600);
Ethernet.begin(mac,ip,dnServer,gateway,subnet);
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
delay(1000);
}
void loop()
{
////
// Listening for the pinDevid1 state
////
if (digitalRead(pinDevid1) == HIGH && pinDevid1State == false) // switch on pinDevid1 is ON
{
if(DEBUG){Serial.println("pinDevid1 is HIGH");}
pinDevid1State = true;
//Sending request to PushingBox when the pin is HIGHT
sendToPushingBox(DEVID1);
}
if (digitalRead(pinDevid1) == LOW && pinDevid1State == true) // switch on pinDevid1 is OFF
{
if(DEBUG){Serial.println("pinDevid1 is LOW");}
pinDevid1State = false;
//Sending request to PushingBox when the pin is LOW
//sendToPushingBox(DEVID1); //Here you can run an other scenario by creating a DEVID2 variable
}
//DEBUG part
// this write the respons from PushingBox Server.
// You should see a "200 OK"
if (client.available()) {
char c = client.read();
if(DEBUG){Serial.print(c);}
}
// if there's no net connection, but there was one last time
// through the loop, then stop the client:
if (!client.connected() && lastConnected) {
if(DEBUG){Serial.println();}
if(DEBUG){Serial.println("disconnecting.");}
client.stop();
}
lastConnected = client.connected();
}
//Function for sending the request to PushingBox
void sendToPushingBox(char devid[]){
client.stop();
if(DEBUG){Serial.println("connecting...");}
if (client.connect(serverName, 80)) {
if(DEBUG){Serial.println("connected");}
if(DEBUG){Serial.println("sending request");}
client.print("GET /pushingbox?devid=");
client.print(devid);
client.println(" HTTP/1.1");
client.print("Host: ");
client.println(serverName);
client.println("User-Agent: Arduino");
client.println();
}
else {
if(DEBUG){Serial.println("connection failed");}
}
} |
|