Week3
1.ติดตั้ง MQTT Lens
2.ให้กด connection +
-> connection name ให้ใส่ชื่อที่เราต้องการ
-> Hostname ใส่ e.g.iot.eclipse.org -> connection name ให้ใส่ชื่อที่เราต้องการ
-> กด create connection
ทดสอบ iot eclips broker ด้วย ESP8266
เปิด Arduino IDE >> พิมพ์ตาม Code ด้านล่าง
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
const char* ssid = "name_wifi";
const char* password = "password_wifi";
const char* mqtt_server = "iot.eclipse.org";
//IPAddress My_mqtt_server(192, 168, 43, 26);
const char* UName = "";
const char* PassW = "";
const char* topic1 = "test";
int TestLED = D4;
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;
void setup_wifi() {
delay(10);
Serial.println();
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());
pinMode(TestLED, OUTPUT);
}
void callback(char* topic, byte* payload, unsigned int length)
{ Serial.print("Message arrived [");
Serial.print(topic1);
Serial.print("] ");
for (int i = 0; i < length; i++)
{ Serial.print((char)payload[i]);
}
Serial.println();
Serial.print(" ---> ");
Serial.println((char)payload[0]);
if ((char)payload[0] == 'o') digitalWrite(TestLED, HIGH);
if ((char)payload[0] == 'f') digitalWrite(TestLED, LOW);
}
void reconnect()
{ while (!client.connected())
{ Serial.print("Attempting MQTT connection...");
String clientId = "ESP8266Client-";
clientId += String(random(0xffff), HEX);
if (client.connect(clientId.c_str(), UName, PassW))
{ Serial.println("connected");
client.publish(topic1, "Hello World Pk007");
client.subscribe(topic1);
} else
{ Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
delay(5000);
}
}
}
void setup()
{ Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
pinMode(TestLED, OUTPUT);
}
void loop()
{ if (!client.connected()) reconnect();
client.loop();
long now = millis();
if (now - lastMsg > 2000)
{ lastMsg = now;
++value;
snprintf (msg, 75, "hello world #%ld", value);
Serial.print("Publish message: ");
Serial.println(msg);
client.publish(topic1, msg);
}
}
การติดตั้ง MQTT Broker บน Raspberry Pi
เปิด Terminal บน Raspberry Pi พิมพ์คำสั่งดังนี้
mkdir mosquitto
-> cd mosquitto
-> wget http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key
-> sudo apt-key add mosquitto-repo.gpg.key
-> cd /etc/apt/sources.list.d/
-> sudo wget http://repo.mosquitto.org/debian/mosquitto-stretch.list
-> sudo apt-get update
-> sudo apt-cache search mosquitto
-> sudo apt-get install mosquitto
-> sudo apt-get install mosquitto-clients
-> service mosquitto status
-> ps -ef | grep mosq
-> netstat -tln | grep 1883
-> sudo nano /etc/mosquitto/mosquitto.conf
Blynk Broker
1.ติดตั้ง Blynk app ในโทรศัพท์มือถือ2.ติดตั้ง libraries บน arduino IDE ไปที่ sketch
-> Inclund Library
-> Manage library
-> ค้นหา blynk เเล้วติดตั้ง
ดู version ของ java ในRPi ว่าล่าสุดมั้ย
java –version
จากนั้นติดตั้งให้ JDK
sudo apt-get install oracle-java8-jdk
โหลด Blynk Server
wget https://github.com/blynkkk/blynk-server/releases/download/v0.35.1/server-0.35.1-java8.jar
Starting the server
java -jar server-0.33.5-java8.jar -dataFolder /home/pi/Blynk
สั่ง เปิด-ปิดไฟด้วยแอป และ MQTT Lens
No comments:
Post a Comment