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:
Mon Nov 06 15:04:43 2017 +0000
Revision:
5:42113ea1b319
Parent:
4:3642f63079ec
Child:
6:54965cdc7955
Child:
7:8eaef8890eee
use EZO Dissolved_oxygen;

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