[PYTHON] I tried logging the amount of power generated by solar power generation. (4) Real-time monitoring with Salesforce

[I tried logging the amount of power generated by solar power generation. Table of contents]

Real-time monitoring with Salesforse

Up to the last time, it has become possible to log the current and voltage values ​​measured by INA260 with Raspberry Pi, but since it is a big deal, I would like to upload it on the Internet so that I can check the amount of power generation from anywhere.

I think there are various options for that method, but this time I used Salesforce's Developer Edition.

● Benefits of using Salesforce

  1. Must be PaaS (Platform as a Service) Cloud services can be broadly divided into three types: IaaS (Infrastracture as a Service), PaaS (Platform as a Service), and SaaS (Software as a Service). Salesforce belongs to PaaS. PaaS does not require environment construction like IaaS, and has a higher degree of freedom than SaaS, and rich functions can be freely and easily customized.

  2. Developer Edition is free Salesforce basically requires you to purchase a paid license, but the Developer Edition is full-featured and available for free. However, there are the following restrictions.

・ The number of users that can be created is 2. ・ Available data storage 5MByte ・ Available file storage 20MByte

For more information, see [Salesforce Features and Edition Assignments-Salesforce Help] (https://help.salesforce.com/articleView?id=overview_limits_general.htm&type=5) "See Data Storage and File Storage Allocation.

Data storage 5M may seem small, but BigObject can store up to 1 million records. This will be explained later.

● Salesforce Developer Edition registration

Salesforce Developer Edition can be used by registering as a user from the following site. Please note that there may be a time lag between registration and receipt of the email.

Salesforce Developers User Registration

● Login to Salesforce

To log in to Salesforce, go to the following site.

Salesforce Login (https://login.salesforce.com/?locale=jp)

Enter the registered user name and password to log in. At the first login, after the guidance, the following screen (service settings) is basically displayed.

image.png

There are many menus, and you may not know what to do, but here we will set it according to the following procedure.

  1. Create a custom object
  2. OAuth settings and API activation
  3. Input test data via SOAP API
  4. Create a report
  5. Creating a dashboard

● Create custom objects

Salesforce has standard and custom objects. These correspond to database tables.

Standard object: An object prepared for applications such as CRM from the beginning. Customizable. Custom Objects: Objects that are completely user-definable.

The following is the procedure for creating a custom object.

  1. Click the gear icon at the top right of the screen. Select "Settings". image.png

  2. From the menu on the left side of the screen Platform Tools-Select "Objects and Items"-"Object Manager" image.png

  3. Click Create on the right side of the screen and select Custom Object from the drop-down menu.

image.png

image.png

  1. Create a custom object

Set the name of the custom object and the API access name. After saving, the API access name of the custom object will have "__c" at the end. Here, the settings are as follows.

SF1.png

  1. Create a custom tab

If you check "Launch new custom tab wizard after saving custom object" in 4, "New custom tab screen" is automatically displayed after saving the custom object. If you forget to check it, Click the "New" button from Settings-Platform Tools- "User Interface"-"Tabs" to launch the new custom tab creation screen.

image.png

Here, select the tab style and click "Next". After that, there are settings for the user to display and the application to be displayed, but keep the defaults and proceed with "Next" to save.

  1. Create custom items

Again, from Settings-Platform Tools- "Objects and Items"-"Object Manager", select the custom object you just created.

image.png

Select "Items and Relations" from the left menu. SF2.png

Click the "New" button at the top right of the screen.

SF3.png

Select the data type of the item. Here, as an example, the item of current value is created, so select "Numeric value" and click "Next".

image.png

Set the item name, number of digits, etc. and click "Next".

SF4.png

Leave the default item-level security settings and click Next.

image.png

You can leave the defaults for adding to the page layout. Click "Save" or "Save & New".

Follow the same procedure as above to add the necessary items.

● OAuth settings and API activation

Use the REST API or SOAP API to upload measurements from the Raspberry Pi to Salesforce. To enable it, you need to check "Enable API" in the user profile.

Display the user list in "Settings"-"Users". Open the profile of the user used for DB access and press the "Edit" button. Then, check "Enable API" and save.

Also, when accessing the API from the outside, a security token is required in addition to the password. If you don't know your security token, you can reissue it by following the steps below.

  1. Click the character icon at the top right of the screen and click "Settings".
  2. Select "Reset My Security Token" from the left menu.
  3. Press the "Reset Security Token" button.

When the above is executed, an email containing the security token will be sent to the registered email address.

Next, create a "connected application" to configure OAuth settings. It can be created from "Settings"-"Applications"-"Connected applications".

This is described in detail on the following site, so I referred to it.

I tried accessing Salesforce REST API from Python using simple_salesforce

Get Salesforce data using REST API (https://qiita.com/nsuhara/items/19cf8ec89b88fb3deb39)

● Input test data via REST API

From here, it will be the work on the Raspberry Pi side. I modified the source code of the previous article and added code to upload measurements to a custom Salesforce object. Use the Salesforce REAST API to access Salesforce.

Salesforce REST API Developer's Guide (https://developer.salesforce.com/docs/atlas.ja-jp.api_rest.meta/api_rest/quickstart.htm)

There seem to be several libraries for python in Salesforce, but here we use simple_salesforce. The site I introduced earlier describes the procedure for installing the library, so I referred to it.

I tried accessing Salesforce REST API from Python using simple_salesforce

import serial
import struct
import datetime as dt
import json
from simple_salesforce import Salesforce

USERNAME = "username"
PASSWORD = 'password'
SECURITY_TOKEN = "Security token"

use_port = "/dev/ttyACM0"

_serial = serial.Serial(use_port)
_serial.baudrate = 115200
_serial.parity = serial.PARITY_NONE
_serial.bytesize = serial.EIGHTBITS
_serial.stopbits = serial.STOPBITS_ONE
_serial.timeout = 15

def Login():
    sf = Salesforce(username=USERNAME, password=PASSWORD,
                    security_token=SECURITY_TOKEN)
    return sf

sf = Login()
try:                                                                                                                        while True:
        rx = _serial.readline()
        #rx = rx.strip()
        st = "{0},{1}".format(dt.datetime.now(), rx)
        print(st)
        with open('/home/pi/current_{}.log'.format(dt.date.today()), mode="a") as f:
            f.write(st)

        datas = st.split(",")
        if len(datas)<3:
            continue

        t1 = datas[0][0:19]                                                                                                     t2 = t1.replace(" ","T") + "Z"                                                                                          print("{0},{1}".format(t1,t2))
        try:
            for i in range(1,len(datas)):
                datas[i] = datas[i].replace("b'","").replace("\\r\\n'","").strip()

            if len(datas[1])==0 or len(datas[2])==0 or len(datas[1])>10 or len(datas[2])>10 :
                continue
            if datas[1][:1].isdigit()==False or datas[2][:1].isdigit()==False:
                continue

            res = sf.GeneratorLog__c.create({'Name': t1, #The Name item is a character string type and the date and time are set.
                                             'MeasDate__c': t2, #Date and time(Datetime type)
                                             'Current__c': float(datas[1]), #Current value
                                             'Voltage__c': float(datas[2]), #Voltage value
                                             'Temperature__c': 20.00}) #Since the temperature will be measured in the future, only the items were created, but a fixed value is set.
            if res.get('error'):
                raise Exception(response.get('error_description'))

            print(json.dumps(res, indent=4))

        except Exception as e:
                sf = Login() #If you keep logging in, an exception will occur due to a timeout, so log in again and try again.
                res = sf.GeneratorLog__c.create({'Name': t1,
                                                 'MeasDate__c': t2,
                                                 'Current__c': float(datas[1]),
                                                 'Voltage__c': float(datas[2]),
                                                'Temperature__c': 20.00})
                print(json.dumps(res, indent=4))

except Exception as ex:
    print(ex)
finally:
    _serial.close()

If you connect a solar panel or a test battery to the INA260 and execute it, you can see that the data has been uploaded from the Salesforce screen.

image.png

By the way, you can select the tab to display by clicking the pen mark at the right end of the tab display field in the Lightning Experience application to display the created custom tab. Please note that you want to customize the tab display in the Classic application. By pressing the application selection button at the left end of the tab display field, tab selection was possible even from "Show All". (Recently, due to the transition to Lightning Experience, you may not be able to do what you were able to do before, so be careful)

SF.png

Any application will do, so add the following tab. ・ Power generation log (created custom tab) ・ Report (explained below) ・ Dashboard (explained below)

● Creating a report

Logging data is not fun just by storing it. After all, the tendency is not well understood unless it is graphed. Create a report for graphing.

Select the Report tab and click "Create New Report" at the top of the screen. At this time, click the button that is not Classic.

image.png

On the "Select Report Type" screen, existing objects are listed. Select the custom object (power generation log) you created.

image.png

from here ・ Display items ・ Search conditions ・ Grouping And prepare the data to make a graph.

For the graph I want to draw, I want to display the time on the X-axis and the current on the Y-axis for each day. Therefore, a current is added to the display item. Enter "current" in the search field under "column" on the left side of the screen, or open "item" on the left side of the screen and select "current". Then, you can see that "current" is added to the table in the center of the screen.

image.png

However, the "Add Graph" button at the top of the screen remains disabled. It is necessary to group them to display the graph. Therefore, we will display the average current every 10 minutes for each day.

Here, return to "Service settings"-"Objects and items" and add a custom item to the custom object (power generation log).

item name Data type Formula Remarks
Day Formula DATEVALUE( LEFT(Name,10))
Minute time Formula VALUE(MID(Name,12,2) & "." & MID(Name,15,1)) Set time and tens of minutes

By the way, in the Name item, the measurement date and time is entered as a character string type as "yyyy-MM-dd HH: mm: ss".

Immediately add the created custom item to the report. Added "Day" and "Minute" in "Group Rows".

image.png

Also, if you click "Current" in "Column", the summary menu will be displayed, so select the average.

image.png

Finally, the "Add Graph" button is enabled. Click "Add Graph".

image.png

Click the gear button at the top right of the graph.

image.png

Display graph: Line Title: Current measurement X-axis: minutes and hours Series: day Y-axis: current average

If you can confirm that the graph is displayed normally on the screen, save it.

image.png

Similarly, you may want to report on voltage and power.

● Creating a dashboard

Add to the dashboard to make the report easier to read.

Select the Dashboard tab and click "New Dashboard" at the top of the screen. Enter the name of the dashboard and click "Create". When you press the "+ Component" button at the top of the screen, a list of reports will be displayed, so select the report you want to display. Next, a form similar to the graph creation of the report is displayed, so set it in the same way as the graph creation of the report.

image.png

Click the "Add" button to add the graph component on the screen. If you add and save the reports you want to display in the same way, it will look like this.

SF5.png

Now you can log in to Salesforce and access your dashboard from anywhere to monitor your power generation in real time. However, there is one caveat. Please note that the data will not be updated unless you press the update button at the top right of the screen.

[I tried logging the amount of power generated by solar power generation. Table of contents]

Recommended Posts

I tried logging the amount of power generated by solar power generation. (4) Real-time monitoring with Salesforce
The 15th offline real-time I tried to solve the problem of how to write with python
I tried to find the entropy of the image with python
I tried "gamma correction" of the image with Python + OpenCV
I tried to visualize the power consumption of my house with Nature Remo E lite
I tried to predict the sales of game software with VARISTA by referring to the article of Codexa
I tried to verify the speaker identification by the Speaker Recognition API of Azure Cognitive Services with Python. # 1
I tried to verify the speaker identification by the Speaker Recognition API of Azure Cognitive Services with Python. # 2
I tried scraping the ranking of Qiita Advent Calendar with Python
I tried to automate the watering of the planter with Raspberry Pi
I tried to expand the size of the logical volume with LVM
I tried running the DNN part of OpenPose with Chainer CPU
I tried to improve the efficiency of daily work with Python
I tried sentence generation with GPT-2
I tried to open the latest data of the Excel file managed by date in the folder with Python
765 I tried to identify the three professional families by CNN (with Chainer 2.0.0)
I tried to get the authentication code of Qiita API with Python.
I tried to automatically extract the movements of PES players with software
I tried to find the optimal path of the dreamland by (quantum) annealing
I tried to verify and analyze the acceleration of Python by Cython
I tried to analyze the negativeness of Nono Morikubo. [Compare with Posipa]
I tried to streamline the standard role of new employees with Python
I tried to visualize the text of the novel "Weathering with You" with WordCloud
I tried to get the movie information of TMDb API with Python
I tried to verify the result of A / B test by chi-square test
I tried to predict the behavior of the new coronavirus with the SEIR model.
I tried the asynchronous server of Django 3.0
I tried to predict the number of people infected with coronavirus in Japan by the method of the latest paper in China
I tried playing with the image with Pillow
I tried to easily visualize the tweets of JAWS DAYS 2017 with Python + ELK
I tried to predict the presence or absence of snow by machine learning.
I tried running the TensorFlow tutorial with comments (text classification of movie reviews)
I tried object detection with YOLO v3 (TensorFlow 2.1) on the GPU of windows!
I tried to rescue the data of the laptop by booting it on Ubuntu
The story of making soracom_exporter (I tried to monitor SORACOM Air with Prometheus)
I tried searching for files under the folder with Python by file name
I tried to create a model with the sample of Amazon SageMaker Autopilot
I tried to automatically send the literature of the new coronavirus to LINE with Python
I tried "smoothing" the image with Python + OpenCV
I tried hundreds of millions of SQLite with python
I tried the pivot table function of pandas
I tried cluster analysis of the weather map
I tried image recognition of CIFAR-10 with Keras-Learning-
I tried image recognition of CIFAR-10 with Keras-Image recognition-
I tried to save the data with discord
I tried Flask with Remote-Containers of VS Code
I tried to touch the API of ebay
I tried to correct the keystone of the image
I tried "binarizing" the image with Python + OpenCV
I tried using the Datetime module by Python
I tried using the image filter of OpenCV
I tried playing with the calculator on tkinter
I tried to predict the price of ETF
I tried to vectorize the lyrics of Hinatazaka46!
I tried to make something like a chatbot with the Seq2Seq model of TensorFlow
I tried to put out the frequent word ranking of LINE talk with Python
I want to plot the location information of GTFS Realtime on Jupyter! (With balloon)
I tried to automate the article update of Livedoor blog with Python and selenium.
I tried to visualize the characteristics of new coronavirus infected person information with wordcloud
I tried to visualize the running data of the racing game (Assetto Corsa) with Plotly
I tried to compare the processing speed with dplyr of R and pandas of Python