I have an ESP8266-01 connected to an Arduino Mega via TX and RX. My ESP has been flashed so AT commands won't work. The ESP has a simple MQTT code that as of right now is sending "Hello World from ESP_MQTT_PUB_SUB......" to my RPI 3 over local wifi network. The Arduino does nothing at this time because I can't figure out (understand) a way to send the data from the arduino to my ESP and then have the ESP send that data to my RPI. Most of the stuff that I see uses Ethernet or going out of the local network to a web page, NEITHER are what I want to do. I'm guessing that the ESP is both a server and client (PUB_SUB code) because it sends messages and at the same time can receive from my other RPI.
Just to be clear, I'm not looking for someone to write a sketch for me, just help me understand how.
My final plan is to have my ESP connected (via wired to TX, RX) to my Arduino Mega that has a 2x16 LCD screen, 10 water solenoids, 10 Hygrometers, 3 DHT22s, & a rain detector in my garden (about 30 feet from my wireless router) and send the data that it receives through my local network inside my house to my RPI (about 15 from the router the opposite direction) and display the data on my screen. But right now I'm just asking for help with retrieving and forwarding data as it comes available.
NOTE: These are not my actual codes, I use them for testing and learning.
Here is the test code that I'm using for the ESP8266-01 :
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
const char* ssid = "MyRouter";
const char* password = "MyPsswd";
const char* mqtt_server = "MyPi3IP";
#define mqtt_user "Username"
#define mqtt_password "Password"
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
randomSeed(micros());
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection using ESP_MQTT-PUB-SUB...");
// Create a random client ID
String clientId = "ESP8266Client-";
clientId += String(random(0xffff), HEX);
// Attempt to connect
if (client.connect("ESP8266Client", mqtt_user, mqtt_password)) {
Serial.println("connected");
// Once connected, publish an announcement...
client.publish("outTopic", "hello world");
// ... and resubscribe
client.subscribe("inTopic");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void setup() {
pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
long now = millis();
if (now - lastMsg > 2000) {
lastMsg = now;
++value;
snprintf (msg, 75, "Hello World from ESP_MQTT_PUB_SUB #%ld", value);
Serial.print("Publish message: ");
Serial.println(msg);
client.publish("outTopic", msg);
}
}
Here is the test code that I'm using for the DHT sensor on my Arduino Uno:
#include <Adafruit_Sensor.h>
#include <DHT.h>;
//Constants
#define DHTPIN 2 // what pin we're connected to
#define DHTTYPE DHT11 // DHT 11 (AM2302)
DHT dht(DHTPIN, DHTTYPE); //// Initialize DHT sensor for normal 16mhz Arduino
float hum; //Stores humidity value
float temp; //Stores temperature value
void setup() {
Serial.begin(115200);
dht.begin();
}
void loop() {
/////////////////////////////////DHT11///////////////////////////////
{
Serial.println(" Temperature and Humidity (DHT22)");
delay(1000);
hum = dht.readHumidity();
temp = dht.readTemperature() * 1.8 + 32.1;
Serial.print("Humidity = ");
Serial.print(hum);
Serial.println(" %");
Serial.print("Temp = ");
Serial.print(temp);
Serial.println(" degrees Fahrenheit");
Serial.println("");
Serial.println("");
delay(2000); // waits 2000 milliseconds (2 sec).
}
}