<commit_before>/*
 Drive an ITHO PWM controller board with an ESP8266

 PWM code from https://github.com/StefanBruens/ESP8266_new_pwm

 Note: specs are 5V-10V, 3.5kHz-4.5kHz; 5V is not enough so drive it at 7.5 or 9v.
 We're using 4kHz PWM in this example. We dim the led based on the duty cycle as well.

 Code was written for a NodeMCU Amica v2 board, may need adaptation for others.
*/

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include "Arduino.h"
#include <Syslog.h>
#include "WiFiUdp.h"
#include <ESP8266mDNS.h>
#include <ArduinoOTA.h>

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

// Copy config.h.example to config.h and update for your network
#include "config.h"

WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;

// A UDP instance to let us send and receive packets over UDP
WiFiUDP udpClient;

Syslog syslog(udpClient);


// 4khz in 200ns periods = (1000000000 / 4000) / 200 = 250000/200 = 1250
uint32 pwm_period_init = 1250;
uint32 pwm_duty_init[2] = {625,625}; // We default to 50%
uint32 io_info[2][3] = {
    {PERIPHS_IO_MUX_MTDI_U, FUNC_GPIO12, 12},
    {PERIPHS_IO_MUX_GPIO2_U, FUNC_GPIO2, 2},
};

void setup() {
  digitalWrite(D0, HIGH);
  Serial.begin(115200);
  Serial.setDebugOutput(true);
  setup_wifi();
  ArduinoOTA.begin();

  syslog.server(SYSLOG_SERVER, SYSLOG_PORT);
  syslog.deviceHostname(DEVICE_HOSTNAME);
  syslog.appName(APP_NAME);
  syslog.defaultPriority(LOG_DEBUG);

  client.setServer(MQTT_SERVER, 1883);
  client.setCallback(callback);

  pinMode(12, OUTPUT);
  pinMode(BUILTIN_LED, OUTPUT);     // Initialize the BUILTIN_LED pin as a PWM
  pwm_init(pwm_period_init, pwm_duty_init, 2, io_info);
  pwm_start();

}

void setup_wifi() {

  delay(10);
  // First shut down AP
  WiFi.enableAP(false);
  WiFi.softAPdisconnect(true);
  // Set it to auto connect and reconnect
  WiFi.setAutoConnect(true);
  WiFi.setAutoReconnect(true);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(WIFI_SSID);

  WiFi.begin(WIFI_SSID, WIFI_KEY);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
<commit_msg>Attempted fix at intermittent breaking of wifi reconnects<commit_after>/*
 Drive an ITHO PWM controller board with an ESP8266

 PWM code from https://github.com/StefanBruens/ESP8266_new_pwm

 Note: specs are 5V-10V, 3.5kHz-4.5kHz; 5V is not enough so drive it at 7.5 or 9v.
 We're using 4kHz PWM in this example. We dim the led based on the duty cycle as well.

 Code was written for a NodeMCU Amica v2 board, may need adaptation for others.
*/

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include "Arduino.h"
#include <Syslog.h>
#include "WiFiUdp.h"
#include <ESP8266mDNS.h>
#include <ArduinoOTA.h>

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

// Copy config.h.example to config.h and update for your network
#include "config.h"

WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;

// A UDP instance to let us send and receive packets over UDP
WiFiUDP udpClient;

Syslog syslog(udpClient);


// 4khz in 200ns periods = (1000000000 / 4000) / 200 = 250000/200 = 1250
uint32 pwm_period_init = 1250;
uint32 pwm_duty_init[2] = {625,625}; // We default to 50%
uint32 io_info[2][3] = {
    {PERIPHS_IO_MUX_MTDI_U, FUNC_GPIO12, 12},
    {PERIPHS_IO_MUX_GPIO2_U, FUNC_GPIO2, 2},
};

void setup() {
  digitalWrite(D0, HIGH);
  Serial.begin(115200);
  Serial.setDebugOutput(true);
  setup_wifi();
  ArduinoOTA.begin();

  syslog.server(SYSLOG_SERVER, SYSLOG_PORT);
  syslog.deviceHostname(DEVICE_HOSTNAME);
  syslog.appName(APP_NAME);
  syslog.defaultPriority(LOG_DEBUG);

  client.setServer(MQTT_SERVER, 1883);
  client.setCallback(callback);

  pinMode(12, OUTPUT);
  pinMode(BUILTIN_LED, OUTPUT);     // Initialize the BUILTIN_LED pin as a PWM
  pwm_init(pwm_period_init, pwm_duty_init, 2, io_info);
  pwm_start();

}

void setup_wifi() {

  delay(10);
  // First shut down AP
  WiFi.enableAP(false);
  WiFi.softAPdisconnect(true);
  // Set it to auto connect and reconnect
  WiFi.setAutoConnect(true);
  WiFi.setAutoReconnect(true);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(WIFI_SSID);

  // https://github.com/esp8266/Arduino/issues/2186#
  if (WiFi.status() != WL_CONNECTED) { WiFi.begin(WIFI_SSID, WIFI_KEY); }

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

{'fraction_non_alphanumeric': 0.12138728323699421, 'fraction_numerical': 0.05246776345042241, 'mean_word_length': 4.243589743589744, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '179874', 'n_tokens_mistral': 1936, 'n_tokens_neox': 1736, 'n_words': 441}