4

RTL8170 is a new module, which is in many ways better than the old ESP8266 module. We are able to flash this module with Arduino using an Ameba Writer, and it should be able to support any code that is supported by the ESP (with some adjustments). It has been officially supported by Realtek's environment but it does not use all the GPIO (only 2). Luckily, pvvx developed an environment for Arduino IDE, which we can install and use through this tutorial: https://hackaday.io/project/19163/logs

I have uploaded a modified SimpleWebServerWiFi sketch from Ameba RTL8170 examples by both trying the official Realtek and pvvx's environments, and using LEDs to troubleshoot (I don't have an SPI/I2C LCD or a Serial-to-USB) I found the module being able to connect to my Samsung's hotspot but unable to communicate with the module because server.available() never turns true. When my module connects to my Samsung Note 4's hotspot, I see its IP (192.168.43.123) and fail to connect to it via a browser (on the phone or with a Windows laptop). Android port scanning apps indicate that the target IP address of the module has no open ports. Code below.

Update2: I've made adjustments to the code according to suggestions, but it still didn't respond. Updated code below:

   /*-----------------------------------------------------------------|
  |  Build using pvvx's enviroment: https://github.com/pvvx/RtlDuino  |
  |  Tutorial: https://hackaday.io/project/19163/logs                 |
  |=================================================================*/

#include <WiFi.h>

char ssid[] = "WiFi_SSID";      //  your network SSID (name)
char pass[] = "password";   // your network password
int keyIndex = 0;   // your network key Index number (needed only for WEP)
String currentLine = "";   // make a String to hold incoming data from the client

int status = WL_IDLE_STATUS;
WiFiServer server(80);

void setup() {
  pinMode(1,OUTPUT);


  //Serial.begin(38400);      // initialize serial communication
  pinMode(LED_BUILTIN, OUTPUT);      // set the LED pin mode
  // check for the presence of the shield:
  if (WiFi.status() == WL_NO_SHIELD) {
    //Serial.println("WiFi shield not present");
    while (true); // don't continue    
  }
  String fv = WiFi.firmwareVersion();
  if (fv != "1.1.0") {
    //Serial.println("Please upgrade the firmware");
  }

  // attempt to connect to Wifi network:
  while (status != WL_CONNECTED) {
    //Serial.print("Attempting to connect to Network named: ");
    //Serial.println(ssid);         // print the network name (SSID);

    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:
    status = WiFi.begin(ssid, pass);
    // wait 0.2 seconds for connection:
    delay(200);
  }

  server.begin();                   // start the web server on port 80

}


void loop() {

  WiFiClient client = server.available();   // listen for incoming clients

  if (client) {                             // if you get a client,

    //Serial.println("new client");           // print a message out the serial port
    while (client.connected()) {            // loop while the client's connected

      if (client.available()) {             // if there's bytes to read from the client,  

        char c = client.read();             // read a byte, then
        //Serial.write(c);                    // print it out the serial monitor
        if (c == '\n') {                    // if the byte is a newline character

          // if the current line is blank, you got two newline characters in a row.
          // that's the end of the client HTTP request, so send a response:
          if (currentLine.length() == 0) {

            // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
            // and a content-type so the client knows what's coming, then a blank line:
            client.println("HTTP/1.1 200 OK");
            client.println("Content-Length: 88");
            client.println("Content-Type: text/html");
            client.println("Connection: Closed");
            client.println();

            // the content of the HTTP response follows the header:
            client.print("Click <a href=\"/H\">here</a> turn the BUILTIN_LED on<br>");
            client.print("Click <a href=\"/L\">here</a> turn the BUILTIN_LED off<br>");

            // The HTTP response ends with another blank line:
            client.println();
            // break out of the while loop:
            //break;
          } else {    // if you got a newline, then clear currentLine:
            //digitalWrite(1,HIGH); // Doesn't turn ON
            currentLine = "";
          }
        } else if (c != '\r') {  // if you got anything else but a carriage return character,
          //digitalWrite(1,HIGH); // Turns ON
          currentLine += c;      // add it to the end of the currentLine
        }
        //digitalWrite(1,HIGH); // Turns ON
        // Check to see if the client request was "GET /H" or "GET /L":
        if (currentLine.endsWith("GET /H")) {
          digitalWrite(LED_BUILTIN, HIGH);               // GET /H turns the LED on
        }
        if (currentLine.endsWith("GET /L")) {
          digitalWrite(LED_BUILTIN, LOW);                // GET /L turns the LED off
        }
      }
    }
    //digitalWrite(1,HIGH); // Turns ON
    // close the connection:
    client.stop();
  }
}

Update 1:

I am still unable to fix the code. If no one can fix this code, please feel free to provide a totally new code.

P.S.: If this question keeps stagnating, I'll remove it and recreate it.

Any help is appreciated. Thank you!

2 Answers 2

1

Inside the while loop, where you give the link to click you have a break that prevents the handling of the answer from the client.

I would reconsider the use of that break and structure the code to not use it at all.

2
  • How would I correctly rewrite this code?
    – DemSec
    Commented Mar 5, 2017 at 0:07
  • Could you please give a code snippet example? Thanks!
    – DemSec
    Commented Mar 5, 2017 at 0:33
0

If you must use String, get its declaration out of loop(). You're only fragmenting your heap and reducing available RAM. Instead, declare it in the file scope and reserve sufficient space for a request line with the reserve() method in your setup().

Though response headers are supposed to be case-insensitive, it's still a good idea to follow the 'standard', spaces and all, like this for instance:

HTTP/1.1 200 OK
Content-Length: 88
Content-Type: text/html
Connection: Closed

Try sending all the fields above, especially the content length. And get rid of the break like mico said. There's no way your builtin LED will turn on or off if you keep breaking right after you respond to the client.

2
  • With these adjustments to the code, it still didn't respond
    – DemSec
    Commented Mar 21, 2017 at 8:34
  • (Updated code above)
    – DemSec
    Commented Mar 21, 2017 at 8:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.