watersensor and DS1280 temp sensor
Dependencies: watersenor_and_temp_code MQTT NetworkSocketAPI X_NUCLEO_IDW01M1v2 mbed
Fork of DS1820_IDW01M1 by
Revision 5:06af79bf75a5, committed 2017-11-05
- Comitter:
- e58136782000
- Date:
- Sun Nov 05 06:50:14 2017 +0000
- Parent:
- 4:3642f63079ec
- Commit message:
- watersensor and temp sensor finish
Changed in this revision
| main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Sat Nov 04 00:14:23 2017 +0800
+++ b/main.cpp Sun Nov 05 06:50:14 2017 +0000
@@ -10,17 +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.111";
#define MQTT_PORT 1883
//MQTT use Topic
-#define TOPIC1 "1"
-#define TOPIC2 "3"
+#define TOPIC2 "1"
+#define TOPIC1 "3"
#define SUB_TOPIC "LED"
//Wifi network
-#define SSID "tnta"
-#define PASSW "tnta2355818"
+#define SSID "18-2F"
+#define PASSW "062432779"
DigitalOut myled(LED1);
//BME280 bmpSensor;
@@ -39,7 +39,7 @@
Serial serial(USBTX, USBRX);
DS1820 ds1820(PA_8); // substitute PA_9 with actual mbed pin name connected to the DS1820 data pin
-
+DigitalIn sensor_in(D4);
MQTT::Message message;
@@ -150,13 +150,13 @@
wait(timeout);
}
}
-int publish (MQTT::Client<MQTTWiFi, Countdown, MQTT_MAX_PACKET_SIZE>* client,MQTTWiFi* ipstack,int data){
+int publish1(MQTT::Client<MQTTWiFi, Countdown, MQTT_MAX_PACKET_SIZE>* client,MQTTWiFi* ipstack,int data){
MQTT::Message message;
char *pubTopic = TOPIC1;
char buf[MQTT_MAX_PAYLOAD_SIZE];
- printf("Temp = %d\n", data);
+// printf("Temp = %d\n", data);
sprintf(buf,"%d",data);
message.qos = MQTT::QOS0;
message.retained = false;
@@ -164,10 +164,31 @@
message.payload = (void*)buf;
message.payloadlen = strlen(buf);
- printf("Publishing %s\n\r", buf);
+ printf("Temp = %s\n\r", buf);
return client->publish(pubTopic, message);
}
-
+int publish2(MQTT::Client<MQTTWiFi, Countdown, MQTT_MAX_PACKET_SIZE>* client,MQTTWiFi* ipstack,int data){
+
+
+ MQTT::Message message;
+ char *pubTopic = TOPIC1;
+ char buf[MQTT_MAX_PAYLOAD_SIZE];
+ if(data)
+ {
+ printf("LED:OFF \n");
+ }
+ else
+ {
+ printf("LED:ON \n");
+ }
+ sprintf(buf,"%d",data);
+ message.qos = MQTT::QOS0;
+ message.retained = false;
+ message.dup = false;
+ message.payload = (void*)buf;
+ message.payloadlen = strlen(buf);
+ return client->publish(pubTopic, message);
+}
int main()
{
myled =0;
@@ -192,21 +213,38 @@
attemptConnect(&client, &ipstack);
int count = 0;
- int databuffer=0;
+ int databuffer1;
+ int databuffer2;
// tyeld.start();
while (true){
if(!ds1820.begin())
+ {
return -1;
+ }
else
+ {
ds1820.startConversion();
-
- if (++count == 3)
+ }
+
+ if(sensor_in)
+ {
+ databuffer2 = 0;
+ }
+ else
+ {
+ databuffer2 = 1;
+ }
+ if (++count == 20)
{ // Publish a message every second
- serial.printf("temp = %3.1f\r\n", ds1820.read()); // read temperature
- databuffer = ds1820.read();
+// serial.printf("temp = %3.1f\r\n", ds1820.read()); // read temperature
+ databuffer1 = ds1820.read();
ds1820.startConversion(); // start temperature conversion
wait(1.0); // let DS1820 complete the temperature conversion
- if (publish(&client, &ipstack,databuffer) != 0)
+ if (publish1(&client, &ipstack,databuffer1) != 0)
+ {
+ attemptConnect(&client, &ipstack); // if we have lost the connection
+ }
+ if (publish2(&client, &ipstack,databuffer2) != 0)
{
attemptConnect(&client, &ipstack); // if we have lost the connection
}
@@ -214,7 +252,7 @@
}
else
{
- serial.printf("count: %d \n",count);
+// serial.printf("count: %d \n",count);
}
// int start = tyeld.read_ms();
//client.yield(10); // allow the MQTT client to receive messages
