turbidity sensor

Dependencies:   watersenor_and_temp_code MQTT NetworkSocketAPI X_NUCLEO_IDW01M1v2 mbed

Fork of Dissolved_oxygen_sensor_online by wang tang

Revision:
5:42113ea1b319
Parent:
4:3642f63079ec
Child:
6:f037cfd92bde
diff -r 3642f63079ec -r 42113ea1b319 main.cpp
--- a/main.cpp	Sat Nov 04 00:14:23 2017 +0800
+++ b/main.cpp	Mon Nov 06 15:04:43 2017 +0000
@@ -10,18 +10,17 @@
 #define MQTT_MAX_PACKET_SIZE 250
 #define MQTT_MAX_PAYLOAD_SIZE 300
 //Configuration value needed to connect Red-node
-#define BROKER_URL "192.168.20.116";
+#define BROKER_URL "192.168.1.110";
 #define MQTT_PORT 1883
 //MQTT use Topic 
-#define TOPIC1 "1"
-#define TOPIC2 "3"
+#define TOPIC "1"
 #define SUB_TOPIC "LED"
 
 
 //Wifi network
-#define SSID "tnta"
-#define PASSW "tnta2355818"
-
+#define SSID "18-2F"
+#define PASSW "062432779"
+Serial serial(USBTX, USBRX);
 DigitalOut myled(LED1);
 //BME280 bmpSensor;
 int connack_rc = 0;    // MQTT connack return code
@@ -35,18 +34,11 @@
 int retryAttempt = 0;
 char subscription_url[MQTT_MAX_PAYLOAD_SIZE];
 
-
-
-Serial serial(USBTX, USBRX);
-DS1820  ds1820(PA_8);    // substitute PA_9 with actual mbed pin name connected to the DS1820 data pin
-
+MQTT::Message message;
+MQTTString TopicName={TOPIC};
+MQTT::MessageData MsgData1(TopicName, message);
 
-
-MQTT::Message message;
-MQTTString TopicName1={TOPIC1};
-MQTTString TopicName2={TOPIC2};
-MQTT::MessageData MsgData1(TopicName1, message);
-MQTT::MessageData MsgData2(TopicName2, message);
+BME280 BME280;
 
 void subscribe_LED(char* msg){
 	int value = atoi(msg);
@@ -86,8 +78,7 @@
     LOG("Nucleo IP ADDRESS: %s\n\r", WiFi.get_ip_address());
     LOG("Nucleo MAC ADDRESS: %s\n\r", WiFi.get_mac_address());
     LOG("Server Hostname: %s port: %d\n\r", hostname, MQTT_PORT);
-    LOG("Topic1: %s\n\r", TOPIC1);
-    LOG("Topic2: %s\n\r", TOPIC2);
+    LOG("Topic1: %s\n\r", TOPIC);
     //need subscrie
     LOG("=====================================\n\r");
     netConnecting = true;
@@ -150,13 +141,13 @@
             wait(timeout);
     }
 }
-int publish (MQTT::Client<MQTTWiFi, Countdown, MQTT_MAX_PACKET_SIZE>* client,MQTTWiFi* ipstack,int data){
+int publish(MQTT::Client<MQTTWiFi, Countdown, MQTT_MAX_PACKET_SIZE>* client,MQTTWiFi* ipstack,int data){
 	
     
 	MQTT::Message message;
-	char *pubTopic = TOPIC1;
+	char *pubTopic = TOPIC;
 	char buf[MQTT_MAX_PAYLOAD_SIZE];
-	printf("Temp = %d\n", data);
+	printf("Dissolved oxygen = %d\n", data);
 	sprintf(buf,"%d",data);
 	message.qos = MQTT::QOS0;
 	message.retained = false;
@@ -168,6 +159,37 @@
     return client->publish(pubTopic, message);
 }
 
+int ASCIIChangeInteger(int datain)
+{
+	int databuffer1;
+	if(datain >= 0x30)
+	{
+		if(datain >= 0x3A)
+		{
+			serial.printf("ASCIIChangeInteger datain too big \n");			
+		}
+		else
+		{
+			databuffer1 = datain - 0x30;			
+		}
+	}
+	else
+	{
+			serial.printf("ASCIIChangeInteger datain too small \n");		
+	}
+	serial.printf("ASCIIChangeInteger databuffer1 : %d \n", databuffer1);
+	return databuffer1;
+}
+
+int Conversionfunction(int Tensdigit, int Digits, int TheFirstDecimalPlace, int TheSecondDecimalPlace)
+{
+	int databuffer;
+	serial.printf("dataTensdigit: %d\n",Tensdigit);
+	serial.printf("Digits: %d\n",Digits);	
+	databuffer = ASCIIChangeInteger(Tensdigit)*10 + ASCIIChangeInteger(Digits);
+	serial.printf("datanumber: %d\n",databuffer);
+	return databuffer;
+}
 int main()
 {
 	myled =0;
@@ -189,24 +211,22 @@
 	printf("ip: %s\n",ipstack.getWiFi().get_ip_address() );
 
 	MQTT::Client<MQTTWiFi, Countdown, MQTT_MAX_PACKET_SIZE> client(ipstack);
-	attemptConnect(&client, &ipstack);
-	   
+	attemptConnect(&client, &ipstack); 
+	sensor.format(8,Serial::None,1);
    int count = 0;  
-   int databuffer=0;  
+   int databuffer3 = 0;  
 //    tyeld.start();    
     while (true){
-        if(!ds1820.begin())
-            return -1;
-        else
-            ds1820.startConversion();
-    
+    	BME280.receive();
+    	serial.printf("data 1 : %d\n",BME280.datareadfuiction(0));
+        serial.printf("data 2 : %d\n",BME280.datareadfuiction(1));
+        serial.printf("data 3 : %d\n",BME280.datareadfuiction(2));
+        serial.printf("data 4 : %d\n",BME280.datareadfuiction(3));
+        serial.printf("data 5 : %d\n",BME280.datareadfuiction(4));
+		databuffer3 = Conversionfunction(BME280.datareadfuiction(3),BME280.datareadfuiction(4),BME280.datareadfuiction(0),BME280.datareadfuiction(1));
     	if (++count == 3)
     	{               // Publish a message every second
-            serial.printf("temp = %3.1f\r\n", ds1820.read());     // read temperature
-            databuffer = ds1820.read();
-            ds1820.startConversion();     // start temperature conversion
-            wait(1.0);                    // let DS1820 complete the temperature conversion
-    		if (publish(&client, &ipstack,databuffer) != 0) 
+    		if (publish(&client, &ipstack,databuffer3) != 0) 
     		{ 
 	     	   	attemptConnect(&client, &ipstack);   // if we have lost the connection                
 	     	}
@@ -220,4 +240,4 @@
     	//client.yield(10);  // allow the MQTT client to receive messages
 //  	printf ("tyeld: %d\n\r",tyeld.read_ms()-start);
     	}
-}
\ No newline at end of file
+}