use Dussolved_oxygen_sensor_on_wifi

Dependencies:   watersenor_and_temp_code MQTT NetworkSocketAPI X_NUCLEO_IDW01M1v2 mbed

Fork of DS1820_IDW01M1 by CHANG rozen

Committer:
rozendhyan@gmail.com
Date:
Tue Oct 31 23:56:09 2017 +0800
Revision:
1:2ee9e5685a1e
Parent:
0:80c7778c82b8
Child:
2:eab5b5c8271d
add subscribe

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rozendhyan@gmail.com 0:80c7778c82b8 1 #include "mbed.h"
rozendhyan@gmail.com 0:80c7778c82b8 2 #include "BME280.hpp"
rozendhyan@gmail.com 0:80c7778c82b8 3 #include "SpwfInterface.h"
rozendhyan@gmail.com 0:80c7778c82b8 4 #include "TCPSocket.h"
rozendhyan@gmail.com 0:80c7778c82b8 5 #include "MQTTClient.h"
rozendhyan@gmail.com 0:80c7778c82b8 6 #include "MQTTWiFi.h"
rozendhyan@gmail.com 0:80c7778c82b8 7
rozendhyan@gmail.com 0:80c7778c82b8 8 // MQTT use
rozendhyan@gmail.com 0:80c7778c82b8 9 #define MQTT_MAX_PACKET_SIZE 250
rozendhyan@gmail.com 0:80c7778c82b8 10 #define MQTT_MAX_PAYLOAD_SIZE 300
rozendhyan@gmail.com 0:80c7778c82b8 11 //Configuration value needed to connect Red-node
rozendhyan@gmail.com 0:80c7778c82b8 12 #define BROKER_URL "192.168.0.10";
rozendhyan@gmail.com 0:80c7778c82b8 13 #define MQTT_PORT 1883
rozendhyan@gmail.com 0:80c7778c82b8 14 //MQTT use Topic
rozendhyan@gmail.com 0:80c7778c82b8 15 #define TOPIC "temp"
rozendhyan@gmail.com 1:2ee9e5685a1e 16 #define SUB_TOPIC "LED"
rozendhyan@gmail.com 0:80c7778c82b8 17
rozendhyan@gmail.com 0:80c7778c82b8 18
rozendhyan@gmail.com 0:80c7778c82b8 19 //Wifi network
rozendhyan@gmail.com 0:80c7778c82b8 20 #define SSID "megu megu fire"
rozendhyan@gmail.com 0:80c7778c82b8 21 #define PASSW "66666667"
rozendhyan@gmail.com 0:80c7778c82b8 22
rozendhyan@gmail.com 0:80c7778c82b8 23 DigitalOut myled(LED1);
rozendhyan@gmail.com 0:80c7778c82b8 24 BME280 bmpSensor;
rozendhyan@gmail.com 0:80c7778c82b8 25
rozendhyan@gmail.com 0:80c7778c82b8 26 int connack_rc = 0; // MQTT connack return code
rozendhyan@gmail.com 0:80c7778c82b8 27 const char * ip_addr = "";
rozendhyan@gmail.com 0:80c7778c82b8 28 char *host_addr = "";
rozendhyan@gmail.com 0:80c7778c82b8 29 bool netConnecting = false;
rozendhyan@gmail.com 0:80c7778c82b8 30 int connectTimeout = 1000;
rozendhyan@gmail.com 0:80c7778c82b8 31 bool mqttConnecting = false;
rozendhyan@gmail.com 0:80c7778c82b8 32 bool netConnected = false;
rozendhyan@gmail.com 0:80c7778c82b8 33 bool connected = false;
rozendhyan@gmail.com 0:80c7778c82b8 34 int retryAttempt = 0;
rozendhyan@gmail.com 0:80c7778c82b8 35 char subscription_url[MQTT_MAX_PAYLOAD_SIZE];
rozendhyan@gmail.com 0:80c7778c82b8 36
rozendhyan@gmail.com 0:80c7778c82b8 37 MQTT::Message message;
rozendhyan@gmail.com 0:80c7778c82b8 38 MQTTString TopicName={TOPIC};
rozendhyan@gmail.com 0:80c7778c82b8 39 MQTT::MessageData MsgData(TopicName, message);
rozendhyan@gmail.com 0:80c7778c82b8 40
rozendhyan@gmail.com 1:2ee9e5685a1e 41 void subscribe_LED(char* msg){
rozendhyan@gmail.com 1:2ee9e5685a1e 42 int value = atoi(msg);
rozendhyan@gmail.com 1:2ee9e5685a1e 43 //printf("value = %d\n", value);
rozendhyan@gmail.com 1:2ee9e5685a1e 44 if(value ==1){
rozendhyan@gmail.com 1:2ee9e5685a1e 45 myled = !myled;
rozendhyan@gmail.com 1:2ee9e5685a1e 46 }
rozendhyan@gmail.com 1:2ee9e5685a1e 47
rozendhyan@gmail.com 1:2ee9e5685a1e 48 }
rozendhyan@gmail.com 1:2ee9e5685a1e 49
rozendhyan@gmail.com 0:80c7778c82b8 50
rozendhyan@gmail.com 0:80c7778c82b8 51 void subscribe_cb(MQTT::MessageData & msgMQTT) {
rozendhyan@gmail.com 0:80c7778c82b8 52 char msg[MQTT_MAX_PAYLOAD_SIZE];
rozendhyan@gmail.com 0:80c7778c82b8 53 msg[0]='\0';
rozendhyan@gmail.com 0:80c7778c82b8 54 strncat (msg, (char*)msgMQTT.message.payload, msgMQTT.message.payloadlen);
rozendhyan@gmail.com 0:80c7778c82b8 55 printf ("--->>> subscribe_cb msg: %s\n\r", msg);
rozendhyan@gmail.com 1:2ee9e5685a1e 56 subscribe_LED(msg);
rozendhyan@gmail.com 0:80c7778c82b8 57 }
rozendhyan@gmail.com 0:80c7778c82b8 58 int subscribe(MQTT::Client<MQTTWiFi, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTWiFi* ipstack)
rozendhyan@gmail.com 0:80c7778c82b8 59 {
rozendhyan@gmail.com 1:2ee9e5685a1e 60 char* pubTopic = SUB_TOPIC;
rozendhyan@gmail.com 0:80c7778c82b8 61 return client->subscribe(pubTopic, MQTT::QOS1, subscribe_cb);
rozendhyan@gmail.com 0:80c7778c82b8 62 }
rozendhyan@gmail.com 0:80c7778c82b8 63
rozendhyan@gmail.com 0:80c7778c82b8 64
rozendhyan@gmail.com 0:80c7778c82b8 65 int connect(MQTT::Client<MQTTWiFi, Countdown, MQTT_MAX_PACKET_SIZE>* client,MQTTWiFi* ipstack){
rozendhyan@gmail.com 0:80c7778c82b8 66 const char* host = BROKER_URL;
rozendhyan@gmail.com 0:80c7778c82b8 67
rozendhyan@gmail.com 0:80c7778c82b8 68 char hostname[strlen(host) + 1];
rozendhyan@gmail.com 0:80c7778c82b8 69 sprintf(hostname,"%s", host);
rozendhyan@gmail.com 0:80c7778c82b8 70
rozendhyan@gmail.com 0:80c7778c82b8 71 SpwfSAInterface& WiFi = ipstack->getWiFi();
rozendhyan@gmail.com 0:80c7778c82b8 72
rozendhyan@gmail.com 0:80c7778c82b8 73 //Network Debug statements
rozendhyan@gmail.com 0:80c7778c82b8 74 LOG("=====================================\n\r");
rozendhyan@gmail.com 0:80c7778c82b8 75 LOG("Connecting WiFi.\n\r");
rozendhyan@gmail.com 0:80c7778c82b8 76 LOG("Nucleo IP ADDRESS: %s\n\r", WiFi.get_ip_address());
rozendhyan@gmail.com 0:80c7778c82b8 77 LOG("Nucleo MAC ADDRESS: %s\n\r", WiFi.get_mac_address());
rozendhyan@gmail.com 0:80c7778c82b8 78 LOG("Server Hostname: %s port: %d\n\r", hostname, MQTT_PORT);
rozendhyan@gmail.com 0:80c7778c82b8 79 LOG("Topic: %s\n\r", TOPIC);
rozendhyan@gmail.com 0:80c7778c82b8 80 //need subscrie
rozendhyan@gmail.com 0:80c7778c82b8 81 LOG("=====================================\n\r");
rozendhyan@gmail.com 0:80c7778c82b8 82 netConnecting = true;
rozendhyan@gmail.com 0:80c7778c82b8 83 ipstack->open(&ipstack->getWiFi());
rozendhyan@gmail.com 0:80c7778c82b8 84 int rc = ipstack->connect(hostname,MQTT_PORT,connectTimeout);
rozendhyan@gmail.com 0:80c7778c82b8 85 if (rc != 0)
rozendhyan@gmail.com 0:80c7778c82b8 86 {
rozendhyan@gmail.com 0:80c7778c82b8 87 WARN("IP Stack connect returned: %d\n", rc);
rozendhyan@gmail.com 0:80c7778c82b8 88 return rc;
rozendhyan@gmail.com 0:80c7778c82b8 89 }
rozendhyan@gmail.com 0:80c7778c82b8 90 printf ("--->TCP Connected\n\r");
rozendhyan@gmail.com 0:80c7778c82b8 91 netConnected = true;
rozendhyan@gmail.com 0:80c7778c82b8 92 netConnecting = false;
rozendhyan@gmail.com 0:80c7778c82b8 93
rozendhyan@gmail.com 0:80c7778c82b8 94 //MQTT Connect
rozendhyan@gmail.com 0:80c7778c82b8 95 mqttConnecting = true;
rozendhyan@gmail.com 0:80c7778c82b8 96 MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
rozendhyan@gmail.com 0:80c7778c82b8 97 data.MQTTVersion = 4;
rozendhyan@gmail.com 0:80c7778c82b8 98 data.struct_version =0;
rozendhyan@gmail.com 0:80c7778c82b8 99
rozendhyan@gmail.com 0:80c7778c82b8 100 if((rc = client->connect(data)) == 0){
rozendhyan@gmail.com 0:80c7778c82b8 101 connected = true;
rozendhyan@gmail.com 0:80c7778c82b8 102 printf("--->MQTT Connected\n\r");
rozendhyan@gmail.com 1:2ee9e5685a1e 103 //#ifdef SUBSCRIBE
rozendhyan@gmail.com 1:2ee9e5685a1e 104 if (!subscribe(client, ipstack)) printf ("--->>>MQTT subscribed to: %s\n\r",SUB_TOPIC);
rozendhyan@gmail.com 1:2ee9e5685a1e 105 //#endif
rozendhyan@gmail.com 0:80c7778c82b8 106 }else {
rozendhyan@gmail.com 0:80c7778c82b8 107 WARN("MQTT connect returned %d\n", rc);
rozendhyan@gmail.com 0:80c7778c82b8 108 }
rozendhyan@gmail.com 0:80c7778c82b8 109 if (rc >= 0)
rozendhyan@gmail.com 0:80c7778c82b8 110 connack_rc = rc;
rozendhyan@gmail.com 0:80c7778c82b8 111 mqttConnecting = false;
rozendhyan@gmail.com 0:80c7778c82b8 112 return rc;
rozendhyan@gmail.com 0:80c7778c82b8 113
rozendhyan@gmail.com 0:80c7778c82b8 114 }
rozendhyan@gmail.com 0:80c7778c82b8 115 int getConnTimeout(int attemptNumber)
rozendhyan@gmail.com 0:80c7778c82b8 116 { // First 10 attempts try within 3 seconds, next 10 attempts retry after every 1 minute
rozendhyan@gmail.com 0:80c7778c82b8 117 // after 20 attempts, retry every 10 minutes
rozendhyan@gmail.com 0:80c7778c82b8 118 return (attemptNumber < 10) ? 3 : (attemptNumber < 20) ? 60 : 600;
rozendhyan@gmail.com 0:80c7778c82b8 119 }
rozendhyan@gmail.com 0:80c7778c82b8 120 void attemptConnect(MQTT::Client<MQTTWiFi, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTWiFi* ipstack)
rozendhyan@gmail.com 0:80c7778c82b8 121 {
rozendhyan@gmail.com 0:80c7778c82b8 122 connected = false;
rozendhyan@gmail.com 0:80c7778c82b8 123
rozendhyan@gmail.com 0:80c7778c82b8 124 while (connect(client, ipstack) != MQTT_CONNECTION_ACCEPTED)
rozendhyan@gmail.com 0:80c7778c82b8 125 {
rozendhyan@gmail.com 0:80c7778c82b8 126 /*if (connack_rc == MQTT_NOT_AUTHORIZED || connack_rc == MQTT_BAD_USERNAME_OR_PASSWORD) {
rozendhyan@gmail.com 0:80c7778c82b8 127 printf ("File: %s, Line: %d Error: %d\n\r",__FILE__,__LINE__, connack_rc);
rozendhyan@gmail.com 0:80c7778c82b8 128 return; // don't reattempt to connect if credentials are wrong
rozendhyan@gmail.com 0:80c7778c82b8 129 } */
rozendhyan@gmail.com 0:80c7778c82b8 130 int timeout = getConnTimeout(++retryAttempt);
rozendhyan@gmail.com 0:80c7778c82b8 131 WARN("Retry attempt number %d waiting %d\n", retryAttempt, timeout);
rozendhyan@gmail.com 0:80c7778c82b8 132
rozendhyan@gmail.com 0:80c7778c82b8 133 // if ipstack and client were on the heap we could deconstruct and goto a label where they are constructed
rozendhyan@gmail.com 0:80c7778c82b8 134 // or maybe just add the proper members to do this disconnect and call attemptConnect(...)
rozendhyan@gmail.com 0:80c7778c82b8 135 // this works - reset the system when the retry count gets to a threshold
rozendhyan@gmail.com 0:80c7778c82b8 136 if (retryAttempt == 5)
rozendhyan@gmail.com 0:80c7778c82b8 137 NVIC_SystemReset();
rozendhyan@gmail.com 0:80c7778c82b8 138 else
rozendhyan@gmail.com 0:80c7778c82b8 139 wait(timeout);
rozendhyan@gmail.com 0:80c7778c82b8 140 }
rozendhyan@gmail.com 0:80c7778c82b8 141 }
rozendhyan@gmail.com 0:80c7778c82b8 142 int publish (MQTT::Client<MQTTWiFi, Countdown, MQTT_MAX_PACKET_SIZE>* client,MQTTWiFi* ipstack){
rozendhyan@gmail.com 0:80c7778c82b8 143
rozendhyan@gmail.com 0:80c7778c82b8 144
rozendhyan@gmail.com 0:80c7778c82b8 145 MQTT::Message message;
rozendhyan@gmail.com 0:80c7778c82b8 146 int data = bmpSensor.Temp_read();
rozendhyan@gmail.com 0:80c7778c82b8 147 char *pubTopic = TOPIC;
rozendhyan@gmail.com 0:80c7778c82b8 148 char buf[MQTT_MAX_PAYLOAD_SIZE];
rozendhyan@gmail.com 0:80c7778c82b8 149
rozendhyan@gmail.com 0:80c7778c82b8 150
rozendhyan@gmail.com 0:80c7778c82b8 151 printf("Temp = %d\n", data);
rozendhyan@gmail.com 0:80c7778c82b8 152 sprintf(buf,"%d",data);
rozendhyan@gmail.com 0:80c7778c82b8 153 message.qos = MQTT::QOS0;
rozendhyan@gmail.com 0:80c7778c82b8 154 message.retained = false;
rozendhyan@gmail.com 0:80c7778c82b8 155 message.dup = false;
rozendhyan@gmail.com 0:80c7778c82b8 156 message.payload = (void*)buf;
rozendhyan@gmail.com 0:80c7778c82b8 157 message.payloadlen = strlen(buf);
rozendhyan@gmail.com 0:80c7778c82b8 158
rozendhyan@gmail.com 0:80c7778c82b8 159 printf("Publishing %s\n\r", buf);
rozendhyan@gmail.com 0:80c7778c82b8 160 return client->publish(pubTopic, message);
rozendhyan@gmail.com 0:80c7778c82b8 161 }
rozendhyan@gmail.com 0:80c7778c82b8 162
rozendhyan@gmail.com 0:80c7778c82b8 163 int main(int argc, char const *argv[])
rozendhyan@gmail.com 0:80c7778c82b8 164 {
rozendhyan@gmail.com 0:80c7778c82b8 165 myled =0;
rozendhyan@gmail.com 0:80c7778c82b8 166 /* code */
rozendhyan@gmail.com 0:80c7778c82b8 167 const char *ssid = SSID;
rozendhyan@gmail.com 0:80c7778c82b8 168 const char *seckey = PASSW;
rozendhyan@gmail.com 0:80c7778c82b8 169 //use SpwfSAInterface connect AP
rozendhyan@gmail.com 0:80c7778c82b8 170 SpwfSAInterface spwf(D8,D2, false);
rozendhyan@gmail.com 0:80c7778c82b8 171
rozendhyan@gmail.com 0:80c7778c82b8 172 printf("\r\nX-NUCLEO-IDW01M1 mbed \n");
rozendhyan@gmail.com 0:80c7778c82b8 173 printf("\r\nconnecting to AP\n");
rozendhyan@gmail.com 0:80c7778c82b8 174 //connect to Wifi
rozendhyan@gmail.com 0:80c7778c82b8 175 MQTTWiFi ipstack(spwf, ssid, seckey, NSAPI_SECURITY_WPA2);
rozendhyan@gmail.com 0:80c7778c82b8 176 //check wifi has got ip_address
rozendhyan@gmail.com 0:80c7778c82b8 177 if(ipstack.getWiFi().get_ip_address() == 0){
rozendhyan@gmail.com 0:80c7778c82b8 178 printf("Connect WiFi is failed!\nPlease check your ssid and passwd is correct");
rozendhyan@gmail.com 0:80c7778c82b8 179 return 0;
rozendhyan@gmail.com 0:80c7778c82b8 180 }
rozendhyan@gmail.com 0:80c7778c82b8 181 printf("ip: %s\n",ipstack.getWiFi().get_ip_address() );
rozendhyan@gmail.com 0:80c7778c82b8 182
rozendhyan@gmail.com 0:80c7778c82b8 183 MQTT::Client<MQTTWiFi, Countdown, MQTT_MAX_PACKET_SIZE> client(ipstack);
rozendhyan@gmail.com 0:80c7778c82b8 184 attemptConnect(&client, &ipstack);
rozendhyan@gmail.com 0:80c7778c82b8 185
rozendhyan@gmail.com 1:2ee9e5685a1e 186
rozendhyan@gmail.com 0:80c7778c82b8 187 int count = 0;
rozendhyan@gmail.com 0:80c7778c82b8 188 // tyeld.start();
rozendhyan@gmail.com 0:80c7778c82b8 189 while (true)
rozendhyan@gmail.com 0:80c7778c82b8 190 {
rozendhyan@gmail.com 0:80c7778c82b8 191 if (++count == 100)
rozendhyan@gmail.com 0:80c7778c82b8 192 { // Publish a message every second
rozendhyan@gmail.com 0:80c7778c82b8 193 if (publish(&client, &ipstack) != 0) {
rozendhyan@gmail.com 1:2ee9e5685a1e 194
rozendhyan@gmail.com 0:80c7778c82b8 195 attemptConnect(&client, &ipstack); // if we have lost the connection
rozendhyan@gmail.com 1:2ee9e5685a1e 196 }
rozendhyan@gmail.com 0:80c7778c82b8 197 count = 0;
rozendhyan@gmail.com 0:80c7778c82b8 198 }
rozendhyan@gmail.com 0:80c7778c82b8 199 // int start = tyeld.read_ms();
rozendhyan@gmail.com 0:80c7778c82b8 200 client.yield(10); // allow the MQTT client to receive messages
rozendhyan@gmail.com 0:80c7778c82b8 201 // printf ("tyeld: %d\n\r",tyeld.read_ms()-start);
rozendhyan@gmail.com 0:80c7778c82b8 202
rozendhyan@gmail.com 0:80c7778c82b8 203 }
rozendhyan@gmail.com 0:80c7778c82b8 204
rozendhyan@gmail.com 0:80c7778c82b8 205 return 0;
rozendhyan@gmail.com 0:80c7778c82b8 206 }