Trying to get NodeMCU to publish MQTT to the broker. Any suggestions would be appreciated. Here is my code:
//#include <ArduinoOTA.h>
// This sketch is for a NodeMCU 8266 board with a DHT22 sensor
// The sensor values are read and then sent using MQTT
// The original code sent the data to a server
// on a Raspberry Pi running OpenHAB
// This code permits OTA (over the air) reflashing of the 8266
// Originally uploaded February 21, 2019
// This code is for the indoor sensor 192.168.50.49
// This code doesn't compile so I removed the ArduinoOTA by commenting it out
// This is set up to try the new MQTT server at .168 rather than the OpenHAB server
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WifiUdp.h>
#include <DHT.h>
#include <PubSubClient.h>
// #include <ArduinoOTA.h>
/******WiFi and MQTT Information (change as needed)**********/
#define wifi_ssid "gaspc1"
#define wifi_password "1020304050"
#define mqtt_server "192.168.50.168" // This is the IP address of the MQTT Server or Broker
#define mqtt_user "chippewa"
#define mqtt_password "8952"
#define mqtt_port 1883
/******Set the MQTT Packet size to one large enough to work***********/
#define MQTT_MAX_Packet_Size 512
//#define indoor_temp_topic "IndoorTemp"
//#define indoor_humidity_topic "IndoorHumidity"
/******Define sensor type and dati pin************/
DHT dht(D7,DHT22);
/******Needed for WiFi and MQTT************/
WiFiClient espClient;
PubSubClient client(espClient);
/******Declare Variables***************/
float PoolIndoorTemp;
float PoolIndoorHumidity;
int IndoorTempAdjustment = 0; //used to calibrate sensor
int IndoorHumidityAdjustment = 0;
int PoolIndoorTempSetPoint = 70;
int PoolIndoorTempBuffer = 2;
int PoolIndoorHumiditySetPoint = 5;
int PoolIndoorHumidityBuffer = 2; // to separate on and off by the nubmer of degrees
// to prevent short cycling
char message_buff[100];
unsigned long interval=2000; //The wait time
unsigned long previousMillis=0; //millis() returns and unsigned long
/******The beginning of the actual code ***********/
void setup() {
// put your setup code here, to run once:
/******OTA Update****************/
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server,mqtt_port);
client.setCallback(callback);
Serial.println(WiFi.localIP());
}
void callback(char* topic, byte* payload, unsigned int length)
{
Serial.print("Message arrived [");
Serial.print(topic);
int i=0;
for (i=0;i<length;i++)
{
Serial.print((char)payload[i]);
message_buff[i] = payload[i];
}
message_buff[i] = '\0';
String msgString = String(message_buff);
// if (msgString.equals("OFF"))
// {
// client.publish("openhab/himitsu/command","acknowledging OFF");
// }
// else if(msgString.equals("ON"))
// {
// client.publish("openhab/himitsu/command","acknowledging ON");
// }
Serial.println();
}
/******WiFi Setup ***************/
void setup_wifi()
{
delay(10);
Serial.println();
Serial.print("connecting to wifi");
Serial.println(wifi_ssid);
WiFi.mode(WIFI_STA);
WiFi.begin(wifi_ssid, wifi_password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print("connecting to wifir");
}
Serial.println("");
Serial.println("WiFi Connected");
Serial.println("IP Address: ");
Serial.println(WiFi.localIP());
}
/******OTA setup Stuff************/
// ArduinoOTA.onStart([]()
//{
// String type;
// if(ArduinoOTA.getCommand()==U_FLASH)
// type = "sketch";
// else //U_SPIFFS
// type = "filesystem";
// If updating SPIFFS this is hte place to umount SPIFFS using SPIFFS.end()
// Serial.println("Start updating " +type);
// });
// ArduinoOTA.onEnd([]()
//{
// Serial.println("\nEnd");
//});
// ArduinoOTA.onProgress([](unsigned int progress, unsigned int total){
// Serial.printf("Progress: %u%%\r",(progress / (total / 100)));
//});
// ArduinoOTA.onError([](ota_error_t error){
// Serial.printf("Error[%u]: ",error);
// if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
// else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
// else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
// else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
// else if (error == OTA_END_ERROR) Serial.println("End Failed");
//});
//ArduinoOTA.begin();
//Serial.println("Ready");
//Serial.print("IP Address: ");
//Serial.println(WiFi.localIP());
//}
/******Actual Program begins here ****************/
void loop()
{
// put your main code here, to run repeatedly:
/******First step is to check for OTA updates**********/
//ArduinoOTA.handle();
/******Timer to slow things down******/
unsigned long currentMillis = millis();
if((unsigned long)(currentMillis - previousMillis)>= interval)
{
previousMillis = millis(); //save current time
// the closing brace is at the end of all code
/******check for sensor reading errors and pring them******/
/******Read sensor and adjst for accuracy*****/
dht.begin();
PoolIndoorTemp = (dht.readTemperature(true) + IndoorTempAdjustment); //"true" will return in F; w/o true in C
PoolIndoorHumidity = (dht.readHumidity() + IndoorHumidityAdjustment);
Serial.println(PoolIndoorTemp,0);
Serial.println(PoolIndoorHumidity,0);
/******Print readings for display*****/
Serial.print("Indoor - Temperature: "); //prints the labels
Serial.print(PoolIndoorTemp,0);
Serial.print("\xC2\xB0"); // This adds the degree symbol
Serial.println("F"); // Adds F after degree symbol
Serial.print("Indoor - Relative Humidity: ");
Serial.print(PoolIndoorHumidity,0);
Serial.println("%"); //Adds the % symbol
/******Publish data over MQTT*****/
char IndoorTemp [6]; //Defines IndoorTemp as character type
dtostrf(PoolIndoorTemp,3,0,IndoorTemp); //converts digital to string
char IndoorHumidity [6];
dtostrf(PoolIndoorHumidity,3,0,IndoorHumidity);
client.publish("indoor_temp_topic",IndoorTemp);
client.publish("indoor_humidity_topic",IndoorHumidity);
Serial.println(IndoorTemp);
Serial.println(IndoorHumidity);
}
}