Guest User

Untitled

a guest
Mar 3rd, 2018
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.92 KB | None | 0 0
  1. $ xxd -i client.crt client_bin.crt
  2. $ xxd -i client.key client_bin.key
  3. $ cat client.* > certificate.h
  4.  
  5. // ------------- Includes ---------------
  6. #include <certificate.h>
  7. #include <PubSubClient.h>
  8. #include <ESP8266WiFi.h>
  9. // ------------- Wifi -------------------
  10. const char* ssid = "SSID";
  11. const char* password = "password";
  12. // ------------- MQTT -------------------
  13. const char* mqttServer = "mqtt.ranheim3r.com";
  14. const int mqttPort = 8883;
  15. const char* mqttTopic = "garagedoor";
  16. const char* mqttUser = "test";
  17. const char* mqttPassword = "1234";
  18. // =====================================
  19.  
  20. // Set up secure comms
  21. WiFiClientSecure espClient;
  22. PubSubClient client(espClient);
  23.  
  24. void setup() {
  25. // Set up serial for debug
  26. Serial.begin(115200);
  27.  
  28. // Connect to Wifi
  29. Serial.print("connecting to ");
  30. Serial.println(ssid);
  31. WiFi.mode(WIFI_STA);
  32. WiFi.begin(ssid, password);
  33.  
  34. while (WiFi.status() != WL_CONNECTED) {
  35. delay(500);
  36. Serial.print(".");
  37. }
  38. Serial.println("");
  39. Serial.println("WiFi connected");
  40. Serial.println("IP address: ");
  41. Serial.println(WiFi.localIP());
  42.  
  43. espClient.setCertificate(client_crt, client_crt_len);
  44. espClient.setPrivateKey(client_key, client_key_len);
  45.  
  46. client.setServer(mqttServer, mqttPort);
  47. client.setCallback(callback);
  48.  
  49. // Init future I/O
  50. }
  51.  
  52. void reconnect() {
  53. // Loop until we're reconnected
  54. while (!client.connected()) {
  55. Serial.print("Attempting MQTT connection...");
  56. // Attempt to connect
  57. if (client.connect("ESPclient", mqttUser, mqttPassword)) {
  58. Serial.println("connected to Mqtt broker");
  59. client.subscribe(mqttTopic);
  60. } else {
  61. Serial.print("failed, rc=");
  62. Serial.print(client.state());
  63. Serial.println(" try again in 5 seconds");
  64. // Wait 5 seconds before retrying
  65. delay(5000);
  66. }
  67. }
  68. }
  69.  
  70. void loop() {
  71. // Connect/Reconnect to mqtt broker & subscribe
  72. if (!client.connected()) {
  73. reconnect();
  74. }
  75. client.loop();
  76. // Act on messages
  77. }
  78.  
  79. void callback(char* topic, byte* payload, unsigned int length) {
  80. Serial.print("Message arrived [");
  81. Serial.print(topic);
  82. Serial.print("] ");
  83. for (int i = 0; i < length; i++) {
  84. Serial.print((char)payload[i]);
  85. }
  86. Serial.println();
  87.  
  88. // Activate the door (Test using BUILTIN_LED) if an 1 was received as first character
  89. if ((char)payload[0] == '1') {
  90. digitalWrite(BUILTIN_LED, LOW); // Turn the LED on (Note that LOW is the voltage level
  91. // but actually the LED is on; this is because
  92. // it is acive low on the ESP-01)
  93. delay(1000);
  94. digitalWrite (BUILTIN_LED, HIGH);
  95. } else {
  96. // Then nothing...
  97. }
  98. }
  99.  
  100. 1520115630: mosquitto version 1.4.10 (build date Mon, 29 May 2017 13:43:29 +0100) starting
  101. 1520115630: Config loaded from /etc/mosquitto/conf.d/mosquitto.conf.
  102. 1520115630: Opening ipv4 listen socket on port 8883.
  103. 1520115630: Opening ipv6 listen socket on port 8883.
  104. 1520115632: New connection from 192.168.1.170 on port 8883.
  105. 1520115632: OpenSSL Error: error:1417C0C7:SSL routines:tls_process_client_certificate:peer did not return a certificate
  106. 1520115632: Socket error on client <unknown>, disconnecting.
  107. 1520115637: New connection from 192.168.1.170 on port 8883.
  108. 1520115637: OpenSSL Error: error:1417C0C7:SSL routines:tls_process_client_certificate:peer did not return a certificate
  109. 1520115637: Socket error on client <unknown>, disconnecting.
  110.  
  111. WiFi connected
  112. IP address:
  113. 192.168.1.170
  114. Attempting MQTT connection...failed, rc=-2 try again in 5 seconds
  115. Attempting MQTT connection...failed, rc=-2 try again in 5 seconds
  116.  
  117. {
  118. "connectionProfiles" : [ {
  119. "recentSubscriptionTopics" : [ ],
  120. "recentPublishTopics" : [ ],
  121. "preDefinedMessages" : [ ],
  122. "brokerAddress" : "mqtt.ranheim3r.com",
  123. "brokerPort" : "8883",
  124. "lastPublishTopic" : null,
  125. "lastSubscriptionTopic" : null,
  126. "profileName" : "local mosquitto",
  127. "profileType" : "MQTT_BROKER",
  128. "scriptsPath" : "",
  129. "connectionOptions" : {
  130. "clientId" : "paho60251542020336",
  131. "connectionTimeout" : 30,
  132. "keepAliveInterval" : 60,
  133. "mqttVersionUseDefault" : true,
  134. "mqttVersion" : "3.1.1",
  135. "cleanSession" : true,
  136. "automaticReconnect" : true,
  137. "maxInflight" : 10,
  138. "userName" : "test",
  139. "password" : "1234",
  140. "lwtQos" : 0,
  141. "lwtRetained" : false,
  142. "lastWillDestination" : "",
  143. "testament" : "",
  144. "useProxy" : false,
  145. "useHttpsProxy" : false,
  146. "httpsProxyHost" : "",
  147. "httpsProxyPort" : "",
  148. "httpProxyHost" : "",
  149. "httpProxyPort" : "",
  150. "httpProxyUser" : "",
  151. "httpProxyPassword" : "",
  152. "httpProxyHeaderUserAgent" : "defaultClient",
  153. "enableSSLTLS" : true,
  154. "sslTlsProtocol" : "TLSv1.1",
  155. "certificateFilesPemFormat" : true,
  156. "selfCreatedCaFile" : "C:\ca.crt",
  157. "caFile" : "C:\ca.crt",
  158. "clientCertificateFile" : "C:\client2.crt",
  159. "clientKeyFile" : "C:\client2.key",
  160. "clientKeyPassword" : "",
  161. "useSelfCreatedCA" : false,
  162. "useServerSignedCertificate" : false,
  163. "useTrustedKeystoreFile" : false,
  164. "useCertificateFiles" : true,
  165. "useKeystoreFiles" : false,
  166. "clientKeystoreFilesPemFormat" : false,
  167. "trustedKeystoreOnlyFile" : "",
  168. "trustedKeystoreOnlyPassword" : "",
  169. "trustedKeystoreFile" : "C:\jssecacerts",
  170. "trustedKeystoreAlias" : "192.168.1.68",
  171. "trustedKeystorePassword" : "",
  172. "clientKeystoreFile" : "",
  173. "clientKeystorePassword" : "",
  174. "clientKeyPairAlias" : "",
  175. "clientKeyPairPassword" : ""
  176. },
  177. "notificatonsEnabled" : false,
  178. "showLastestMessagesOnly" : false,
  179. "lastSelectedPayloadDecoderId" : "plain_text_decoder",
  180. "googleCloudIotOptions" : {
  181. "projectId" : "ENTER-YOUR-IOT-PROJECT-ID",
  182. "registryId" : "ENTER-YOUR-REGISTRY-ID",
  183. "deviceId" : "ENTER-YOUR-DEVICE-ID",
  184. "cloudRegion" : "us-central1",
  185. "privateKeyFile" : "rsa_private_pkcs8",
  186. "algorithm" : "RS256",
  187. "mqttBridgeHostname" : "mqtt.googleapis.com",
  188. "mqttBridgePort" : 443
  189. }
  190. } ]
  191. }
Add Comment
Please, Sign In to add comment