agregando petalot

This commit is contained in:
2023-05-29 04:38:34 -07:00
parent 20401231ba
commit 5d8b8c9db0
68 changed files with 11283 additions and 0 deletions

View File

@@ -0,0 +1,9 @@
# How to flash the firmware
- Install this https://github.com/esphome/esphome-flasher
- Open esphome-flasher
- Select your serial port where you have connected the wemos
- Go to browser and search and open petalot.ino.d1_mini.bin
- Press Flash ESP
- Connect your phone to the WIFI "PETALOT-XXXXXX" and browser to 192.168.4.1 (If the page does not load try deactivating your mobile data connection)
- (optional) Enter SSID, password, IP address, gateway and subnet for your local WIFI
- (optional) Open the IP address used in the previous step in your browser

View File

@@ -0,0 +1,195 @@
#include <ArduinoJson.h>
#include "FS.h"
String msg;
String status;
double To;
int Vo = 0;
int Tm;
double Kp;
double Ki;
double Kd;
double Max;
String LocalIP;
String Gateway;
String Subnet;
int R1;
char ssid[64];
char password[64];
String ifttt_event_name = "petalot_stopped";
String ifttt_api_key = "";
StaticJsonDocument<512> doc;
const char *confFile = "/config.json";
String printConf() {
String confString;
serializeJson(doc, confString);
return confString;
}
void saveConfiguration() {
SPIFFS.remove("/config.json");
File file = SPIFFS.open("/config.json", "w");
if (!file) {
msg = "Failed to create file";
return;
}
doc["To"] = To;
doc["Vo"] = Vo;
doc["Tm"] = Tm;
doc["Kp"] = Kp;
doc["Ki"] = Ki;
doc["Kd"] = Kd;
doc["Max"] = Max;
doc["ssid"] = ssid;
doc["password"] = password;
doc["LocalIP"] = LocalIP;
doc["Subnet"] = Subnet;
doc["Gateway"] = Gateway;
doc["R1"] = R1;
doc["ifttt_event_name"] = ifttt_event_name;
doc["ifttt_api_key"] = ifttt_api_key;
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 = 230;
Kp = 23.0;
Ki = 0.043;
Kd = 160.0;
Max = 200;
LocalIP = "";
Subnet = "255.255.255.0";
Gateway = "";
R1 = 10000;
ifttt_event_name = "";
ifttt_api_key = "";
saveConfiguration();
}
void readConfigurationSerial(){
StaticJsonDocument<512> docInput;
if (Serial.available() > 0)
{
// Deserialize the JSON document
DeserializationError error = deserializeJson(docInput, Serial);
if (error)
{
Serial.println(F("deserializeJson() failed: "));
Serial.println(error.c_str());
return;
} else {
//Serial.println("json ok");
doc=docInput;
//serializeJson(doc,Serial);
File file = SPIFFS.open("/config.json", "w");
if (!file) {
msg = "Failed to create file";
return;
}
if (serializeJson(doc, file) == 0) {
msg = "Failed to write to file";
}
file.close();
Serial.println("Configuration updated, restarting...");
analogWrite(PIN_HEATER, 0);
ESP.restart();
}
}
}
void loadConfiguration(bool reset=false) {
File file = SPIFFS.open("/config.json", "r");
if (!file) {
msg = "Failed to open /config.json";
Serial.println("Failed to open /config.json");
resetConfiguration();
analogWrite(PIN_HEATER, 0);
ESP.restart();
}
DeserializationError error = deserializeJson(doc, file);
if (error) {
msg = "Failed to read file, using default configuration";
Serial.println("Failed to read file, using default configuration");
resetConfiguration();
return;
}
file.close();
strlcpy(ssid,
doc["ssid"] | "",
sizeof(ssid));
strlcpy(password,
doc["password"] | "",
sizeof(password));
To = doc["To"] | 220;
//To = Tco;
Vo = doc["Vo"] | 40;
//Vo = Vco;
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>():200;
LocalIP = doc["LocalIP"] | "";
Subnet = doc["Subnet"] | "255.255.255.0";
Gateway = doc["Gateway"] | "";
R1 = doc["R1"] | 10000;
ifttt_event_name = doc["ifttt_event_name"] | "";
ifttt_api_key = doc["ifttt_api_key"] | "";
Serial.println();
Serial.println("To:Temperature");
Serial.println("Vo:Speed");
Serial.println("Tm:Maximum Temperature");
Serial.println("Kp:Kp");
Serial.println("Ki:Ki");
Serial.println("Kd:Kd");
Serial.println("R1:R1");
Serial.println("Max:Maximum value for MOSFET (0-255)");
Serial.println("ssid:SSID");
Serial.println("password:SSID Password");
Serial.println("LocalIP:IP address");
Serial.println("Subnet:Subnet");
Serial.println("Gateway:Gateway");
Serial.println("ifttt_event_name:IFTTT Event Name");
Serial.println("ifttt_api_key:IFTTT API Key");
Serial.println(printConf());
}
void initConf() {
if (!SPIFFS.begin()) {
msg = "Error mounting the file system";
return;
}
loadConfiguration();
}

View File

@@ -0,0 +1,118 @@
#include <PID_v1.h>
double T; //current temp
bool F = false;
bool Fc = false;
bool Fi = false;
double Output; //pid output
PID myPID(&T, &Output, &To, Kp, Ki, Kd, DIRECT);
double tempLastSample;
double tempLastFilament;
double tempLastNoFilament;
double tempLastStart;
//thermistor
float logR2, R2;
//steinhart-hart coeficients for thermistor
float c1 = 0.8438162826e-03, c2 = 2.059601750e-04, c3 = 0.8615484887e-07;
double Thermistor(float Volts) {
R2 = R1 * (1023.0 / (float)Volts - 1.0); //calculate resistance on thermistor
logR2 = log(R2);
T = (1.0 / (c1 + c2 * logR2 + c3 * logR2 * logR2 * logR2)); // temperature in Kelvin
T = T - 273.15; //convert Kelvin to Celcius
return T;
}
void start(){
if (tempLastStart==0){
status = "working";
V = Vo;
tempLastStart = millis();
if (tempLastStart==0) tempLastStart = 1;
}
}
void stop(){
status = "stopped";
V = 0;
tempLastStart = 0;
ifttt();
}
void initHotend(){
myPID.SetTunings(Kp, Ki, Kd);
myPID.SetOutputLimits(0,Max);
pinMode(LED_BUILTIN , OUTPUT);
pinMode(PIN_FILAMENT , INPUT);
if (status=="") start();
}
void hotendReadTempTask() {
if (status == "stopped" && myPID.GetMode() == AUTOMATIC){
myPID.SetMode(MANUAL);
Output = 0;
}
if (status == "working" && myPID.GetMode() != AUTOMATIC){
myPID.SetMode(AUTOMATIC);
}
if (millis() >= tempLastSample + 100)
{
Thermistor(analogRead(PIN_THERMISTOR)); //Volt to temp, update T
if (T > Tm || isnan(T)){
Output = 0;
} else {
myPID.Compute();
}
if (status == "working"){
start();
if (T > 150 || T > To + 20 ) {
digitalWrite(LED_BUILTIN , LOW);// target temperature ready
} else {
digitalWrite(LED_BUILTIN , !digitalRead(LED_BUILTIN));//reaching tarjet temp
}
} else {
digitalWrite(LED_BUILTIN , HIGH);
}
analogWrite(PIN_HEATER, Output);
Fc = digitalRead(PIN_FILAMENT);
if (Fc && !F) {
tempLastFilament = millis();
start();
}
if (!Fc && F) {
tempLastFilament = 0;
tempLastNoFilament = millis();
}
F = Fc;
if (Fc && tempLastFilament > 0 && millis() >= tempLastFilament + 3*1000){
Fi = true;
}
if (!Fc && Fi && tempLastNoFilament > 0 && millis() >= tempLastNoFilament + 500) { // no filament
stop();
tempLastNoFilament = 0;
Fi = false;
}
if (!Fc && !Fi && tempLastStart > 0 && millis() >= tempLastStart + 5*60*1000) { // no filament for 5 min
stop();
}
tempLastSample = millis();
}
}

View File

@@ -0,0 +1,38 @@
#include <ArduinoOTA.h>
bool debugMode = false;
void initOTA() {
ArduinoOTA.onStart([]() {
String type;
if (ArduinoOTA.getCommand() == U_FLASH) {
type = "sketch";
} else {
type = "filesystem";
}
Serial.println("Start updating " + type);
});
ArduinoOTA.onEnd([]() {
Serial.println("\nEnd");
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
});
ArduinoOTA.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) {
Serial.println("Auth Failed");
} else if (error == OTA_BEGIN_ERROR) {
Serial.println("Begin Failed");
} else if (error == OTA_CONNECT_ERROR) {
Serial.println("Connect Failed");
} else if (error == OTA_RECEIVE_ERROR) {
Serial.println("Receive Failed");
} else if (error == OTA_END_ERROR) {
Serial.println("End Failed");
}
});
ArduinoOTA.begin();
}

View File

@@ -0,0 +1,27 @@
#include "pins.hpp"
#include "conf.hpp"
#include "wifi.hpp"
#include "stepper.hpp"
#include "hotend.hpp"
#include "server.hpp"
#include "ota.hpp"
void setup() {
Serial.begin(115200);
delay(1000);
initConf();
initWiFi();
initOTA();
initHotend();
initStepper();
InitServer();
}
void loop() {
wifiTask();
server.handleClient();
hotendReadTempTask();
stepperRunTask();
ArduinoOTA.handle();
readConfigurationSerial();
}

View File

@@ -0,0 +1,9 @@
#define PIN_EN D1
#define PIN_STEP D2
#define PIN_DIR D3
#define PIN_THERMISTOR A0
#define PIN_HEATER D0
//v1 and v1.1
#define PIN_FILAMENT D7

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,31 @@
#include <AccelStepper.h>
int V;
AccelStepper stepper(AccelStepper::FULL2WIRE,PIN_STEP,PIN_DIR);
int stepsPerRevolution = 200;
bool stepperEnable = false;
void initStepper(){
stepper.setPinsInverted(true,false,true); //set enable pin inverted
stepper.setEnablePin(PIN_EN);
stepper.disableOutputs();
stepper.setMaxSpeed(40*stepsPerRevolution+1);
}
void stepperRunTask(){
if (status == "stopped" && stepperEnable) {
stepper.disableOutputs();
stepperEnable = false;
}
if (status == "working" && !stepperEnable) {
stepper.enableOutputs();
stepperEnable = true;
}
if (status == "working") {
stepper.setSpeed(Vo*stepsPerRevolution);
stepper.runSpeed();
}
}

View File

@@ -0,0 +1,102 @@
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <ESP8266mDNS.h>
bool apmode = false;
IPAddress local_ip;
double tempLastWifiTask;
double tempStartWifiTask;
bool wifiReady = false;
String IpAddress2String(const IPAddress& ipAddress)
{
return String(ipAddress[0]) + String(".") +\
String(ipAddress[1]) + String(".") +\
String(ipAddress[2]) + String(".") +\
String(ipAddress[3]) ;
}
void AP(){
apmode = true;
WiFi.disconnect(true);
IPAddress local_IP(192,168,4,1);
IPAddress gateway(192,168,4,1);
IPAddress subnet(255,255,255,0);
WiFi.softAPConfig(local_IP, gateway, subnet);
unsigned char mac[6];
char APNAME[40];
WiFi.macAddress(mac);
sprintf(APNAME, "PETALOT-%02X%02X%02X", mac[3], mac[4], mac[5]);
if (WiFi.softAP(APNAME)) {
Serial.println("AP Ready");
apmode = true;
}else{
Serial.println("AP Failed!");
}
}
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;
}
if (WiFi.status() == WL_CONNECT_FAILED) {
AP();
wifiReady=true;
return;
}
if (millis() >= tempStartWifiTask + 10000){
AP();
wifiReady=true;
return;
}
Serial.print(".");
tempLastWifiTask = millis();
}
}
}
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();
}
}
int ifttt(String value_1="", String value_2="", String value_3="")
{
if (ifttt_event_name=="" || ifttt_api_key=="") return 0;
WiFiClient client;
HTTPClient http;
http.begin(client, "http://maker.ifttt.com/trigger/" +
ifttt_event_name+"/with/key/"+ifttt_api_key+
"?value1="+value_1+"&value2="+value_2+"&value3="+value_3);
int httpCode = http.GET();
http.end();
return httpCode;
}