use DS1820 update temp to Node-red

Dependencies:   watersenor_and_temp_code MQTT NetworkSocketAPI X_NUCLEO_IDW01M1v2 mbed

Files at this revision

API Documentation at this revision

Comitter:
rozendhyan@gmail.com
Date:
Sat Nov 04 00:14:23 2017 +0800
Parent:
3:30d9b8d9adc9
Commit message:
edit DS1820

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed_settings.pyc Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Fri Nov 03 01:57:41 2017 +0000
+++ b/main.cpp	Sat Nov 04 00:14:23 2017 +0800
@@ -10,7 +10,7 @@
 #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.149";
+#define BROKER_URL "192.168.20.116";
 #define MQTT_PORT 1883
 //MQTT use Topic 
 #define TOPIC1 "1"
@@ -23,7 +23,7 @@
 #define PASSW "tnta2355818"
 
 DigitalOut myled(LED1);
-BME280 bmpSensor;
+//BME280 bmpSensor;
 int connack_rc = 0;    // MQTT connack return code
 const char * ip_addr = "";
 char *host_addr = "";
@@ -36,8 +36,10 @@
 char subscription_url[MQTT_MAX_PAYLOAD_SIZE];
 
 
-DS1820  ds1820(PA_9);
+
 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;
@@ -166,10 +168,10 @@
     return client->publish(pubTopic, message);
 }
 
-int main(int argc, char const *argv[])
+int main()
 {
 	myled =0;
-	/* code */
+    
 	
 	const char *ssid = SSID;
 	const char *seckey = PASSW;
@@ -188,31 +190,22 @@
 
 	MQTT::Client<MQTTWiFi, Countdown, MQTT_MAX_PACKET_SIZE> client(ipstack);
 	attemptConnect(&client, &ipstack);
-	ds1820.begin();	   
-   int count = 0;    
+	   
+   int count = 0;  
+   int databuffer=0;  
 //    tyeld.start();    
-    while (true)
-    {
-    	int databuffer;
-
-    	//if(ds1820.begin()) 
-    	//{
-    	    //ds1820.startConversion();   // start temperature conversion
-    	    //client.yield(1);           // let DS1820 complete the temperature conversion
-    	    //serial.printf("temp test = %d \r\n", ds1820.read());     // read temperature
-    	    //wait(1);
-    	    //databuffer = ds1820.read()*10;
-    	    //ds1820.startConversion();     // start temperature conversion
-			//client.yield(1);                      // let DS1820 complete the temperature conversion
-    	//}
-    	//else
-    	//{
-		//	serial.printf("No DS1820 sensor found!\r\n");
-		//	databuffer = 0;
-    	//} 
-
+    while (true){
+        if(!ds1820.begin())
+            return -1;
+        else
+            ds1820.startConversion();
+    
     	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) 
     		{ 
 	     	   	attemptConnect(&client, &ipstack);   // if we have lost the connection                
@@ -222,9 +215,9 @@
     	else
     	{
     		serial.printf("count: %d \n",count);
-    	}        
+    	}      
 //  	int start = tyeld.read_ms();
-    	client.yield(10);  // allow the MQTT client to receive messages
+    	//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
Binary file mbed_settings.pyc has changed