How do I use Servo with ESP8266_DirectConnect example code?

Hi all,
How do I merge the example Servo code with the ESP8266Direct_Connect example code?

Here is both examples:

/**************************************************************
 * Blynk is a platform with iOS and Android apps to control
 * Arduino, Raspberry Pi and the likes over the Internet.
 * You can easily build graphic interfaces for all your
 * projects by simply dragging and dropping widgets.
 *
 *   Downloads, docs, tutorials: http://www.blynk.cc
 *   Blynk community:            http://community.blynk.cc
 *   Social networks:            http://www.fb.com/blynkapp
 *                               http://twitter.com/blynk_app
 *
 * Blynk library is licensed under MIT license
 * This example code is in public domain.
 *
 **************************************************************
 * This example shows how to connect to ESP8266 directly
 * without using intermediate server.
 *
 * Please note: this example is for experienced users.
 *
 * !!!
 * NOTE: Blynk Apps currently do not support direct TCP connection.
 * Wait for new updates!
 *
 **************************************************************/

#define BLYNK_USE_DIRECT_CONNECT
//#define BLYNK_DEBUG
#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleUserDefined.h>

// Uncomment this to set WiFi to Access Point mode:
#define WIFI_MODE_AP

// Set these to your credentials.
const char ssid[]     = "Blynk-AP";    // WiFi name
const char password[] = "12345678";   // WiFi password

// Set port of the server
const int port = 8442;

// Auth token doesn't matter with direct-connect
char auth[] = "";

// Description of the dashboard
char profile[] = R"raw({"dashBoards":[
  {"id":1,"name":"Direct connect","boardType":"ESP8266","widgets":[
    {"id":2,"type":"DIGIT4_DISPLAY","pinType":"VIRTUAL","pin":9,"x":5,"y":1,"frequency":1000}
  ]}
]})raw";

// Virtual handlers for our widgets...

BLYNK_READ(V9) {
  Blynk.virtualWrite(9, millis() / 1000);
}

// Next goes the hard stuff: connection management, etc...

WiFiServer server(port);
WiFiClient client;

// This function is used by Blynk to receive data
size_t BlynkStreamRead(void* buf, size_t len)
{
  return client.readBytes((byte*)buf, len);
}

// This function is used by Blynk to send data
size_t BlynkStreamWrite(const void* buf, size_t len)
{
  return client.write((byte*)buf, len);
}

void setup()
{
  delay(1000);
  // Setup your connection here.
  Serial.begin(9600);
  Serial.println();

  Blynk.begin(auth);
  Blynk.setProfile(profile);

#ifdef WIFI_MODE_AP
  BLYNK_LOG("Configuring WiFi access point: %s", ssid);
  WiFi.softAP(ssid, password);
  Serial.print("AP IP address: ");
  Serial.println(WiFi.softAPIP());
#else
  BLYNK_LOG("Connecting to WiFi %s", ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println();
  Serial.print("STA IP address: ");
  Serial.println(WiFi.localIP());
#endif

  server.begin();
  BLYNK_LOG("Listening on port: %d", port);
}

void loop()
{
  client = server.available();
  // Make sure that Blynk.run() is called
  // only when the connection is established
  if (client) {
    Serial.print("Client connected from ");
    Serial.print(client.remoteIP());
    Serial.print(":");
    Serial.println(client.remotePort());

    // Wait for connection
    unsigned long start = millis();
    while (!client.connected() && (millis() - start < 1000)) {
      delay(5);
    }

    // Go!!!
    Blynk.startSession();
    while (client.connected()) {
      // Okay, handle Blynk protocol
      bool hasIncomingData = (client.available() > 0);
      // Tell Blynk if it has incoming data
      // (this allows to skip unneeded BlynkStreamRead calls)
      if (!Blynk.run(hasIncomingData)) {
        Serial.print("Error happened or disconnected.");
        break;
      }
    }
    client.stop();
    Serial.println("Client disconnected.");
  }
}
[/code]


And the other example script:

```/**************************************************************
 * Blynk is a platform with iOS and Android apps to control
 * Arduino, Raspberry Pi and the likes over the Internet.
 * You can easily build graphic interfaces for all your
 * projects by simply dragging and dropping widgets.
 *
 *   Downloads, docs, tutorials: http://www.blynk.cc
 *   Blynk community:            http://community.blynk.cc
 *   Social networks:            http://www.fb.com/blynkapp
 *                               http://twitter.com/blynk_app
 *
 * Blynk library is licensed under MIT license
 * This example code is in public domain.
 *
 **************************************************************
 * Rotate a servo using a slider!
 *
 * App dashboard setup:
 *   Slider widget (0...180) on V3
 *
 **************************************************************/

#define BLYNK_PRINT Serial
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#include <Servo.h>

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

Servo servo;

void setup()
{
  Serial.begin(9600); // See the connection status in Serial Monitor
  Blynk.begin(auth);
  servo.attach(9);
}

BLYNK_WRITE(V3)
{
  servo.write(param.asInt());
}

void loop()
{
  Blynk.run();
}

Hi @triops124,
Try this code (for Local Server)

//#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <Servo.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "blablablablabla";
Servo servo;
void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, "xxxxx", "yyyyyyy" ,IPAddress (000,000,000,000));
  servo.attach(2);
}

BLYNK_WRITE(V3)
{
  servo.write(param.asInt());
}
void loop()
{
  Blynk.run();
} 

Servo attached on GPIO2

Kind regards!

1 Like

Thanks for responding!
How do I fill in this line:
Blynk.begin(auth, "xxxxx", "yyyyyyy" ,IPAddress (000,000,000,000));
What goes in the “xxxxx” and “yyyyyy” and “000,000,000,000” sections?

The whole purpose of the script is to drive the servo as will be specified in the script, servo does one movement when blynk app is connected, a different movement when blynk app is disconnected. The reason why I am using blynk for servo driving is because I cannot get other code outside of blynk to drive a servo at all, and I wanted to give blynk a shot for this.

Hi @triops124,

“xxxxx”: Wi-Fi name
“yyyyyy”: Password
000,000,000,000: Local Server IP

Are you a Local Server guy?

@psoro I don’t do any ‘networking’ but didn’t one of the Blynk guys say the other day that the commas are full stops?

Hi again,
I don’t know what a “local server guy” is haha. I’m trying to run the code script on the esp8266 with the esp8266 acting as a wifi access point for the phone running the blynk app to connect to directly.
Thanks for answering the 1st part of my question! :smile:
The whole purpose of the script is to drive the servo as will be specified in the script, servo does one movement when blynk app is connected, a different movement when blynk app is disconnected. The reason why I am using blynk for servo driving is because I cannot get other code outside of blynk to drive a servo at all, and I wanted to give blynk a shot for this.

You cannot do that. There is, at this moment, no way to get the Blynk App to talk directly to the ESP running Blynk. You always need a server. I think it’s somewhere on the roadmap, but for now, this doesn’t work.

@Costas, I use commas and it works… but yes, someone said something regarding full stops and commas… I’ll have a look

Is the commas specific to you i.e. some countries use commas where other countries use fullstops?

In the UK a fish supper might be £7.50 but in some European countries it would be €9,25.

Darn! Ok. Thanks for the info

BTW, direct connect is not yet available on the app side :wink:

1 Like

@Costas, I know what you mean, I’m from Spain and for me it’s 9,25€ not 9.25€… anyway, I work with people from UK and Germany… Now I understand almost everything… lol

guys, if you use a string, the ip format is “10.0.0.1” with stops
if you use a function call, you have commas as each segment is sent as a function parameter

1 Like