Created
September 18, 2016 04:50
-
-
Save zinntikumugai/352224363de842cdfd8a6e64ca86aec9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//LAN | |
#include <Ethernet.h> | |
#include <SPI.h> | |
//DHTsernser | |
#include <DHT.h> | |
/*//////////////////////////// | |
* LANシールド | |
* 4, 10, 11, 12, 13 | |
* LAN: 10 | |
* SD: 4 | |
* 共通: 11, 12, 13 | |
*///////////////////////////// | |
//////// | |
//LAN Setting | |
/////// | |
byte mac[] = { | |
0x90, 0xA2, 0xDA, 0x74, 0x3D, 0x5D | |
}; | |
IPAddress ip(192, 168, 1, 5); | |
EthernetServer server(80); | |
/////// | |
//DHT Setting | |
/////// | |
#define DHTPIN 2 | |
#define DHTTYPE 11 | |
DHT dht(DHTPIN, DHTTYPE); | |
void setup() { | |
Serial.begin(9600); | |
while(!Serial) { | |
; // wait for serial port to connect. Needed for native USB port only | |
} | |
Serial.print("Starting Server..."); | |
Ethernet.begin(mac, ip); | |
dht.begin(); | |
Serial.println("ok"); | |
Serial.print("Server IPAdress :"); | |
Serial.println(Ethernet.localIP()); | |
} | |
void loop() { | |
EthernetClient client = server.available(); | |
if( client ) { | |
Serial.println("New Client.."); | |
boolean currentLineIsBlank = true; | |
while( client.connected() ) { | |
if( client.available() ) { | |
char c = client.read(); | |
Serial.write(c); | |
//DHTのデータを取得 | |
float h = dht.readHumidity(); | |
float t = dht.readTemperature(); | |
float f = dht.readTemperature(true); | |
boolean dhtcan = true; | |
if (isnan(h) || isnan(t) || isnan(f)) { | |
dhtcan = false; | |
} | |
float hif = dht.computeHeatIndex(f, h); | |
float hic = dht.computeHeatIndex(t, h, false); | |
if( c == '\n' && currentLineIsBlank ) { | |
client.println("HTTP/1.1 200 OK"); | |
client.println("Content-Type: application/json"); | |
client.println("charset=utf-8"); | |
client.println("Connection: close"); | |
client.println(); | |
client.print("{"); | |
client.print("\"ipaddress\":\""); | |
client.print(Ethernet.localIP()); | |
client.print("\",\"macaddress\":\""); | |
// | |
for( int cnt=0; cnt < 4; cnt++ ) { | |
client.print("0x"); | |
client.print(mac[cnt], HEX); | |
if( cnt < 3 ) { | |
client.print(", "); | |
} | |
} | |
client.print("\",\"dht\": {\"dhttype\":\""); | |
client.print(DHTTYPE); | |
client.print("\","); | |
if( dhtcan ) { | |
client.print("\"humidity\":\""); | |
client.print(h); | |
client.print("\",\"temperature\":{\"c\":\""); | |
client.print(t); | |
client.print("\",\"f\":\""); | |
client.print(f); | |
client.print("\"},\"heat\":{\"c\":\""); | |
client.print(hic); | |
client.print("\",\"f\":\""); | |
client.print(hif); | |
client.print("\"}"); | |
}else { | |
client.print("\"error\":\"null\""); | |
} | |
client.println("}}"); | |
break; | |
} | |
if( c == '\n' ) { | |
currentLineIsBlank = true; | |
} else if( c != '\r' ) { | |
currentLineIsBlank = false; | |
} | |
} | |
} | |
delay(1); | |
client.stop(); | |
Serial.println("Client Disconnected"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment