<commit_before>// Using Sonoff wifi switch, 80MHz, 1M
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <WiFiClientSecure.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>
#include <Ticker.h>
#include "/usr/local/src/rpi2_setting.h"

extern "C" {
#include "user_interface.h"
}

#define BUTTON_PIN 0
#define RELAY_PIN 12
#define LED_PIN 13

#define BETWEEN_RELAY_ACTIVE 1000
#define REPORT_INTERVAL 5000 // in msec

IPAddress mqtt_server = MQTT_SERVER;

const char* subscribe_topic = "light/bedroomlight";
const char* reporting_topic = "light/bedroomlight/report";

long lastReconnectAttempt = 0;
volatile bool bUpdated = false;
volatile bool bRelayState = false;
volatile bool bRelayReady = false; 
String clientName;
unsigned long lastRelayActionmillis;
unsigned long startMills;

void ICACHE_RAM_ATTR callback(char* intopic, byte* inpayload, unsigned int length);

WiFiClientSecure sslclient;
PubSubClient client(mqtt_server, 8883, callback, sslclient);
Ticker ticker;

void tick()
{
  //toggle state
  int state = digitalRead(LED_PIN);  // get the current state of GPIO13 pin
  digitalWrite(LED_PIN, !state);     // set pin to the opposite state
}

bool ICACHE_RAM_ATTR sendmqttMsg(const char* topictosend, String payloadtosend, bool retain = false)
{
  unsigned int msg_length = payloadtosend.length();

  byte* p = (byte*)malloc(msg_length);
  memcpy(p, (char*) payloadtosend.c_str(), msg_length);

  if (client.publish(topictosend, p, msg_length, retain))
  {
    free(p);
    client.loop();

    Serial.print("[MQTT] out topic : ");
    Serial.print(topictosend);
    Serial.print(" payload: ");
    Serial.print(payloadtosend);
    Serial.println(" published");

    return true;

  } else {
    free(p);
    client.loop();

    Serial.print("[MQTT] out topic : ");
    Serial.print(topictosend);
    Serial.print(" payload: ");
    Serial.print(payloadtosend);
    Serial.println(" publish failed");

    return false;
  }
}

void ICACHE_RAM_ATTR sendCheck()
{
  String payload;
  if (bRelayState)
  {
      payload = "on";
  }
  else
  {
      payload = "false";
  }
  sendmqttMsg(reporting_topic, payload, true);
}

void ICACHE_RAM_ATTR sendUpdate()
{
  DynamicJsonBuffer jsonBuffer;
  JsonObject& root = jsonBuffer.createObject();
  if (bRelayState)
  {   
     root["cmd"] = "on";
  }
  else
  {
     root["cmd"] = "off";
  }
  String json;
  root.printTo(json);

  sendmqttMsg(subscribe_topic, json, true);
}

void ICACHE_RAM_ATTR parseMqttMsg(String receivedpayload, String receivedtopic)
{
  char json[] = "{\"cmd\":\"off\"}";

  receivedpayload.toCharArray(json, 150);
  StaticJsonBuffer<150> jsonBuffer;
  JsonObject& root = jsonBuffer.parseObject(json);

  if (!root.success())
  {
    return;
  }

  if (receivedtopic == subscribe_topic)
  {
    if (root.containsKey("cmd"))
    {
      const char* mqtt_relay_state = root["cmd"];
      if ( String(mqtt_relay_state) == "on")
      {
        Serial.println("call on");
        bRelayState = HIGH;
      }
      else
      {
        Serial.println("call off");
        bRelayState = LOW;
      }
      bUpdated = true;
    }
  }
}

void ICACHE_RAM_ATTR callback(char* intopic, byte* inpayload, unsigned int length)
{
  String receivedtopic = intopic;
  String receivedpayload ;

  for (unsigned int i = 0; i < length; i++)
  {
    receivedpayload += (char)inpayload[i];
  }

  Serial.print("[MQTT] intopic : ");
  Serial.print(receivedtopic);
  Serial.print(" payload: ");
  Serial.println(receivedpayload);

  parseMqttMsg(receivedpayload, receivedtopic);
}

bool verifytls()
{
  Serial.print("[MQTT] tls connecting to ");
  Serial.println(mqtt_server);
  if (!sslclient.connect(mqtt_server, 8883))
  {
    Serial.println("[MQTT] tls connection failed");<commit_msg>ignore mqtt msg BETWEEN_RELAY_ACTIVE time<commit_after>// Using Sonoff wifi switch, 80MHz, 1M
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <WiFiClientSecure.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>
#include <Ticker.h>
#include "/usr/local/src/rpi2_setting.h"

extern "C" {
#include "user_interface.h"
}

#define BUTTON_PIN 0
#define RELAY_PIN 12
#define LED_PIN 13

#define BETWEEN_RELAY_ACTIVE 3000
#define REPORT_INTERVAL 5000 // in msec

IPAddress mqtt_server = MQTT_SERVER;

const char* subscribe_topic = "light/bedroomlight";
const char* reporting_topic = "light/bedroomlight/report";

long lastReconnectAttempt = 0;
volatile bool bUpdated = false;
volatile bool bRelayState = false;
volatile bool bRelayReady = false; 
String clientName;
unsigned long lastRelayActionmillis;
unsigned long startMills;

void ICACHE_RAM_ATTR callback(char* intopic, byte* inpayload, unsigned int length);

WiFiClientSecure sslclient;
PubSubClient client(mqtt_server, 8883, callback, sslclient);
Ticker ticker;

void tick()
{
  //toggle state
  int state = digitalRead(LED_PIN);  // get the current state of GPIO13 pin
  digitalWrite(LED_PIN, !state);     // set pin to the opposite state
}

bool ICACHE_RAM_ATTR sendmqttMsg(const char* topictosend, String payloadtosend, bool retain = false)
{
  unsigned int msg_length = payloadtosend.length();

  byte* p = (byte*)malloc(msg_length);
  memcpy(p, (char*) payloadtosend.c_str(), msg_length);

  if (client.publish(topictosend, p, msg_length, retain))
  {
    free(p);
    client.loop();

    Serial.print("[MQTT] out topic : ");
    Serial.print(topictosend);
    Serial.print(" payload: ");
    Serial.print(payloadtosend);
    Serial.println(" published");

    return true;

  } else {
    free(p);
    client.loop();

    Serial.print("[MQTT] out topic : ");
    Serial.print(topictosend);
    Serial.print(" payload: ");
    Serial.print(payloadtosend);
    Serial.println(" publish failed");

    return false;
  }
}

void ICACHE_RAM_ATTR sendCheck()
{
  String payload;
  if (bRelayState)
  {
      payload = "on";
  }
  else
  {
      payload = "false";
  }
  sendmqttMsg(reporting_topic, payload, true);
}

void ICACHE_RAM_ATTR sendUpdate()
{
  DynamicJsonBuffer jsonBuffer;
  JsonObject& root = jsonBuffer.createObject();
  if (bRelayState)
  {   
     root["cmd"] = "on";
  }
  else
  {
     root["cmd"] = "off";
  }
  String json;
  root.printTo(json);

  sendmqttMsg(subscribe_topic, json, true);
}

void ICACHE_RAM_ATTR parseMqttMsg(String receivedpayload, String receivedtopic)
{
  char json[] = "{\"cmd\":\"off\"}";

  receivedpayload.toCharArray(json, 150);
  StaticJsonBuffer<150> jsonBuffer;
  JsonObject& root = jsonBuffer.parseObject(json);

  if (!root.success())
  {
    return;
  }

  if (receivedtopic == subscribe_topic)
  {
    if (root.containsKey("cmd"))
    {
      const char* mqtt_relay_state = root["cmd"];
      if (bRelayReady)
      {
        if ( String(mqtt_relay_state) == "on")
        {
          Serial.println("call on");
          bRelayState = HIGH;
        }
        else
        {
          Serial.println("call off");
          bRelayState = LOW;
        }
        bUpdated = true;
     }
    }
  }
}

void ICACHE_RAM_ATTR callback(char* intopic, byte* inpayload, unsigned int length)
{
  String receivedtopic = intopic;
  String receivedpayload ;

  for (unsigned int i = 0; i < length; i++)
  {
    receivedpayload += (char)inpayload[i];
  }

  Serial.print("[MQTT] intopic : ");
  Serial.print(receivedtopic);
  Serial.print(" payload: ");
  Serial.println(receivedpayload);

  parseMqttMsg(receivedpayload, receivedtopic);
}

bool verifytls()
{
  Serial.print("[MQTT] tls connecting to ");
  Serial.println(mqtt_server);
  if (!sslclient.connect(mqtt_server, 8883))
  {
    Serial.println("[MQTT] tls connection failed");
{'fraction_non_alphanumeric': 0.12897342365815528, 'fraction_numerical': 0.011203751954142784, 'mean_word_length': 3.798125, 'pattern_counts': {'":': 2, '<': 23, '<?xml version=': 0, '>': 21, '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': '6986571', 'n_tokens_mistral': 2786, 'n_tokens_neox': 2655, 'n_words': 601}