Skip to content

Commit

Permalink
wifi task
Browse files Browse the repository at this point in the history
  • Loading branch information
function3d committed Jan 10, 2023
1 parent 1b67a3e commit 72d849e
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 50 deletions.
13 changes: 7 additions & 6 deletions Firmware/petalot/conf.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ String printConf() {
}

void saveConfiguration() {

SPIFFS.remove("/config.json");
File file = SPIFFS.open("/config.json", "w");
if (!file) {
msg = "Failed to create file";
Expand All @@ -57,23 +57,24 @@ void saveConfiguration() {
if (serializeJson(doc, file) == 0) {
msg = "Failed to write to file";
}

Serial.println(printConf());
file.close();
analogWrite(PIN_HEATER, 0);
ESP.restart();

}

void resetConfiguration(){
Serial.println("reset");
strcpy(ssid, "");
strcpy(password, "");
To = 220;
Vo = 40;
Tm = 250;
Tm = 230;
Kp = 23.0;
Ki = 0.043;
Kd = 160.0;
Max = 210;
Max = 200;
LocalIP = "";
Subnet = "255.255.255.0";
Gateway = "";
Expand Down Expand Up @@ -146,11 +147,11 @@ void loadConfiguration(bool reset=false) {
//To = Tco;
Vo = doc["Vo"] | 40;
//Vo = Vco;
Tm = doc["Tm"] | 250;
Tm = doc["Tm"] | 230;
Kp = doc["Kp"]?doc["Kp"].as<double>():23.0;
Ki = doc["Ki"]?doc["Ki"].as<double>():0.043;
Kd = doc["Kd"]?doc["Kd"].as<double>():160.0;
Max = doc["Max"]?doc["Max"].as<double>():210;
Max = doc["Max"]?doc["Max"].as<double>():200;
LocalIP = doc["LocalIP"] | "";
Subnet = doc["Subnet"] | "255.255.255.0";
Gateway = doc["Gateway"] | "";
Expand Down
2 changes: 0 additions & 2 deletions Firmware/petalot/hotend.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ void initHotend(){
}




void hotendReadTempTask() {
if (status == "stopped" && myPID.GetMode() == AUTOMATIC){
myPID.SetMode(MANUAL);
Expand Down
10 changes: 3 additions & 7 deletions Firmware/petalot/petalot.ino
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
#include "ota.hpp"

void setup() {


Serial.begin(115200);
delay(1000);
initConf();
Expand All @@ -20,12 +18,10 @@ void setup() {
}

void loop() {
wifiTask();
server.handleClient();
//if (!apmode) {
hotendReadTempTask();
stepperRunTask();
//}
hotendReadTempTask();
stepperRunTask();
ArduinoOTA.handle();
readConfigurationSerial();

}
Binary file modified Firmware/petalot/petalot.ino.d1_mini.bin
Binary file not shown.
2 changes: 1 addition & 1 deletion Firmware/petalot/pins.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
#define PIN_THERMISTOR A0
#define PIN_HEATER D0

//v1 and v1.1 v0.9 D8
//v1 and v1.1
#define PIN_FILAMENT D7
75 changes: 41 additions & 34 deletions Firmware/petalot/wifi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
bool apmode = false;
IPAddress local_ip;



double tempLastWifiTask;
double tempStartWifiTask;
bool wifiReady = false;
String IpAddress2String(const IPAddress& ipAddress)
{
return String(ipAddress[0]) + String(".") +\
Expand Down Expand Up @@ -36,48 +37,54 @@ void AP(){
}
}

void initWiFi()
{

if (!ssid){
AP();
//status = "stopped";
void wifiTask(){
if (!wifiReady){
if (millis() >= tempLastWifiTask + 500){
if (WiFi.status() == WL_CONNECTED) {
MDNS.begin("Petalot");
Serial.println();
Serial.println(WiFi.localIP());
wifiReady=true;
return;
} else {
IPAddress localip;
localip.fromString(LocalIP.c_str());
IPAddress subnet;
subnet.fromString(Subnet.c_str());
IPAddress gatewayip;
gatewayip.fromString(Gateway.c_str());

WiFi.begin(ssid, password); //Conexión a la red
if (!WiFi.config(localip, gatewayip, subnet,IPAddress(8, 8, 8, 8))) {
Serial.println("config wifi ips failed");
}
unsigned long wifiConnectStart = millis();
while (WiFi.status() != WL_CONNECTED) {
}
if (WiFi.status() == WL_CONNECT_FAILED) {
AP();
//status = "stopped";
Serial.println("Error conectado a la wifi");
wifiReady=true;
return;
}
if (millis() - wifiConnectStart > 10000) {
if (millis() >= tempStartWifiTask + 10000){
AP();
//status = "stopped";
wifiReady=true;
return;
}
delay(500);
Serial.print(".");
tempLastWifiTask = millis();

}
if (!MDNS.begin("Petalot"))
{
Serial.println("Error iniciando mDNS");
}
//Serial.println("mDNS iniciado");
Serial.println();
Serial.println(WiFi.localIP());
}
}

void initWiFi()
{

if (!ssid){
AP();
wifiReady=true;
return;
} else {
IPAddress localip;
localip.fromString(LocalIP.c_str());
IPAddress subnet;
subnet.fromString(Subnet.c_str());
IPAddress gatewayip;
gatewayip.fromString(Gateway.c_str());
Serial.print("Connecting to ");
Serial.print(ssid);
WiFi.begin(ssid, password); //Conexión a la red
if (!WiFi.config(localip, gatewayip, subnet,IPAddress(8, 8, 8, 8))) {
Serial.println("config wifi ips failed");
}
tempStartWifiTask = millis();
}
}

Expand Down
Binary file modified STLs/Spool holder.stl
Binary file not shown.

0 comments on commit 72d849e

Please sign in to comment.