<commit_before>#define DEBUG_MODE

#include <ESP8266WiFi.h>
#include <ArduinoJson.h>
#include <MqttWrapper.h>
#include <PubSubClient.h>
#include <WiFiHelper.h>

const char* ssid     = "CMMC.32";
const char* pass     = "<PASSWORD>";

MqttWrapper *mqtt;
WiFiHelper *wifi;

void callback(const MQTT::Publish& pub) {
  if (pub.payload_string() == "0") {
    Serial.print(" => ");
    Serial.println(pub.payload_string());
  }
  else if (pub.payload_string() == "1") {
    Serial.print(" => ");
    Serial.println(pub.payload_string());
  }
  else {
    Serial.print(pub.topic());
    Serial.print(" => ");
    Serial.println(pub.payload_string());
  }
}

void hook_prepare_data(JsonObject** root) {
  JsonObject& data = (*(*root))["d"];

  data["myName"] = "NAT";
  data["adc"] = analogRead(A0);;

}

void hook_configuration(MqttWrapper::Config config) {
  uint8_t mac[6];
  WiFi.macAddress(mac);
  String result;
  for (int i = 0; i < 6; ++i)
  {
    result += String(mac[i], 16);
  }

  *(config.clientId) = String("d:quickstart:arduino:") + result;
  *(config.username) = String("test");
  *(config.password) = String("<PASSWORD>");
  *(config.channelId) = String("esp8266/");
  *(config.topicPub) = "iot-2/evt/status/fmt/json";
}

void hook_publish_data(char* data) {
  Serial.print("PUBLISH: ->");
  Serial.println(data);
}

void setup() {
  Serial.begin(115200);
  pinMode(0, INPUT_PULLUP);
  delay(10);
  Serial.println();
  Serial.println();

  wifi->on_connected([](const char* message) {    Serial.println ("WIFI CONNECTED"); });
  wifi->on_disconnected([](const char* message) { Serial.println ("WIFI DISCONNECTED"); });
  wifi->begin();

  mqtt = new MqttWrapper("m20.cloudmqtt.com", 19642, hook_configuration);
  mqtt->connect(callback);
  mqtt->set_prepare_data_hook(hook_prepare_data, 5000);
  mqtt->set_publish_data_hook(hook_publish_data);
}

void loop() {
  wifi->loop();
  mqtt->loop();

  // ตรวจจับการกด Switch
  if (digitalRead(0) == LOW) {
    // วนลูปจนกว่าจะเอาปล่อย Switch
    while (digitalRead(0) == LOW) {
      mqtt->loop();
      yield();
    }
    String status = "0";
    mqtt->sync_pub(status);
  }

}<commit_msg>Fix wrong commit<commit_after>#ifndef WIFI_HELPER_H
#define WIFI_HELPER_H

#include "ESP8266WiFi.h"
#include <functional>



#ifdef DEBUG_MODE
    #ifndef DEBUG_PRINTER
        #define DEBUG_PRINTER Serial
    #endif
    #ifdef DEBUG_LEVEL_VERBOSE
        #define DEBUG_PRINT(...) { DEBUG_PRINTER.print(__VA_ARGS__); }
        #define DEBUG_PRINTLN(...) { DEBUG_PRINTER.println(__VA_ARGS__); }

        #define INFO_PRINT(...) { }
        #define INFO_PRINTLN(...) { }
    #else
        #define DEBUG_LEVEL_INFO
    #endif

    #ifdef DEBUG_LEVEL_INFO
        #define DEBUG_PRINT(...) {}
        #define DEBUG_PRINTLN(...) {}

        #define INFO_PRINT(...) { DEBUG_PRINTER.print(__VA_ARGS__); }
        #define INFO_PRINTLN(...) { DEBUG_PRINTER.println(__VA_ARGS__); }
    #endif
#else
    #define DEBUG_PRINT(...) {}
    #define DEBUG_PRINTLN(...) {}

    #define INFO_PRINT(...) { }
    #define INFO_PRINTLN(...) { }
#endif




class WiFiHelper
{
    public: 
        typedef std::function<void(const char*)> wifi_callback_t;

        WiFiHelper(const char* ssid, const char* password);
        ~WiFiHelper();

        void init_config(const char*, const char*);


        void begin() {
            _connect();
        }

        void loop() {
            if (WiFi.status() == WL_CONNECTED) {
                // return WiFi.status();
            }
            else {
                INFO_PRINTLN("CLASS: WIFI CONNECTED...");
                if (_user_on_disconnected != NULL) {
                   _user_on_disconnected("class: disconnected");
                }
                _connect();
            }
        }

        void on_disconnected(wifi_callback_t callback = NULL) {
            _user_on_disconnected = callback;
        }

        void on_connected(wifi_callback_t callback = NULL) {
            _user_on_connected = callback;
        }

        void on_connecting(wifi_callback_t callback = NULL) {
            _user_on_connecting = callback;
        }

    protected:

    private:
        String _ssid = "";
        String _password = "";
        String _mac = "";

        wifi_callback_t _user_on_disconnected = NULL;
        wifi_callback_t _user_on_connected  = NULL;
        wifi_callback_t _user_on_connecting = NULL;

        unsigned long _retries = 0;
        unsigned long prev_millis;


        void _connect() {
            _retries = 0;
            WiFi.begin(_ssid.c_str(), _password.c_str());

            while ((WiFi.status() != WL_CONNECTED))
            {
                if (_user_on_connecting != NULL) {
                   _user_on_connecting("class: connecting");
                }                

                DEBUG_PRINT(WiFi.status());
                DEBUG_PRINTLN(" ");

                INFO_PRINT(WiFi.status());
                INFO_PRINTLN(" ");
                _retries++;
                delay(500);
            }        

            INFO_PRINTLN();
            INFO_PRINTLN("CLASS: WIFI CONNECTED...");

            if (_user_on_connected != NULL) {
               _user_on_connected("class: connected");
            }

        }
};



#endif//WIFI_HELPER_H
{'fraction_non_alphanumeric': 0.14641566837801864, 'fraction_numerical': 0.009887811370983077, 'mean_word_length': 2.3524537922243467, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1243238', 'n_tokens_mistral': 1840, 'n_tokens_neox': 1736, 'n_words': 305}