A place users can post their projects. If you have a small project and would like your own dedicated place to post and have others chat about it then this is your spot.

User avatar
By Marc_H
#19821 Hi guys,
at first sorry for bad english! I hope you can help me ;)
I write a sketch to control my RGB-LED-Stripes via the ESP8266 and 3 N-Channel MOSFETs, it works fine but sometimes without any Error via Serial or hang up all ticker's the server don't answer anymore, i use a LED on port 14 turn on and of every second by a ticker and it works the hole time without a problem!?

My Code:
Code: Select all/*
 *  This sketch demonstrates how to set up a simple HTTP-like server.
 *  The server will set a GPIO pin depending on the request
 *    http://server_ip/gpio/0 will set the GPIO2 low,
 *    http://server_ip/gpio/1 will set the GPIO2 high
 *  server_ip is the IP address of the ESP8266 module, will be
 *  printed to Serial when the module is connected.
 */

#include <ESP8266WiFi.h>
#include <Ticker.h>

Ticker fader;
Ticker aLife;

const char* ssid = "SSID";
const char* password = "PW";

// Create an instance of the server
// specify the port to listen on as an argument
WiFiServer server(80);

unsigned int rColor;
unsigned int gColor;
unsigned int bColor;

boolean fadeEnabled = false;
boolean fadeUp = true;
unsigned int fadeState = 0;
double fadeSpeed = 5;

void aLifeLed()
{
  if (digitalRead(14) == true)
  {
    digitalWrite(14, LOW);
  }
  else
  {
    digitalWrite(14, HIGH);
  }
}

void fadeStart()
{
  fadeState = 0;
  fadeEnabled = true;
  fader.attach(fadeSpeed/1000, fadeChange);
}

void fadeStop()
{
  fadeEnabled = false;
  fader.detach();
}

void fadeChange()
{
  if (fadeState <= 0)
  {
    fadeUp = true;
  }
  else if (fadeState >= 1023)
  {
    fadeUp = false;
  }

  if (fadeUp == true)
  {
    fadeState++;
  }
  else
  {
    fadeState--;
  }
 
  analogWrite(15,fadeState);
  analogWrite(13,fadeState);
  analogWrite(12,fadeState);
}

void setup() {
  Serial.begin(115200);
  delay(10);

  pinMode(12, OUTPUT);
  pinMode(13, OUTPUT);
  pinMode(15, OUTPUT);

  pinMode(14, OUTPUT);

  aLife.attach(1, aLifeLed);

  ESP.wdtEnable(WDTO_0MS); // Allready working?
 
  // Connect to AcessPoint
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);

  Serial.println("Verbinde mit WLAN");

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("");
  Serial.print("WiFi \"");
  Serial.print(ssid);
  Serial.println("\" verbunden");
 
  // Print the IP address
  Serial.print("Eigene IP:");
  Serial.println(WiFi.localIP());

  // Start the server
  server.begin();
  Serial.println("Server wurde gestartet");

}

void loop() {

  // Check if a client has connected
  WiFiClient client = server.available();
  if (!client) {
    return;
  }
 
  // Wait until the client sends some data
  while(!client.available()){
    delay(10);
  }
 
  // Read the first line of the request
  String req = client.readStringUntil('\r');
  //Serial.println(req);
  client.flush();
 
  // Match the request
  if (req.indexOf("/rgb?") != -1)
  {
    String r = "";
    String g = "";
    String b = "";

    // read colors from request
    int rStart = req.indexOf("r=") + 2;
    int gStart = req.indexOf("g=") + 2;
    int bStart = req.indexOf("b=") + 2;

    int rEnd = req.indexOf("&",rStart);
    int gEnd = req.indexOf("&",gStart);
    int bEnd = req.indexOf(" ",rStart);
   
    r = req.substring(rStart,rEnd);
    g = req.substring(gStart,gEnd);
    b = req.substring(bStart,bEnd);

    // Set LED Color
    rColor = r.toInt();
    gColor = g.toInt();
    bColor = b.toInt();

    fadeStop();
   
    analogWrite(15,rColor);
    analogWrite(13,gColor);
    analogWrite(12,bColor);
  }
  else if (req.indexOf("/rgb") != -1)
  {
    // Show Site only
  }
  else if (req.indexOf("/fade?I") != -1)
  {
    fadeStart();
  }
  else if (req.indexOf("/fade?O") != -1)
  {
    fadeStop();
  }
  else {
    Serial.println("invalid request");
    client.flush(); // Need to flush?
    return;
  }

  client.flush(); // Need to flush?;

  // Prepare the response
  String s = "<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN'><html><head><meta http-equiv='Cache-Control' content='no-store' /><meta name='viewport' content='width=100'/><link rel='icon' type='image/png' href='data:image/gif;base64,R0lGODlhDwAPAKECAAAAzMzM/////wAAACwAAAAADwAPAAACIISPeQHsrZ5ModrLlN48CXF8m2iQ3YmmKqVlRtW4MLwWACH+H09wdGltaXplZCBieSBVbGVhZCBTbWFydFNhdmVyIQAAOw==";
  s += "' /><title>RGB ColorControl V0.1(Alpha)</title>";
  s += "<script>function changeBackColor () { var red = document.getElementById('r').value; var green = document.getElementById('g').value; var blue = document.getElementById('b').value; red = Math.round(red / 4); green = Math.round(green / 4); blue = Math.round(blue / 4);";
  s += " document.body.style.backgroundColor = 'rgb(' + red + ',' + green + ',' + blue + ')';} ";
  s += "</script></head><body onload='javascript:changeBackColor()'";
  s += "><form action='rgb' method='get'><center>";
  s += "<input type='range' id='r' name='r' value='";
  s += rColor;
  s += "' oninput='javascript:changeBackColor()' onchange='javascript:changeBackColor()' min='0' max='1023'/><br>";
  s += "<input type='range' id='g' name='g' value='";
  s += gColor;
  s += "' oninput='javascript:changeBackColor()' onchange='javascript:changeBackColor()' min='0' max='1023'/><br>";
  s += "<input type='range' id='b' name='b' value='";
  s += bColor;
  s += "' oninput='javascript:changeBackColor()' onchange='javascript:changeBackColor()' min='0' max='1023'/><br><br><button type='submit'>Erleuchten</button></center></form></body></html>";

  client.print(s);
  delay(1);
}


Serial Output:
Code: Select allÈHÌc/hÌøD�•„….ørlVerbinde mit WLAN
.....

WiFi "ChaosWG" verbunden
Eigene IP:192.168.178.37
Server wurde gestartet


I use this SDK-Board:
Image