watersensor and tmp
Dependencies: DS1820 MQTT NetworkSocketAPI X_NUCLEO_IDW01M1v2 mbed
Fork of IDW01M1-MQTT by
Revision 2:d2fb91dc221e, committed 2017-11-01
- Comitter:
- e58136782000
- Date:
- Wed Nov 01 07:25:04 2017 +0000
- Parent:
- 1:2ee9e5685a1e
- Child:
- 3:0511bd22e20f
- Commit message:
- watersensor and tep
Changed in this revision
--- a/BME280/BME280.hpp Tue Oct 31 23:56:09 2017 +0800
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,74 +0,0 @@
-#include "mbed.h"
-
-#ifndef BME280_HPP
-#define BME280_HPP
-
-#define BME280_size 14
-
-Serial sensor(PA_11,PA_12);
-Serial pc(USBTX,USBRX);
-int AT[3] = {0xA5,0x52,0xF7};
-class BME280{
-public:
- void judgement_data(char buf[]);
- int Temp_read();
-
-private:
- void receive();
- void send_AT();
- char buf[BME280_size];
- int Lux,Temp,Preesure,Hum,Height;
-
-};
-void BME280::judgement_data(char buf[]){
- int i=0;
- if(buf[i++]== 0x5A){
- //pc.printf("first_step\n");
- if(buf[i++]== 0x5A){
- //pc.printf("second_step\n");
- //i++;//go to data type
- if(buf[i] == 0x15){
- //pc.printf("Lux type\n");
- i++;//04
- Lux = ((buf[i+1]<<24) | (buf[i+2]<<16) | (buf[i+3]<<8) | buf[i+4])/100;
- //Lux = (0x00<<24) | (0x00<<16) | (0xFE<<8) | 0x40;//test
- //pc.printf("Lux = %d\n", Lux);
- }else if(buf[i] == 0x45){
- //pc.printf("temp type\n");
- i++;//0A
- Temp = ((buf[i+1]<<8)|(buf[i+2]))/100;
- Preesure = ((buf[i+3]<<24)|(buf[i+4]<<16)|(buf[i+5]<<8)|(buf[i+6]))/100;
- Hum = ((buf[i+7]<<8)|(buf[i+8]))/100;
- Height = ((buf[i+9]<<8)|(buf[i+10]));
- //pc.printf("Temp = %d\n", Temp);
- //pc.printf("Preesure = %d\n", Preesure);
- //pc.printf("Hum = %d\n", Hum);
- //pc.printf("Height = %d\n", Height);
- }
- }
- }
-}
-void BME280::receive(){
- for(int i=0;i<BME280_size;i++){
- buf[i] = sensor.getc();
- //pc.putc(buf[i]);
- }
-
-}
-void BME280::send_AT(){
- for(int i=0;i<3;i++){
- sensor.putc(AT[i]);
- //pc.putc(AT[i]);
- }
-}
-int BME280::Temp_read(){
- send_AT();
- //wait(0.2);
- receive();
- //wait(0.2);
- judgement_data(this->buf);
-
- return Temp;
-}
-
-#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/DS1820.lib Wed Nov 01 07:25:04 2017 +0000 @@ -0,0 +1,1 @@ +http://developer.mbed.org/users/hudakz/code/DS1820/#8dfdd1603e4d
--- a/main.cpp Tue Oct 31 23:56:09 2017 +0800
+++ b/main.cpp Wed Nov 01 07:25:04 2017 +0000
@@ -1,5 +1,5 @@
#include "mbed.h"
-#include "BME280.hpp"
+#include "DS1820.h"
#include "SpwfInterface.h"
#include "TCPSocket.h"
#include "MQTTClient.h"
@@ -9,19 +9,22 @@
#define MQTT_MAX_PACKET_SIZE 250
#define MQTT_MAX_PAYLOAD_SIZE 300
//Configuration value needed to connect Red-node
-#define BROKER_URL "192.168.0.10";
+#define BROKER_URL "192.168.20.116";
#define MQTT_PORT 1883
//MQTT use Topic
-#define TOPIC "temp"
+#define TOPIC0 "1"
+#define TOPIC1 "3"
#define SUB_TOPIC "LED"
//Wifi network
-#define SSID "megu megu fire"
-#define PASSW "66666667"
+#define SSID "tnta"
+#define PASSW "tnta2355818"
+Serial serial(USBTX, USBRX);
DigitalOut myled(LED1);
-BME280 bmpSensor;
+DS1820 ds1820(PA_9);
+DigitalIn sensor_in(D4);
int connack_rc = 0; // MQTT connack return code
const char * ip_addr = "";
@@ -35,8 +38,10 @@
char subscription_url[MQTT_MAX_PAYLOAD_SIZE];
MQTT::Message message;
-MQTTString TopicName={TOPIC};
-MQTT::MessageData MsgData(TopicName, message);
+MQTTString TopicName1 = {TOPIC0};
+MQTTString TopicName2 = {TOPIC1};
+MQTT::MessageData MsgData1(TopicName1, message);
+MQTT::MessageData MsgData2(TopicName2, message);
void subscribe_LED(char* msg){
int value = atoi(msg);
@@ -76,7 +81,8 @@
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("Topic: %s\n\r", TOPIC);
+ LOG("Topic: %s\n\r", TOPIC0);
+ LOG("Topic: %s\n\r", TOPIC1);
//need subscrie
LOG("=====================================\n\r");
netConnecting = true;
@@ -139,17 +145,31 @@
wait(timeout);
}
}
-int publish (MQTT::Client<MQTTWiFi, Countdown, MQTT_MAX_PACKET_SIZE>* client,MQTTWiFi* ipstack){
+int publish0 (MQTT::Client<MQTTWiFi, Countdown, MQTT_MAX_PACKET_SIZE>* client,MQTTWiFi* ipstack){
MQTT::Message message;
- int data = bmpSensor.Temp_read();
- char *pubTopic = TOPIC;
+ char *pubTopic = TOPIC0;
+ char buf[MQTT_MAX_PAYLOAD_SIZE];
+ serial.printf("Temp = %d\n",ds1820.read());
+ serial.printf(buf,"%d",ds1820.read());
+ message.qos = MQTT::QOS0;
+ message.retained = false;
+ message.dup = false;
+ message.payload = (void*)buf;
+ message.payloadlen = strlen(buf);
+ printf("Publishing %s\n\r", buf);
+ return client->publish(pubTopic, message);
+}
+
+int publish1 (MQTT::Client<MQTTWiFi, Countdown, MQTT_MAX_PACKET_SIZE>* client,MQTTWiFi* ipstack)
+{
+ MQTT::Message message;
+ char *pubTopic = TOPIC1;
char buf[MQTT_MAX_PAYLOAD_SIZE];
-
- printf("Temp = %d\n", data);
- sprintf(buf,"%d",data);
+ serial.printf("Watersensor = %d\n", sensor_in);
+ serial.printf(buf,"%d",sensor_in);
message.qos = MQTT::QOS0;
message.retained = false;
message.dup = false;
@@ -169,11 +189,12 @@
//use SpwfSAInterface connect AP
SpwfSAInterface spwf(D8,D2, false);
- printf("\r\nX-NUCLEO-IDW01M1 mbed \n");
- printf("\r\nconnecting to AP\n");
+ serial.printf("\r\nX-NUCLEO-IDW01M1 mbed \n");
+ serial.printf("\r\nconnecting to AP\n");
//connect to Wifi
MQTTWiFi ipstack(spwf, ssid, seckey, NSAPI_SECURITY_WPA2);
//check wifi has got ip_address
+ serial.printf(ipstack.getWiFi().get_ip_address());
if(ipstack.getWiFi().get_ip_address() == 0){
printf("Connect WiFi is failed!\nPlease check your ssid and passwd is correct");
return 0;
@@ -189,12 +210,17 @@
while (true)
{
if (++count == 100)
- { // Publish a message every second
- if (publish(&client, &ipstack) != 0) {
-
+ { // Publish a message every second
+ if (publish0(&client, &ipstack) != 0)
+ {
+ attemptConnect(&client, &ipstack); // if we have lost the connection
+ }
+ if (publish1(&client, &ipstack) != 0)
+ {
attemptConnect(&client, &ipstack); // if we have lost the connection
}
count = 0;
+ wait(5);
}
// int start = tyeld.read_ms();
client.yield(10); // allow the MQTT client to receive messages
@@ -202,5 +228,4 @@
}
- return 0;
}
\ No newline at end of file
