Exposure to the high-pass IoT framework AllJoyn not long, but is deeply attracted. In my opinion, the reason to promote my deep learning has three points: 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 is the trend, Qualcomm in the field layout, 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 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 strong promotion of Qualcomm, personal love of C + + and so on.
Recently, based on the previous study, the use of AllJoyn and the domestic popular Yeelink IoT platform completed a simple web of things small system. We know that with the rapid development of the Internet and the rise of IoT in the world, a new mode of execution is 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 sensors on a wide variety of gateways and networks, with the main thrust of advocating for direct access to the resources of smart terminals and gateways through the rest Web API. Users can visit the Internet to access the terminal's data resources, which is the typical Internet model. and the Yeelink platform just can provide this function demand, 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 current mature Internet, two kinds of LAN transmission, AllJoyn with the inherent local transmission capacity is here to be reflected , the following is the perceptual layer, the Arduino terminal as a gateway to a variety of perceptual devices. What the structure 0 sees:
The two main functions of the system are as follows:
1, upload temperature sensor set temperature value to Yeelink platform, on the platform to display in an easy-to-read manner;
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
On the introduction of AllJoyn, I believe that Wikipedia and official documents will be much more specific than I can refer to the link after the article. According to my understanding is to use it to achieve interconnection between neighboring devices, no matter what device, just to support AllJoyn, through WiFi, Bluetooth can be connected to the high-speed, information sharing and timely communication. One of its advantages is the support of multi-programming languages and multi-platform, it is convenient for developers to use
Yeelink
Yeelink is a domestic open internet of things platform, each user is free to add devices and sensors, using the RESTful interface provided by the platform, to achieve the code access to each sensor, so as to achieve 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 on the server in the Windows 7 system to do this experiment, if implemented in a Linux environment, the need to change some platform-related code. The integrated development environment is visual Studio 2012, a very powerful IDE, and the same is the VS project file under the Samples directory generated with the SCons command under the x86 platform. The client implementation is the popular IDE--ARDUINO-1.5.6-R2 with open source hardware, it supports the Arduino due Development Board, it can be used for file editing and burning.
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 be high-speed system prototype, a lot of cost savings, under appropriate circumstances is a very good technical solution. Developers are pleased that the open source hardware community is very popular, so there are very 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. Since the detailed sensor data acquisition is not associated with AllJoyn, the AllJoyn-based transmission data is described in the example of temperature sensitivity, similar to the other sensor data. 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 the person who has just started learning x86 platform AllJoyn, the first can be directly under the VS edit generation, after all, there is a better code hint function, proficient after the use of Notepad and other 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, not easy to find errors.
---------------------------------------------------------------------------------------------------- ----------- ---
2 Structural Framework
The system together has two Arduino due Development Board as a client, because it is thin client, so it is necessary to provide standard client daemon to connect, which in the official document is very clear, no longer repeat; Windows 7 pc as a server, Publish Service for thin client connection, maybe some friends notice this is not the same as the official example Ledctrl and Aj_ledservice, the role of customer and service is reversed, thin client is no longer a service but a customer; On the other hand, PC server through the Internet and Yeelink platform to interact with the implementation of data upload and receive instructions, received instructions and through the AllJoyn bus control thin client, thus realizing the Yeelink platform based on AllJoyn transmission data and control functions. Its structural block Diagram 1 sees:
3 Specific explanations of each subsystem
3.1 Yeelink Platform
To use Yeelink resources, you need to register a unique account on the website and add devices and sensors to the user center. For example, as seen, I joined 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. Detailed operation documentation is available 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 and not write the complete code, and I hope the reader understands
The service-side main process, for example, is seen:
is the main thread of the process, because on the PC is multi-threaded execution, where the listener object, the bus object has an additional thread execution, 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. Here are the key details on how to design the service side
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, a ledswitch signal with a uint8_t-type parameter. Finally activating the interface and starting the bus
Next, we create the listening and bus objects, respectively, to the bus register listening and bus object instances, the last bus instance starts to connect the local router
In the listener class, we implemented a few virtual functions again, 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 invoked on their own initiative; 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, the bus is added to the interface already set, for the Sendtemp method to add the method processing function, the same time to the private member Ledswitchmember set the value
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 several properties that are not limited, and four global arrays are defined:
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";
corresponding 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
The host and path together make up the URL of the sensor, specifying the yeelink documentation that can be seen
As for the initial enable Windows socket function, also need to yeelink_server, fill in the relevant structure of the domain, such as the following see:
This part is platform-related, if ported to Linux platform, need to change
In the Bus object class, another member function emitledswitchsignal is used for the main thread to transmit the signal to the LED thin client
Enclose the sent reference as a message parameter, send it with the signal, and note that SessionID is Ledclientid
Let's go back to main thread main, connect and start announcing the service, three steps: Request,createsession,advertise
Finally enter the loop and poll me for the control switch state I added in the 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 that the CPU fully able to 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 it's almost the same as the upload temperature, or the assembly (just now a GET request), the initial socket, just in the reception I do:
The relevant attention points have been described in the gaze. 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. Just switch the status change, send this message to the LED thin client
3.3 Warm-feeling thin client
Before writing the client code, first create the directory, 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 at the same time:
Dallastemperature.cpp,dallastemperature.h,onewire.cpp,onewire.h, which belongs to the library to get the temperature, and all the Ino files are put together
In addition create another CPP file, for AllJoyn related core file, transfer temperature value. The following highlights this document
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 server, the interface must have a sendtemp method, carry a string reference, the other and dummy function, as a fill; Send_temp is represented as aj_prx_message_id type
In Aj_main, for example, 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:
Because I do not care about the subsequent solution message process, so the focus is the previous sentence, method calls such as the following:
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 sense of temperature, except that there are only 2 files, a Ino, a AllJoyn-based CPP
The main change is in the sampleinterface, adding the Ledswitch signal "!ledswitch instr>y", and then defining the # define # Led_switch aj_prx_message_id (0, 0, 2)
The beginning phase of course, like a warm feeling, startclient success, will start to solve the message, because the service side of the signal to come over:
When the signal comes over, verify the message ID, discover that it is led_switch, and get the switch status by solving the number. Assuming 1, the LED is lit and 0 is off. In addition, in this client will not have to sleep, because it is passive reception of the service side of the signal
4 Demonstration Verification
After service, two client code implementation, the server generated EXE files, connect the hardware, theclient burned into two Arduino due Development Board respectively. Rough graphs For example with:
The red board has a DS18B20 temperature sensor, three pin numbers are plugged into one of the Arduino board pins. Two boards with the host PC through the router in the same LAN inside. It is also important to emphasize that to enable the PC server to communicate with the board, you must start the thin Clientsdk bin SampleDaemon.exe program , because the server is not bound daemon, it is to provide daemon for thin client use , its source code can \alljoyn-14.02.00-src\alljoyn_core\samples\sampledaemon find
The following first look at the validation 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, there are examples such as the following display:
If there is no sampledaemon, there will be no last sentence output; Next, run the server-side program on the command line:
As you can see, 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 feel off, so the state changes on the launch of a signal, the same time received a temperature at the end of the heat value; serial terminal printing also indicates that the transmission temperature is successful, the output of the character ' F ' is my output function small problem, temporarily 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.
In another client, open the serial port form:
The server received another customer's access, nameownerchanged was called several times, if I followed by the mouse click on the yeelink control switch, that is seen:
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, the same time 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 a lot of other sensors, so as to enrich the function
2, can let the third thin client serve as a service, for example, with Arduino board, mobile phone, etc., assuming that the processing capacity to meet the conditions. As a service side of multiple customers, the ability to process data should be stronger, assuming only a single thread, like the Arduino board, can handle the merits of verification
3, can set the temperature sensor threshold value, once the temperature exceeds the given value to take the alarm, ringing buzzer and other equipment
6 References Link
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