Advertisement
Guest User

Untitled

a guest
Jan 27th, 2017
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.23 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <WiFiClientSecure.h>
  3. #include <PubSubClient.h>
  4. #include <ESP8266mDNS.h>
  5. #include <WiFiUdp.h>
  6. #include <ArduinoOTA.h>
  7.  
  8. #include "/usr/local/src/ap_setting.h"
  9.  
  10. #define DEBUG_PRINT 1
  11.  
  12. #define MQTT_TEST_SERVER { 192, 168, 10, 144 }
  13. #define MQTT_TEST_SERVER_HOST "mqtttest.iot"
  14. #define MQTT_TEST_USER "test1"
  15. #define MQTT_TEST_PASS "test123"
  16.  
  17. // *****************************
  18. const char* ssid = WIFI_SSID;
  19. const char* password = WIFI_PASSWORD;
  20. const char* otapassword = OTA_PASSWORD;
  21.  
  22. const char* mqttuser = MQTT_TEST_USER;
  23. const char* mqttpass = MQTT_TEST_PASS;
  24. const char* mqtt_server_host = MQTT_TEST_SERVER_HOST;
  25.  
  26. IPAddress mqtt_server = MQTT_TEST_SERVER;
  27.  
  28. //---------------------------
  29. #define IPSET_STATIC { 192, 168, 10, 50 }
  30. #define IPSET_GATEWAY { 192, 168, 10, 1 }
  31. #define IPSET_SUBNET { 255, 255, 255, 0 }
  32. #define IPSET_DNS { 192, 168, 10, 10 }
  33.  
  34. //
  35. byte ip_static[] = IPSET_STATIC;
  36. byte ip_gateway[] = IPSET_GATEWAY;
  37. byte ip_subnet[] = IPSET_SUBNET;
  38. byte ip_dns[] = IPSET_DNS;
  39. // ****************
  40.  
  41. // openssl x509 -fingerprint -in mqttserver.crt
  42. const char* fingerprint = "70 B2 BF 0D 4E 2A 54 FC DD C3 75 03 CD 42 20 71 9C 4A 97 37";
  43. // connecting to mqtttest.iot
  44. // certificate matches
  45. // fingerprint test
  46. //const char* fingerprint = "70 B2 BF 0D 4E 2A 54 FC DD C3 75 03 CD 42 20 71 9C 4A 97 36";
  47. // connecting to mqtttest.iot
  48. //certificate doesn't match
  49. //-------------------------------
  50. char* topic = "pubtest";
  51.  
  52. String clientName;
  53.  
  54. long lastReconnectAttempt = 0;
  55. long lastMsg = 0;
  56. int test_para = 2000;
  57. unsigned long startMills;
  58.  
  59. //------------------------------
  60. WiFiClientSecure wifiClient;
  61. //WiFiClient wifiClient;
  62. //PubSubClient client(mqtt_server, 8883, wifiClient);
  63. PubSubClient client(mqtt_server_host, 8883, wifiClient);
  64.  
  65. //----------------------
  66. String macToStr(const uint8_t* mac);
  67. void sendmqttMsg(char* topictosend, String payload);
  68.  
  69. void verifytls() {
  70. // Use WiFiClientSecure class to create TLS connection
  71. Serial.print("connecting to ");
  72. Serial.println(mqtt_server_host);
  73. if (!wifiClient.connect(mqtt_server_host, 8883)) {
  74. Serial.println("connection failed");
  75. return;
  76. }
  77.  
  78. if (wifiClient.verify(fingerprint, mqtt_server_host)) {
  79. Serial.println("certificate matches");
  80. } else {
  81. Serial.println("certificate doesn't match");
  82. }
  83. }
  84.  
  85. //-----------------------
  86. boolean reconnect()
  87. {
  88. if (!client.connected()) {
  89. if (client.connect((char*) clientName.c_str(), mqttuser, mqttpass)) {
  90. if (DEBUG_PRINT) {
  91. Serial.println("===> mqtt connected");
  92. }
  93. } else {
  94. if (DEBUG_PRINT) {
  95. Serial.print("---> mqtt failed, rc=");
  96. Serial.println(client.state());
  97. }
  98. }
  99. }
  100. return client.connected();
  101. }
  102.  
  103. void wifi_connect()
  104. {
  105. if (WiFi.status() != WL_CONNECTED) {
  106. // WIFI
  107. if (DEBUG_PRINT) {
  108. Serial.println();
  109. Serial.print("===> WIFI ---> Connecting to ");
  110. Serial.println(ssid);
  111. }
  112. delay(10);
  113. WiFi.mode(WIFI_STA);
  114. WiFi.begin(ssid, password);
  115. WiFi.config(IPAddress(ip_static), IPAddress(ip_gateway), IPAddress(ip_subnet), IPAddress(ip_dns));
  116.  
  117. int Attempt = 0;
  118. while (WiFi.status() != WL_CONNECTED) {
  119. if (DEBUG_PRINT) {
  120. Serial.print(". ");
  121. Serial.print(Attempt);
  122. }
  123. delay(100);
  124. Attempt++;
  125. if (Attempt == 150)
  126. {
  127. if (DEBUG_PRINT) {
  128. Serial.println();
  129. Serial.println("-----> Could not connect to WIFI");
  130. }
  131. ESP.restart();
  132. delay(200);
  133. }
  134.  
  135. }
  136.  
  137. if (DEBUG_PRINT) {
  138. Serial.println();
  139. Serial.print("===> WiFi connected");
  140. Serial.print(" ------> IP address: ");
  141. Serial.println(WiFi.localIP());
  142. }
  143. }
  144. }
  145.  
  146. void setup()
  147. {
  148. startMills = millis();
  149.  
  150. if (DEBUG_PRINT) {
  151. Serial.begin(115200);
  152. }
  153.  
  154. wifi_connect();
  155.  
  156. clientName += "esp8266-";
  157. uint8_t mac[6];
  158. WiFi.macAddress(mac);
  159. clientName += macToStr(mac);
  160. clientName += "-";
  161. clientName += String(micros() & 0xff, 16);
  162.  
  163.  
  164. verifytls();
  165.  
  166. //OTA
  167. // Port defaults to 8266
  168. //ArduinoOTA.setPort(8266);
  169.  
  170. // Hostname defaults to esp8266-[ChipID]
  171. ArduinoOTA.setHostname("esp-test");
  172.  
  173.  
  174. // No authentication by default
  175. ArduinoOTA.setPassword(otapassword);
  176.  
  177. ArduinoOTA.onStart([]() {
  178. //Serial.println("Start");
  179. });
  180. ArduinoOTA.onEnd([]() {
  181. //Serial.println("\nEnd");
  182. });
  183. ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
  184. //Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
  185. });
  186. ArduinoOTA.onError([](ota_error_t error) {
  187. ESP.restart();
  188. /*
  189. if (error == OTA_AUTH_ERROR) abort();
  190. else if (error == OTA_BEGIN_ERROR) abort();
  191. else if (error == OTA_CONNECT_ERROR) abort();
  192. else if (error == OTA_RECEIVE_ERROR) abort();
  193. else if (error == OTA_END_ERROR) abort();
  194. */
  195. });
  196.  
  197. ArduinoOTA.begin();
  198. }
  199.  
  200. void loop()
  201. {
  202. if (WiFi.status() == WL_CONNECTED) {
  203. if (!client.connected()) {
  204. long now = millis();
  205. if (now - lastReconnectAttempt > 2000) {
  206. lastReconnectAttempt = now;
  207. if (reconnect()) {
  208. lastReconnectAttempt = 0;
  209. }
  210. }
  211. } else {
  212. long now = millis();
  213. if (now - lastMsg > test_para) {
  214. lastMsg = now;
  215. String payload = "{\"startMills\":";
  216. payload += (millis() - startMills);
  217. payload += "}";
  218. sendmqttMsg(topic, payload);
  219. }
  220. client.loop();
  221. ArduinoOTA.handle();
  222. }
  223. } else {
  224. wifi_connect();
  225. }
  226.  
  227. }
  228.  
  229. void sendmqttMsg(char* topictosend, String payload)
  230. {
  231.  
  232. if (client.connected()) {
  233. if (DEBUG_PRINT) {
  234. Serial.print("Sending payload: ");
  235. Serial.print(payload);
  236. }
  237.  
  238. unsigned int msg_length = payload.length();
  239.  
  240. if (DEBUG_PRINT) {
  241. Serial.print(" length: ");
  242. Serial.println(msg_length);
  243. }
  244.  
  245. byte* p = (byte*)malloc(msg_length);
  246. memcpy(p, (char*) payload.c_str(), msg_length);
  247.  
  248. if ( client.publish(topictosend, p, msg_length)) {
  249. if (DEBUG_PRINT) {
  250. Serial.println("Publish ok");
  251. }
  252. free(p);
  253. //return 1;
  254. } else {
  255. if (DEBUG_PRINT) {
  256. Serial.println("Publish failed");
  257. }
  258. free(p);
  259. //return 0;
  260. }
  261. }
  262. }
  263.  
  264. String macToStr(const uint8_t* mac)
  265. {
  266. String result;
  267. for (int i = 0; i < 6; ++i) {
  268. result += String(mac[i], 16);
  269. if (i < 5)
  270. result += ':';
  271. }
  272. return result;
  273. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement