BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Trouble getting a Local Server Running on Raspberry PI 3

localserver
#1

I am trying to use a local server running on a Raspberry Pi 3 connected to an AP which is a D-Link ACI 1200 router (DIR-842). This local server will be used in a high school robotics class to teach IoT concepts with NodeMCU ESP8266 12-E modules. Instead of using the Blynk server I would like to set up the local server to increase speed of the applications and give the students enough credits to build more complex apps. I had this local server up and running last fall at home but I am struggling to connect the ESP to it. I should also say that I am a high school teacher, not a programmer or engineer. My networking experience in skills are improving but lacking.

I am able to get the wifi client sketch below to work:

    This sketch establishes a TCP connection to a "quote of the day" service.
    It sends a "hello" message, and then prints received data.
*/

#include <ESP8266WiFi.h>

#ifndef STASSID
#define STASSID "myssid"
#define STAPSK  "mypass"
#endif

const char* ssid     = STASSID;
const char* password = STAPSK;

const char* host = "djxmmx.net";
const uint16_t port = 17;

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

  // We start by connecting to a WiFi network

  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  /* Explicitly set the ESP8266 to be a WiFi-client, otherwise, it by default,
     would try to act as both a client and an access-point and could cause
     network-issues with your other WiFi-devices on your WiFi-network. */
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);

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

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void loop() {
  Serial.print("connecting to ");
  Serial.print(host);
  Serial.print(':');
  Serial.println(port);

  // Use WiFiClient class to create TCP connections
  WiFiClient client;
  if (!client.connect(host, port)) {
    Serial.println("connection failed");
    delay(5000);
    return;
  }

  // This will send a string to the server
  Serial.println("sending data to server");
  if (client.connected()) {
    client.println("hello from ESP8266");
  }

  // wait for data to be available
  unsigned long timeout = millis();
  while (client.available() == 0) {
    if (millis() - timeout > 5000) {
      Serial.println(">>> Client Timeout !");
      client.stop();
      delay(60000);
      return;
    }
  }

  // Read all the lines of the reply from server and print them to Serial
  Serial.println("receiving from remote server");
  // not testing 'client.connected()' since we do not need to send data here
  while (client.available()) {
    char ch = static_cast<char>(client.read());
    Serial.print(ch);
  }

  // Close the connection
  Serial.println();
  Serial.println("closing connection");
  client.stop();

  delay(300000); // execute once every 5 minutes, don't flood remote service
}

However, when I upload this sketch with an auth token for a simple app on Blynk I can run it on the Blynk server but not on my local server. Here is that sketch:

#include <BlynkSimpleEsp8266.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "1f48...ca49";

// WiFi credentials.
char ssid[] = "myssid";
char pass[] = "mypass";

int pinV0;
int redLED = 2;
int count = 0;

void setup() {

  Blynk.begin(auth, ssid, pass);
  pinMode (redLED, OUTPUT);
  digitalWrite (redLED, HIGH);
  delay (1000);

  Serial.begin (9600);
  
}

void loop() {

  Serial.print("test ");
  Serial.println(count);
  delay(2000);
  count++;

  Blynk.run();

  if (pinV0 == 1){
    digitalWrite(redLED, HIGH);
  }

  else {
    digitalWrite(redLED, LOW);
  }
  
}

BLYNK_WRITE(V0){
  pinV0 = param.asInt(); // assigns incoming value button to pinV0 variable
}

My Blynk local server is running and is connected to the internet. What are some troubleshooting steps to take?

0 Likes

#2

but you connect to Blynk cloud with this sketch, not to your local server. add two more parameters. the IP address of your server and the port if you have other then 80

0 Likes

#3

Does than mean using this line:

Blynk.begin(auth, IPAddress(xxx,xxx,xxx,xxx), 8080);

My current IP for the server here is 192.168.0.140:9443

How would I modify that?

0 Likes

#4

I mean:

Blynk.begin(auth, SSID, pass, IPAddress(XXX,XXX,XXX,XXX), 8080);
0 Likes

#5

mine looks like this… to my local server

Blynk.begin(auth, IPAddress(192,168,1,237), 8080);

so yours would be

Blynk.begin(auth, IPAddress(192,168,0,140), 8080);

try it … tell us what you get.

0 Likes

#6

It appears that the IDE considers IPAddress as a char variable. Do I need to declare it somewhere?

error: initializing argument 2 of ‘void BlynkWifi::begin(const char*, const char*, const char*, const char*, uint16_t)’

void begin(const char* auth,
^

exit status 1
conversion from ‘IPAddress’ to ‘const char*’ is ambiguous

0 Likes

#7

Also, I have a password and ssid on this router.

0 Likes

#8

I just skip all that IPAddress preamble and build all my Local Server connection commands like this…

char auth[] = "xxxxxxxxxx";
char ssid[] = "xxxxxxxxxx";
char pass[] = "xxxxxxxxxx";
char server[] = "xxx.xxx.xxx.xxx";  // IP for your Local Server
int port = 8080;

void setup() {
  WiFi.begin(ssid, pass); 
  Blynk.config(auth, server, port);
  Blynk.connect();
}
0 Likes

#9
Blynk.begin(auth, SSID, pass, "XXX.XXX.XXX,XXX", 8080);

it is a host name string. not IPAddress type

0 Likes

#10

Thank you Gunner!

That did the trick. It is up and running on my local server.

I am noticing that the reaction time is slower than with the Blynk cloud server even though I was using the same AP for both. Any suggestions?

0 Likes

#11

I found the opposite, with the Local Server being much faster… primarily due the local networking instead of the internet. The server itself is plenty fast even on small RPi’s

0 Likes

#12

That is what I expected too. The local server should be faster because the requests are not going to the internet. Is there a way to measure the time (besides a stopwatch)?

0 Likes