Hello,
I copied project from the web for DHT22 and ESP8266 to record data on the thingspeak.
Here is the circuit schematic
Here is the code
/* Arduino IOT - Temperature (oC) and Humidity (%) on the web
*Use the DHT-22 sensor to read temperature and humidity values
*Send these values to www.thingSpeak.com with the ESP8266 serial Wifi module
Dev: Michalis Vasilakis // Date:23/2/2016 // Update: 25/2/2015 // Ver. 1.3
More info: http://www.ardumotive.com/iot-wifi-temp-and-humidity.html
Tip: open the serial monitor for debugging */
//Libraires
#include <stdlib.h>
#include <DHT.h>
/*------------------------DHT SENSOR------------------------*/
#define DHTPIN 2 // DHT data pin connected to Arduino pin 2
#define DHTTYPE DHT22 // DHT 22 (or AM2302)
DHT dht(DHTPIN, DHTTYPE); // Initialize the DHT sensor
/*----------------------------------------------------------*/
/*-----------------ESP8266 Serial WiFi Module---------------*/
#define SSID "name of my home wifi network" // "SSID-WiFiname"
#define PASS "password" // "password"
#define IP "184.106.153.149"// thingspeak.com ip
String msg = "GET /update?key=YOUR_WRITE_KEY"; //change it with your key...
/*-----------------------------------------------------------*/
//Variables
float temp;
int hum;
String tempC;
int error;
void setup()
{
Serial.begin(9600); //or use default 115200.
Serial.println("AT");
delay(5000);
if(Serial.find("OK")){
connectWiFi();
}
}
void loop(){
//Read temperature and humidity values from DHT sensor:
start: //label
error=0;
temp = dht.readTemperature();
hum = dht.readHumidity();
char buffer[10];
// there is a useful c function called dtostrf() which will convert a float to a char array
//so it can then be printed easily. The format is: dtostrf(floatvar, StringLengthIncDecimalPoint, numVarsAfterDecimal, charbuf);
tempC = dtostrf(temp, 4, 1, buffer);
updateTemp();
//Resend if transmission is not completed
if (error==1){
goto start; //go to label "start"
}
delay(30000); //Update
}
void updateTemp(){
String cmd = "AT+CIPSTART=\"TCP\",\"";
cmd += IP;
cmd += "\",80";
Serial.println(cmd);
delay(2000);
if(Serial.find("Error")){
return;
}
cmd = msg ;
cmd += "&field1="; //field 1 for temperature
cmd += tempC;
cmd += "&field2="; //field 2 for humidity
cmd += String(hum);
cmd += "\r\n";
Serial.print("AT+CIPSEND=");
Serial.println(cmd.length());
if(Serial.find(">")){
Serial.print(cmd);
}
else{
Serial.println("AT+CIPCLOSE");
//Resend...
error=1;
}
}
boolean connectWiFi(){
Serial.println("AT+CWMODE=1");
delay(2000);
String cmd="AT+CWJAP=\"";
cmd+=SSID;
cmd+="\",\"";
cmd+=PASS;
cmd+="\"";
Serial.println(cmd);
delay(5000);
if(Serial.find("OK")){
return true;
}else{
return false;
}
}
When I run this code
message in serial port is following
AT+CIPSTART="TCP","184.106.153.149",80
AT+CIPSEND=69
AT+CIPCLOSE
AT+CIPSTART="TCP","184.106.153.149",80
AT+CIPSEND=69
AT+CIPCLOSE
AT+CIPSTART="TCP","184.106.153.149",80
.....
I noticed just solely for wanting to read their temperature sensor, many people connect their temperature sensor to an Arduino, then an ESP8266 to the Arduino to send the data by WiFi. I just cannot understand why. You can connect your temperature sensor directly to the ESP8266, and program it directly from the Arduino IDE. Wouldn't that be much simpler, and save you an extra Arduino and space?
How is the ESP8266 connected to the Arduino? It seems you are using the hardware serial port for both ESP8266 and your PC so this may be causing some kind of conflict.
According to the serial console your program never actually sends the data, probably because it doesn't find the required > character.
Do you have a USB-TTL serial adapter that you can use to connect the ESP8266 directly to your computer? It would be useful to manually type your command sequence and view the responses so you know what the ESP8266 is sending back.
But if you are connecting to the serial monitor via the USB connector on your UNO that also uses Arduino TX& RX pins so there is potential conflict there. Do you have any way to power your Uno without plugging it in to the USB port?
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);
}