SHARE
TWEET

fullcode

a guest Jun 16th, 2019 79 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <ESP8266WiFi.h>
  2. #include <EEPROM.h>
  3. #include <PubSubClient.h>
  4. #include <WiFiUdp.h>
  5. #include <NTPClient.h>
  6. #include <time.h>
  7.  
  8.  
  9.  
  10. const char* ssid = "";
  11. const char* password =  "";
  12. const char* mqttServer = "";
  13. const int mqttPort = 1883;
  14. const char* mqttUser = "";
  15. const char* mqttPassword = "";
  16.  
  17. unsigned long lastSend;
  18.  
  19.  
  20.  
  21. WiFiClient espClient;
  22. PubSubClient client(espClient);
  23.  
  24. char buffer[100] = {0};
  25. char message_buff[100];
  26. String msgString = "";
  27.  
  28.  
  29.  
  30.  
  31. unsigned long time_second;
  32.  
  33.  
  34. void setup() {
  35.  
  36.   Serial.begin(115200);
  37.  
  38.  
  39.   client.setServer(mqttServer, mqttPort);
  40.   client.setCallback(callback);
  41.    
  42.   setup_wifi();
  43.  
  44.  
  45.  
  46. }
  47.  
  48.  
  49.  
  50. void setup_wifi() {
  51.  
  52.   delay(10);
  53.   Serial.printf("Connecting to %s ", ssid);
  54.   WiFi.begin(ssid, password);
  55.  
  56.  
  57.   while (WiFi.status() != WL_CONNECTED)
  58.   {
  59.     Serial.print(".");
  60.     delay(5000);
  61.   }
  62.  
  63.   if(client.connect("ESP8266Client", mqttUser, mqttPassword )){
  64.     Serial.println("connected");  
  65.     Serial.println(WiFi.localIP());
  66.      randomSeed(micros());
  67.   }else{
  68.      Serial.print("failed with state ");
  69.      Serial.print(client.state());
  70.      delay(2000);
  71.   }
  72.  
  73. }
  74.  
  75.  
  76.  
  77. void loop()
  78. {
  79.  
  80. send_mqtt ();
  81.  
  82.  
  83.  
  84.  
  85.  
  86.   client.loop();
  87.   if (!client.connected())
  88.   {
  89.      reconnect();
  90.  
  91.   }
  92.  
  93.   if (WiFi.status() != WL_CONNECTED)
  94.   {
  95.     setup_wifi();
  96.   }
  97.  
  98.  
  99.   delay(250);
  100. }
  101.  
  102.  
  103.  
  104.  
  105. void callback(char* topic, byte* payload, unsigned int length) {
  106.  Serial.print("Message arrived [");
  107.  Serial.print(topic);
  108.  Serial.print("] ");
  109.  int i = 0;
  110.  for (int i=0;i<length;i++) {
  111.   char receivedChar = (char)payload[i];
  112.   message_buff[i] = payload[i];
  113.  
  114.  
  115.  
  116.   }
  117.  
  118.  
  119. }
  120.  
  121.  
  122.  
  123.  
  124. void reconnect() {
  125.  
  126.  while (!client.connected()) {
  127.  Serial.print("Connecting...");
  128.  
  129.  if (client.connect("ESP8266 Client")) {
  130.   Serial.println("connected");
  131.  
  132.   client.subscribe("ledStatus");
  133.  } else {
  134.   Serial.print("failed, rc=");
  135.   Serial.print(client.state());
  136.   Serial.println("try again");
  137.  
  138.   delay(10000);
  139.   }
  140.  }
  141. }
  142.  
  143. void send_mqtt (){
  144.   if(millis()- lastSend > 5000){
  145.     //Serial.println("kirim status");
  146.   String json_strg = "{\"type\":\"HOME0001\",\"id\":\"HOME\",\"status\":\"OK\",\"type\":\"HOME0001\",\"id\":\"HOME\",\"status\":\"OK\",\"type\":\"HOME0001\",\"id\":\"HOME\",\"status\":\"OK\",\"type\":\"HOME0001\",\"id\":\"HOME\",\"status\":\"OK\",\"type\":\"HOME0001\",\"id\":\"HOME\",\"status\":\"OK\",\"type\":\"HOME0001\",\"id\":\"HOME\",\"status\":\"OK\",\"type\":\"HOME0001\",\"id\":\"HOME\",\"status\":\"OK\",\"type\":\"HOME0001\",\"id\":\"HOME\",\"status\":\"OK\"}";
  147.  char *stat = const_cast<char*>(json_strg.c_str());
  148.   client.publish("IOT/STATUS", stat);
  149.   lastSend = millis();
  150.   }
  151. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top