Backup feed emoncms 8.4.0

Hi all, here I am with the program code I made to backup the feed from one emocms to another (on 2 different sites).

I tested the program on a Wemos D1 and it works great.

Turned on day and night in 1 week makes 1 year backup with 20 seconds resolution.

It must be adapted to one’s needs, however as it is written part of making the backup from the date 6 June 2018 at 0.00.00 (just change the timestamp value)

Every 2 hours it stores the timestamp value so that if it goes off it doesn’t start over.

First of all you will need to create the feed manually with the timestamp of the departure date and then activate the program.

always sorry for the bad translation but I’m using a translator.

Available for information.

Greetings

#include <ESP8266WiFi.h>
#include <EEPROM.h>

// feed 
String feed = "b2";                                   //nome del feed in emocms da fare il backup
int IdFeed = 32;                                      //id del feed equivalente nel nuovo emoncms

// parametri Emoncms
const char* host = "xxxxxx.altervista.org";           //indirizzo sito 1 (leggere i dati)
String apikeyW = "---";                               //api key WRITE
String apikeyR = "---";                               //api key READ
int node = 0; 

const char* host2 = "yyyyyyyy.altervista.org";        //indirizzo sito 2 (scrivere i dati)
String apikeyW2 = "---";                              //api key WRITE 2
String apikeyR2 = "---";                              //api key READ 2

float valore_feed;
float valore_feed_old;
int valore_feed_uguale = 0;

// parametri rete
const char* ssid = "ssid";                            //WIFI nome
const char* password = "pw";                          //WIFI password

//Variabili interne
long timestamp = 1528236000;                          //valore "timestamp Unix" riferito al 6 giugno 2018 ore 0.00.00  ----> inzio data di backup
long timestamp_old;
long timestamp_step = 20;                             //tempo campionamento (20 secondi)
int address = 0;
int ok_lettura = 0;
int ok_scrittura = 0;

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

  EEPROM.begin(12);

  Serial.println(F("Backup dal sito xxxx a yyyy"));
  Serial.println();
  
  WiFi.begin(ssid, password);
  delay(1000);

  Serial.print(F("Connessione a rete WiFi: "));
  Serial.print(ssid);

  //loop di connessione al WiFi (esce quando si è collegato)
  while (WiFi.status() != WL_CONNECTED) {
    delay(400);
    Serial.print(F("."));
  }
  Serial.println(F("riuscita"));
  
  //EEPROM.put(address, timestamp);                   // da programmare la prima volta, poi riprogrammare commentando!!
  //EEPROM.commit();
  
  Serial.println(F(""));
  Serial.print(F("Backup del feed a partire dal timestamp : "));

  EEPROM.get(address, timestamp);                     // recupera l'ultimo timestamp salvato nella memoria (salva il timestamp ogni 2 ore)
  Serial.println(timestamp);
  timestamp_old = timestamp;

}


void loop() {
  emocR();

  if (timestamp - timestamp_old > 7200) {             // (7200 secondi = 2 ore) tempo di salvataggio timestamp
    Serial.println(F("Memorizzazione Timestamp"));
    EEPROM.put(address, timestamp);
    EEPROM.commit();
    timestamp_old = timestamp;
  }
  
  if (valore_feed == valore_feed_old) {               // se il valore del feed appena letto è uguale al feed precedente non invia il dato ad emoncms nuovo
    valore_feed_uguale = 1;
    Serial.println(F("Valore Feed uguale alla lettura precedente - salto"));
  } else {
    valore_feed_old = valore_feed;
    if (valore_feed_uguale == 1) emocW_old();
    if (valore_feed_uguale == 0) emocW();
  }

  if (ok_lettura == 1) {
    timestamp = timestamp + timestamp_step;
    ok_lettura = 0;
  }
  
  Serial.print(F("Timestamp : "));
  Serial.println(timestamp);
  //delay(1000);
}


// Invia valori Emoncms Mimmos : ---http://mimmos.altervista.org/emoncms/input/post?time=1667508125&node=0&json={power1:100,power2:200,power3:300}
void emocW(){
  Serial.println(F(""));
  Serial.println(F("Trasmissione dati Emoncms yyyyyy"));
  WiFiClient client;
  if (!client.connect(host,80)) {
    Serial.println(F("Connessione fallita"));
    return;
  }
 
//  digitalWrite (led, HIGH);
  
  client.print(String("GET ") + "/emoncms/input/post?node=0&time=");
  client.print(timestamp);
  client.print("&json={");
  client.print(feed);
  client.print(":");
  client.print(valore_feed);
  client.print("}&apikey=");
  client.print(apikeyW);
  client.print(" HTTP/1.1\r\n");
  client.print("Host: yyyyyy.altervista.org\r\n");
  client.print("Connection: close\r\n\r\n");
  

  while (client.connected() || client.available()) {  
    if (client.available()) {
      String line = client.readStringUntil('\n');
      Serial.println(line);
    }
  }

client.stop();
valore_feed_old = valore_feed;
//digitalWrite (led, LOW);
}


void emocW_old(){
  Serial.println(F(""));
  Serial.println(F("Trasmissione dati Emoncms yyyyyy OLD"));
  WiFiClient client;
  if (!client.connect(host,80)) {
    Serial.println(F("Connessione fallita"));
    return;
  }
 
//  digitalWrite (led, HIGH);
  
  client.print(String("GET ") + "/emoncms/input/post?node=0&time=");
  client.print(timestamp - timestamp_step);
  client.print("&json={");
  client.print(feed);
  client.print(":");
  client.print(valore_feed_old);
  client.print("}&apikey=");
  client.print(apikeyW);
  client.print(" HTTP/1.1\r\n");
  client.print("Host: yyyyyyy.altervista.org\r\n");
  client.print("Connection: close\r\n\r\n");
  

  while (client.connected() || client.available()) {  
    if (client.available()) {
      String line = client.readStringUntil('\n');
      Serial.println(line);
    }
  }

client.stop();
valore_feed_uguale = 0;
//digitalWrite (led, LOW);
}


// Richiesta valori a Emoncms Filippis
void emocR() {
  int capoLINEA = 0;
  int trovato = 0;
  Serial.println(F(""));
  Serial.println(F("Ricezione dati da Emoncms xxxxxxx"));
  WiFiClient client;
  if (!client.connect(host2,80)) {
    Serial.println("Connessione fallita");
    return;
  }
 
//  digitalWrite (led, HIGH);
  
  client.print(String("GET ") + "/emoncms840/feed/data.json?id=");
  client.print(IdFeed);
  client.print("&start=");
  client.print(timestamp);
  client.print("000&end=");
  client.print(timestamp + timestamp_step);
  client.print("000&dp=1&apikey=");
  client.print(apikeyR2);
  client.print(" HTTP/1.1\r\n");
  client.print("Host: xxxxxxx.altervista.org\r\n");
  client.print("Connection: close\r\n\r\n");


  while (client.connected() || client.available()) {  
    if (client.available()) {
      String line = client.readStringUntil('\n');
      Serial.println(line);
      //capoLINEA = capoLINEA+1;
      if (line.substring(15, 16) == ",") {
        valore_feed = line.substring(16, 23).toFloat();
        Serial.print(F("TROVATO : "));
        Serial.println(valore_feed);
      }
    }
  }

  client.stop();
  ok_lettura = 1;
//  digitalWrite (led, LOW);
}