2018年4月17日 星期二

4-Digit 7 Segment LED Display & MQTT









/*
 * Recive the data published by the MQTT client and display it on the 4-digital display
 */
//======================================================
// #include <TM1637Display.h>
// #include "TM1637Display.h"
// Library使用「#include <library.h>」來呼叫不在同個目錄裡的程式庫,而雙引號用來包含在同目錄裡的檔案。
//======================================================
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include "TM1637.h"

// Update these with values suitable for your network.
const char* ssid = "74170287";//put your wifi ssid here
const char* password = "24063173";//put your wifi password here
const char* mqtt_server = "broker.mqttdashboard.com";
//const char* mqtt_server = "iot.eclipse.org";

const int CLK = D2; //Set the CLK pin connection to the display
const int DIO = D3; //Set the DIO pin connection to the display

TM1637 tm1637(CLK, DIO); //set up the 4-Digit Display.

WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;
//========================================
void setup_wifi() {
   delay(100);
  // We start by connecting to a WiFi network
    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)
{
  tm1637.clearDisplay();   //Clear display
  Serial.print("Command from MQTT broker is : [");
  Serial.print(topic);
  Serial.println();
  Serial.print(" publish data is:");
  for(int i=0;i<length;i++)
  {
    tm1637.display(i,payload[i]-48);   // 0 = 48  , 1=49
    Serial.print(payload[i]);
  }
  Serial.println();
} //end callback
//========================================
void reconnect() {
  // Loop until we're reconnected
  while (!client.connected())
  {
    Serial.print("Attempting MQTT connection...");
    // Create a random client ID
    String clientId = "ESP8266Client-";
    clientId += String(random(0xffff), HEX);
    // Attempt to connect
    //if you MQTT broker has clientID,username and password
    //please change following line to    if (client.connect(clientId,userName,passWord))
    if (client.connect(clientId.c_str()))
    {
      Serial.println("connected");
     //once connected to MQTT broker, subscribe command if any
      client.subscribe("alex9ufo_Command");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 6 seconds before retrying
      delay(6000);
    }
  }
} //end reconnect()
//========================================
void setup() {
  Serial.begin(115200);
  setup_wifi();
  client.setServer(mqtt_server, 1883);

  tm1637.init();
  tm1637.set(BRIGHT_TYPICAL);//BRIGHT_TYPICAL = 2,BRIGHT_DARKEST = 0,BRIGHTEST = 7;
  tm1637.clearDisplay();
}
//========================================
void loop() {
  if (!client.connected()) {
    reconnect();
  }
  client.setCallback(callback);
  client.loop();
}
//========================================

沒有留言:

張貼留言

113 學年度第 1 學期 RFID應用課程 Arduino程式

113 學年度第 1 學期 RFID應用課程 Arduino程式 https://www.mediafire.com/file/zr0h0p3iosq12jw/MFRC522+(2).7z/file 內含修改過後的 MFRC522 程式庫 (原程式有錯誤) //定義MFRC522...