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