Wednesday, October 7, 2020

Publish Data using MQTT Server (HiveMQ)

1. http://www.hivemq.com/demos/websocket-client/ and connect the server  

 s

 

#include <WiFi.h>
#include <Wire.h>
#include <PubSubClient.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "DHT.h"              // DHT sensor library
#include <NTPClient.h>
#include "Time_a.h"  
#include <TimeLib.h>  // unix time libray  

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
int battery_level=38;
#define DHTPIN 26     // DHT conected to  pin 26
#define DHTTYPE DHT11 //   

unsigned long offset_days = 3;    // 3 days
//unsigned long t_unix_date1;

const char* ssid     = "YOUR SSID"; // change here
const char* password = "YOUR PASS PRASE"; // change here

const char* mqtt_server = "broker.hivemq.com";
int status = WL_IDLE_STATUS;   // the Wifi radio's status

////////////////wifi cnnected image////////////////////////////////////////////////////////////////
static const unsigned char PROGMEM image_wifi[] = {                                             ///
// 'wifi-icon-260nw-329468402', 12x12px                                                         ///
0xf1, 0xf0, 0xc0, 0x30, 0x9f, 0x90, 0x30, 0xc0, 0xc0, 0x30, 0xdf, 0xb0, 0xf0, 0xf0, 0xe6, 0x70, ///
0xf9, 0xf0, 0xf9, 0xf0, 0xf9, 0xf0, 0xff, 0xf0};                                                ///                                                            
/////////////////////////battery status ///////////////////////////////////////////////////////////
static const unsigned char PROGMEM battery_empty[] = {                                          ///
// 'battery empty', 15x24px                                                                     ///
0x00, 0x00, 0x0f, 0xe0, 0x0f, 0xe0, 0x7f, 0xfc, 0x60, 0x0c, 0x60, 0x0c, 0x60, 0x0c, 0x60, 0x0c, ///
0x60, 0x0c, 0x60, 0x0c, 0x60, 0x0c, 0x60, 0x0c, 0x60, 0x0c, 0x60, 0x0c, 0x60, 0x0c, 0x6f, 0xec, ///
0x6f, 0xec, 0x6f, 0xec, 0x60, 0x0c, 0x60, 0x0c, 0x7f, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};///                                                                                               ///
static const unsigned char PROGMEM battery_half[] = {                                           ///
// 'battery half', 15x24px                                                                      ///
0x00, 0x00, 0x0f, 0xe0, 0x0f, 0xe0, 0x7f, 0xfc, 0x60, 0x0c, 0x60, 0x0c, 0x60, 0x0c, 0x60, 0x0c, ///
0x60, 0x0c, 0x60, 0x0c, 0x6f, 0xec, 0x6f, 0xec, 0x6f, 0xec, 0x60, 0x0c, 0x60, 0x0c, 0x6f, 0xec, ///
0x6f, 0xec, 0x6f, 0xec, 0x60, 0x0c, 0x7f, 0xfc, 0x7f, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};///
                                                                                                ///
static const unsigned char PROGMEM battery_full[] = {                                           ///
// 'battery full', 15x24px                                                                      ///
0x00, 0x00, 0x0f, 0xe0, 0x0f, 0xe0, 0x3f, 0xfc, 0x60, 0x0c, 0x6f, 0xec, 0x6f, 0xec, 0x6f, 0xec, ///
0x60, 0x0c, 0x60, 0x0c, 0x6f, 0xec, 0x6f, 0xec, 0x6f, 0xec, 0x60, 0x0c, 0x6f, 0xec, 0x6f, 0xec, ///
0x6f, 0xec, 0x60, 0x0c, 0x3f, 0xfc, 0x3f, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};///
                                                                                                ///
///////////////////////////////////////////////////////////////////////////////////////////////////
WiFiClient espClient;
PubSubClient client(espClient);
char mqtt_msg[10];

// Define NTP Client to get time
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP);
time_t et;

DHT dht(DHTPIN, DHTTYPE);

  float dht11_hum, dht11_temp;
  uint32_t t;        //unix time
  uint32_t count=0;
  Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

/////////////////////////////////////////////////////////////////
void setup() {
  Serial.begin(115200);
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");}

 
 count++;
  Serial.println("");
  Serial.print("Connected to WiFi network with IP Address: ");
  Serial.println(WiFi.localIP());


///////////////// time update///////////////////////////////////////////////////
   timeClient.begin();
   timeClient.setTimeOffset(0); // GMT +5:30 = 19800
   while(!timeClient.update()) {
   timeClient.forceUpdate();
  }
    setTime(timeClient.getEpochTime());  // update ESP32 time
    if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
    Serial.println(F("SSD1306 allocation failed"));
    for(;;);
  }
 

 
  delay(2000);
  display.clearDisplay();         //clear OLED

///////////// to display wifi connection status https://www.arduino.cc/en/Reference/WiFiStatus   
    //if(WiFi.status()== WL_CONNECTED){
    // Draw bitmap on the screen
display.drawBitmap(115, 0, image_wifi, 12, 12, 1);
display.display();
   // while(true);};  

///////////////battery level display ///////////////////////////////   
if (battery_level<30){                                           ///
  display.drawBitmap(110, 40, battery_empty, 15, 24, 1);         ///
  display.display();};                                           ///
if(battery_level>31){
  if(battery_level<50){///    
   display.drawBitmap(110, 40, battery_half, 15, 24, 1);         ///    
  display.display();}};                                           ///  
if(battery_level>51){                                            ///
   display.drawBitmap(110, 40, battery_full, 15, 24, 1);         ///
  display.display();};                                           ///  
                                                                 ///
//splay.clearDisplay();         //clear OLED                   ///
////////////////////////////////////////////////////////////////////  

  dht.begin();                      // Start the DHT11 sensor
  client.setServer(mqtt_server, 1883);
}

void loop() {
/// read data from sensors //////////////////////////////////
  dht11_hum  = dht.readHumidity();    // humidity         ///
  dht11_temp = dht.readTemperature(); // temp             ///
  t          = now();                 // time stamp       ///
                                                          ///
/////////////////////////////////////////////////////////////

////////////// unix time convert ///////////////////
 Serial.print(t);  // https://arduino.stackexchange.com/questions/38765/convert-to-and-from-unix-timestamp
 printf("  : %4d-%02d-%02d %02d:%02d:%02d\n", year(t), month(t), day(t), hour(t), minute(t), second(t));



/////////////Display data on OLED ///////////////////////////

  display.setTextSize(1);         //set OLED text size
  display.setTextColor(WHITE);    // set text colour
  display.print(WiFi.localIP());  //Display IP of ESP
  delay(2000);
  //display.clearDisplay();       //clear OLED
 
  display.setTextSize(2);         // Draw 2X-scale text
  display.setTextColor(WHITE);
  display.setCursor(2, 15);
  display.print(dht11_temp);
  display.print(" C");
  display.setCursor(2, 30);
  display.print(dht11_hum);
  display.print(" %");
  display.setTextSize(1);         // Draw 1X-scale text
  display.setCursor(1, 55);
  display.print(t);
  display.setCursor(64, 55);
  display.print(count);
  display.display();

///////////


////////////////// publish data using MQTT////////////////////////////////   
  sprintf(mqtt_msg,"%2.4f",dht11_hum);
  client.connect("CLIENT NAME"); // change here
  client.publish("CLIENT/PUBLISH1", mqtt_msg);  // change here;; Ex home/humidity
  Serial.println(mqtt_msg);
  //delay(30000);
  sprintf(mqtt_msg,"%2.4f",dht11_temp);
  client.connect("CLIENT NAME"); // change here
  client.publish("CLIENT/PUBLISH2", mqtt_msg);   // change here;; Ex, home/temp
  Serial.println(mqtt_msg);
  sprintf(mqtt_msg,"%u",t);
  client.connect("CLIENT NAME"); // change here
  client.publish("CLIENT/PUBLISH3", mqtt_msg);  // change here; Ex, home/time
  Serial.println(mqtt_msg);
  sprintf(mqtt_msg,"%2.4f-%2.2f",dht11_hum,dht11_temp);
 client.connect("CLIENT NAME");
 client.publish("CLIENT/PUBLISH4",mqtt_msg);
 Serial.println(mqtt_msg);

 
  delay(30000);
////////sleep for 20 secound/////////////////////
   //  esp_sleep_enable_ext0_wakeup(GPIO_NUM_13, 0);
   
  esp_sleep_enable_timer_wakeup(20000000);  
  esp_deep_sleep_start();
 
}

No comments:

Search This Blog