arduino2/ESP32_AD_test/ESP32_AD_test.ino
2021-02-10 18:05:49 +01:00

134 lines
3.8 KiB
C++

#include <WiFi.h>
#include "SSD1306.h" // alias for `#include "SSD1306Wire.h"
// jfs Wemos lolin32
// jfs Heltec WiFi kit 32 (weisses Board)
// #define HELTEC
#define DEBUG
// Initialize the OLED display using Wire library
#ifdef HELTEC
SSD1306 display(0x3c, 4, 15);
#else
SSD1306 display(0x3c, 5, 4);
#endif
//// WIFI
const int mxSize=4;
String ssids[mxSize] ={"GAST","pipanet","FRITZ!Box Gastzugang","WLAN-DE8245"};
String ssidp[mxSize] = {"passatvr6","passatvr6","praxis123","4955065570896956"};
boolean conok =false;
void netfound(int i){
display.clear();
display.setColor(BLACK);
display.fillRect(0, 0, 128, 10);
display.setColor(WHITE);
display.drawString(0,0,String(i));
display.drawString(20,0,"networks found");
display.display();
}
boolean init_wifi(){
boolean ok = false;
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(100);
int n = WiFi.scanNetworks();
Serial.println("scan done");
if (n == 0) {
Serial.println("no networks found");
netfound(0);
} else {
Serial.print(n);
Serial.println(" networks found");
netfound(n);
for (int i = 0; i < n; ++i) {
for (int p=0;p<mxSize;p++){
if (WiFi.SSID(i).equals(ssids[p])){
String pp = ssidp[p];
String ss = WiFi.SSID(i);
WiFi.begin(ss.c_str(), pp.c_str());
i=n;
ok = true;
}
}
delay(10);
}
}
return ok;
}
const int PWM_FREQ = 5000; // freq limits depend on resolution
const byte PWM_CHANNEL = 0; // channels 0-15
const byte PWM_RES = 16; //resolution 1-16 bits
const byte PWM_PIN = 39;
void setup() {
Serial.begin(230400);
display.init();
display.flipScreenVertically();
display.clear();
display.drawString(0, 0, "Starting...");
display.display();
while (!init_wifi()){
delay(200);
}
display.drawString(0, 10, "Connecting to WiFi...");
display.display();
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
display.drawString(0, 20, "\nWiFi connected, IP address: ");
display.drawString(0, 30, WiFi.localIP().toString());
//server.begin(); // server starten
display.display();
ledcAttachPin(PWM_PIN, PWM_CHANNEL); // assign pin to channel
ledcSetup(PWM_CHANNEL, PWM_FREQ, PWM_RES);
}
int duty_cycle=0;
void generator(){
if (duty_cycle < 2550){
if (duty_cycle%5==0){
int i = duty_cycle;
ledcWrite(PWM_CHANNEL, i);
}
duty_cycle += 1;
}
else {
duty_cycle=0;
}
}
void loop() {
generator();
double volt2 = ReadVoltage(36)*127000/27000;
//double volt2 = ReadVoltage0dB(36);
//Serial.print(volt2,1);
//Serial.print(" ");
Serial.println(volt2);
display.clear();
display.drawString(0, 10,"IP "+ WiFi.localIP().toString());
display.drawString(0,20,"Volt 36 "+String(volt2));
//display.drawString(0,30,"Volt 14 "+String(volt2));
display.display();
delay(1);
}
double ReadVoltage0dB(byte pin){
double reading = analogRead(pin); // Reference voltage is 3v3 so maximum reading is 3v3 = 4095 in range 0 to 4095
if(reading < 1 || reading > 4095) return 0;
return reading*3.3/4095;
}
double ReadVoltage(byte pin){
double reading = analogRead(pin); // Reference voltage is 3v3 so maximum reading is 3v3 = 4095 in range 0 to 4095
if(reading < 1 || reading > 4095) return 0;
// return -0.000000000009824 * pow(reading,3) + 0.000000016557283 * pow(reading,2) + 0.000854596860691 * reading + 0.065440348345433;
return -0.000000000000016 * pow(reading,4) + 0.000000000118171 * pow(reading,3)- 0.000000301211691 * pow(reading,2)+ 0.001109019271794 * reading + 0.034143524634089;
} // Added an improved polynomial, use either, comment out as required