NodeMCU / ping to Thingspeak / every 20sec

knihovna – pozor nešla mi na starém Arduino IDE ..

https://github.com/dancol90/ESP8266Ping

jsou tam i 2 základní příklady + zde příklad doplněný o odesílání pingu na ThingSpeak (zdarma kanál může přijímat co 15sekund a mít 3 miliony záznamů).

Dále to hodlám rozšířit na více IP adres, které pingám.

/*
ukladani pingu na thingspeak
*/

#include "ThingSpeak.h"
#include <ESP8266WiFi.h>
#include <ESP8266Ping.h>

const IPAddress remote_ip(192, 168, 13, 199); // my PC

#define SECRET_SSID "xxxxxxxxxx"    // replace MySSID with your WiFi network name
#define SECRET_PASS "xxxxxxxxxxx"  // replace MyPassword with your WiFi password

#define SECRET_CH_ID xxxxxxxxxx     // replace 0000000 with your channel number
#define SECRET_WRITE_APIKEY "xxxxxxxxxxxxxx" // replace XYZ with your channel write API Key


char ssid[] = SECRET_SSID;   // your network SSID (name) 
char pass[] = SECRET_PASS;   // your network password
int keyIndex = 0;            // your network key Index number (needed only for WEP)
WiFiClient  client;

unsigned long myChannelNumber = SECRET_CH_ID;
const char * myWriteAPIKey = SECRET_WRITE_APIKEY;

int number = 0;

void setup() {
  Serial.begin(115200);  // Initialize serial

  WiFi.mode(WIFI_STA); 
  ThingSpeak.begin(client);  // Initialize ThingSpeak
}

void loop() {

  // Connect or reconnect to WiFi
  if(WiFi.status() != WL_CONNECTED){
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(SECRET_SSID);
    while(WiFi.status() != WL_CONNECTED){
      WiFi.begin(ssid, pass);  // Connect to WPA/WPA2 network. Change this line if using open or WEP network
      Serial.print(".");
      delay(5000);     
    } 
    Serial.println("\nConnected.");
  }


  Serial.print("Pinging ip ");
  Serial.println(remote_ip);

  if(Ping.ping(remote_ip)) {
    Serial.println("Success!!");
    number = 1;
  } else {
    Serial.println("Error :(");
    number = 0;
  }

  
  
  // Write to ThingSpeak. There are up to 8 fields in a channel, allowing you to store up to 8 different
  // pieces of information in a channel.  Here, we write to field 1.
  int x = ThingSpeak.writeField(myChannelNumber, 1, number, myWriteAPIKey);
  if(x == 200){
    Serial.println("Channel update successful.");
  }
  else{
    Serial.println("Problem updating channel. HTTP error code " + String(x));
  }

 
  delay(20000); // Wait 20 seconds to update the channel again
}

 

Napsat komentář