Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: watersenor_and_temp_code MQTT NetworkSocketAPI X_NUCLEO_IDW01M1v2 mbed
Fork of Dissolved_oxygen_sensor_online by
main.cpp
00001 #include "mbed.h" 00002 #include "BME280.hpp" 00003 #include "SpwfInterface.h" 00004 #include "TCPSocket.h" 00005 #include "MQTTClient.h" 00006 #include "MQTTWiFi.h" 00007 #include "DS1820.h" 00008 00009 // MQTT use 00010 #define MQTT_MAX_PACKET_SIZE 250 00011 #define MQTT_MAX_PAYLOAD_SIZE 300 00012 //Configuration value needed to connect Red-node 00013 #define BROKER_URL "192.168.1.116"; 00014 #define MQTT_PORT 1883 00015 //MQTT use Topic 00016 #define TOPIC "1" 00017 #define SUB_TOPIC "LED" 00018 00019 00020 //Wifi network 00021 #define SSID "18-2F" 00022 #define PASSW "062432779" 00023 Serial serial(USBTX, USBRX); 00024 DigitalOut myled(LED1); 00025 //BME280 bmpSensor; 00026 int connack_rc = 0; // MQTT connack return code 00027 const char * ip_addr = ""; 00028 char *host_addr = ""; 00029 bool netConnecting = false; 00030 int connectTimeout = 1000; 00031 bool mqttConnecting = false; 00032 bool netConnected = false; 00033 bool connected = false; 00034 int retryAttempt = 0; 00035 char subscription_url[MQTT_MAX_PAYLOAD_SIZE]; 00036 00037 MQTT::Message message; 00038 MQTTString TopicName={TOPIC}; 00039 MQTT::MessageData MsgData1(TopicName, message); 00040 00041 BME280 BME280; 00042 00043 void subscribe_LED(char* msg){ 00044 int value = atoi(msg); 00045 //printf("value = %d\n", value); 00046 if(value ==1){ 00047 myled = !myled; 00048 } 00049 00050 } 00051 00052 00053 void subscribe_cb(MQTT::MessageData & msgMQTT) { 00054 char msg[MQTT_MAX_PAYLOAD_SIZE]; 00055 msg[0]='\0'; 00056 strncat (msg, (char*)msgMQTT.message.payload, msgMQTT.message.payloadlen); 00057 printf ("--->>> subscribe_cb msg: %s\n\r", msg); 00058 subscribe_LED(msg); 00059 } 00060 int subscribe(MQTT::Client<MQTTWiFi, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTWiFi* ipstack) 00061 { 00062 char* pubTopic = SUB_TOPIC; 00063 return client->subscribe(pubTopic, MQTT::QOS1, subscribe_cb); 00064 } 00065 00066 00067 int connect(MQTT::Client<MQTTWiFi, Countdown, MQTT_MAX_PACKET_SIZE>* client,MQTTWiFi* ipstack){ 00068 const char* host = BROKER_URL; 00069 00070 char hostname[strlen(host) + 1]; 00071 sprintf(hostname,"%s", host); 00072 00073 SpwfSAInterface& WiFi = ipstack->getWiFi(); 00074 00075 //Network Debug statements 00076 LOG("=====================================\n\r"); 00077 LOG("Connecting WiFi.\n\r"); 00078 LOG("Nucleo IP ADDRESS: %s\n\r", WiFi.get_ip_address()); 00079 LOG("Nucleo MAC ADDRESS: %s\n\r", WiFi.get_mac_address()); 00080 LOG("Server Hostname: %s port: %d\n\r", hostname, MQTT_PORT); 00081 LOG("Topic1: %s\n\r", TOPIC); 00082 //need subscrie 00083 LOG("=====================================\n\r"); 00084 netConnecting = true; 00085 ipstack->open(&ipstack->getWiFi()); 00086 int rc = ipstack->connect(hostname,MQTT_PORT,connectTimeout); 00087 if (rc != 0) 00088 { 00089 WARN("IP Stack connect returned: %d\n", rc); 00090 return rc; 00091 } 00092 printf ("--->TCP Connected\n\r"); 00093 netConnected = true; 00094 netConnecting = false; 00095 00096 //MQTT Connect 00097 mqttConnecting = true; 00098 MQTTPacket_connectData data = MQTTPacket_connectData_initializer; 00099 data.MQTTVersion = 4; 00100 data.struct_version =0; 00101 00102 if((rc = client->connect(data)) == 0){ 00103 connected = true; 00104 printf("--->MQTT Connected\n\r"); 00105 //#ifdef SUBSCRIBE 00106 if (!subscribe(client, ipstack)) printf ("--->>>MQTT subscribed to: %s\n\r",SUB_TOPIC); 00107 //#endif 00108 }else { 00109 WARN("MQTT connect returned %d\n", rc); 00110 } 00111 if (rc >= 0) 00112 connack_rc = rc; 00113 mqttConnecting = false; 00114 return rc; 00115 00116 } 00117 int getConnTimeout(int attemptNumber) 00118 { // First 10 attempts try within 3 seconds, next 10 attempts retry after every 1 minute 00119 // after 20 attempts, retry every 10 minutes 00120 return (attemptNumber < 10) ? 3 : (attemptNumber < 20) ? 60 : 600; 00121 } 00122 void attemptConnect(MQTT::Client<MQTTWiFi, Countdown, MQTT_MAX_PACKET_SIZE>* client, MQTTWiFi* ipstack) 00123 { 00124 connected = false; 00125 00126 while (connect(client, ipstack) != MQTT_CONNECTION_ACCEPTED) 00127 { 00128 /*if (connack_rc == MQTT_NOT_AUTHORIZED || connack_rc == MQTT_BAD_USERNAME_OR_PASSWORD) { 00129 printf ("File: %s, Line: %d Error: %d\n\r",__FILE__,__LINE__, connack_rc); 00130 return; // don't reattempt to connect if credentials are wrong 00131 } */ 00132 int timeout = getConnTimeout(++retryAttempt); 00133 WARN("Retry attempt number %d waiting %d\n", retryAttempt, timeout); 00134 00135 // if ipstack and client were on the heap we could deconstruct and goto a label where they are constructed 00136 // or maybe just add the proper members to do this disconnect and call attemptConnect(...) 00137 // this works - reset the system when the retry count gets to a threshold 00138 if (retryAttempt == 5) 00139 NVIC_SystemReset(); 00140 else 00141 wait(timeout); 00142 } 00143 } 00144 int publish(MQTT::Client<MQTTWiFi, Countdown, MQTT_MAX_PACKET_SIZE>* client,MQTTWiFi* ipstack,int data){ 00145 00146 00147 MQTT::Message message; 00148 char *pubTopic = TOPIC; 00149 char buf[MQTT_MAX_PAYLOAD_SIZE]; 00150 printf("Dissolved oxygen = %d\n", data); 00151 sprintf(buf,"%d",data); 00152 message.qos = MQTT::QOS0; 00153 message.retained = false; 00154 message.dup = false; 00155 message.payload = (void*)buf; 00156 message.payloadlen = strlen(buf); 00157 00158 printf("Publishing %s\n\r", buf); 00159 return client->publish(pubTopic, message); 00160 } 00161 00162 int ASCIIChangeInteger(int datain) 00163 { 00164 int databuffer1; 00165 if(datain >= 0x30) 00166 { 00167 if(datain >= 0x3A) 00168 { 00169 serial.printf("ASCIIChangeInteger datain too big \n"); 00170 } 00171 else 00172 { 00173 databuffer1 = datain - 0x30; 00174 } 00175 } 00176 else 00177 { 00178 serial.printf("ASCIIChangeInteger datain too small \n"); 00179 } 00180 serial.printf("ASCIIChangeInteger databuffer1 : %d \n", databuffer1); 00181 return databuffer1; 00182 } 00183 00184 int Conversionfunction(int Tensdigit, int Digits, int TheFirstDecimalPlace, int TheSecondDecimalPlace) 00185 { 00186 int databuffer; 00187 serial.printf("dataTensdigit: %d\n",Tensdigit); 00188 serial.printf("Digits: %d\n",Digits); 00189 databuffer = ASCIIChangeInteger(Tensdigit)*10 + ASCIIChangeInteger(Digits); 00190 serial.printf("datanumber: %d\n",databuffer); 00191 return databuffer; 00192 } 00193 int main() 00194 { 00195 myled =0; 00196 00197 00198 const char *ssid = SSID; 00199 const char *seckey = PASSW; 00200 //use SpwfSAInterface connect AP 00201 SpwfSAInterface spwf(D8,D2, false); 00202 printf("\r\nX-NUCLEO-IDW01M1 mbed \n"); 00203 printf("\r\nconnecting to AP\n"); 00204 //connect to Wifi 00205 MQTTWiFi ipstack(spwf, ssid, seckey, NSAPI_SECURITY_WPA2); 00206 //check wifi has got ip_address 00207 if(ipstack.getWiFi().get_ip_address() == 0){ 00208 printf("Connect WiFi is failed!\nPlease check your ssid and passwd is correct"); 00209 return 0; 00210 } 00211 printf("ip: %s\n",ipstack.getWiFi().get_ip_address() ); 00212 00213 MQTT::Client<MQTTWiFi, Countdown, MQTT_MAX_PACKET_SIZE> client(ipstack); 00214 attemptConnect(&client, &ipstack); 00215 sensor.format(8,Serial::None,1); 00216 int count = 0; 00217 int databuffer3 = 0; 00218 // tyeld.start(); 00219 while (true){ 00220 BME280.receive(); 00221 serial.printf("data 1 : %d\n",BME280.datareadfuiction(0)); 00222 serial.printf("data 2 : %d\n",BME280.datareadfuiction(1)); 00223 serial.printf("data 3 : %d\n",BME280.datareadfuiction(2)); 00224 serial.printf("data 4 : %d\n",BME280.datareadfuiction(3)); 00225 serial.printf("data 5 : %d\n",BME280.datareadfuiction(4)); 00226 databuffer3 = Conversionfunction(BME280.datareadfuiction(3),BME280.datareadfuiction(4),BME280.datareadfuiction(0),BME280.datareadfuiction(1)); 00227 if (++count == 3) 00228 { // Publish a message every second 00229 if (publish(&client, &ipstack,databuffer3) != 0) 00230 { 00231 attemptConnect(&client, &ipstack); // if we have lost the connection 00232 } 00233 count = 0; 00234 } 00235 else 00236 { 00237 serial.printf("count: %d \n",count); 00238 } 00239 // int start = tyeld.read_ms(); 00240 //client.yield(10); // allow the MQTT client to receive messages 00241 // printf ("tyeld: %d\n\r",tyeld.read_ms()-start); 00242 } 00243 }
Generated on Thu Jul 14 2022 00:17:19 by
1.7.2
