123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- #include <ESP8266WiFi.h>
- #include <MQTT.h>
- const char ssid[] = "yckj01";
- const char pass[] = "yckj2015";
- const char clientId[] = "client1";
- const char username[] = "user1";
- const char password[] = "123456";
- WiFiClient net;
- MQTTClient client;
- unsigned long lastMillis = 0;
- void connect()
- {
- Serial.print("checking wifi...");
- while (WiFi.status() != WL_CONNECTED)
- {
- Serial.print(".");
- delay(1000);
- }
- Serial.print("\nconnecting...");
- while (!client.connect(clientId, username, password))
- {
- Serial.print(".");
- delay(1000);
- }
- Serial.println("\nconnected!");
- client.subscribe("/allot/request");
-
- }
- void messageReceived(String &topic, String &payload)
- {
- Serial.println("incoming: " + topic + " - " + payload);
-
-
-
-
- }
- void setup()
- {
- Serial.begin(115200);
- WiFi.begin(ssid, pass);
-
-
-
-
- client.begin("qqyun.itbbn.top", 1883, net);
- client.onMessage(messageReceived);
- client.setKeepAlive(30);
- connect();
- }
- void loop()
- {
- client.loop();
- if (!client.connected())
- {
- connect();
- }
-
- if (millis() - lastMillis > 1000)
- {
- lastMillis = millis();
-
- }
- String inputString = "";
- while (Serial.available())
- {
- inputString = inputString + char(Serial.read());
- delay(2);
- }
- if (inputString.length() > 0)
- {
- Serial.println("inputString:" + inputString);
- client.publish("/allot/request", inputString);
- }
- }
|