1 | #include "rca.h"
|
---|
2 | #include "ESP8266.h"
|
---|
3 | #include "Client_ESP8266.h"
|
---|
4 | #include <PubSubClient.h>
|
---|
5 | #include <ArduinoJson.h>
|
---|
6 |
|
---|
7 | #define ORGANIZATION "" //gDID
|
---|
8 | #define DEVICE_TYPE "" //foCXE^Cv
|
---|
9 | #define DEVICE_ID "" //foCXID
|
---|
10 | #define PASSWORD "" //FØg[N
|
---|
11 |
|
---|
12 | char serverName[] = ORGANIZATION ".messaging.internetofthings.ibmcloud.com";
|
---|
13 | String clientName = String("d:") + String(ORGANIZATION) + String(":") + String(DEVICE_TYPE) + String(":") + String(DEVICE_ID);
|
---|
14 | String topicName = String("iot-2/evt/status/fmt/json");
|
---|
15 | char username[] = "use-token-auth";
|
---|
16 | char password[] = PASSWORD;
|
---|
17 |
|
---|
18 | ESP8266Client wifi;
|
---|
19 | PubSubClient client(serverName, 1883, 0, wifi);
|
---|
20 |
|
---|
21 | #define SSID ""
|
---|
22 | #define WIFI_PASSWORD ""
|
---|
23 |
|
---|
24 | void MqttConnect()
|
---|
25 | {
|
---|
26 | while(!client.connected())
|
---|
27 | {
|
---|
28 | char clientStr[128];
|
---|
29 | clientName.toCharArray(clientStr,sizeof(clientStr));
|
---|
30 |
|
---|
31 | Serial.print("Trying to connect to: ");
|
---|
32 | Serial.println(clientStr);
|
---|
33 | client.connect(clientStr,username,password);
|
---|
34 | }
|
---|
35 | }
|
---|
36 |
|
---|
37 | void MqttRequest(int level)
|
---|
38 | {
|
---|
39 | MqttConnect();
|
---|
40 |
|
---|
41 | if(client.connected())
|
---|
42 | {
|
---|
43 | char topicStr[128];
|
---|
44 | topicName.toCharArray(topicStr,sizeof(topicStr));
|
---|
45 | char jsonStr[200];
|
---|
46 | DynamicJsonBuffer jsonBuffer;
|
---|
47 | JsonObject& root = jsonBuffer.createObject();
|
---|
48 | JsonObject& d = root.createNestedObject("d");
|
---|
49 | d["cds"] = level;
|
---|
50 | root.printTo(jsonStr, sizeof(jsonStr));
|
---|
51 |
|
---|
52 | boolean pubresult = client.publish(topicStr,jsonStr);
|
---|
53 |
|
---|
54 | Serial.print("attempt to send ");
|
---|
55 | Serial.println(jsonStr);
|
---|
56 | Serial.print("to ");
|
---|
57 | Serial.println(topicStr);
|
---|
58 |
|
---|
59 | if(pubresult)
|
---|
60 | Serial.println("successfully sent");
|
---|
61 | else
|
---|
62 | Serial.println("unsuccessfully sent");
|
---|
63 | }
|
---|
64 | else
|
---|
65 | {
|
---|
66 | Serial.println("connect error");
|
---|
67 | }
|
---|
68 | }
|
---|
69 |
|
---|
70 | void setup()
|
---|
71 | {
|
---|
72 | int ret;
|
---|
73 |
|
---|
74 | Serial.println("Echo Server : Start!");
|
---|
75 |
|
---|
76 | ret = wifi.begin(Serial5, 115200);
|
---|
77 |
|
---|
78 | if(ret == 1) {
|
---|
79 | Serial.print("Cannot communicate with ESP8266.");
|
---|
80 | while(1);
|
---|
81 | } else if(ret == 2) {
|
---|
82 | Serial.println("FW Version mismatch.");
|
---|
83 | Serial.print("FW Version:");
|
---|
84 | Serial.println(wifi.getVersion().c_str());
|
---|
85 | Serial.print("Supported FW Version:");
|
---|
86 | Serial.println(ESP8266_SUPPORT_VERSION);
|
---|
87 | while(1);
|
---|
88 | } else {
|
---|
89 | Serial.print("begin ok\r\n");
|
---|
90 | }
|
---|
91 |
|
---|
92 | Serial.print("FW Version:");
|
---|
93 | Serial.println(wifi.getVersion().c_str());
|
---|
94 |
|
---|
95 | if (wifi.setOprToStation()) {
|
---|
96 | Serial.print("to station ok\r\n");
|
---|
97 | } else {
|
---|
98 | Serial.print("to station err\r\n");
|
---|
99 | }
|
---|
100 |
|
---|
101 | if (wifi.joinAP(SSID, WIFI_PASSWORD)) {
|
---|
102 | Serial.print("Join AP success\r\n");
|
---|
103 | Serial.print("IP: ");
|
---|
104 | Serial.println(wifi.getLocalIP().c_str());
|
---|
105 | } else {
|
---|
106 | Serial.print("Join AP failure\r\n");
|
---|
107 | }
|
---|
108 |
|
---|
109 | if (wifi.stopServer()) {
|
---|
110 | Serial.print("Stop server ok\r\n");
|
---|
111 | } else {
|
---|
112 | Serial.print("Stop server err\r\n");
|
---|
113 | }
|
---|
114 |
|
---|
115 | if (wifi.disableMUX()) {
|
---|
116 | Serial.print("single ok\r\n");
|
---|
117 | } else {
|
---|
118 | Serial.print("single err\r\n");
|
---|
119 | }
|
---|
120 |
|
---|
121 | MqttConnect();
|
---|
122 | Serial.println("MQTT set up");
|
---|
123 |
|
---|
124 | Wire.begin();
|
---|
125 | TSL2561.init();
|
---|
126 | }
|
---|
127 |
|
---|
128 | int cnt = 0;
|
---|
129 |
|
---|
130 | void loop()
|
---|
131 | {
|
---|
132 | MqttRequest(cnt++);
|
---|
133 | delay(1000);
|
---|
134 | client.loop();
|
---|
135 | }
|
---|