With a freshly launched vscode, first upload works
subsequent uploads fail even after cleaning
console dump:
raise SerialException("could not open port {!r}: {!r}".format(self.portstr, ctypes.WinError()))
serial.serialutil.SerialException: could not open port 'COM5': WindowsError(5, 'Access is denied.')
*** [upload] Error 1
hardware: d1 r32 (esp32) w/ ch240
Because restarting vscode each time solves the issue for one upload, it's unlikely to be caused by code running on the esp32 but in any case I'll post the code here
`#include
// #include
const int OLED_SDA=32, OLED_SCL=33, MCP_SDA=32, MCP_SCL=33;
const char* ssid = "<3 2.4";
const char* password = "love8boubou";
int screenW = 128;
int screenH = 64;
int centerX = screenW/2;
int centerY = (screenH/2);
SSD1306Wire display(0x3c, OLED_SDA,OLED_SCL);
void initDisplay()
{
display.init();
display.flipScreenVertically();
display.clear();
}
void connectWIFI()
{
display.setTextAlignment(TEXT_ALIGN_CENTER);
display.setColor(WHITE);
display.setFont(ArialMT_Plain_10);
display.drawString(centerX, centerY, "Connecting to Wi-Fi");
display.display();
Serial.println("Connecting to Wi-Fi");
WiFi.mode(WIFI_STA);
WiFi.begin (ssid, password);
int waitCounter=0;
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
display.drawProgressBar(20, centerY+20, 128-40, 8, waitCounter);
waitCounter++;
waitCounter %=101;
display.display();
delay(100);
}
Serial.println("success!");
Serial.print("IP Address is: ");
Serial.println(WiFi.localIP());
}
// ------------ SERVER
WebServer server(80);
int LDRPin = 5;
int LDRReading = 0;
int milisInterval = 2000;
int count = 0;
void handleRoot() {
LDRReading = analogRead(LDRPin);
String html ="
server.send(200, "text/html", html);
}
void getData() {
//This is a JSON formatted string that will be served. You can change the values to whatever like.
// {"data":[{"dataValue":"1024"},{"dataValue":"23"}]} This is essentially what is will output you can add more if you like
LDRReading = analogRead(LDRPin);
String text2 = "{\"data\":[";
text2 += "{\"dataValue\":\"";
text2 += LDRReading;
text2 += "\"},";
text2 += "{\"dataValue\":\"";
text2 += count;
text2 += "\"}";
text2 += "]}";
server.send(200, "text/html", text2);
count++;
}
void Server_Task(void *parameter)
{
for (;;)
{
server.handleClient();
delay(10);
}
vTaskDelete(NULL);
}
void InitAndLaunchServer()
{
server.on("/", handleRoot);
server.on("/data", getData);
server.begin();
Serial.println("HTTP server started");
display.clear();
display.drawString(centerX, centerY-12, "connect to");
display.drawString(centerX, centerY, "http://" + WiFi.localIP().toString());
display.display();
xTaskCreatePinnedToCore(Server_Task, "web server", 8192, NULL, 2, NULL, 1);
}
// Adafruit_MCP23017 mcp;//0x20-0x27
void InitRelays()
{
MCP_begin(00,MCP_SDA, MCP_SCL);
// mcp.begin(00, MCP_SDA, MCP_SCL);
for (int i=0; i<16;i++)
MCP_pinMode(i,OUTPUT);
// mcp.pinMode(i,OUTPUT);
}
int relayIndex = 0;
void SwithRelay()
{
MCP_digitalWrite(relayIndex,!MCP_digitalRead(relayIndex));
// mcp.digitalWrite(relayIndex,!mcp.digitalRead(relayIndex));
Serial.println(String(relayIndex) +"-"+ MCP_digitalRead(relayIndex));
// Serial.println(String(relayIndex) +"-"+ mcp.digitalRead(relayIndex));
relayIndex++;
relayIndex %= 16;//RELAY_COUNT;
}
void UpdateDisplay4Relay()
{
for (int i=0; i<16; i++)
{
int R = 3;
int X = centerX - (R2+2)15/2 + i(R2+2);
int Y = screenH - R-1 ;
display.setColor(WHITE);
display.drawCircle(X, Y, R);
display.setColor(MCP_digitalRead(i)==0?BLACK:WHITE);
// display.setColor(mcp.digitalRead(i)==0?BLACK:WHITE);
display.fillCircle(X, Y, R-2);
display.setColor(WHITE);
}
display.display();
}
void setup()
{
Serial.begin(115200);
while (!Serial){delay(100);}
// initDisplay();
// connectWIFI();
// InitAndLaunchServer();
InitRelays();
}
unsigned long timerSwitchRelay = 0;
const int DELAY_BETWEEN_RELAY_SWITCH = 100;
void loop()
{
if (millis() > timerSwitchRelay)
{
SwithRelay();
// UpdateDisplay4Relay();
timerSwitchRelay = millis() + DELAY_BETWEEN_RELAY_SWITCH;
}
}
////////////////////////
// MCP23017
////////////////////////
#include
// registers
uint8_t i2caddr;
// minihelper to keep Arduino backward compatibility
static inline void wiresend(uint8_t x) {
Wire.write((uint8_t) x);
Wire.send(x);
}
static inline uint8_t wirerecv(void) {
return Wire.read();
return Wire.receive();
}
/**
/**
/**
/**
/**
// set the value for the particular bit
bitWrite(regValue,bit,pValue);
writeRegister(regAddr,regValue);
}
/**
Wire.begin(sda, scl);
// set defaults!
// all inputs on port A and B
writeRegister(MCP23017_IODIRA,0xff);
writeRegister(MCP23017_IODIRB,0xff);
}
/**
void MCP_digitalWrite(uint8_t pin, uint8_t d) {
uint8_t gpio;
uint8_t bit=bitForPin(pin);
// read the current GPIO output latches
uint8_t regAddr=regForPin(pin,MCP23017_OLATA,MCP23017_OLATB);
gpio = readRegister(regAddr);
// set the pin and direction
bitWrite(gpio,bit,d);
// write the new GPIO
regAddr=regForPin(pin,MCP23017_GPIOA,MCP23017_GPIOB);
writeRegister(regAddr,gpio);
}
void MCP_pullUp(uint8_t p, uint8_t d) {
updateRegisterBit(p,d,MCP23017_GPPUA,MCP23017_GPPUB);
}
uint8_t MCP_digitalRead(uint8_t pin) {
uint8_t bit=bitForPin(pin);
uint8_t regAddr=regForPin(pin,MCP23017_GPIOA,MCP23017_GPIOB);
return (readRegister(regAddr) >> bit) & 0x1;
}`
It seems that COM port is opened in another monitor. Try to reconnect board.
may be the alignment error or indentation error in the script... double check those lines ...
Most helpful comment
It seems that COM port is opened in another monitor. Try to reconnect board.