简体   繁体   中英

ESP8266 with DHT11

I'm trying to get the ESP8266-01 to work with my DHT11 sensor and post data to Thingspeak.

To upload my code to the ESP8266 I'm using a TTL to USB with the model FTDI232, and have selected the 3.3v output.

I was able to upload the "blink" example from the esp examples in the first try, then try to upload my code with the sensor and was not successful, getting the and error when uploading the sketch.

The code I'm trying to upload is the following:

// www.arduinesp.com
//
// Plot DTH11 data on thingspeak.com using an ESP8266
// April 11 2015
// Author: Jeroen Beemster
// Website: www.arduinesp.com

#include <DHT.h>
#include <ESP8266WiFi.h>

// replace with your channel’s thingspeak API key,
String apiKey = "EQE7GT28UMUW17CH";
const char* ssid = "GC-Office-WLAN1";
const char* password = "bsontkgcmxitwn30of01101911";

const char* server = "api.thingspeak.com";
#define DHTPIN 2 // what pin we’re connected to

DHT dht(DHTPIN, DHT11,15);
WiFiClient client;

void setup() {
Serial.begin(115200);
delay(10);
dht.begin();

WiFi.begin(ssid, password);

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");

}

void loop() {

float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}

if (client.connect(server,80)) { // "184.106.153.149" or api.thingspeak.com
String postStr = apiKey;
postStr +="&field1=";
postStr += String(t);
postStr +="&field2=";
postStr += String(h);
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);

Serial.print("Temperature: ");
Serial.print(t);
Serial.print(" degrees Celcius Humidity: ");
Serial.print(h);
Serial.println("% send to Thingspeak");
}
client.stop();

Serial.println("Waiting…");
// thingspeak needs minimum 15 sec delay between updates
delay(60000);
}

And the error message I get is the following:

Arduino:1.6.12 (Windows 7), Tarjeta:"Generic ESP8266 Module, 80 MHz, 40MHz, DIO, 512000, 512K (64K SPIFFS), ck, Disabled, None"

El Sketch usa 233,113 bytes (53%) del espacio de almacenamiento de programa. El máximo es 434,160 bytes.
Las variables Globales usan 32,432 bytes (39%) de la memoria dinámica, dejando 49,488 bytes para las variables locales. El máximo es 81,920 bytes.
Uploading 237264 bytes from C:\Users\nmnerzul\AppData\Local\Temp\arduino_build_881344/humedadesp8266solo.ino.bin to flash at 0x00000000
..........warning: espcomm_send_command: didn't receive command response
warning: espcomm_send_command(FLASH_DOWNLOAD_DATA) failed
warning: espcomm_send_command: wrong direction/command: 0x01 0x03, expected 0x01 0x04
error: espcomm_upload_mem failed
error: espcomm_upload_mem failed

I'm using an 3.3 external power supply with 1A. I connected this to power up the ESP8266 module.
My setting in Arduino IDE are as follows.

Arduino IDE设置

I have tried the following:

Reset module before updating, change of flash size (to 1M at 512k), change speed (tested 512000, 9600, 115200) and I'm only able to upload the blink example.

Hope the information is enough. Thanks for your time. conectipnsftdi模块

This problem is not really related to the code. It is for sure problem with hardware connections/data sync.

While trying my soulution please try not to mess with software. Go with recommended 115200 upload speed with ESP, and if the PCB of ESP is black it is more likely that the module is 1M instead of 512 Flash size (but flash size should not meter here).

So, I can not clearly see the connection between USBtoTTL and ESP01 so please check if your connections look exacly like this:

╭────────────┬─────────────╮
│    ESP01   │   USBtoTTL  │
╞════════════╪═════════════╡
│    RX      │     TX      │
│    TX      │     RX      │
│    VCC     │     VCC     │
│    CH_PD   │     VCC     │
│    GND     │     GND     │
│    GPIO0   │     GND     │
└────────────┴─────────────┘

This way, when you power up your ESP you should see only one blink of embedded blue LED which means that the module is now in flashing mode .

TRY TO CLICK UPLOAD BUTTON IN ARDUINO IDE, JUST AFTER YOU SEE THIS ONE BLINK

If this step is valid for you, please check if GND of your power supply is common with USBtoTTL and your ESP.

Last and unfortunately the most possible reason is poor quality of wires/connection between them. I was working with several types of ESP modules for over a year now, and I still have sometimes some fails while uploading. Anything you can do is to try different USB cable, and reduce the amount of connections on breadboard. I suggest buy/DIY an USBtoTTL->ESP01 adapter on solid prototype board with short soldered wires.

If this will not help, please feed me with information, if this is the only type of error you have, or maybe it has failed sometimes on uploading or something.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM