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:
e58136782000
Date:
Fri Nov 03 01:57:41 2017 +0000
Revision:
3:30d9b8d9adc9
Parent:
2:eab5b5c8271d
Child:
4:3642f63079ec
watersensor

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