2017年10月22日 星期日
I2C 1602 LCD & MQTT Dashboard
/*
* Subscribe message from remote MQTT client and dispaly the message on the I2C led
* Tutorial URL http://osoyoo.com/2017/05/21/nodemcu-lesson-18-i2c-1602-lcd-mqtt/
* CopyRight www.osoyoo.com
* Software:
* Arduino IDE(version 1.6.4+)
* ESP8266 Board Package and the Serial Port Driver
* MQTT Client(MQTTBox here)
* Arduino library: PubSubClient
* Arduino library: LiquidCrystal_I2C
* Connection
* NodeMCU I2C 1602 LCD
* Vin VCC
* GND GND
* D1 SCL
* D2 SDA
* =====================
*
*
* MQTT Client Settings
* About how to config the MQTT client,check this link.
* Topics Settings:
* Topic to publish: OsoyooCommand --> Alex9ufo1602-I2C-LCDCommand
* Payload Type: Strings/JSON/XML/Characters
* Running Result
*/
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <Wire.h> // This library is already built in to the Arduino IDE
#include <LiquidCrystal_I2C.h> //This library you can add via Include Library > Manage Library >
LiquidCrystal_I2C lcd(0x27, 16, 2); /*0x27 means the address of this 1602 I2C LCD display,different lcd may have the different address,
if the LCD do not work,please read below post about how to get the right address for your I2C LCD dispaly:
http://osoyoo.com/2014/12/07/16x2-i2c-liquidcrystal-displaylcd/ */
// Update these with values suitable for your network.
//const char* ssid = "******";//put your own wifi ssid here
//const char* password = "******";// put your wifi password here
const char* ssid = "PTS-2F";//put your wifi ssid here
const char* password = "" ;//put your wifi password here
const char* mqtt_server = "broker.mqttdashboard.com";// choose your mqtt server
//const char* mqtt_server = "iot.eclipse.org";
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)
{
Serial.print("Command from MQTT broker is : [");
Serial.print(topic);
Serial.println();
Serial.print(" publish data is:");
lcd.clear();
{
for(int i=0;i<length;i++)
{
Serial.print((char)payload[i]);
lcd.setCursor(0, 0);
lcd.print("Alex9ufo1602-I2C-LCD"); // Start Print text to Line 1
lcd.setCursor(i, 1);
lcd.write((char)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("Alex9ufo1602-I2C-LCDCommand");
} 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);
lcd.begin(); // initializing the LCD
lcd.backlight(); // Enable or Turn On the backlight
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.setCallback(callback);
client.loop();
}
訂閱:
張貼留言 (Atom)
2024_09 作業3 以Node-Red 為主
2024_09 作業3 (以Node-Red 為主 Arduino 可能需要配合修改 ) Arduino 可能需要修改的部分 1)mqtt broker 2) 主題Topic (發行 接收) 3) WIFI ssid , password const char br...
-
python pip 不是内部或外部命令 -- 解決方法 要安裝 Pyqt5 1. 首先,開啟命令提示字元。 2. 輸入 pip3 install pyqt5 好像不能執行 ! ! 錯誤顯示 : ‘ pip3 ’ 不是內部或外部命令、可執行的程式或批...
-
課程講義 下載 11/20 1) PPT 下載 + 程式下載 http://www.mediafire.com/file/cru4py7e8pptfda/106%E5%8B%A4%E7%9B%8A2-1.rar 11/27 2) PPT 下載...
-
• 認 識 PreFix、InFix、PostFix PreFix(前序式):* + 1 2 + 3 4 InFix(中序式): (1+2)*(3+4) PostFix(後序式):1 2 + 3 4 + * 後 序式的運算 例如: 運算時由 後序式的...
沒有留言:
張貼留言