एक पैसा के लिए IoT: एक वेब इंटरफेस के साथ एक डिवाइस बनाते हैं


: , Senior Embedded Developer, DataArt

, - , 0.5 DeviceHive ESP8266. , , : .



:

IoT , DeviceHive.
IoT : . 1-, .
ESP8266 DeviceHive v 0.3.
IoT : . 2, .

— DeviceHive ESP8266 0.5. , , , DeviceHive ESP8266. -. -, .

, , GitHub. .

, .

1. RESTful API


80 HTTP- RESTful API. . . , , 'gpio/read'. , RESTful API 'http://device-id.local/api/gpio/read' JSON, . (AccessKey), :



curl.

curl -i -X POST  \
    -H "Authorization: Bearer YourAccessKeyHere="  \
    http://esp-demo.local/api/gpio/read

, HTTP-. , . , . . . GET, POST — . , , , JSON.

2. mDNS


, , , esp-demo.local. Multicast Domain Name System, , mDNS , IP-. , , DNS- .

mDNS , DNS, UDP . . DeviceId ( ). DeviceId.local.

RFC6763, , mDNS. . , mDNS. , DeviceId 'esp-demo', 'avahi-discovery':



, , . .

3.


. . , DS18B20, , , , . . 'devices/ds18b20/read' JSON '{«temperature»:24.5000}'. .

MPU-6050(, , ):



4. -


RESTful API HTTP-, -. , , . , 'http://deviceid.local/', - . , RESTful API (, base64), -, RESTful API ( ) , -. . 64.

, JavaScript - -. , .




DS18B20 -. GND 3V3 - GPIO-. GPIO2, . :



ESP8266, microUSB, USB-TTL. . , , . Micro-USB . , 3-4 .

-. - DS18B20. 'http://deviceid.local/ds18b20.html'. , , :



. , , - , -, .

, -, .

.
<html>
 <head>
 <script type="text/javascript">
    function send(onOff) {
      localStorage['accesskey'] = document.getElementById('accesskey').value;
      var xmlhttp = new XMLHttpRequest();  
      xmlhttp.open('POST', "http://" + window.location.hostname + '/api/gpio/write', true);
      xmlhttp.setRequestHeader("Authorization", "Bearer " + localStorage['accesskey']);
      xmlhttp.onreadystatechange = function() {
        if(xmlhttp.readyState == 4){
          if(xmlhttp.status < 200 || xmlhttp.status > 299) {
            alert('ERROR: Command return ' + xmlhttp.status + ' ' + xmlhttp.responseText, true);
          }
        }
      }
     var json = {}
     json["5"] = onOff ? 1 : 0;
     xmlhttp.send(JSON.stringify(json));
   }
   function init() {
     document.getElementById('accesskey').value = localStorage['accesskey'];
   }
 </script>
 </head>
 <body onload="init()">
  <label>AccesKey: </label><input type="password" id="accesskey"><br><br>
  <input type="button" value="On" onclick="send(true);">   <input type="button" value="Off" onclick="send(false);">
 </body>
</html>

, localStorage, , . / XMLHttpRequest(), RESTful API.
. SSR-40DA, , 220 , . . . , , 40 . , :


, . , !

Source: https://habr.com/ru/post/hi398813/


All Articles