The author's contact with the Qualcomm IoT framework AllJoyn not long, but is deeply attracted. In my opinion, there are three reasons to promote my deep study: First, AllJoyn open source, the hardware and software of open source always have a kind of inexplicable love, although may not all go down; second, the trend, the Internet of Things is far from universal, but it is the trend, Qualcomm in the field layout, is committed to creating comfortable and efficient smart home scene , introduced the AllJoyn software framework, adapted to the development trend; third, the document is rich, the use of open source software, especially the framework, if there is no documentation to help, I believe that few developers are willing to try, AllJoyn in this area do a good job, in the future need to do better. Of course, there are some additional reasons, including the promotion of Qualcomm, the personal love of C + + and so on.
Recently, based on previous studies, I have completed a simple web of things small system using AllJoyn and the popular Yeelink IoT platform in China. We know that with the rapid development of Internet and the rise of IoT in the world, a new mode of operation is also quietly born, that is, web of things, referred to as WOT. It can be understood as part of the IoT, centralizing the Web-based control and management of resources in the Internet of things, including gateways and gateway-based sensors, with the main thrust of promoting direct access to the resources of smart terminals and gateways through the rest Web API. Users can access the data resources of the terminal by accessing the Internet, which is the typical Internet mode. and the Yeelink platform just can provide such functional requirements, so I chose it as the application layer, and in the network layer can be subdivided into two, one is the public network transmission, that is, with the use of the current mature Internet, two kinds of LAN transmission, AllJoyn and innate local transmission capacity is here to be reflected At the bottom of the perceptual layer, the Arduino terminal as a gateway to a variety of perceptual devices. As shown in structure 0:
The current system implementation of the two major functions are as follows:
1. Upload temperature value from temperature sensor to Yeelink platform and display it in easy-to-read mode on the platform;
2, by clicking on the platform of the virtual switch to the perceptual layer of the Arduino terminal issued commands to control the light off the LED lights;
1 tools and development environment
AllJoyn
Regarding AllJoyn's introduction, I believe that Wikipedia and official documents will be much more detailed than I can refer to the link below. According to my current understanding is to use it can achieve interconnection between neighboring devices, no matter what device, as long as support AllJoyn, through WiFi, Bluetooth can be connected quickly, to achieve information sharing and timely communication. One of the benefits of this is that it supports multiple programming languages and multiple platforms, and is very easy for developers to use.
Yeelink
Yeelink is a domestic open internet of things platform, each registered user can add devices and sensors for free, using the RESTful interface provided by the platform to achieve code access to each sensor, which can realize sensor data upload and control terminal and other functions. With such a free platform, I believe it is a great boon for developers!
Software Environment
I am currently working on the Windows 7 system for this experiment, if running in a Linux environment, you need to modify some of the platform-related code. The integrated development environment is visual Studio 2012, a powerful IDE, and under the Samples folder generated under the SCons command under the x86 platform is also the VS project file. The client implementation is a popular IDE--ARDUINO-1.5.6-R2 with open source hardware, which supports the Arduino due Development Board, which allows for file editing and writing.
Hardware environment
In addition to the x86 PC, the big point is just the Arduino due Development Board. Recently, the prevalence of smart hardware has also facilitated the development of open source hardware, with the relevant mature hardware such as Arduino can quickly do system prototypes, a lot of cost savings, in appropriate cases is a good technical solution. Developers are pleased that the open source hardware community is very popular, so there is a good problem solving resources.
There are Arduino boards, but no sensors are available. For the sake of convenience, what I'm showing now is just a ds18b20 temperature sensor. Because the specific sensor data acquisition and AllJoyn are not related, so take the temperature as an example to explain the data transmission based on AllJoyn, the other sensor data is similar. In addition, in order to coordinate with the control command, a light emitting diode is provided, which is, of course, on the Arduino due board, on the 13th pin.
---------------------------------------------------------------------------------------------------- ----------- ---
Friendship suggestion: It is suggested that beginners learn x86 platform under the AllJoyn, the first can be directly under the VS edit generation, after all, there is a better code hint function, proficiency can then be used Notepad tools. If just learn to write code on the Notepad, it will be very frustrating, because a lot of functions and parameters you do not know, it is not easy to find errors.
---------------------------------------------------------------------------------------------------- ----------- ---
2 Structural Framework
The system has a total of two Arduino due Development Board as a client, because it is a thin client, it is necessary to provide a standard client daemon to connect, which in the official document is very clear, no longer repeat; the Windows 7 pc as a server, publishing services for thin client connections, Maybe a friend noticed this. Unlike the official example Ledctrl and Aj_ledservice, the role of the customer and the service is reversed, and the thin client is no longer a service but a customer; On the other hand, the PC server interacts with the Yeelink platform via the Internet. It realizes the data uploading and receiving instruction, the received instruction and the thin client is controlled by the AllJoyn bus, thus realizes the data transmission and control function based on AllJoyn under the Yeelink platform. Its structure is shown in Block 1:
3 each subsystem detailed
3.1 Yeelink Platform
To use Yeelink resources, you must register a unique account on the website and add devices and sensors to the user center. As shown, I added the Arduino device
Next in the "My Devices" item, add the temperature sensor and the control switch, the system will generate a unique URL for each device, through the URL to access the specific sensor. The specific operation of the document can be consulted here: Http://www.yeelink.net/develop/api
3.2 pc Service side
When it comes to the next service and customer implementations, I will parse the core code in detail, rather than writing the complete code, hoping the reader will understand
The server-side mainstream Cheng shows:
is the main thread of the process, because on the PC is multi-threaded operation, where the listener object, the bus object has an additional thread running, they are asynchronous, it means that when the event occurs, can be quickly responded to, for example, when the server received a warm-feeling thin client came to see the temperature, The bus object invokes its method handler to upload to the Yeelink platform. Let's talk about how to design the service side with the key details
The bus object is created first and then the interface is added to the bus object. There is a Sendtemp method in the interface, with a string input parameter, and a ledswitch signal with a uint8_t type parameter. Finally activating the interface and starting the bus
Next, the Listener and bus objects are created, respectively, the bus registers the listening and bus object instances, and the last bus instance begins to connect to the local router
In the Listener class, we re-implemented a few virtual functions, including
BOOL Acceptsessionjoiner (sessionportsessionport, const char* Joiner, const sessionopts& opts)
void sessionjoined (Sessionport sessionport,sessionid ID, const char* joiner)
void nameownerchanged (const char* name,const char* previousowner, const char* Newowner)
The first two are server-side specific, when there is a customer access, will be automatically called; the last service and customer are callable, and when a service or customer enters or exits, a name change occurs on the bus, so it is called, sometimes more than once. The implementation of these three virtual functions is basically a conventional notation, it is not explained here
In the construction of the bus object instance, I did this:
First, add the interface already set to the bus, add the method handler function for the Sendtemp method, and set the value to the private member Ledswitchmember.
In the method processing function, obtain the passed temperature value, the string form, on the Yeelink platform to upload:
What I need to emphasize is that in the Makestring method, to properly assemble an HTTP POST request, there are a few properties that are not few, so we define four global arrays:
Char yeelink_server[] = "api.yeelink.net";
Char temp_path[] = "/v1.0/device/9966/sensor/19877/datapoints";
Char switch_path[] = "/v1.0/device/9966/sensor/22595/datapoints";
Char apikey[] = "d3d565a5923afdd82105e0e5a";
Corresponds to the following entry in the POST request:
POST /v1.0/device/9966/sensor/19877/datapoints http/1.1
Host: api.yeelink.net
U-apikey: d3d565a5923afdd82105e0e5a
Host and path together make up the URL of the sensor, detailed instructions can be found in the Yeelink documentation
As for the start of the Windows socket function, it is also necessary to yeelink_server, fill in the relevant structure of the domain, as follows:
This section is platform-dependent and needs to be modified if ported to a Linux platform
In the Bus object class, there is also a member function emitledswitchsignal for the main thread to transmit the signal to the LED thin client
Encapsulates the sent parameters into a message parameter, sent out with the signal, note that the SessionID is the LED client ID
We went back to the main thread main, and after connect we started publishing the service, three steps: Request,createsession,advertise
Finally, enter the loop and poll the control switch status I added in Yeelink:
Here I take the passive polling switch state of the way, in fact, is not the best, it is best to switch state a change, like a hardware interrupt, immediately notify the CPU, and before this CPU completely can do other things. But this need Yeelink platform of active send, seemingly not too good to do, so on the partition time polling state. Time interval also must choose, big, LED light change has the delay; small, the request is too frequent and was Yeelink refused. So how do we poll? In fact, the upload temperature is similar, or assembly (but now a GET request), the first to make a socket, but in the reception I do this:
The relevant notes have been described in the comments. The reason for the status change is to transmit the signal, but also for the sake of performance, there is no need to be fired when the state is not changed. The last value returned is a uint8_t type, with a value of 0 or 1,0 to turn off the LED, and 1 to illuminate the LED light. Send this message to the LED thin client as long as the switch status changes
3.3 Temperature Sensitive thin client
Before writing the client code, first create the folder, the name needs to be consistent with the. ino file, which is the default behavior of the Arduino environment. I will focus on the transmission of temperature sensing data, as for the temperature value, readers can refer to the Arduino Chinese community this post: http://www.arduino.cn/thread-1345-1-1.html, download four source files simultaneously:
Dallastemperature.cpp,dallastemperature.h,onewire.cpp,onewire.h, this belongs to the library for getting the temperature, all together with the Ino file
In addition create another CPP file, for AllJoyn related core file, transfer temperature value. This file is highlighted below
First of all, we should pay attention to the following several data structure writing:
Service name, path, port, interface name must be consistent with the service side, the interface has to have a sendtemp method, carry a string parameter, the other and dummy function, as a fill; Send_temp is represented as aj_prx_message_id type
In Aj_main, the following work is done first:
The startclient will not return until the service is successfully connected.
Next is the core action, which gets the temperature, method call, Sleep, and recirculation:
Since I do not care about the subsequent solution message process, so the focus is the previous sentence, the method is called as follows:
First marshal the method, then marshal the parameter, that is, the temperature value, and finally deliver. This causes the service-side method callback function to be called
3.4 led Thin client
This end is similar to the warm sense in some places, but there are only 2 files, a Ino, a AllJoyn-based CPP
The main change is in sampleinterface, add Ledswitch signal "!ledswitch instr>y", then predefined # define Led_switch aj_prx_message_id (0, 0, 2)
The beginning of course is similar to the temperature sense, startclient success, began to solve the message, because the service side of the signal to come over:
When the signal comes over, verify the message ID, find that it is led_switch, solve the parameter, get the switch state. If 1, the LED is lit and 0 is off. In addition, there is no need to sleep on this client, because it is the signal of the passive reception server.
4 Demonstration Verification
After service, two client code implementation, the server generated EXE files, connect the hardware, the client burned into two Arduino due Development Board respectively. A rough picture such as:
The red board has a DS18B20 temperature sensor, three pin numbers are plugged into one of the Arduino board's pins. Two boards with the host PC through the router in the same LAN inside. Also have to emphasize that, to enable the PC server to communicate with the board, you must start the Thin Client SDK Bin SampleDaemon.exe program , because the server does not have a binding daemon, it is to provide daemon to thin client use, its source code can \alljoyn-14.02.00-src\alljoyn_core\samples\sampledaemon found
Let's look at the verification of the temperature sensor
Connect the hardware power on, open the Yeelink platform temperature sensor Display page, easy to refresh at any time; Click the Arduino IDE, press Shift+ctrl+m to open the serial terminal, the following display:
If there is no sampledaemon, there will be no last sentence output; Next, execute the server-side program at the command line:
As shown, when the server is started, the client access is discovered and the session starts after the access is successful. Because my switch first makes the state is open, Cheng think off, so the state changes on the launch of a signal, at the same time received a warm sense of the temperature value; the printing of the serial terminal also indicates that the transmission temperature is successful, the output of the character ' F ' is a small problem of my output function, regardless of it. A few seconds later, it became like this:
Hey? Why does the temperature rise? Oh, that's because I put my finger on the ds18b20! Of course it's warming up. Now let's look at the reaction of yeelink on the temperature:
The original temperature value of 26.6 was shown on September 13, 14 at 12:26:34, before the previous data. This also enables the ability to monitor the temperature on-line.
On another client, open the serial port window:
The server received another client's access, Nameownerchanged was called several times, if I followed by the mouse to click on the yeelink control switch, that is shown:
The response of the service and the thin client is:
From the results, I was opened a switch, and closed a switch, resulting in the client has received 1, 0, the process of observing the LED light reaction is a light off, while the temperature value can also be received in:
It should be a transmission error before the a character appears.
To this end, the entire pre-conceived function is basically implemented
5 Worth Improvement
1, can add more sensors, so as to enrich the function
2, can let the third thin client serve as a service, such as with Arduino board, mobile phone, etc., if the processing capacity to meet the conditions. Because as a service side of multiple customers, data processing capacity should be stronger, if only single-threaded, like Arduino board, can handle the merits of verification
3, can set the temperature sensor threshold, once the temperature exceeds the given value to take alarm, ringing buzzer and other equipment
6 Reference Links
AllJoyn Official 1:https://allseenalliance.org/
AllJoyn Official 2:https://www.alljoyn.org/
Yeelink official website: http://www.yeelink.net/
Arduino Due Introduction: Http://www.arduino.cc/en/Main/ArduinoBoardDue
Arduino Chinese Community: http://www.arduino.cn/
"AllJoyn topic" Research on sensor data uploading and instruction downlink based on AllJoyn and Yeelink