Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Arduino.h>
- #include "ESP8266WiFi.h"
- #include "PubSubClient.h"
- WiFiClient wifi_cli;
- PubSubClient client(wifi_cli);
- byte tmp;
- int tmp_l;
- String strPayload;
- char* tt;
- void callback(char* topic, byte* payload, unsigned int length){
- Serial.println("Arrived");
- Serial.println(topic);
- payload[length] = '\0'; //ovo je jako bitan deo u mom kodu to je specijalan karakter koji oznacava kraj stringa
- strPayload = String((char*)payload);//cuvam u globalnu prom
- Serial.println();
- }
- 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 (client.connect(clientId.c_str(),"guest","guest99")) {
- Serial.println("connected");
- // Once connected, publish an announcement...
- client.publish("outTopic", "hello world");
- // ... and resubscribe
- client.subscribe("inTopic");
- } else {
- Serial.print("failed, rc=");
- Serial.print(client.state());
- Serial.println(" try again in 5 seconds");
- // Wait 5 seconds before retrying
- delay(5000);
- }
- }
- }
- void setup() {
- // put your setup code here, to run once:
- Serial.begin(9600);
- // Set WiFi to station mode and disconnect from an AP if it was previously connected
- WiFi.mode(WIFI_STA);
- WiFi.disconnect();
- delay(100);
- WiFi.mode(WIFI_STA);
- WiFi.begin("SSladic", "7zvezdica");
- Serial.println("");
- // Wait for connection
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println("");
- Serial.print("Connected to SSladic");
- Serial.print("IP address: ");
- Serial.println(WiFi.localIP());
- client.setServer("mqtt.inviewscada.com",1883);
- client.setCallback(callback);
- Serial.println("Setup done");
- }
- void loop() {
- if (!client.connected()) {
- reconnect();
- }
- client.loop();
- Serial.println(strPayload);
- delay(1000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement