Websever_Client_03
#include <ESP8266WiFi.h>
#include <DHT11.h>
const char* ssid = "android1234";// 자신의 무선공유기 명
const char* password = "dddddddddd";//자신의 무선공유기 비밀번호
String apiKey = "0FKP99L1W0DXYESP";//자신의 Thingspeak API 키 값
const char* sitenm = "api.thingspeak.com";
long interval = 20000;
int ledPin = 2; //
int sent = 0;
int pin = D5;
long previousMillis = 0;
boolean dhtState = false; //false: temp true: humi
DHT11 dht11(pin);
WiFiServer server(80);
void setup() {
Serial.begin(115200);
delay(10);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, HIGH);
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(100);
Serial.println("Setup done");
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// Start the server
server.begin();
Serial.println("Server started");
// Print the IP address
Serial.print("Use this URL to connect: ");
Serial.print("http://");
Serial.print(WiFi.localIP());
Serial.println("/");
}
void loop() {
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
return;
}
// Wait until the client sends some data
Serial.println("new client");
while(!client.available()){
delay(1);
}
// Read the first line of the request
String request = client.readStringUntil('\r');
Serial.println(request);
client.flush();
// Match the request
int value = LOW;
if (request.indexOf("/LED=ON") != -1) {
digitalWrite(ledPin, LOW);
value = HIGH;
}
if (request.indexOf("/LED=OFF") != -1) {
digitalWrite(ledPin, HIGH);
value = LOW;
}
// Return the response
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println(""); // do not forget this one
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.println("<body>");
client.print("Led pin is now: ");
if(value == HIGH) {
client.print(" on");
} else {
client.print("Off");
}
client.println("<br><br>");
client.println("<a href=\"/LED=ON\"\"><button>Turn on </button></a>");
client.println("<a href=\"/LED=OFF\"\"><button>Turn Off </button></a><br />");
client.println("</body>");
client.println("</html>");
delay(1);
Serial.println("Client disonnected");
Serial.println("");
unsigned long currentMillis = millis();
Serial.println(currentMillis);
if( currentMillis - previousMillis > interval ) {
previousMillis = currentMillis;
if( dhtState == false ) {
temperatureClient();
dhtState = true;
}
else {
humidityClient();
dhtState = false;
}
}
}
void temperatureClient() {
WiFiClient client;
int err;
float temp, humi;
if((err=dht11.read(humi, temp))==0)
{
Serial.print("temperature:");
Serial.print(temp);
Serial.println();
}
else
{
Serial.println();
Serial.print("Error No :");
Serial.print(err);
Serial.println();
}
// Send temperature data.
if (client.connect(sitenm, 80)) {
Serial.println("WiFi Client connected ");
String postStr = apiKey;
postStr += "&field1=";
postStr += String(temp);
postStr += "\r\n\r\n";
client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: " + apiKey + "\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(postStr.length());
client.print("\n\n");
client.print(postStr);
delay(100);
}//end if temperature
client.stop();
}//End of temperature Thingspeak
void humidityClient() {
WiFiClient client;
int err;
float temp, humi;
if((err=dht11.read(humi, temp))==0)
{
Serial.print(" humidity:");
Serial.print(humi);
Serial.println();
}
else
{
Serial.println();
Serial.print("Error No :");
Serial.print(err);
Serial.println();
}
//Send humidity data.
if (client.connect(sitenm, 80)) {
String humidStr = apiKey;
humidStr += "&field2=";
humidStr += String(humi);
humidStr += "\r\n\r\n";
client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: " + apiKey + "\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(humidStr.length());
client.print("\n\n");
client.print(humidStr);
delay(100);
}//end if
sent++;
client.stop();
}//End of humidity Thingspeak
'아두이노프로세싱 프로그래밍' 카테고리의 다른 글
아두이노 와이어링 & 와이파이 코딩 목차 파일 (0) | 2012.01.03 |
---|---|
Landscape 형 디지털 시계와 아날로그 시계가 포함된 HTML/JavaScript 계산기:I (0) | 2012.01.01 |
온습도계를 가진 아날로그시계 (0) | 2011.12.30 |
계산기+디지털시계+아날로그시계 HTML/JavaScript 코드 (0) | 2011.12.29 |
아두이노 NodeMCU 무선 와이파이 코딩: 아날로그 시계로 장식된 HTML/JavaScript 계산기 (0) | 2011.12.28 |