I am trying to create a project where I will be collecting data (humidity, temp, soil moisture, light, etc.) from my garden and uploading it online to ThingSpeak.
For simplicity, I am using a DHT11, ESP8266 ESP-01, an UNO, and a breadboard.
I broke each step apart and was able to accomplish each task individually.
I was able to connect the ESP8266 ESP-01 to the Arduino Uno (pulling off the ATmega328) and write data to ThingSpeak with the board changed to "Generic ESP8266 Module".
#include <ESP8266WiFi.h>
#include "ThingSpeak.h"
char ssid[] = "xxx";
char pass[] = "xxx";
int status = WL_IDLE_STATUS;
unsigned long myChannelNumber = xxx;
const char * myWriteAPIKey = "xxx";
WiFiClient client;
void setup() {
Serial.begin(9600);
WiFi.mode(WIFI_STA);
ThingSpeak.begin(client);
}
void loop() {
// Connect or reconnect to WiFi
if (WiFi.status() != WL_CONNECTED) {
Serial.print("Attempting to connect to internet: ");
while (WiFi.status() != WL_CONNECTED) {
WiFi.begin(ssid, pass);
Serial.print(".");
delay(5000);
}
Serial.println("\nConnected.");
}
// Write value to Field 1 of a ThingSpeak Channel
ThingSpeak.setField(1,"1");
int httpCode = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
delay(5000);
}
I was also able to collect data from the DHT11 connected to my Uno with the microcontroller back on the Uno and the board set to "Arduino Uno" in my IDE.
#include "DHT.h"
#define DHTPIN A0
#define DHTTYPE DHT11
DHT dht(DHTPIN,DHTTYPE);
void setup() {
Serial.begin(9600);
dht.begin();
}
void loop() {
delay(2000);
float h = dht.readHumidity();
float f = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(f)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
Serial.print("Humidity: ");
Serial.print(h);
Serial.print("% ");
Serial.print("Temperature: ");
Serial.print(f);
Serial.println("°F ");
delay(5000);
}
I would now like to combine these 2 projects together where the UNO will read the humidity (I'd like to later add on other sensors but I'm keeping it simple for now) and send it to the ESP8266 for it to send to ThingSpeak.
My current hardware schematic:
My DHT has the data pin connected to A0 and VCC is connected to the 5v of my Uno. And ground is connected to ground.
My ESP8266 has the RX and TX connected to the RX and TX of the Uno (my understanding is that I will have to use SoftwareSerial moving forward and moving them to different pins now when I combine these 2 projects). The 3v and EN is connected to my breadboard that is connected to the 3v of the Uno. And the ground is connected to the ground (when I upload my code to the ESP8266 (is this called flashing?) I have the I03 pin set to ground. After it has uploaded I pull that off and briefly connect RST to ground to reset it).
Questions:
-
I think I need 2 separate set of codes. One that I will upload to the Uno and the other to the ESP8266, right? My logic is that the Uno will read the DHT and then write that data to the ESP which will be listening. Then when the data is received, it will send it to ThingSpeak.
-
The Uno has a separate pin for 5v and 3v. I just need to set ESP8266 to 3v and DHT11 to 5v and I should be good to go?
-
I saw someone mention that I can have 2 separate IDEs going on for me to troubleshoot this. I'm guessing I would use Serial Monitor for debugging? But both of those IDEs will use that one same Serial Monitor? How does this work out if one IDE is set to run on the "Generic ESP8266 Module" and the other for "Arduino Uno"? If this is the case, would I write something like "Serial.begin(9600);" for each project so I can view what each project prints out on the same Serial Monitor?
-
If I use SoftwareSerial for my ESP8266, does it matter what pins I set it to? I'm currently using 2 and 3 for RX and TX.
-
Can I use the same ground where power is being pulled from 3v and 5v? I'm guessing I cannot and that I should be using a separate ground pin for each different voltage.
-
If I need to have 2 sets of different code for each equipment, how do I upload them? For the ESP, I would pull the microcontroller out of the Uno and then move my RX and TX to pins 0 and 1. Upload the code onto the ESP and hit reset real fast. Then I would move the RX an TX to pins 2 and 3, put the microcontroller back into the Uno and upload the Uno codes?
My new Uno Code:
#include "DHT.h"
#include <SoftwareSerial.h>
#define DHTPIN A0
#define DHTTYPE DHT11
SoftwareSerial esp(2,3);
DHT dht(DHTPIN,DHTTYPE);
void setup() {
Serial.begin(9600);
esp.begin(38400);
dht.begin();
}
void loop() {
delay(2000);
float h = dht.readHumidity();
float f = dht.readTemperature(true);
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(f)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
Serial.print("Humidity: ");
Serial.print(h);
Serial.print("% ");
Serial.print("Temperature: ");
Serial.print(f);
Serial.println("°F ");
esp.write(h); //How would I send over multiple data?
esp.write(f) //Would I create a string and break it down later?
//Is there a way I can confirm if data was written to my ESP on this end of the Uno?
}
delay(5000);
}
My new ESP code. At this point I'm just trying to get it to read data:
#include <SoftwareSerial.h>
SoftwareSerial esp(2, 3); // RX, TX
void setup() {
Serial.begin(9600);
esp.begin(38400); //I'm actually not even sure why I'm setting it to this baud
}
void loop() {
Serial.println("Data from ESP:");
while (esp.available() > 0) {
char inByte = esp.read();
Serial.println(inByte);
}
delay(5000);
}
But I actually don't understand now how this would work out. If I upload my codes through pin 0 and 1 (I think this is the only way I can upload the codes from the Uno to ESP?) then I would have to move the pins to 2 and 3 since that's what I have SoftwareSerial set to, right? Then I don't think I can use Serial Monitor anymore??
I feel like I'm making this way harder than it needs to be. Uploading data to the ESP8266 also takes a while too since it keeps failing (RX and TX to pins 0 and 1 of uno. 3v3 and EN to 3v. Ground and IO3 to ground. Then after it finishes upload I will connect ground to RST to briefly). A lot of times I get the error code "esptool.FatalError: Failed to connect to ESP8266: Timed out waiting for packet header"