Multi services widget, or multiple connections support for esp 8266

Hi all, in some of my projects I need to connect to the server (for example wunderground.com to retrieve the weather), in order to do this I must necessarily or disconnect myself from Blynk or insert a microcontroller via bridge retrieves and sends this data . It would be very helpful if you could have a widget that fetches .json or XML to pass this data as of thing speak, or otherwise support the multiple connections of esp8266 although I do not know what can be exploited with little ram microcontrollers

@Thomas until recently we were regularly picking up the time from time.nist.gov for our ESP8266 whilst connected to Blynk. We have now changed it to just pick up the time prior to connecting to Blynk and then adjusting our clock with millis().

Are you sure you can’t access wunderground.com whilst connected to Blynk?

ESP for sure supports multiple connections not sure why you have problems.

Honestly I have not tried it, I assumed that by setting a multiple connection Blynk had problems, I’ll try thanks

Hi, I have done some testing but I could not run the wunderground connection while connected to Blynk , if I try to set up multiple connections , I do not connect to the server Blynk , a sign that the library does not support multi-connection mode , as I suspected , @Costas how do you query a ntp server while connected to Blynk ?

@Thomas not sure where we picked the code up from but I think it is a standard ESP example.

Take a look at the NTPClient.ino at https://github.com/sandeepmistry/esp8266-Arduino/blob/master/esp8266com/esp8266/libraries/ESP8266WiFi/examples/NTPClient/NTPClient.ino and the sketch below. We originally had it checking the time at intervals (30 or 60 seconds and then changed it to just check the time on start up as the Arduino takes care of the time with millis once it is given the starting time. In the coming weeks we might remove it altogether from our system as we now have the RTC widget which checks the time every 10 minutes. Shout if you want to see how NTPClient.ino was integrated into a full Blynk sketch .

#include <ESP8266WiFi.h>
#include <WiFiUdp.h>

char ssid[] = "Office";  //  your network SSID (name)
char pass[] = "xxxxxxxxx";       // your network password

unsigned int localPort = 2390;      // local port to listen for UDP packets
//IPAddress timeServer(129, 6, 15, 28); // time.nist.gov NTP server
IPAddress timeServerIP; // time.nist.gov NTP server address
const char* ntpServerName = "time.nist.gov";
//const char* ntpServerName = "ntps1-0.cs.tu-berlin.de"; // 130,149,17,21
//const char* ntpServerName = "ptbtime1.ptb.de"; // 192,53,103,108
const int NTP_PACKET_SIZE = 48; // NTP time stamp is in the first 48 bytes of the message
byte packetBuffer[ NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets
WiFiUDP udp; // A UDP instance to let us send and receive packets over UDP

unsigned long sendNTPpacket(IPAddress& address) // send an NTP request to the time server at the given address
{
  memset(packetBuffer, 0, NTP_PACKET_SIZE); // set all bytes in the buffer to 0
  packetBuffer[0] = 0b11100011;   // LI, Version, Mode
  packetBuffer[1] = 0;     // Stratum, or type of clock
  packetBuffer[2] = 6;     // Polling Interval
  packetBuffer[3] = 0xEC;  // Peer Clock Precision
  packetBuffer[12]  = 49;
  packetBuffer[13]  = 0x4E;
  packetBuffer[14]  = 49;
  packetBuffer[15]  = 52;

  udp.beginPacket(address, 123); //NTP requests are to port 123
  udp.write(packetBuffer, NTP_PACKET_SIZE);
  udp.endPacket();
}

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

  
  Serial.print("Connecting to "); // We start by connecting to a WiFi network
  Serial.println(ssid);
  WiFi.begin(ssid, pass);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

  udp.begin(localPort);
  Serial.print("Local port: ");
  Serial.println(udp.localPort());
}

void ntptime(){
  
  WiFi.hostByName(ntpServerName, timeServerIP);  //get a random server from the pool

  sendNTPpacket(timeServerIP); // send an NTP packet to a time server
  delay(1000); // wait to see if a reply is available
  
  int cb = udp.parsePacket();
  if (!cb) {
    Serial.println("no packet yet");
  }
  else {

    udp.read(packetBuffer, NTP_PACKET_SIZE); // read the packet into the buffer

    unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
    unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
    unsigned long secsSince1900 = highWord << 16 | lowWord;
    unsigned long gmtplus2 = secsSince1900 + 7200;  // PJA for Cyprus
    const unsigned long seventyYears = 2208988800UL;
    unsigned long epoch = gmtplus2 - seventyYears;

    Serial.print("The time in Cyprus is ");       // UTC is the time at Greenwich Meridian (GMT)
    Serial.print((epoch  % 86400L) / 3600); // print the hour (86400 equals secs per day)
    Serial.print(':');
    if ( ((epoch % 3600) / 60) < 10 ) {
      // In the first 10 minutes of each hour, we'll want a leading '0'
      Serial.print('0');
    }
    Serial.print((epoch  % 3600) / 60); // print the minute (3600 equals secs per minute)
    Serial.print(':');
    if ( (epoch % 60) < 10 ) {
      // In the first 10 seconds of each minute, we'll want a leading '0'
      Serial.print('0');
    }
    Serial.println(epoch % 60); // print the second
  }
  
  delay(10000); // wait ten seconds before asking for the time again  
  
}

void loop()
{
  ntptime();

}
1 Like

Thanks @Costas but What I can not seem to do is work with Blynk and make requests to wunderground in the same sketch, use the esp 8266 as a shield for Arduino Mega but I am unable to make them work together.

Here the link to test sketch

Do I need all 3 files or just one of them?

All 3 files I’ve used multitab, thanks for your precious support

Hi @Thomas we did take a look at your sketch but we didn’t get very far. I believe the api.wunderground.com provides data in json or xml format. When we were obtaining the time from time servers at periodic intervals I don’t think we were using multiple connection mode. We simply requested the data (as per the standard ntp example) and it was provided.

We are now starting to think about low power and deep sleep so we will probably be making use of Blynk.disconnect and Blynk.connect but we were not previously using disconnect for accessing the ntp server during our Blynk sessions.

How often do you need to collect the weather data for your sketch to function? Couldn’t you simply issue a disconnect from Blynk command say for 5 seconds every hour, pick up the required weather data and then issue a reconnect to Blynk command?

1 Like

Hello, is the format is json and the only method I have found is to disconnect myself Blynk and reconnect after downloading weather information, I can give you some advice, when you do blink. disconnect is also performed the disconnection of both the serial it is connected to the esp the serial debug, it is therefore necessary to re-initialize the ports through Serial.begin before groped a new connection or blynk.begin for a connection Blynk

Thanks for your insight into the serial port behaviour on Blynk disconnect etc but as we are looking at deep sleep it involves a complete reset of the ESP so out sketches will periodically restart afresh.