Advertisement
Guest User

Untitled

a guest
Jun 21st, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.73 KB | None | 0 0
  1. //#define DEBUG
  2.  
  3. #include <ESP8266WiFi.h>
  4. #ifdef DEBUG
  5. #include <SoftEasyTransfer.h>
  6. #include <SoftwareSerial.h>
  7. #else
  8. #include <EasyTransfer.h>
  9. #endif
  10. #include <MQTT.h>
  11.  
  12.  
  13. #define DATA_LEN 55
  14.  
  15. #define AP_CONNECT_TIME 30 //s
  16.  
  17. #ifdef DEBUG
  18. SoftwareSerial mySerial(D2, D3); // RX, TX
  19. SoftEasyTransfer ETin;
  20. SoftEasyTransfer ETout;
  21. #else
  22. EasyTransfer ETin;
  23. EasyTransfer ETout;
  24. #endif
  25.  
  26.  
  27. unsigned long startTime;
  28.  
  29. typedef enum {
  30. CMD_NONE,
  31. CMD_RESET,
  32. CMD_INIT_FINISHED,
  33. CMD_CONNECT_TO_AP,
  34. CMD_AP_CONNECTED,
  35. CMD_AP_CONN_TIMEOUT,
  36. CMD_SET_AP_NAME,
  37. CMD_SET_AP_PASSWORD,
  38. CMD_SET_CLOUD_HOST,
  39. CMD_SET_CLOUD_USER,
  40. CMD_SET_CLOUD_PASSWORD,
  41. CMD_SET_CLOUD_CONNECT,
  42.  
  43. CMD_SET_CLOUD_CONNECTED,
  44. CMD_SET_CLOUD_DISCONNECTED,
  45.  
  46. CMD_GET_NEW_MODULE,
  47. CMD_SET_MODULE_ID,
  48. CMD_SET_MODULE_TYPE,
  49.  
  50.  
  51. CMD_GET_NEW_PARAMETER,
  52.  
  53. CMD_SET_PARAMETER_ISCOMMAND,
  54. CMD_SET_PARAMETER_DESCRIPTION,
  55. CMD_SET_PARAMETER_UNIT,
  56. CMD_SET_PARAMETER_DBLOGGING,
  57. CMD_SET_PARAMETER_CHARTSTEPS,
  58. CMD_SET_PARAMETER_UINOTIFICATIONS,
  59. CMD_SET_PARAMETER_DBAVGINTERVAL,
  60.  
  61. CMD_SUBSCRIBE_PARAMETER,
  62. CMD_SET_PARAMETER_VALUE,
  63.  
  64. CMD_PING,
  65. CMD_PONG,
  66. } comad_type;
  67.  
  68.  
  69.  
  70. struct DATA_STRUCTURE{
  71. //put your variable definitions here for the data you want to send
  72. //THIS MUST BE EXACTLY THE SAME ON THE OTHER ARDUINO
  73. int8_t header;
  74. int8_t command;
  75. char data[DATA_LEN + 1 ];
  76. } __attribute__((packed));
  77.  
  78.  
  79. DATA_STRUCTURE OutgoingData;
  80. DATA_STRUCTURE IncomingData;
  81.  
  82.  
  83. char ap_ssid[30];
  84. char ap_pass[30];
  85.  
  86. char cloud_host[30];
  87. char cloud_user[20];
  88. char cloud_pass[20];
  89.  
  90. char str1[DATA_LEN+1];
  91. char str2[DATA_LEN+1];
  92.  
  93. uint16_t moduleId;
  94. String topic;
  95. String valueStr;
  96.  
  97.  
  98. MQTT *myMqttPtr;
  99.  
  100. void onSTAGotIP(WiFiEventStationModeGotIP event) {
  101. #ifdef DEBUG
  102. Serial.printf("Got IP: %s\n", event.ip.toString().c_str());
  103. #endif
  104. }
  105.  
  106. void onSTADisconnected(WiFiEventStationModeDisconnected event_info) {
  107. #ifdef DEBUG
  108. Serial.printf("Disconnected from SSID: %s\n", event_info.ssid.c_str());
  109. #endif
  110. OutgoingData.command = CMD_AP_CONN_TIMEOUT;
  111. }
  112.  
  113.  
  114. void setup() {
  115.  
  116. static WiFiEventHandler e1, e2;
  117.  
  118.  
  119. // debug serial
  120. #ifdef DEBUG
  121. Serial.begin(115200);
  122. Serial.println();
  123. Serial.println("Start ESP Cloud GW");
  124. #endif
  125.  
  126. // default host
  127. strcpy(cloud_host, "cloud.iot-playground.com");
  128.  
  129. moduleId = 0;
  130. myMqttPtr = NULL;
  131.  
  132. OutgoingData.header = 0x68;
  133.  
  134. e1 = WiFi.onStationModeGotIP(onSTAGotIP);
  135. e2 = WiFi.onStationModeDisconnected(onSTADisconnected);
  136.  
  137. // init serial protocol
  138. #ifdef DEBUG
  139. mySerial.begin(9600);
  140. delay(50);
  141. ETin.begin(details(IncomingData), &mySerial);
  142. ETout.begin(details(OutgoingData), &mySerial);
  143. #else
  144. Serial.begin(9600);
  145. ETin.begin(details(IncomingData), &Serial);
  146. ETout.begin(details(OutgoingData), &Serial);
  147. #endif
  148.  
  149. delay(100);
  150. OutgoingData.command = CMD_INIT_FINISHED;
  151. OutgoingData.header = 0x68;
  152. ETout.sendData();
  153. OutgoingData.command = CMD_NONE;
  154. #ifdef DEBUG
  155. Serial.println("SEND CMD_INIT_FINISHED");
  156. #endif
  157. }
  158.  
  159. void loop() {
  160. int interval;
  161.  
  162. if (OutgoingData.command != CMD_NONE)
  163. {
  164. OutgoingData.header = 0x68;
  165. ETout.sendData();
  166. OutgoingData.command = CMD_NONE;
  167. delay(100);
  168. #ifdef DEBUG
  169. Serial.println("Send data");
  170. #endif
  171. }
  172.  
  173. if (IsTimeout(100))
  174. {
  175. ResetTimer();
  176. if(ETin.receiveData())
  177. {
  178. int i = 0;
  179. String clientName = "";
  180.  
  181. if (IncomingData.header == 0x68)
  182. {
  183. IncomingData.data[DATA_LEN] = 0;
  184. switch(IncomingData.command)
  185. {
  186. case CMD_NONE:
  187. break;
  188. case CMD_RESET:
  189. #ifdef DEBUG
  190. Serial.print("REC CMD_RESET");
  191. #endif
  192. delay(50);
  193. ESP.reset();
  194. break;
  195. case CMD_SET_AP_NAME:
  196. strcpy(ap_ssid, (char *)IncomingData.data);
  197. #ifdef DEBUG
  198. Serial.print("REC CMD_SET_AP_NAME ");
  199. Serial.println(ap_ssid);
  200. #endif
  201. break;
  202. case CMD_SET_AP_PASSWORD:
  203. strcpy(ap_pass, (char *)IncomingData.data);
  204. #ifdef DEBUG
  205. Serial.print("CMD_SET_AP_PASSWORD ");
  206. Serial.println(ap_pass);
  207. #endif
  208. break;
  209. case CMD_CONNECT_TO_AP:
  210. WiFi.mode(WIFI_STA);
  211. WiFi.begin(ap_ssid, ap_pass);
  212.  
  213. i = 0;
  214. #ifdef DEBUG
  215. Serial.println("Connecting to AP");
  216. #endif
  217. while (WiFi.status() != WL_CONNECTED && i++ < (AP_CONNECT_TIME*2) ) {
  218. delay(500);
  219. #ifdef DEBUG
  220. Serial.print(".");
  221. #endif
  222. }
  223.  
  224. if (WiFi.status() == WL_CONNECTED)
  225. {
  226. OutgoingData.command = CMD_AP_CONNECTED;
  227. #ifdef DEBUG
  228. Serial.println("Connected");
  229. #endif
  230. }
  231. else
  232. {
  233. OutgoingData.command = CMD_AP_CONN_TIMEOUT;
  234. #ifdef DEBUG
  235. Serial.println("Timeout");
  236. #endif
  237. }
  238. break;
  239. case CMD_SET_CLOUD_HOST:
  240. strcpy(cloud_host, (char *)IncomingData.data);
  241. #ifdef DEBUG
  242. Serial.print("REC CMD_SET_CLOUD_HOST ");
  243. Serial.println(cloud_host);
  244. #endif
  245. break;
  246. case CMD_SET_CLOUD_USER:
  247. strcpy(cloud_user, (char *)IncomingData.data);
  248. #ifdef DEBUG
  249. Serial.print("REC CMD_SET_CLOUD_USER ");
  250. Serial.println(cloud_user);
  251. #endif
  252. break;
  253. case CMD_SET_CLOUD_PASSWORD:
  254. strcpy(cloud_pass, (char *)IncomingData.data);
  255. #ifdef DEBUG
  256. Serial.print("REC CMD_SET_CLOUD_PASSWORD ");
  257. Serial.println(cloud_pass);
  258. #endif
  259. break;
  260. case CMD_GET_NEW_MODULE:
  261. #ifdef DEBUG
  262. Serial.print("REC CMD_GET_NEW_MODULE ");
  263. #endif
  264. moduleId = myMqttPtr->NewModule();
  265. #ifdef DEBUG
  266. Serial.println(moduleId);
  267. #endif
  268. OutgoingData.command = CMD_SET_MODULE_ID;
  269.  
  270. OutgoingData.data[0] = moduleId & 0xFF;
  271. OutgoingData.data[1] = (moduleId >> 8) & 0xFF;
  272. break;
  273. case CMD_SET_MODULE_ID:
  274. #ifdef DEBUG
  275. Serial.print("REC CMD_SET_MODULE_ID ");
  276. #endif
  277. moduleId = (IncomingData.data[1] << 8) + (uint8_t)IncomingData.data[0];
  278. #ifdef DEBUG
  279. Serial.println(moduleId);
  280. #endif
  281. break;
  282. case CMD_SET_MODULE_TYPE:
  283. #ifdef DEBUG
  284. Serial.print("CMD_SET_MODULE_TYPE ");
  285. #endif
  286. myMqttPtr->SetModuleType(moduleId, IncomingData.data);
  287. #ifdef DEBUG
  288. Serial.println(IncomingData.data);
  289. #endif
  290. break;
  291. case CMD_GET_NEW_PARAMETER:
  292. #ifdef DEBUG
  293. Serial.print("REC CMD_GET_NEW_PARAMETER ");
  294. Serial.println((char *)IncomingData.data);
  295. #endif
  296. myMqttPtr->NewModuleParameter(moduleId, (char *)IncomingData.data);
  297. break;
  298. case CMD_SET_PARAMETER_ISCOMMAND:
  299. #ifdef DEBUG
  300. Serial.print("REC CMD_SET_PARAMETER_ISCOMMAND");
  301. #endif
  302. parseStrings((char *)IncomingData.data, str1, str2);
  303. if (strcmp("true", str2) == 0 || strcmp("True", str2) || strcmp("TRUE", str2))
  304. myMqttPtr->SetParameterIsCommand(moduleId, str1, true);
  305. else
  306. myMqttPtr->SetParameterIsCommand(moduleId, str1, false);
  307. break;
  308. case CMD_SET_PARAMETER_UNIT:
  309. #ifdef DEBUG
  310. Serial.print("REC CMD_SET_PARAMETER_UNIT ");
  311. #endif
  312. parseStrings((char *)IncomingData.data, str1, str2);
  313. myMqttPtr->SetParameterUnit(moduleId, str1, str2);
  314. break;
  315. case CMD_SET_PARAMETER_DESCRIPTION:
  316. #ifdef DEBUG
  317. Serial.print("REC CMD_SET_PARAMETER_DESCRIPTION ");
  318. #endif
  319. parseStrings((char *)IncomingData.data, str1, str2);
  320. myMqttPtr->SetParameterDescription(moduleId, str1, str2);
  321. break;
  322. case CMD_SET_PARAMETER_DBLOGGING:
  323. #ifdef DEBUG
  324. Serial.print("REC CMD_SET_PARAMETER_DBLOGGING ");
  325. #endif
  326. parseStrings((char *)IncomingData.data, str1, str2);
  327. if (strcmp("true", str2) == 0 || strcmp("True", str2) || strcmp("TRUE", str2))
  328. myMqttPtr->SetParameterDBLogging(moduleId, str1, true);
  329. else
  330. myMqttPtr->SetParameterDBLogging(moduleId, str1, false);
  331. break;
  332. case CMD_SET_PARAMETER_CHARTSTEPS:
  333. #ifdef DEBUG
  334. Serial.print("REC CMD_SET_PARAMETER_CHARTSTEPS ");
  335. #endif
  336. parseStrings((char *)IncomingData.data, str1, str2);
  337. if (strcmp("true", str2) == 0 || strcmp("True", str2) || strcmp("TRUE", str2))
  338. myMqttPtr->SetParameterChartSteps(moduleId, str1, true);
  339. else
  340. myMqttPtr->SetParameterChartSteps(moduleId, str1, false);
  341. break;
  342. case CMD_SET_PARAMETER_UINOTIFICATIONS:
  343. #ifdef DEBUG
  344. Serial.print("REC CMD_SET_PARAMETER_UINOTIFICATIONS ");
  345. #endif
  346. parseStrings((char *)IncomingData.data, str1, str2);
  347. if (strcmp("true", str2) == 0 || strcmp("True", str2) || strcmp("TRUE", str2))
  348. myMqttPtr->SetParameterUINotifications(moduleId, str1, true);
  349. else
  350. myMqttPtr->SetParameterUINotifications(moduleId, str1, false);
  351. break;
  352. case CMD_SET_PARAMETER_DBAVGINTERVAL:
  353. #ifdef DEBUG
  354. Serial.print("REC CMD_SET_PARAMETER_DBAVGINTERVAL ");
  355. #endif
  356. parseStrings((char *)IncomingData.data, str1, str2);
  357. interval = atoi(str2);
  358. myMqttPtr->SetParameterDbAvgInterval(moduleId, str1, interval);
  359. break;
  360. case CMD_SUBSCRIBE_PARAMETER:
  361. #ifdef DEBUG
  362. Serial.print("CMD_SUBSCRIBE_PARAMETER ");
  363. #endif
  364. myMqttPtr->subscribe("/"+String(moduleId)+ "/" +IncomingData.data);
  365. #ifdef DEBUG
  366. Serial.println(IncomingData.data);
  367. #endif
  368. break;
  369. case CMD_SET_PARAMETER_VALUE:
  370. #ifdef DEBUG
  371. Serial.print("REC CMD_SET_PARAMETER_VALUE ");
  372. #endif
  373. parseStrings((char *)IncomingData.data, str1, str2);
  374. topic = "/"+String(moduleId)+ "/" + str1;
  375. #ifdef DEBUG
  376. Serial.print(topic);
  377. Serial.print(" ");
  378. Serial.println(str2);
  379. #endif
  380. valueStr = str2;
  381. myMqttPtr->publish(topic, valueStr, 0, 1);
  382. delay(100);
  383. break;
  384. case CMD_SET_CLOUD_CONNECT:
  385. #ifdef DEBUG
  386. Serial.println("REC CMD_SET_CLOUD_CONNECT");
  387. #endif
  388. myMqttPtr = new MQTT("", cloud_host, 1883);
  389.  
  390. clientName = "";
  391. uint8_t mac[6];
  392. WiFi.macAddress(mac);
  393. clientName += macToStr(mac);
  394. clientName += "-";
  395. clientName += String(micros() & 0xff, 16);
  396. myMqttPtr->setClientId((char*) clientName.c_str());
  397.  
  398. myMqttPtr->onConnected(myConnectedCb);
  399. myMqttPtr->onDisconnected(myDisconnectedCb);
  400. myMqttPtr->onPublished(myPublishedCb);
  401. myMqttPtr->onData(myDataCb);
  402. #ifdef DEBUG
  403. Serial.println("connect mqtt...");
  404. #endif
  405. myMqttPtr->setUserPwd(cloud_user, cloud_pass);
  406. myMqttPtr->connect();
  407. delay(20);
  408. break;
  409. case CMD_PING:
  410. OutgoingData.command = CMD_PONG;
  411. ETout.sendData();
  412. OutgoingData.command = CMD_NONE;
  413. #ifdef DEBUG
  414. Serial.println("PING PONG");
  415. #endif
  416. break;
  417. }
  418. }
  419. }
  420. }
  421. }
  422.  
  423. void parseStrings(const char* in, char* out1, char* out2)
  424. {
  425. strcpy(out1, in);
  426. int len = strlen(out1) + 1;
  427.  
  428. char* in1;
  429. in1 = (char *)in;
  430. in1 = in1 + len;
  431.  
  432. if (len>1)
  433. strcpy(out2, in1);
  434. else
  435. out2[0] = 0;
  436. }
  437.  
  438. void myConnectedCb() {
  439. #ifdef DEBUG
  440. Serial.println("connected to MQTT server");
  441. #endif
  442. OutgoingData.command = CMD_SET_CLOUD_CONNECTED;
  443. }
  444.  
  445. void myDisconnectedCb() {
  446. #ifdef DEBUG
  447. Serial.println("disconnected...");
  448. #endif
  449. OutgoingData.command = CMD_SET_CLOUD_DISCONNECTED;
  450. }
  451.  
  452. void myPublishedCb() {
  453. #ifdef DEBUG
  454. Serial.println("published.");
  455. #endif
  456. }
  457.  
  458. void CopyValuesToOutgoing(char *out, const char* in1, const char* in2)
  459. {
  460. strcpy(out, in1);
  461. int len = strlen(out) + 1;
  462. char* str1 = out;
  463. str1 += len;
  464. strcpy(str1, in2);
  465. }
  466.  
  467. void myDataCb(String& topic, String& data) {
  468. #ifdef DEBUG
  469. Serial.print("Received topic:");
  470. Serial.print(topic);
  471. Serial.print(": ");
  472. Serial.println(data);
  473. #endif
  474. char* pch;
  475. char tmp[DATA_LEN];
  476. strcpy(tmp, topic.c_str());
  477. pch=strchr(tmp,'/');
  478.  
  479. if (pch != NULL)
  480. {
  481. pch=strchr(pch+1,'/');
  482.  
  483. if (pch != NULL)
  484. {
  485. pch++;
  486. CopyValuesToOutgoing(OutgoingData.data, pch, data.c_str());
  487. OutgoingData.command = CMD_SET_PARAMETER_VALUE;
  488. #ifdef DEBUG
  489. Serial.print("parameter received: ");
  490. Serial.println(OutgoingData.data);
  491. #endif
  492. }
  493. }
  494. }
  495.  
  496. String macToStr(const uint8_t* mac)
  497. {
  498. String result;
  499. for (int i = 0; i < 6; ++i) {
  500. result += String(mac[i], 16);
  501. if (i < 5)
  502. result += ':';
  503. }
  504. return result;
  505. }
  506.  
  507. void ResetTimer()
  508. {
  509. startTime = millis();
  510. }
  511.  
  512. boolean IsTimeout(long timeout)
  513. {
  514. unsigned long now = millis();
  515. if (startTime <= now)
  516. {
  517. if ( (unsigned long)(now - startTime ) < timeout )
  518. return false;
  519. }
  520. else
  521. {
  522. if ( (unsigned long)(startTime - now) < timeout )
  523. return false;
  524. }
  525. return true;
  526. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement