🔍

mqtt

// This example sketch connects to the public shiftr.io instance and sends a message on every keystroke.
// After starting the sketch you can find the client here: https://www.shiftr.io/try.
//
// Note: If you're running the sketch via the Android Mode you need to set the INTERNET permission
// in Android > Sketch Permissions.
//
// by Joël Gähwiler
// https://github.com/256dpi/processing-mqtt
  
 import processing.data.*;
  
import mqtt.*;
  
MQTTClient client;
 
float energy_val = 1111;
float today = 0;
float yesterday = 0;
 
float _prev_energy = 0;
 
float current_verbrauch  = 0;

PGraphics HISTOGRAM_BUFF;
 
int current_TIME_ID = 0;
int current_VERBRAUCH = 0;
int itick = -1;
 
// 8640 requests per day! // request each 10s
int request_amount = 8640; 
 
int[] verbauche = new int[request_amount];
  
class Adapter implements MQTTListener {
  void clientConnected() {
  
    client.subscribe("tele/tasmota_C9ECC1/SENSOR");
        println("client connected");
  }
  
  void messageReceived(String topic, byte[] payload) {
      
  // println("new message: " + topic + " - " + new String(payload));
     
    String str = new String(payload);
     
     JSONObject j = parseJSONObject(str);
   JSONObject energyo = j.getJSONObject("ENERGY");
      
        // Extract the timestamp from the JSON
    String timestamp = j.getString("Time");
    
    String daytime = timestamp.split("T")[1];
    
  
     current_TIME_ID = getMessageCountForTime(daytime);
   
     
    energy_val = energyo.getFloat("Total") ;
    
   current_verbrauch = energy_val - _prev_energy;
     
   today = (int)energyo.getFloat("Today");
     
   yesterday = energyo.getFloat("Yesterday");
 
    
    current_VERBRAUCH = (int)current_verbrauch*1000;
    
    DRAW_ON_BUFFER(current_TIME_ID, (int)(current_verbrauch*1000));
     
     
    //println (  energyo.getFloat("Current")[0] );
     
     /*
      if(itick>-1){
        verbauche[itick] = (int)(current_verbrauch*2000);
      }
      itick++;
       
      if(itick>799){
        itick=0;
        clearHISTOGRAM();
      }
       */
      _prev_energy = energy_val;
     
   /// println(energyo);
  }
  
  
  
  
  void connectionLost() {
    println("connection lost");
  }
}
  
Adapter adapter;
  
void setup() {
   
    size(800,600);
  background(0);
  noCursor();
  smooth();
   
  HISTOGRAM_BUFF = createGraphics(8640, 128);
   
  adapter = new Adapter();
  client = new MQTTClient(this, adapter);
  
  
 client.connect("mqtt://172.22.22.22", "processing");
 
frameRate(1);
 
   
}
 
 void clearHISTOGRAM(){
  
     for(int i=0;i< 800; i++){
     verbauche[i] = 0;
  }
  
 }
  
void draw() {
 
  UPDATE();

}
 void UPDATE(){
 
     background(0);
   fill(0);
   textSize(9);
     
  //textAlign(CENTER);
  //text( round(current_verbrauch * 1000) +"", width/2,height/2);
   
   text("STROMVERBRAUCH HEUTE BIS JETZT: " + today +" / GESTERN GESAMT: " + yesterday, 20,20);
  
 
   
 
    // ---------------------------------------------
     // --------------CURRENT VERBRAUCH--------------------
      // ---------------------------------------------
    pushMatrix();
   translate(width-10,80);
   fill(255);
   rotate(radians(90));
   text("JETZT", 0,0 );
   popMatrix();
   
   image(HISTOGRAM_BUFF,-current_TIME_ID+width-20,20);
   
   stroke( 44);
   line(0,200,width,200);
    fill(111);
   text("STROMVERBRAUCH DER LETZTEN ZWEI STUNDEN", 10,14);
   
     // ---------------------------------------------
     // --------------DAYLONG VERBRAUCH--------------------
      // ---------------------------------------------
   
    image(HISTOGRAM_BUFF,20,250,width-20,200);
    
     
    
   float bit_h = float(width)/24.0;
    for(int i=0;i<24;i++){
     
     fill(255);
     text(i  , i*bit_h +4, 230);
   }
   stroke( 44);
   line(0,400,width,400);
   fill(111);
   text("STROMVERBRAUCH IM VERLAUF DES TAGES", 10,214);
   
    // ---------------------------------------------
     // --------------LASTDAYS VERBRAUCH--------------------
      // ---------------------------------------------
  
 
  textSize(14);
   fill(255);
  text( "VERBRAUCH MOMENTAN = " + current_VERBRAUCH, 20,430 );
  textSize(11);
   fill(200);
  text( "ENTSPRICHT ~ " + current_VERBRAUCH*200 + " x HANDY LADEGERÄTEN", 20,460 );
  text( "ENTSPRICHT ~ " + current_VERBRAUCH*2 + " x FERNSEHGERÄTEN", 20,480 );
  text( "ENTSPRICHT ~ " + current_VERBRAUCH + " x WASSERKOCHERN", 20,500 );
  
  // -----------------------------
  fill(255);
   textSize(14);
   text( "VERBRAUCH SEIT TAGESBEGIN = " + today, 290,430 );
   fill(200);
    textSize(11);
    text( "ENTSPRICHT ~ " + round(today*200) + " x HANDY LADEGERÄTEN", 290,460 );
  text( "ENTSPRICHT ~ " + round(today*2) + " x FERNSEHGERÄTEN", 290,480 );
  text( "ENTSPRICHT ~ " + round(today) + " x WASSERKOCHERN", 290,500 );
  
   fill(255);
    textSize(14);
   text( "VERBRAUCH GESTERN " + yesterday, 590,430 );
   fill(200);
   
    
  
 
 }
 

 
void drawALLDAY(){
 
  float cut = float(width) / float(request_amount/10);
   
     for(int i=0;i< request_amount/10; i++){
       
       noStroke();
       fill(222);
       rect(i,22,1,random(222));
       
     }
   
 
 
 }
 
 
void DRAW_ON_BUFFER(int _cid, int _v) {
  
  
  //println(_v);
  
  HISTOGRAM_BUFF.beginDraw();
  //dynamicGraphics.background(255); // Set the background to white
  
  // draw stroke with alpha of strength _v
  HISTOGRAM_BUFF.stroke(255, 128+_v*.5);  
  
  if(_v  >=  128){
    // if has crazy peak - get red!
    HISTOGRAM_BUFF.stroke(255,0,0); // Set stroke color to red
  }
  
  if(_v >=127){_v=127;}
  if(_cid >=8640){_v=8639;}
  
  HISTOGRAM_BUFF.line(_cid, 0, _cid, _v);
  
  HISTOGRAM_BUFF.endDraw();
}
 
 
 
void drawHISTOGRAM(){
 
 
    for(int i=0;i< 800; i++){
     
         
       fill(11);
       noStroke();
     
      if(itick==i+1){
      fill(255,0,0);}
       
       float yi = verbauche[i];
       rect(i,height,1,-yi*2+2);
       
    }
 
}
  /*
void keyPressed() {
  // todo _ check if connected
  client.publish("/hello", "world"); 
     
}

*/


int getMessageCountForTime(String time) {
  // Split the time string into hours, minutes, and seconds
  String[] parts = split(time, ':');
  int hours = int(parts[0]);
  int minutes = int(parts[1]);
  int seconds = int(parts[2]);

  // Convert the time to seconds
  int totalSeconds = hours * 3600 + minutes * 60 + seconds;

  // Calculate the count based on 10-second intervals
  int count = totalSeconds / 10;

  return count;
}