element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      • Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Upcycle It
  • Challenges & Projects
  • Design Challenges
  • Upcycle It
  • More
  • Cancel
Upcycle It
Blog PDU Log #6 - WS2E, MongoDB Map [Upcycle It Challenge]
  • Blog
  • Forum
  • Documents
  • Polls
  • Files
  • Events
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: jasonwier92
  • Date Created: 2 May 2017 4:43 PM Date Created
  • Views 713 views
  • Likes 3 likes
  • Comments 5 comments
  • node_red
  • upcycle it
  • upcycled_pdu
  • pdu
  • node
  • intel edison
Related
Recommended

PDU Log #6 - WS2E, MongoDB Map [Upcycle It Challenge]

jasonwier92
jasonwier92
2 May 2017

Goals:

  • Sending data to the Edison to turn on/off items
  • MongoDB and map to how I will use it.

 

Sending Data to the Edison

 

Next on my list was the ability to send data to the Edison when a web event happens. I have added four buttons to the interface to test this. In the future it is much easier to have one button and the button conveys the status of the device.  That will be happening, but small step now to get there and right now I have one other hurdle to jump over before I am ready for that. Here is the four new buttons.

image

Simple code to get that:

 

<div>
       <div>
               Relay #1:
               <button id='relay1off' data-state=0>Off</button>
               <button id='relay1on' data-state='1'>On</button>
      </div>
      <div>
               LED #1:
               <button id='led1off' data-state='0'>Off</buton>
               <button id='led1on' data-state='1'>On</buton>
      </div>
</div>

 

In the future the following will be handled by one block of code, but for now there is a block for each line. Then the code to hand that button clicks events:

        $('#relay1on').on('click',function() {
                console.log('Socket Emit Relay1 ON');
                socket.emit('button', {
                        button: 'relay1',
                        msg: 1
                });
        });
        $('#relay1off').on('click',function() {
                console.log('Socket Emit Relay1 OFF');
                socket.emit('button', {
                        button: 'relay1',
                        msg: 0
                });
        });
        $('#led1on').on('click',function() {
                console.log('Socket Emit LED1 ON');
                socket.emit('button', {
                        button: 'led1',
                        msg: 1
                });
        });
        $('#led1off').on('click',function() {
                console.log('Socket Emit LED1 OFF');
                socket.emit('button', {
                        button: 'led1',
                        msg: 0
                });
        });

 

 

Then on the server side handle the socket.io traffic and generate WS commands

        socket.on('button',function(data) {
                console.log("data: ", data);
                switch (data.button) {
                        case 'relay1':
                                wscRelay1.send(data.msg);
                                console.log("Relay1: ", data.msg);
                                break;
                        case 'led1':
                                wscLED1.send(data.msg);
                                console.log("LED1: ", data.msg);
                                break;
                }
        });

 

Here are the node-red flows.

 

image

 

image

I highlighted some nodes that do not work and are just dreams. These are to handle tracking what state the devices are in.  So that is where the next section comes in to play as we need to track what state stuff is in so that we can draw the user interface correctly.

 

Node-Red Shortcoming

One thing that is hard to do in Node-Red is to know the state of an output. In Python or Node, you can query the the state of an output. This is nice when a new client connects or knowing when a output has been changes via a external source. I have been processing different ways of overcoming information that I need to have and here is a list of how I think I will do it.

 

  1. Store the states in a database, MongoDB
  2. Refresh MongoDB on changes from external sources
  3. Write a watcher that updates MongoDB and if a state was found incorrect, push out a notification.

 

Look at MongoDB

I am not going to cover how to install MongoDB, as you can install it as a docker, regular install, or use a cloud instance of it.  Also you can use any other database to do this function as long as it is supported by Node-Red. Here is how to install MongoDB support in Node-Red.

 

MongoDB in Node-Red

cd ~/.node-red
npm install node-red-node-mongodb
pm2 restart node-red

 

Query data Example

image

 

The Query Function Node builds the information for the query.

{gallery} Mongo Query in Node Red

image

Function Node called Query that builds the query message.

image

Query window.  This has the server connection and the collection.  The collection can be overridden from the query.

image

Example of the data from the function above outputted with the debug node.

 

The Plan

Following is my plan how I want to store information in Mongo. I hope to have Mongo document that tells the LCD what it should be displaying.  Then I can update the time or temperature on a regular interval.

 

MongoDB item for LCD storage

{
     device: 'eddy2',
     control: 'lcd1',
     state: 'custom',
     updated: 1493374858,
     custom: {
          msg1: 'Intel Edison',
          msg2: 'Element14',
          R: 20,
          G: 20,
          B: 20
     }
}

 

Other options are: Time, Outside Temperature, Room Temperature, Chassis Temperature, or Outlet State, etc

 

 

Other items

{
     device: 'eddy2',
     control: 'outlet1',
     state: 'off',
     updated: 1493374858,
     name: "Soldering Iron"
}

 

 

Maybe more information in the future.

 

Some House Keeping

  • Using PM2 for other mqtt-lcd bridge and web interface
    • Issue command from the MQTT-LCD-GW folder:
    • "pm2 start index.js --name 'MQTT-LCD-GW'"
  • I have change the MQTT-LCD-GW to display an initial message
  • While developing the web interface and server I have started using Nodemon. This way when changes are made in the server or a server controlled file, the Node process is automattically restarted.
  • Sign in to reply

Top Comments

  • jasonwier92
    jasonwier92 over 8 years ago +2
    Another update. Reading the state might not be as important as I thought. I am thinking of using 2 - 74H595 to drive the relays and LED's indicating the state of the relay. I might even be able to use…
  • jasonwier92
    jasonwier92 over 8 years ago in reply to Workshopshed +1
    It is, I have used it in the past, but for some reason did not start using it on this project yet. Then one day I questioned why not and started using it. This is a the case for a global install, nodemon…
  • jasonwier92
    jasonwier92 over 8 years ago +1
    Github source for this BLOG: https://github.com/jwier92/Upcycle6
Parents
  • jasonwier92
    jasonwier92 over 8 years ago

    Another update.  Reading the state might not be as important as I thought.  I am thinking of using 2 - 74H595 to drive the relays and LED's indicating the state of the relay.  I might even be able to use just one 74H595 and in that case there is no changing from an external source.  Bad taco in that case.

     

    I hope to mock this all up soon, but my mother has been in the hospital since Friday afternoon, so my life has been consumed by that and what is happening. So when life gets back to normal or steady, I will work hard to get this mocked up.

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • More
    • Cancel
Comment
  • jasonwier92
    jasonwier92 over 8 years ago

    Another update.  Reading the state might not be as important as I thought.  I am thinking of using 2 - 74H595 to drive the relays and LED's indicating the state of the relay.  I might even be able to use just one 74H595 and in that case there is no changing from an external source.  Bad taco in that case.

     

    I hope to mock this all up soon, but my mother has been in the hospital since Friday afternoon, so my life has been consumed by that and what is happening. So when life gets back to normal or steady, I will work hard to get this mocked up.

    • Cancel
    • Vote Up +2 Vote Down
    • Sign in to reply
    • More
    • Cancel
Children
No Data
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube