|
//#include <ESP8266WiFi.h>
#include <WiFi.h>
#include "Arduino.h"
#include "DHT.h"
//#include <DHT22.h>
#define DHTPIN 33
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
//////////////////////////////////////==================================================================================================
const int potPin = 26;
const int ldrPin = 27;
const int pot1Pin = 14;
int potValue = 0;
int pot1Value = 0;
float ldr1=0;
//////////////////////////////////////==================================================================================================
const int potPin1 = 15;
const int potPin2 = 2;
const int potPin3 = 25;
const int potPin5 = 34;
const int potPin4 = 35;
////////////////////////ประกาศส่วนสมาทวาล์ว=====================================================================================================
int led1=0; ///start,stop =15
int led2=0;///Mortor =2
int led3=0;///25
int swit1=0;///34
int swit2=0;//35
////////////////////////=================================================================================================================
const char* ssid = "IoT99"; // Enter SSID here
const char* password = "iot0837286389"; //Enter Password here
// Set dht12 i2c comunication on default Wire pin
//DHT12 dht12;
WiFiClient client;
int timer = 0;
const char* host = "colo2u.com"; //ใส่ IP หรือ Host ของเครื่อง Database ก็ได้
String temperature = "2"; //ตัวแปรที่ต้องการจะส่ง อุณหภูมิ
String humid = "5"; //ตัวแปรที่ต้องการจะส่ง ความชื้น
/////////////////////อินเตอร์รับ////////////////////================================================
volatile int interruptCounter = 0;
int numberOfInterrupts = 0;
portMUX_TYPE mux = portMUX_INITIALIZER_UNLOCKED;
void IRAM_ATTR handleInterrupt() {
portENTER_CRITICAL_ISR(&mux);
interruptCounter++;
portEXIT_CRITICAL_ISR(&mux);
Serial.println("interrupt");
digitalWrite(potPin1,LOW);
}
///////////////////////////////////////====================================================================
void setup()
{
if (swit1==1 && swit2==0){ //เซ็ตวาล์วเปิดอยู่หรือเปล่า
digitalWrite (potPin2,HIGH);//สลับทิศทางหมุน
digitalWrite (potPin1,HIGH);//สั่งมอร์เตอร์หมุน
Serial.print("Valve ON");
}
digitalWrite (potPin1,HIGH);//สั่งมอร์เตอร์หมุน
////////////////////////ส่วนสมาทวาล์ว======================================================================================
pinMode(potPin1,OUTPUT);
pinMode(potPin2,OUTPUT);
pinMode(potPin3,OUTPUT);
pinMode(potPin4,INPUT_PULLUP);
pinMode(potPin5,INPUT_PULLUP);
////////////////////////=================================================================================================================
/////////////////////อินเตอร์รับ interrupe////////////////////================================================
attachInterrupt(digitalPinToInterrupt(potPin4), handleInterrupt, FALLING);
attachInterrupt(digitalPinToInterrupt(potPin5), handleInterrupt, FALLING);
///////////////////////////////////////====================================================================
// Start sensor handshake
dht.begin();
Serial.begin(115200);
Serial.println("Starting...");
if (WiFi.begin(ssid, password)) {
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
}
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
int timeSinceLastRead = 0;
void loop()
{
/////////////////////อินเตอร์รับ interrupe////////////////////================================================
if(interruptCounter>0){
portENTER_CRITICAL(&mux);
interruptCounter--;
portEXIT_CRITICAL(&mux);
numberOfInterrupts++;
Serial.print("An interrupt has occurred. Total: ");
Serial.println(numberOfInterrupts);
}
///////////////////////////////////////====================================================================
////////////////////////////////////////================================================================
potValue = analogRead(potPin);
ldr1 = analogRead(ldrPin)*107/7;
Serial.println(potValue);
Serial.println(ldr1);
////////////////////////////////////////=================================================================
// Report every 2 seconds.
if(timeSinceLastRead > 2000) {
// Reading temperature or humidity takes about 250 milliseconds!
// Read temperature as Celsius (the default)
float t12 = dht.readTemperature();
// Read temperature as Fahrenheit (isFahrenheit = true)
float f12 = dht.readTemperature(true);
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h12 = dht.readHumidity();
bool dht12Read = true;
// Check if any reads failed and exit early (to try again).
if (isnan(h12) || isnan(t12) || isnan(f12)) {
Serial.println("Failed to read from DHT12 sensor!");
dht12Read = false;
}
if (dht12Read){
// Compute heat index in Fahrenheit (the default)
float hif12 = dht.computeHeatIndex(f12, h12);
// Compute heat index in Celsius (isFahreheit = false)
float hic12 = dht.computeHeatIndex(t12, h12, false);
// Compute dew point in Fahrenheit (the default)
//float dpf12 = dht.dewPoint(f12, h12);
// Compute dew point in Celsius (isFahreheit = false)
//float dpc12 = dht.dewPoint(t12, h12, false);
Serial.print("DHT12=> Humidity: ");
Serial.print(h12);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(t12);
Serial.print(" *C ");
Serial.print(f12);
Serial.print(" *F\t");
Serial.print(" Heat index: ");
Serial.print(hic12);
Serial.print(" *C ");
Serial.print(hif12);
Serial.print(" *F");
Serial.print(" Dew point: ");
//Serial.print(dpc12);
Serial.print(" *C ");
//Serial.print(dpf12);
Serial.println(" *F");
}
timeSinceLastRead = 0;
const int httpPort = 80;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
delay(5000);
/////////////////////////////////////////////อ่านพอร์ทของสวิซส์================================================================================================
/* if (swit1==1 && swit2==0){
digitalWrite (potPin3,HIGH);
Serial.print("Valve ON");
}else {
digitalWrite (potPin3,LOW);
Serial.print("Valve OFF");
}
*/
///////////////////////////////////////////////เป็นการเพิ่มค่าในการส่งค่าขึ้นเซิฟเวอร์และสามารถเพิ่มได้==========================================================================================================
humid = String(h12);
temperature = String(t12);
String url = "/iot/update.php?L5="+String(led1);
url += "&L6="+String(led2); //ส่งค่าตัวแปร
url += "&L7="+String(swit1); //ส่งค่าตัวแปร
url += "&L8="+String(swit2); //ส่งค่าตัวแปร
url += "&L9="+String(t12); //ส่งค่าตัวแปร
url += "&L10="+String(h12); //ส่งค่าตัวแปร เพิ่มตัวแปร
////////////////////////////////////////////////===========================================================================================================================================
Serial.print("Requesting URL: ");
Serial.println(url);
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
unsigned long timeout = millis();
while (client.available() == 0) {
if (millis() - timeout > 5000) {
Serial.println(">>> Client Timeout !");
client.stop();
return;
}
}
}
delay(100);
timeSinceLastRead += 100;
} |
|