Hi, i have a arduino nano 33 iot that drives a servo to open and close my window cover via apple home and homebridge. I already posted about this some days ago, but i read somewhere its better to post a new topic instead of asking questions in the old one.
It works like a charm initially, but after some time it stops responds to the HTTP requests. Cant figure out why.
The LED still keeps blinking like normal so the code isnt stuck somewhere.
The router still shows the arduino connected to Wifi, so its not a disconnect issue i think.
I am 100% clueless.
Code:
#include <Stepper.h>
#include <WiFiNINA.h>
#define STEPPER_MOTOR_STEPS_PER_REVOLUTION 2048
#define SHUTTER_Y_MAX 50000
const int stepsPerRevolution = STEPPER_MOTOR_STEPS_PER_REVOLUTION;
const int rolePerMinute = 15;
int led_counter = 0;
bool led_status = 1;
char ssid[] = "wifiname";
char pass[] = "wifipass";
int status = WL_IDLE_STATUS;
int shutter_location_y = 0; //top = 0
int target_location_y = 0;
char completeRequest[1024]="";
char path[256]="";
WiFiServer server(80);
Stepper myStepper(stepsPerRevolution, 6,8,7,9);
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
myStepper.setSpeed(rolePerMinute);
//Setup wifi
connectToWiFi();
server.begin();
}
void connectToWiFi(){
status = WiFi.status();
while (status != WL_CONNECTED) {
status = WiFi.begin(ssid, pass);
delay(5000);
}
}
void loop() {
led_counter += 1;
if(led_counter>=50000){
digitalWrite(LED_BUILTIN,led_status);
led_counter = 0;
led_status = !led_status;
connectToWiFi();
}
WiFiClient client = server.available();
if (client) {
memset(completeRequest,0,1024);
memset(path,0,256);
if (client.available()) {
if(client.read((unsigned char *)completeRequest,1024)<1)return;
getPath();
if(strncmp("/settarget",path,10)==0){
int target_percentage = getTargetInt();
int target_steps = percentageToSteps(target_percentage);
if(target_steps<0 || target_steps > SHUTTER_Y_MAX)return;
target_location_y = target_steps;
client.println("HTTP/1.1 200 OK\r\n");
}
else if(strncmp("/getpos",path,7)==0){
client.println("HTTP/1.1 200 OK\r\nContent-type:application/json\r\n");
client.print("{\"position_y\":");
client.print(stepsToPercentage(shutter_location_y));
client.print("}");
}
else if(strncmp("/gettarget",path,10)==0){
client.println("HTTP/1.1 200 OK\r\nContent-type:application/json\r\n");
client.print("{\"position_y_target\":");
client.print(stepsToPercentage(target_location_y));
client.print("}");
}
else if(strncmp("/getstatus",path,10)==0){
client.println("HTTP/1.1 200 OK\r\nContent-type:application/json\r\n");
client.print("{\"position_y_state\":");
client.print(getPositionState());
client.print("}");
}else{
client.println("HTTP/1.1 200 OK\r\n");
}
client.println();
client.stop();
}
}
client.stop();
tickMovement();
}
void getPath(){
const char *start = strchr(completeRequest, ' ') +1;
const char *end = strchr(start, ' ');
memcpy(path,start,end-start);
}
int getTargetInt(){
return atoi(strchr(path+1, '/') +1);
}
void tickMovement(){
if(shutter_location_y==target_location_y)return;
int stepDirection = target_location_y > shutter_location_y ? 1:-1;
myStepper.step(stepDirection);
shutter_location_y += stepDirection;
//the servo will run super hot if i dont do this
if(shutter_location_y==target_location_y){
digitalWrite(6,LOW);
digitalWrite(7,LOW);
digitalWrite(8,LOW);
digitalWrite(9,LOW);
}
}
int percentageToSteps(int percentage){
return ((int) SHUTTER_Y_MAX) - ((((int)SHUTTER_Y_MAX) * percentage) / 100);
}
int stepsToPercentage(int steps){
return 100 - (steps * 100 ) / (int) SHUTTER_Y_MAX;
}
int getPositionState(){
if(shutter_location_y==target_location_y)return 2;
return shutter_location_y<target_location_y?1:0;
}