Your script works for me on a Wemos D1 Mini publishing to my MQTT broker (192.168.1.156:1883) on a Raspberry Pi. I removed the code for the DS18B20, as I hadn't got one to hand, and just put in a constant (21.6) to simulate the temperature. Looks like the issue could be with your network.
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
// #include <OneWire.h>
// #include <DallasTemperature.h>
// GPIO where the DS18B20 is connected to
//const int oneWireBus = 4;
//OneWire oneWire(oneWireBus);
//DallasTemperature sensors(&oneWire);
const char* ssid = "Teamwork";
const char* password = "***********";
const char* mqtt_server = "192.168.1.156"; ///MQTT Broker
int mqtt_port = 1883;
WiFiClient espClient;
PubSubClient client(espClient);
void setup() {
// Start the Serial Monitor
Serial.begin(19200);
//sensors.begin();
setup_wifi();
client.setServer(mqtt_server, mqtt_port);
}
void setup_wifi() {
delay(10);
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
// Connecting to a WiFi network
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void reconnect() {
// Loop until we're reconnected
Serial.println("In reconnect...");
while (!client.connected()) {
Serial.println("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("ESP8266Client")) {
Serial.println("connected");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
delay(5000);
}
}
}
void loop() {
// char msg[10];
//char msgtext[25];
//String themsg;
//sensors.requestTemperatures();
//float celsius = sensors.getTempCByIndex(0);
//Serial.println(sensors.getTempCByIndex(0));
//char temperaturenow [15];
//dtostrf(celsius,7,3,temperaturenow); // convert floar to char
client.publish("motor/temperature", "21.6");
if (!client.connected()) {
reconnect();
}
client.loop();
delay(1000);
}


