Page 1 of 1
ESP-05 + MQTT
Posted:
Mon Apr 04, 2016 2:38 pm
by Albertus
Good Day
I used arduino ethernet + mqtt with no problem, but would like to run same coding returning temperature + humidity to a topic, but with already bought ESP-05 + arduino mini, could someone guide me to working arduino ino file that works as i tried many libraries without success. i will be happy if humidity and temperature sensor DHT22 could run directly from ESP-05 or if it could be done via serial to arduino mini.
I tried all kinds of coding from searching google for over 3 weeks with no success, very frustrating if it works with cable, but cant get it working wireless.
Please help with any coding that can send me into correct direction.
Thank You
Albertus Geyser
Re: ESP-05 + MQTT
Posted:
Tue Apr 05, 2016 7:44 am
by Albertus
Good Day
I thought this will help to help me.
I need similer coding for arduino + ESP-05 (wifi) instead of Ethernet:
---------------------------------------------------------------------------------
#include <DHT.h>
#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>
#define DHTTYPE DHT22
#define DHTPIN 2
String datain;
byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED };
char datain_buff[8];
IPAddress ip(192, 168, 20, 164);
IPAddress server(192, 168, 20, 15);
EthernetClient ethClient;
DHT dht(DHTPIN, DHT22);
PubSubClient client(ethClient);
void reconnect() {
while (!client.connected()) {
if (client.connect("EnviroClient")) {
} else {
delay(5000);
}
}
}
void setup()
{
client.setServer(server, 1883);
Ethernet.begin(mac, ip);
delay(1500);
}
void loop()
{
if (!client.connected()) {
reconnect();
}
datain = String(dht.readTemperature());
datain.toCharArray(datain_buff, sizeof(datain_buff));
client.publish("enviro/192.168.20.164/temperatureC", datain_buff);
datain = String(Fahrenheit(dht.readTemperature()));
datain.toCharArray(datain_buff, sizeof(datain_buff));
client.publish("enviro/192.168.20.164/temperatureF", datain_buff);
datain = String(Kelvin(dht.readTemperature()));
datain.toCharArray(datain_buff, sizeof(datain_buff));
client.publish("enviro/192.168.20.164/temperatureK", datain_buff);
datain = String(dht.readHumidity());
datain.toCharArray(datain_buff, sizeof(datain_buff));
client.publish("enviro/192.168.20.164/humidity", datain_buff);
datain = String(dewPoint(dht.readTemperature(), dht.readHumidity()));
datain.toCharArray(datain_buff, sizeof(datain_buff));
client.publish("enviro/192.168.20.164/dewpoint", datain_buff);
client.loop();
delay(30000);
}
double Fahrenheit(double celsius)
{
return 1.8 * celsius + 32;
}
double Kelvin(double celsius)
{
return celsius + 273.15;
}
double dewPoint(double celsius, double humidity)
{
double A0= 373.15/(273.15 + celsius);
double SUM = -7.90298 * (A0-1);
SUM += 5.02808 * log10(A0);
SUM += -1.3816e-7 * (pow(10, (11.344*(1-1/A0)))-1) ;
SUM += 8.1328e-3 * (pow(10,(-3.49149*(A0-1)))-1) ;
SUM += log10(1013.246);
double VP = pow(10, SUM-3) * humidity;
double T = log(VP/0.61078);
return (241.88 * T) / (17.558-T);
}
-------------------------------------------------------------------------------------------------------
Hope Someone can help.
Thank You
Albertus Geyser
Re: ESP-05 + MQTT
Posted:
Tue Apr 05, 2016 9:22 am
by Albertus
Good Day
Before i used this to get data from arduino "through" wifi ESP-05, but i need to use MQTT.
---------------------------------------------------------------------------------------------------------
#include <DHT.h>
#include <Wire.h>
#include "rgb_lcd.h"
rgb_lcd lcd;
#define SSID "LN_OFFICE_AP"
#define PASS "AL5004"
#define BUFFER_SIZE 128
#define DHTTYPE DHT22
#define DHTPIN 2
byte TempHi;
byte TempLo;
boolean P_N;
unsigned int Decimal;
float combined_temp_C;
float combined_temp_F;
char * strTempC = NULL;
char * strTempF = NULL;
char buffer[BUFFER_SIZE];
DHT dht(DHTPIN, DHT22);
void setup() {
lcd.begin(16, 2);
lcd.setRGB(0, 255, 0);
Serial.begin(9600);
Serial.setTimeout(10000);
Serial.println("AT+RST");
if(Serial.find("ready"))
{
lcd.setCursor(1, 1);
lcd.clear();
lcd.print("Module ready");
}
else
{
lcd.setCursor(1, 1);
lcd.clear();
lcd.print("No response.");
while(1);
}
delay(1000);
boolean connected=false;
for(int i=0;i<5;i++)
{
if(connectWiFi())
{
connected = true;
break;
}
}
if (!connected){while(1);}
delay(1000);
Serial.println("AT+CIPMUX=1");
delay(1000);
Serial.println("AT+CIPSERVER=1,1084");
delay(1000);
Serial.println("AT+CIPSTO=15");
delay(1000);
}
void loop() {
int con_id, packet_len;
char *pb;
Serial.readBytesUntil('\n', buffer, BUFFER_SIZE);
if(strncmp(buffer, "+IPD,", 5)==0) {
sscanf(buffer+5, "%d,%d", &con_id, &packet_len);
if (packet_len > 0) {
pb = buffer+5;
while(*pb!=':') pb++;
pb++;
delay(100);
clearBuffer();
}
senddata(con_id);
}
}
//----------------------------------------------------------
boolean connectWiFi()
{
Serial.println("AT+CWMODE=1");
String cmd="AT+CWJAP=\"";
cmd+=SSID;
cmd+="\",\"";
cmd+=PASS;
cmd+="\"";
Serial.println(cmd);
delay(2000);
if(Serial.find("OK"))
{
lcd.setCursor(1, 1);
lcd.clear();
lcd.print("Connected");
return true;
}else
{
lcd.setCursor(1, 1);
lcd.clear();
lcd.print("Failed");
return false;
}
}
void senddata(int con_id) {
String Datai;
lcd.setCursor(13, 0);
lcd.print(con_id);
Datai = String(dht.readTemperature());
Datai += String(";");
Datai += String(dht.readHumidity());
Datai += String(";");
Datai += String(dewPoint(dht.readTemperature(), dht.readHumidity()));
Datai += String(";");
Serial.print("AT+CIPSEND=");
Serial.print(con_id);
Serial.print(",");
Serial.println(Datai.length());
delay(10);
if (Serial.find(">")) {
Serial.println(Datai);
}
}
//-------------------------------------------------------
double dewPointFast(double celsius, double humidity)
{
double a = 17.271;
double b = 237.7;
double temp = (a * celsius) / (b + celsius) + log(humidity/100);
double Td = (b * temp) / (a - temp);
return Td;
}
double dewPoint(double celsius, double humidity)
{
double A0= 373.15/(273.15 + celsius);
double SUM = -7.90298 * (A0-1);
SUM += 5.02808 * log10(A0);
SUM += -1.3816e-7 * (pow(10, (11.344*(1-1/A0)))-1) ;
SUM += 8.1328e-3 * (pow(10,(-3.49149*(A0-1)))-1) ;
SUM += log10(1013.246);
double VP = pow(10, SUM-3) * humidity;
double T = log(VP/0.61078);
return (241.88 * T) / (17.558-T);
}
void Cal_Temp()
{
if (TempHi&0x80)
P_N = 0;
else
P_N = 1;
TempHi = TempHi & 0x7F;
TempLo = TempLo & 0xF0;
TempLo = TempLo >>4;
Decimal = TempLo;
Decimal = Decimal * 625;
combined_temp_C= TempHi + TempLo*.0625;
combined_temp_F = combined_temp_C*1.8+32;
sprintf(strTempC, "%f", combined_temp_C);
sprintf(strTempF, "%f", combined_temp_F);
}
void clearBuffer(void) {
while ( Serial.available() > 0 ) {
Serial.read();
}
}
-----------------------------------------------------------------------------------------------------------------------
Hope Someone could guide me.
Thank You
Albertus Geyser
Re: ESP-05 + MQTT
Posted:
Tue Apr 05, 2016 12:24 pm
by bogie
If you go to adafruit.io, they have tutorials on how to connect your esp8266 to their MQTT broker.