Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <PubSubClient.h>
- #include <ESP8266WiFi.h>
- const char *ssid = "ssid";
- const char *pass = "password";
- const char* mqtt_hostname = "hostname";
- const char* mqtt_username = "username";
- const char* mqtt_password = "password";
- const char* mqtt_topic = "topic";
- String clientName;
- int teller, time_1;
- WiFiClient wifiClient;
- PubSubClient client(wifiClient);
- bool connected = false;
- void setup() {
- Serial.begin(115200);
- if (WiFi.status() != WL_CONNECTED) {
- Serial.print("Connecting to ");
- Serial.print(ssid);
- Serial.println("");
- WiFi.begin(ssid, pass);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- };
- Serial.println("WiFi connected");
- }
- // IPAddress serverIp = IPAddress(192,168,1,1);
- client.setServer(hostname, 1883);
- if (!client.connected()) {
- String clientId = "ESP8266Client-";
- clientId += String(random(0xffff), HEX);
- if (client.connect(clientId, mqtt_username, mqtt_password)) {
- connected = true;
- Serial.println("Done");
- }
- }
- }
- void loop() {
- if(connected) {
- teller++;
- time_1 = millis();
- if (client.publish(topic, ""+teller)) {
- Serial.println((millis() - time_1));
- } else {
- Serial.println("failed");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement