Connecting to blynk using the ESP8266 and arduino mega

Hi,
I have a simple code connecting to my local wifi using the ESP8266 and the arduino mega board.
currently the system connects to the wifi and gets an ip (that I can not change ) 197.7.1.150 this is out of my router subnet which is 192.168.1.1-255

I am using the library ESP8266_Lib.h and BlynkSimpleShieldEsp8266.h
is it possible to set the IP address I want or have my router give it one using DHCP?

if someone has an idea what else could be wrong I would be happy to hear
the code is below

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial


#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>

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

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "S#####";
char pass[] = "#########";
char server[] = "188.166.206.43";
int port = 80;

byte arduino_mac[] = { 0x0c, 0x13, 0x00, 0x03, 0x93, 0x02 };
char device_ip[]  = "192.168. 0.80";
char dns_ip[]     = "8.8.8.8";
char gateway_ip[] = "192.168.0.1";
char subnet_mask[] = "255.255.255.0";

// Hardware Serial on Mega, Leonardo, Micro...
#define EspSerial Serial3

// or Software Serial on Uno, Nano...
//#include <SoftwareSerial.h>
//SoftwareSerial EspSerial(2, 3); // RX, TX

// Your ESP8266 baud rate:
#define ESP8266_BAUD 115200

ESP8266 wifi(&EspSerial);

// Attach virtual serial terminal to Virtual Pin V0
WidgetTerminal terminal(V0);

// You can send commands from Terminal to your hardware. Just use
// the same Virtual Pin as your Terminal Widget
BLYNK_WRITE(V0)
{

  // if you type "Marco" into Terminal Widget - it will respond: "Polo:"
  if (String("Marco") == param.asStr()) {
    terminal.println("You said: 'Marco'") ;
    terminal.println("I said: 'Polo'") ;
  } else {

    // Send it back
    terminal.print("You said:");
    terminal.write(param.getBuffer(), param.getLength());
    terminal.println();
  }

  // Ensure everything is sent
  terminal.flush();
}

void setup()
{
  // Debug console
  Serial.begin(9600);

  // Set ESP8266 baud rate
  EspSerial.begin(ESP8266_BAUD);
  Serial.write("----------debug 1 -----");
  //wifi.setStationIp(String ip,String gateway,String netmask,uint8_t pattern)
  wifi.setStationIp(device_ip,gateway_ip,subnet_mask,3);
  delay(3000);
  Serial.write("----------debug 2 -----");
  Serial.print("Join AP success\r\n");
  Serial.print("IP: ");       
  Serial.println(wifi.getLocalIP().c_str());
  //Serial.print("FW Version:");
  //Serial.println(wifi.getVersion().c_str());
  //Serial.print("Status:[");
  //Serial.print(wifi.getIPStatus().c_str());
  //Serial.println("]");
  Serial.print("KICK: ");
  Serial.println(wifi.kick());
  //Serial.println("getAPList");
  //Serial.println(wifi.getAPList().c_str());
  //Serial.println("getIPStatus");
  //Serial.println(wifi.getIPStatus().c_str());



  Blynk.begin(auth,wifi,ssid,pass,server,port);
  // You can also specify server:
  //Blynk.begin(auth, wifi, ssid, pass, "blynk-cloud.com", 80);
  //Blynk.begin(auth, wifi, ssid, pass, IPAddress(188,166,206,43), 8080);
  Serial.write("----------debug 3 -----");

  // This will print Blynk Software version to the Terminal Widget when
  // your hardware gets connected to Blynk Server
  
  terminal.println(F("Blynk v" BLYNK_VERSION ": Device started"));
  terminal.println(F("-------------"));
  terminal.println(F("Type 'Marco' and get a reply, or type"));
  terminal.println(F("anything else and get it printed back."));
  terminal.flush();
  
}

void loop()
{
  Serial.println("ESP8266 board info:");
  Serial.write("---------------");
  Blynk.run();
}
1 Like

To be honest, it looks a bit of a mess to me, and far from simple!

The space here won’t help:

but what’s this all about?..

I’d start with one of the example sketches from the Sketch Builder:

Pete.

here is the code when it is very simple but still has the same problem. it uses the ip 197.7.1.150 instead of what I want it to use. how can I change it

#define BLYNK_PRINT Serial


#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>

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

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "YourNetworkName";
char pass[] = "YourPassword";

// Hardware Serial on Mega, Leonardo, Micro...
#define EspSerial Serial3


// Your ESP8266 baud rate:
#define ESP8266_BAUD 115200

ESP8266 wifi(&EspSerial);


void setup()
{
  // Debug console
  Serial.begin(9600);

  // Set ESP8266 baud rate
  EspSerial.begin(ESP8266_BAUD);
  delay(10);

  Blynk.begin(auth, wifi, ssid, pass);
  // You can also specify server:
  //Blynk.begin(auth, wifi, ssid, pass, "blynk-cloud.com", 80);
  //Blynk.begin(auth, wifi, ssid, pass, IPAddress(192,168,1,100), 8080);


}

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

the results are

[9] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.5.4 on Arduino Mega

[599] Connecting to Sayfanaltman
[3648] AT version:1.1.0.0(May 11 2016 18:09:56)
SDK version:1.5.4(baaeaebb)
Ai-Thinker Technology Co. Ltd.
Jun 13 2016 11:29:20
OK
[10781] +CIFSR:STAIP,"197.7.1.150"

[10788] Connected to WiFi

Blynk%20-%20FTFC

problem solved! when setting the correct IP address

char device_ip[]  = "192.168.1.80";
char dns_ip[]     = "8.8.8.8";
char gateway_ip[] = "192.168.1.1";
char subnet_mask[] = "255.255.255.0";

2 Likes

Backticks… NOT commas, apostrophes or quotes (for code formatting). I fixed it again.