Hi guys, I have a really stupid question here. But I've spent several hours trying to get this to work, so here it goes. I am trying to setup my ESP8266 board where I can toggle things between different modes via a wifi connection. I'm literally just trying to use 0, 1, 2, 3... nothing exotic.
I've read through a bunch and understand that using client.read() returns a char, so that's fine, I have to convert to an integer. My understanding is I should be able to do that simply by -'0'; so int mode = char c - '0' ;
Except that's not working. As I do serial.print(mode); it returns:
1
-35
-38
And later in the code when I call if(mode == 1) { stuff}
it never enters the loop. I've tried calling mode[0], or using atoi(), but I just can't seem to get it to work. This has to be a really simple issue that I'm struggling with. Thoughts?
> char c;
> int mode;
>void(loop){
> if (client) {
>
> while (client.connected()) {
>
> while (client.available() > 0) {
> c = client.read();
> mode = c - 48;
> delay(10);
>
>
> Serial.println(mode);
> }
>
> delay(100);
> }
>
> client.stop();
> Serial.println("Client disconnected");
> }
lets step back. how do you toogle? / How do you give the commands?
plain TCP/IP connection ?
A webserver based on ESP8266Wifi?
Or using the ESP8266Webserver library?
I'm using putty to open a window directly to it for the moment, typing 1, then enter. Getting 1 /r /n makes total sense with that. When I first started messing around with it, I tried using a WebServer, but it was going offline after I pressed the button a couple of times. Though perhaps that's more of my code crashing since it didn't know what to do with it now that I've progressed a bit further and found more issues.
As for toggling, perhaps that's a poor word choice. I have a global int mode = 0; and then in the void(loop){
if(mode == 0) {//do mode 0 stuff}
if (mode == 1) {//do mode 1 stuff}
//etc.
With the info you guys have given me, I'm going to go back a bit and try the webserver again, now that I understand that its returning a character instead of an integer, that might explain why it was going offline after a press.
Okay, so going back to http build, I ended up pretty much cut and pasting and rewriting all of my code to some extend. The good news, it works now. I am able to control the LED modes from my phone/computer. The bad news, there's a huge lack in any of the color changing of them. For this particular use case, that's not a big deal, but I'm sure there's a way to do it that it's not doing that. So I'm going to post my code in its entirety if anyone wants to say where I'm going wrong.
To help out a bit, mode2 is the one that's supposed to have a nice rotating color feature. I think it has to do with putting it in the if(!client){} area. But I found that if it wasn't in that area, it wouldn't do any color changing at all.
>
> #include <ESP8266WiFi.h>
> #include <FastLED.h>
> #define NUM_LEDS 245
>
> CRGB leds[NUM_LEDS];
> const int ledPin = 4;
>
> const char* ssid = "SSID"; /* Add your router's SSID */
> const char* password = "PW"; /*Add the password */
>
> int gpio4Value;
> int gpio5Value;
>
> const int TC_init = 1; // Time constant for staying on a single color before shifting
> int TC = TC_init; // Time constant countdown until next shift
>
>
> int hue1 = 0; // Hue color value
> int hue2 = 128; // Hue color value
> int sat_def = 255; // Default Saturation value
> int val_def = 127; // Default Value value
> int MaxLEDShelf = 20; // Maximum number of LEDs on one side of the shelf
>
>
> int mode1value = 0;
> int mode2value = 0;
> int mode3value = 0;
> int mode4value = 0;
>
>
> WiFiServer espServer(80); /* Instance of WiFiServer with port number 80 */
> /* 80 is the Port Number for HTTP Web Server */
>
> int shelfA[13][20] = { //Brute force Left side of the shelf LED definition
> { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 },
> { 10, 11, 12, 13, 14, 15, 16, 17, 18 },
> { 21, 22, 23, 24, 25, 26, 27, 28 },
> { 29, 30, 31, 32, 33, 34, 35, 36, 37 },
> { 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53 },
> { 56, 57, 58, 59, 60, 61, 62, 63 },
> { 64, 65, 66, 67, 68, 69, 70, 71 },
> { 72, 73, 74, 75, 76, 77, 78 },
> { 81, 82, 83, 84, 85, 86 },
> { 87, 88, 89, 90, 91, 92, 93, 94 },
> { 95, 96, 97, 98, 99, 100, 101 },
> { 103, 104, 105, 106, 107, 108, 109, 110 }
> };
>
> int shelfB[13][20] = {
> //Brute force right side of the shelf LED definition
> { 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245 },
> { 225, 226, 227, 228, 229, 230, 231, 232, 233, 234 },
> { 214, 215, 216, 217, 218, 219, 220, 221, 222 },
> { 205, 206, 207, 208, 209, 210, 211, 212, 213 },
> { 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202 },
> { 178, 179, 180, 181, 182, 183, 184, 185 },
> { 170, 171, 172, 173, 174, 175, 176, 177 },
> { 163, 164, 165, 166, 167, 168, 169 },
> { 155, 156, 157, 158, 159, 160 },
> { 147, 148, 149, 150, 151, 152, 153, 154 },
> { 140, 141, 142, 143, 144, 145, 146 },
> { 131, 132, 133, 134, 135, 136, 137, 138 },
> };
>
>
>
> //Color the left side of a shelf
> void ColorTheStrandA(int LitShelf, int hue, int sat, int val) {
> for (int i = 0; i < MaxLEDShelf; i++) {
> if (i != 0 && int(shelfA[LitShelf][i]) == 0) {
> i++;
> } else {
> leds[int(shelfA[LitShelf][i])] = CHSV(hue, sat, val);
> }
> }
> }
>
> //Color the right side of the shelf
> void ColorTheStrandB(int LitShelf, int hue, int sat, int val) {
> for (int i = 0; i < MaxLEDShelf; i++) {
> if (i != 0 && int(shelfB[LitShelf][i]) == 0) {
> i++;
> } else {
> leds[int(shelfB[LitShelf][i])] = CHSV(hue, sat, val);
> }
> }
> }
>
> void mode1() {
> for (int i = 0; i <= 12; i++) {
> for (int j = 0; j <= MaxLEDShelf; j++) {
> leds[shelfA[i][j]] = CHSV(255, 0, 0);
> leds[shelfB[i][j]] = CHSV(255, 0, 0);
> }
> }
> Serial.println("mode 1");
> FastLED.show();
> }
>
> void mode2() {
>
> ColorTheStrandA(0, hue2, sat_def, val_def);
> ColorTheStrandA(1, hue1, sat_def, val_def);
> ColorTheStrandA(2, hue2, sat_def, val_def);
> ColorTheStrandA(3, hue1, sat_def, val_def);
> ColorTheStrandA(4, hue2, sat_def, val_def);
> ColorTheStrandA(5, hue1, sat_def, val_def);
> ColorTheStrandA(6, hue2, sat_def, val_def);
> ColorTheStrandA(7, hue1, sat_def, val_def);
> ColorTheStrandA(8, hue2, sat_def, val_def);
> ColorTheStrandA(9, hue1, sat_def, val_def);
> ColorTheStrandA(10, hue2, sat_def, val_def);
> ColorTheStrandA(11, hue1, sat_def, val_def);
> ColorTheStrandA(12, hue2, sat_def, val_def);
>
> ColorTheStrandB(0, hue2, sat_def, val_def);
> ColorTheStrandB(1, hue1, sat_def, val_def);
> ColorTheStrandB(2, hue2, sat_def, val_def);
> ColorTheStrandB(3, hue1, sat_def, val_def);
> ColorTheStrandB(4, hue2, sat_def, val_def);
> ColorTheStrandB(5, hue1, sat_def, val_def);
> ColorTheStrandB(6, hue2, sat_def, val_def);
> ColorTheStrandB(7, hue1, sat_def, val_def);
> ColorTheStrandB(8, hue2, sat_def, val_def);
> ColorTheStrandB(9, hue1, sat_def, val_def);
> ColorTheStrandB(10, hue2, sat_def, val_def);
> ColorTheStrandB(11, hue1, sat_def, val_def);
> ColorTheStrandB(12, hue2, sat_def, val_def);
> Serial.println("mode 2");
> FastLED.show();
> }
>
>
> void mode3() {
> for (int i = 0; i <= 12; i++) {
> for (int j = 0; j <= MaxLEDShelf; j++) {
> leds[shelfA[i][j]] = CHSV(255, 0, 128);
> leds[shelfB[i][j]] = CHSV(255, 0, 128);
> }
> }
> Serial.println("mode 3");
> FastLED.show();
> }
>
> void mode4() {
> for (int i = 0; i <= 12; i++) {
> ColorTheStrandA(i, hue1, sat_def, val_def);
> ColorTheStrandB(i, hue2, sat_def, val_def);
> }
> Serial.println("mode 4");
> FastLED.show();
> }
>
> void setup() {
>
> Serial.begin(115200); /* Begin Serial Communication with 115200 Baud Rate */
> /* Configure GPIO4 and GPIO5 Pins as OUTPUTs */
>
> /* Set the initial values of GPIO4 and GPIO5 as LOW*/
> /* Both the LEDs are initially OFF */
>
>
> Serial.print("\n");
> Serial.print("Connecting to: ");
> Serial.println(ssid);
> WiFi.mode(WIFI_STA); /* Configure ESP8266 in STA Mode */
> WiFi.begin(ssid, password); /* Connect to Wi-Fi based on above SSID and Password */
> while (WiFi.status() != WL_CONNECTED) {
> Serial.print("*");
> delay(500);
> }
> Serial.print("\n");
> Serial.print("Connected to Wi-Fi: ");
> Serial.println(WiFi.SSID());
> delay(100);
> /* The next four lines of Code are used for assigning Static IP to ESP8266 */
> /* Do this only if you know what you are doing */
> /* You have to check for free IP Addresses from your Router and */
> /* assign it to ESP8266 */
> /* If you are confirtable with this step, please un-comment the next four lines *
> /* if not, leave it as it is and proceed */
> IPAddress ip(192, 168, 0, 184);
> IPAddress gateway(192, 168, 0, 1);
> IPAddress subnet(255, 255, 255, 0);
> WiFi.config(ip, gateway, subnet);
> //delay(2000);
> Serial.print("\n");
> Serial.println("Starting ESP8266 Web Server...");
> espServer.begin(); /* Start the HTTP web Server */
> Serial.println("ESP8266 Web Server Started");
> Serial.print("\n");
> Serial.print("The URL of ESP8266 Web Server is: ");
> Serial.print("http://");
> Serial.println(WiFi.localIP());
> Serial.print("\n");
> Serial.println("Use the above URL in your Browser to access ESP8266 Web Server\n");
>
> FastLED.addLeds<WS2812B, ledPin, RGB>(leds, NUM_LEDS);
> }
>
> void loop() {
> WiFiClient client = espServer.available(); /* Check if a client is available */
> if (!client) {
> if (mode2value == 1) {
> mode2();
> }
> if (mode1value == 1) {
> mode1();
> }
> if (mode3value == 1) {
> mode3();
> }
> if (mode4value == 1) {
> mode4();
> }
>
> if (TC <= 0) {
> hue1 = hue1 + 8;
> } else {
> TC--;
> }
>
> if (hue1 > 255) {
> hue1 = hue1 - 255;
> }
> hue2 = hue1 + 128;
> if (hue2 > 255) {
> hue2 = hue2 - 255;
> }
> }
>
> Serial.println("New Client!!!");
>
> String request = client.readStringUntil('\r'); /* Read the first line of the request from client */
> Serial.println(request); /* Print the request on the Serial monitor */
> /* The request is in the form of HTTP GET Method */
> client.flush();
>
> /* Extract the URL of the request */
> /* We have four URLs. If IP Address is 192.168.1.6 (for example),
> * then URLs are:
> * 192.168.1.6/GPIO4ON and its request is GET /GPIO4ON HTTP/1.1
> * 192.168.1.6/GPIO4OFF and its request is GET /GPIO4OFF HTTP/1.1
> * 192.168.1.6/GPIO5ON and its request is GET /GPIO5ON HTTP/1.1
> * 192.168.1.6/GPIO4OFF and its request is GET /GPIO5OFF HTTP/1.1
> */
> /* Based on the URL from the request, turn the LEDs ON or OFF */
> if (request.indexOf("/MODE1") != -1) {
> Serial.println("Mode 1 Activated");
> mode1value = 1;
> mode2value = 0;
> mode3value = 0;
> mode4value = 0;
> mode1();
> }
> if (request.indexOf("/MODE2") != -1) {
> Serial.println("Mode 2 Activated");
> mode1value = 0;
> mode2value = 1;
> mode3value = 0;
> mode4value = 0;
> mode2();
> }
> if (request.indexOf("/MODE3") != -1) {
> Serial.println("Mode 3 Activated");
> mode1value = 0;
> mode2value = 0;
> mode3value = 1;
> mode4value = 0;
> mode3();
> }
> if (request.indexOf("/MODE4") != -1) {
> Serial.println("Mode 4 Activated");
> mode1value = 0;
> mode2value = 0;
> mode3value = 0;
> mode4value = 1;
> mode4();
> }
>
> /* HTTP Response in the form of HTML Web Page */
> client.println("HTTP/1.1 200 OK");
> client.println("Content-Type: text/html");
> client.println(); // IMPORTANT
> client.println("<!DOCTYPE HTML>");
> client.println("<html>");
> client.println("<head>");
> client.println("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
> client.println("<link rel=\"icon\" href=\"data:,\">");
> /* CSS Styling for Buttons and Web Page */
> client.println("<style>");
> client.println("html { font-family: Courier New; display: inline-block; margin: 0px auto; text-align: center;}");
> client.println(".button {border: none; color: white; padding: 10px 20px; text-align: center;");
> client.println("text-decoration: none; font-size: 25px; margin: 2px; cursor: pointer;}");
> client.println(".button1 {background-color: #13B3F0;}");
> client.println(".button2 {background-color: #3342FF;}");
> client.println("</style>");
> client.println("</head>");
>
> /* The main body of the Web Page */
> client.println("<body>");
> client.println("<h2>ESP8266 Web Server</h2>");
>
> if (mode1value == 0) {
>
> client.println("<p>Mode 1: Off</p>");
> client.print("<p><a href=\"/MODE1\"><button class=\"button button1\">Click for Mode 1</button></a></p>");
> } else {
>
> client.println("<p>Mode 1: On</p>");
> client.print("<p><a href=\"/MODE1\"><button class=\"button button2\">Click for Mode 1</button></a></p>");
> }
>
> if (mode2value == 0) {
> client.println("<p>Mode 2: Off</p>");
> client.print("<p><a href=\"/MODE2\"><button class=\"button button1\">Click for Mode 2</button></a></p>");
> } else {
>
> client.println("<p>Mode 2: On</p>");
> client.print("<p><a href=\"/MODE2\"><button class=\"button button2\">Click for Mode 2</button></a></p>");
> }
>
> if (mode3value == 0) {
> client.println("<p>Mode 3: Off</p>");
> client.print("<p><a href=\"/MODE3\"><button class=\"button button1\">Click for Mode 3</button></a></p>");
> } else {
>
> client.println("<p>Mode 3: On</p>");
> client.print("<p><a href=\"/MODE3\"><button class=\"button button2\">Click for Mode 3</button></a></p>");
> }
>
> if (mode4value == 0) {
> client.println("<p>Mode 4: Off</p>");
> client.print("<p><a href=\"/MODE4\"><button class=\"button button1\">Click for Mode 4</button></a></p>");
> } else {
>
> client.println("<p>Mode 4: On</p>");
> client.print("<p><a href=\"/MODE4\"><button class=\"button button2\">Click for Mode 4</button></a></p>");
> }
>
> client.println("</body>");
> client.println("</html>");
> client.print("\n");
>
> delay(1);
> /* Close the connection */
> client.stop();
> Serial.println("Client disconnected");
> Serial.print("\n");
> }