//Its code
// Code to use SoftwareSerial
#include <SoftwareSerial.h>
SoftwareSerial espSerial = SoftwareSerial(2,3);// arduino RX pin=2 arduino TX pin=3 connect the arduino RX pin to esp8266 module TX pin - connect the arduino TX pin to esp8266 module RX pin
#include <DHT.h> //Attention: For new DHT11 version library you will need the Adafruit_Sensor library
//Download from here: https://github.com/adafruit/Adafruit_Sensor
// LCD RS=7, EN=8, DS4=9, DS5=10, DS6=11, DS7=12
#include <LiquidCrystal.h>
LiquidCrystal lcd( 7, 8, 9, 10, 11, 12);
//Attention: For new DHT11 version libraries you will need the Adafruit_Sensor library
//Download from here:https://github.com/adafruit/Adafruit_Sensor
//and install to Arduino software
#define DHTPIN 6 // Connect the signal pin of DHT11 sensor to digital pin 6
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
String apiKey = "CUGPJYT9VKOZM1RT"; // replace with your channel's thingspeak WRITE API key
String ssid="DESKTOP-KD"; // Wifi network SSID
String password ="912345678"; // Wifi network password
boolean DEBUG=true;
//======================================================================== showResponce
void showResponse(int waitTime){
long t=millis();
char c;
while (t+waitTime>millis()){
if (espSerial.available()){
c=espSerial.read();
if (DEBUG) Serial.print(c);
}
}
}
//========================================================================
boolean thingSpeakWrite(float value1, float value2){
String cmd = "AT+CIPSTART=\"TCP\",\""; // TCP connection
cmd += "184.106.153.149"; // api.thingspeak.com
cmd += "\",80";
espSerial.println(cmd);
if (DEBUG) Serial.println(cmd);
if(espSerial.find("Error")){
if (DEBUG) Serial.println("AT+CIPSTART error");
return false;
}
String getStr = "GET /update?api_key="; // prepare GET string
getStr += apiKey;
getStr +="&field1=";
getStr += String(value1);
getStr +="&field2=";
getStr += String(value2);
// getStr +="&field3=";
// getStr += String(value3);
// ...
getStr += "\r\n\r\n";
// send data length
cmd = "AT+CIPSEND=";
cmd += String(getStr.length());
espSerial.println(cmd);
if (DEBUG) Serial.println(cmd);
delay(100);
if(espSerial.find(">")){
espSerial.print(getStr);
if (DEBUG) Serial.print(getStr);
}
else{
espSerial.println("AT+CIPCLOSE");
// alert user
if (DEBUG) Serial.println("AT+CIPCLOSE");
return false;
}
return true;
}
//================================================================================ setup
void setup() {
lcd.begin(16, 2);
lcd.clear();
lcd.print("Humidity & Temp");
lcd.setCursor(0,1);
lcd.print(" Measurement ");
delay(2000);
lcd.clear();
lcd.print("VicinityInfoIndia");
lcd.setCursor(0,1);
lcd.print("Welcomes You");
delay(2000);
Serial.begin(9600);
lcd.clear();
lcd.print("WIFI Connecting");
lcd.setCursor(0,1);
lcd.print("Please wait....");
DEBUG=true; // enable debug serial
Serial.begin(9600);
dht.begin(); // Start DHT sensor
espSerial.begin(9600); // enable software serial
// Your esp8266 module's speed is probably at 115200.
// For this reason the first time set the speed to 115200 or to your esp8266 configured speed
// and upload. Then change to 9600 and upload again
espSerial.println("AT+RST"); // Enable this line to reset the module;
showResponse(1000);
espSerial.println("AT+UART_CUR=9600,8,1,0,0"); // Enable this line to set esp8266 serial speed to 9600 bps
showResponse(1000);
espSerial.println("AT+CWMODE=1"); // set esp8266 as client
showResponse(1000);
espSerial.println("AT+CWJAP=\""+ssid+"\",\""+password+"\""); // set your home router SSID and password
showResponse(5000);
if (DEBUG) Serial.println("Setup completed");
delay(1500);
lcd.clear();
lcd.print("WIFI Connected.");
delay(1000);
lcd.setCursor(0,1);
lcd.print("Setup comleted");
delay(3000);
}
// ====================================================================== loop
void loop() {
// Read sensor values
float t = dht.readTemperature();
float h = dht.readHumidity();
if (isnan(t) || isnan(h)) {
if (DEBUG) Serial.println("Failed to read from DHT");
}
else {
if (DEBUG) Serial.println("Temp="+String(t)+" *C");
lcd.setCursor(0,0);
lcd.print("Temperature:");
lcd.print(t);
if (DEBUG) Serial.println("Humidity="+String(h)+" %");
lcd.setCursor(0,1);
lcd.print("Humidity: ");
lcd.print(h);
thingSpeakWrite(t,h); // Write values to thingspeak
}
// thingspeak needs 15 sec delay between updates,
delay(16000);
}
//Its code
// Code to use SoftwareSerial
#include <SoftwareSerial.h>
SoftwareSerial espSerial = SoftwareSerial(2,3);// arduino RX pin=2 arduino TX pin=3 connect the arduino RX pin to esp8266 module TX pin - connect the arduino TX pin to esp8266 module RX pin
#include <DHT.h> //Attention: For new DHT11 version library you will need the Adafruit_Sensor library
//Download from here: https://github.com/adafruit/Adafruit_Sensor
// LCD RS=7, EN=8, DS4=9, DS5=10, DS6=11, DS7=12
#include <LiquidCrystal.h>
LiquidCrystal lcd( 7, 8, 9, 10, 11, 12);
//Attention: For new DHT11 version libraries you will need the Adafruit_Sensor library
//Download from here:https://github.com/adafruit/Adafruit_Sensor
//and install to Arduino software
#define DHTPIN 6 // Connect the signal pin of DHT11 sensor to digital pin 6
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
String apiKey = "CUGPJYT9VKOZM1RT"; // replace with your channel's thingspeak WRITE API key
String ssid="DESKTOP-KD"; // Wifi network SSID
String password ="912345678"; // Wifi network password
boolean DEBUG=true;
//======================================================================== showResponce
void showResponse(int waitTime){
long t=millis();
char c;
while (t+waitTime>millis()){
if (espSerial.available()){
c=espSerial.read();
if (DEBUG) Serial.print(c);
}
}
}
//========================================================================
boolean thingSpeakWrite(float value1, float value2){
String cmd = "AT+CIPSTART=\"TCP\",\""; // TCP connection
cmd += "184.106.153.149"; // api.thingspeak.com
cmd += "\",80";
espSerial.println(cmd);
if (DEBUG) Serial.println(cmd);
if(espSerial.find("Error")){
if (DEBUG) Serial.println("AT+CIPSTART error");
return false;
}
String getStr = "GET /update?api_key="; // prepare GET string
getStr += apiKey;
getStr +="&field1=";
getStr += String(value1);
getStr +="&field2=";
getStr += String(value2);
// getStr +="&field3=";
// getStr += String(value3);
// ...
getStr += "\r\n\r\n";
// send data length
cmd = "AT+CIPSEND=";
cmd += String(getStr.length());
espSerial.println(cmd);
if (DEBUG) Serial.println(cmd);
delay(100);
if(espSerial.find(">")){
espSerial.print(getStr);
if (DEBUG) Serial.print(getStr);
}
else{
espSerial.println("AT+CIPCLOSE");
// alert user
if (DEBUG) Serial.println("AT+CIPCLOSE");
return false;
}
return true;
}
//================================================================================ setup
void setup() {
lcd.begin(16, 2);
lcd.clear();
lcd.print("Humidity & Temp");
lcd.setCursor(0,1);
lcd.print(" Measurement ");
delay(2000);
lcd.clear();
lcd.print("VicinityInfoIndia");
lcd.setCursor(0,1);
lcd.print("Welcomes You");
delay(2000);
Serial.begin(9600);
lcd.clear();
lcd.print("WIFI Connecting");
lcd.setCursor(0,1);
lcd.print("Please wait....");
DEBUG=true; // enable debug serial
Serial.begin(9600);
dht.begin(); // Start DHT sensor
espSerial.begin(9600); // enable software serial
// Your esp8266 module's speed is probably at 115200.
// For this reason the first time set the speed to 115200 or to your esp8266 configured speed
// and upload. Then change to 9600 and upload again
espSerial.println("AT+RST"); // Enable this line to reset the module;
showResponse(1000);
espSerial.println("AT+UART_CUR=9600,8,1,0,0"); // Enable this line to set esp8266 serial speed to 9600 bps
showResponse(1000);
espSerial.println("AT+CWMODE=1"); // set esp8266 as client
showResponse(1000);
espSerial.println("AT+CWJAP=\""+ssid+"\",\""+password+"\""); // set your home router SSID and password
showResponse(5000);
if (DEBUG) Serial.println("Setup completed");
delay(1500);
lcd.clear();
lcd.print("WIFI Connected.");
delay(1000);
lcd.setCursor(0,1);
lcd.print("Setup comleted");
delay(3000);
}
// ====================================================================== loop
void loop() {
// Read sensor values
float t = dht.readTemperature();
float h = dht.readHumidity();
if (isnan(t) || isnan(h)) {
if (DEBUG) Serial.println("Failed to read from DHT");
}
else {
if (DEBUG) Serial.println("Temp="+String(t)+" *C");
lcd.setCursor(0,0);
lcd.print("Temperature:");
lcd.print(t);
if (DEBUG) Serial.println("Humidity="+String(h)+" %");
lcd.setCursor(0,1);
lcd.print("Humidity: ");
lcd.print(h);
thingSpeakWrite(t,h); // Write values to thingspeak
}
// thingspeak needs 15 sec delay between updates,
delay(16000);
}