Thank you everyone for help.
You made me think. Everything works now.
Here is the solution for future searches.
Wiring:
ESP8266 to UNO:
RX - D3
TX - D2
VCC and CH_PD - 3.3V
GND - GND
Firs,t I run this code to establish communication with ESP8266 and to connect it to my network
#include <SoftwareSerial.h>
SoftwareSerial esp8266(2, 3);
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.println("Started");
// set the data rate for the SoftwareSerial port
esp8266.begin(115200);
esp8266.write("AT\r\n");
}
void loop() {
if (esp8266.available()) {
Serial.write(esp8266.read());
}
if (Serial.available()) {
esp8266.write(Serial.read());
}
}
To connect it to your network open Serial monitor and type
AT+CWJAP="<access_point_name>",""
Then I connected DHT22 sensor as below
DHT22 to UNO:
DATA - D4
And run the code below
#include <SoftwareSerial.h>
#include <stdlib.h>
#include <DHT.h>
#define DHTPIN 4 // Set the SDA pin
#define DHTTYPE DHT22 // DHT22 (AM2302) Sensor type setting
// Upload notification LED setting (Adunono On Board LED)
int ledPin = 13;
// Write API key for your own thingspeak channel
String apiKey = "Your API key here";
SoftwareSerial ser(2,3 ); // Create RX / TX, create serial object
DHT dht(DHTPIN, DHTTYPE);
void setup() {
dht.begin();
// Notification LED output setting
pinMode(ledPin, OUTPUT);
// Serial communication speed 9600 baud rate setting
Serial.begin(115200);
// Start the software serial
ser.begin(115200);
}
void loop() {
// blink LED on board
digitalWrite(ledPin, HIGH);
delay(200);
digitalWrite(ledPin, LOW);
// Read DHT11 value
float temp = dht.readTemperature();
float humi = dht.readHumidity();
// Convert String
char buf[16];
String strTemp = dtostrf(temp, 4, 1, buf);
String strHumi = dtostrf(humi, 4, 1, buf);
Serial.println(strTemp);
Serial.println(strHumi);
// TCP connection
String cmd = "AT+CIPSTART=\"TCP\",\"";
cmd += "184.106.153.149"; // api.thingspeak.com connection IP
cmd += "\",80"; // api.thingspeak.com connection port, 80
ser.println(cmd);
if(ser.find("Error")){
Serial.println("AT+CIPSTART error");
return;
}
// Set String, Data to send by GET method
String getStr = "GET /update?api_key=";
getStr += apiKey;
getStr +="&field1=";
getStr += String(strTemp);
getStr +="&field2=";
getStr += String(strHumi);
getStr += "\r\n\r\n";
// Send Data
cmd = "AT+CIPSEND=";
cmd += String(getStr.length());
ser.println(cmd);
if(ser.find(">")){
ser.print(getStr);
}
else{
ser.println("AT+CIPCLOSE");
// alert user
Serial.println("AT+CIPCLOSE");
}
// Thingspeak delay to meet minimum upload interval of 2 minutes
delay(120000);
}
Now it works.