Initial Code v0.1.0

This commit is contained in:
jomjol
2020-08-07 17:42:29 +02:00
parent 0e2475bf0d
commit 4fe26dc0d8
269 changed files with 87264 additions and 0 deletions

View File

@@ -0,0 +1,157 @@
//#pragma warning(disable : 4996)
#include "Helper.h"
//#define ISWINDOWS_TRUE
using namespace std;
std::string FormatFileName(std::string input)
{
#ifdef ISWINDOWS_TRUE
input.erase(0, 1);
std::string os = "/";
std::string ns = "\\";
FindReplace(input, os, ns);
#endif
return input;
}
void FindReplace(std::string& line, std::string& oldString, std::string& newString) {
const size_t oldSize = oldString.length();
// do nothing if line is shorter than the string to find
if (oldSize > line.length()) return;
const size_t newSize = newString.length();
for (size_t pos = 0; ; pos += newSize) {
// Locate the substring to replace
pos = line.find(oldString, pos);
if (pos == std::string::npos) return;
if (oldSize == newSize) {
// if they're same size, use std::string::replace
line.replace(pos, oldSize, newString);
}
else {
// if not same size, replace by erasing and inserting
line.erase(pos, oldSize);
line.insert(pos, newString);
}
}
}
bool ctype_space(const char c, string adddelimiter)
{
if (c == ' ' || c == '\t' || c == '\r' || c == '\n' || c == 11)
{
return true;
}
if (adddelimiter.find(c) != string::npos)
return true;
return false;
}
string trim(string istring, string adddelimiter)
{
bool trimmed = false;
if (ctype_space(istring[istring.length() - 1], adddelimiter))
{
istring.erase(istring.length() - 1);
trimmed = true;
}
if (ctype_space(istring[0], adddelimiter))
{
istring.erase(0, 1);
trimmed = true;
}
if ((trimmed == false) || (istring.size() == 0))
{
return istring;
}
else
{
return trim(istring, adddelimiter);
}
}
size_t findDelimiterPos(string input, string delimiter)
{
size_t pos = std::string::npos;
size_t zw;
string akt_del;
for (int anz = 0; anz < delimiter.length(); ++anz)
{
akt_del = delimiter[anz];
if ((zw = input.find(akt_del)) != std::string::npos)
{
if (pos != std::string::npos)
{
if (zw < pos)
pos = zw;
}
else
pos = zw;
}
}
return pos;
}
void CopyFile(string input, string output)
{
input = FormatFileName(input);
output = FormatFileName(output);
char cTemp;
FILE* fpSourceFile = fopen(input.c_str(), "rb");
FILE* fpTargetFile = fopen(output.c_str(), "wb");
// Code Section
// Read From The Source File - "Copy"
while (fread(&cTemp, 1, 1, fpSourceFile) == 1)
{
// Write To The Target File - "Paste"
fwrite(&cTemp, 1, 1, fpTargetFile);
}
// Close The Files
fclose(fpSourceFile);
fclose(fpTargetFile);
}
string getFileType(string filename)
{
int lastpos = filename.find(".", 0);
int neu_pos;
while ((neu_pos = filename.find(".", lastpos + 1)) > -1)
{
lastpos = neu_pos;
}
string zw = filename.substr(lastpos + 1, filename.size() - lastpos);
return zw;
}
string toUpper(string in)
{
for (int i = 0; i < in.length(); ++i)
in[i] = toupper(in[i]);
return in;
}

View File

@@ -0,0 +1,22 @@
#pragma once
#include <string>
#include <fstream>
using namespace std;
std::string FormatFileName(std::string input);
void FindReplace(std::string& line, std::string& oldString, std::string& newString);
void CopyFile(string input, string output);
size_t findDelimiterPos(string input, string delimiter);
//string trim(string istring);
string trim(string istring, string adddelimiter = "");
bool ctype_space(const char c, string adddelimiter);
string getFileType(string filename);
string toUpper(string in);

View File

@@ -0,0 +1,162 @@
#include "connect_wlan.h"
#include <string.h>
#include "esp_wifi.h"
#include "esp_event_loop.h"
#include "freertos/event_groups.h"
#include "esp_log.h"
#include <fstream>
#include <string>
#include <vector>
#include "Helper.h"
static const char *MAIN_TAG = "connect_wlan";
std::string ssid;
std::string passphrase;
static EventGroupHandle_t wifi_event_group;
#define BLINK_GPIO GPIO_NUM_33
std::vector<string> ZerlegeZeile(std::string input)
{
std::vector<string> Output;
std::string delimiter = " =,";
input = trim(input, delimiter);
size_t pos = findDelimiterPos(input, delimiter);
std::string token;
while (pos != std::string::npos) {
token = input.substr(0, pos);
token = trim(token, delimiter);
Output.push_back(token);
input.erase(0, pos + 1);
input = trim(input, delimiter);
pos = findDelimiterPos(input, delimiter);
}
Output.push_back(input);
return Output;
}
void wifi_connect(){
wifi_config_t cfg = { };
strcpy((char*)cfg.sta.ssid, (const char*)ssid.c_str());
strcpy((char*)cfg.sta.password, (const char*)passphrase.c_str());
ESP_ERROR_CHECK( esp_wifi_disconnect() );
ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_STA, &cfg) );
ESP_ERROR_CHECK( esp_wifi_connect() );
}
void blinkstatus(int dauer)
{
gpio_reset_pin(BLINK_GPIO);
gpio_set_direction(BLINK_GPIO, GPIO_MODE_OUTPUT);
for (int i = 0; i < 3; ++i)
{
gpio_set_level(BLINK_GPIO, 0);
vTaskDelay(dauer / portTICK_PERIOD_MS);
gpio_set_level(BLINK_GPIO, 1);
vTaskDelay(dauer / portTICK_PERIOD_MS);
}
}
static esp_err_t event_handler(void *ctx, system_event_t *event)
{
switch(event->event_id) {
case SYSTEM_EVENT_STA_START:
blinkstatus(200);
wifi_connect();
break;
case SYSTEM_EVENT_STA_GOT_IP:
xEventGroupSetBits(wifi_event_group, CONNECTED_BIT);
blinkstatus(1000);
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
blinkstatus(200);
esp_wifi_connect();
xEventGroupClearBits(wifi_event_group, CONNECTED_BIT);
break;
default:
break;
}
return ESP_OK;
}
void initialise_wifi(std::string _ssid, std::string _passphrase)
{
ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL) );
wifi_event_group = xEventGroupCreate();
ssid = _ssid;
passphrase = _passphrase;
esp_log_level_set("wifi", ESP_LOG_NONE); // disable wifi driver logging
tcpip_adapter_init();
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );
ESP_ERROR_CHECK( esp_wifi_start() );
esp_err_t ret = tcpip_adapter_set_hostname(TCPIP_ADAPTER_IF_STA ,"icircuit");
if(ret != ESP_OK ){
ESP_LOGE(MAIN_TAG,"failed to set hostname:%d",ret);
}
xEventGroupWaitBits(wifi_event_group,CONNECTED_BIT,true,true,portMAX_DELAY);
tcpip_adapter_ip_info_t ip_info;
ESP_ERROR_CHECK(tcpip_adapter_get_ip_info(TCPIP_ADAPTER_IF_STA, &ip_info));
printf("IP : %s\n", ip4addr_ntoa(&ip_info.ip));
}
void LoadWlanFromFile(std::string fn, std::string &_ssid, std::string &_passphrase)
{
string line = "";
std::vector<string> zerlegt;
FILE* pFile;
fn = FormatFileName(fn);
pFile = fopen(fn.c_str(), "r");
if (pFile == NULL)
return;
char zw[1024];
fgets(zw, 1024, pFile);
// printf("%s", zw);
line = std::string(zw);
while ((line.size() > 0) || !(feof(pFile)))
{
// printf("%s", line.c_str());
zerlegt = ZerlegeZeile(line);
if ((zerlegt.size() > 1) && (toUpper(zerlegt[0]) == "SSID"))
_ssid = zerlegt[1];
if ((zerlegt.size() > 1) && (toUpper(zerlegt[0]) == "PASSWORD"))
_passphrase = zerlegt[1];
if (fgets(zw, 1024, pFile) == NULL)
{
line = "";
}
else
{
line = std::string(zw);
}
}
fclose(pFile);
}

View File

@@ -0,0 +1,13 @@
#ifndef CONNECT_WLAN_H
#define CONNECT_WLAN_H
#include <string>
#include "driver/gpio.h"
const int CONNECTED_BIT = BIT0;
void initialise_wifi(std::string _ssid, std::string _passphrase);
void LoadWlanFromFile(std::string fn, std::string &_ssid, std::string &_passphrase);
#endif