I have a new code for my Mega that allows me to send collected DHT22 data to my RPI3. Instead of writing a program for the ESP and a program for the Mega, I have an ALL-IN -ONE just for the Mega and leave the ESP blank. I have the DHT22 connect to the Mega's 5V and gnd with the DHT's data pin connected to pin 2 on the Mega. I have the Mega's TX connected to a Logic Level on a breadboard then to the ESP's RX and vice versa for the RX-TX. The ESP is powered separately by a breadboard power supply. The data is sent through my wifi router to my RPI 3 with the data showing on a terminal screen.
My problem now is that it looks sloppy (mostly talking about the display in my terminal) and my data from my DHT comes in the form of a LETTER and not NUMBERS. Can I get help with those two issues and possibly some better viewing options (I'm kind of experimenting with Node Red but not sure if that will work with what I am trying to achieve with 24 sensors and such.
#include <WiFiEsp.h>
#include <WiFiEspClient.h>
#include <WiFiEspUdp.h>
#include <PubSubClient.h>
#include <Adafruit_Sensor.h> //DHT22 sensor
#include <DHT.h> //DHT22
#define DHTTYPE2 DHT22 // DHT 22 (AM2302)
#define DHTPIN2 2 // what pin we're connected to
DHT dht2(DHTPIN2, DHTTYPE2); // 11 works fine for ESP8266
int reading2;
int chk2;
char hum2; //Stores humidity value
char temp2; //Stores temperature value
char ssid[] = "*******"; // your network SSID (name)
char pass[] = "**********"; // your network password
char* topic = "garden";
char* server = "192.***.*.**";
#define mqtt_user "******"
#define mqtt_password "*********"
int status = WL_IDLE_STATUS; // the Wifi radio's status
// Initialize the Ethernet client object
WiFiEspClient espClient;
PubSubClient client(espClient);
void setup() {
dht2.begin(); // initialize temperature sensor
Serial.begin(9600); // initialize serial for debugging
Serial1.begin(115200); // initialize serial for ESP module
WiFi.init(&Serial1); // initialize ESP module
if (WiFi.status() == WL_NO_SHIELD) // check for the presence of the shield
{
Serial.println("WiFi shield not present");
while (true);// don't continue
}
while ( status != WL_CONNECTED) // attempt to connect to WiFi network
{
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
status = WiFi.begin(ssid, pass); // Connect to WPA/WPA2 network
}
Serial.println("You're connected to the network"); // you're connected now
client.setServer(server, 1883); //connect to MQTT server
client.setCallback(callback);
}
void callback(char* topic, byte* payload, unsigned int length) //print any message received for subscribed topic
{
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++)
{
Serial.print((char)payload[i]);
}
Serial.println();
}
void loop()
{
{
Serial.println("Outside Temperature and Humidity ");
delay(1000); //Delay 1 sec.
hum2 = dht2.readHumidity();
temp2 = dht2.readTemperature() * 1.8 + 32.1;
Serial.print(" Temp = " );
Serial.print(temp2);
Serial.println(" F");
Serial.print(" Humidity = ");
Serial.print(hum2);
Serial.println(" %");
Serial.print("");
Serial.println("");
delay(2000); // waits 2000 milliseconds (2 sec).
}
if (!client.connected())
{
reconnect();
}
client.loop();
}
void reconnect()
{
while (!client.connected()) // Loop until we're reconnected
{
Serial.print("Attempting MQTT connection...");
client.publish("command", "Attempting MQTT connection using ESP_MQTT-PUB-SUB...");
String clientId = "ESP8266Client-"; // Create a random client ID
clientId += String(random(0xffff), HEX);
if (client.connect("ESP8266Client", mqtt_user, mqtt_password)) // Attempt to connect, just a name to identify the client
{
Serial.println("connected");
client.publish("command", "connected");
client.publish("command", "hello world"); // Once connected, publish an announcement...
client.publish("command", " Temp = ", temp2, " F");
client.publish("command", " Humidity = ", hum2, " %");
client.subscribe("presence"); // ... and resubscribe
}
else
{
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
delay(5000); // Wait 5 seconds before retrying
}
}
}
Here is the IDE serial output..........
[WiFiEsp] Initializing ESP module
[WiFiEsp] Initilization successful - 2.0.0
Attempting to connect to WPA SSID: *******
[WiFiEsp] Connected to *******
You're connected to the network
Outside Temperature and Humidity
Temp = F F
Humidity = % %
Attempting MQTT connection...[WiFiEsp] Connecting to 192...
connected
Outside Temperature and Humidity
Temp = F F
Humidity = ( %
Outside Temperature and Humidity
Temp = F F
Humidity = ( %
Here is the MQTT Terminal output.........
Client mosqsub|15719-BeeHiveSe received PUBLISH (d0, q0, r1, m0, 'command', ... (38 bytes))
Humidity = %Attempting MQTT conne
Client mosqsub|15719-BeeHiveSe received PUBLISH (d0, q0, r0, m0, 'command', ... (9 bytes))
connected
Client mosqsub|15719-BeeHiveSe received PUBLISH (d0, q0, r0, m0, 'command', ... (11 bytes))
hello world
Client mosqsub|15719-BeeHiveSe received PUBLISH (d0, q0, r0, m0, 'command', ... (70 bytes))
Temp = F Humidity = %Attempting MQTT connection...Attempting
Client mosqsub|15719-BeeHiveSe received PUBLISH (d0, q0, r0, m0, 'command', ... (37 bytes))
Humidity = %Attempting MQTT conn
Client mosqsub|15719-BeeHiveSe sending PINGREQ
Client mosqsub|15719-BeeHiveSe received PINGRESP
Client mosqsub|15719-BeeHiveSe sending PINGREQ
Client mosqsub|15719-BeeHiveSe received PINGRESP