mqtt esp8266
Dependencies: X_NUCLEO_IKS01A3
PMK2021_MQTT_ESP8266_IKS01A3
Revision 4:c737f3d85a19, committed 2021-04-27
- Comitter:
- nenad
- Date:
- Tue Apr 27 17:32:05 2021 +0000
- Parent:
- 3:43527b1910c7
- Commit message:
- mqtt esp8266 test
Changed in this revision
| X_NUCLEO_IKS01A3.lib | Show annotated file Show diff for this revision Revisions of this file |
| main.cpp | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/X_NUCLEO_IKS01A3.lib Tue Apr 27 17:32:05 2021 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/teams/ST/code/X_NUCLEO_IKS01A3/#aea924ef2645
--- a/main.cpp Mon Apr 26 21:46:12 2021 +0000
+++ b/main.cpp Tue Apr 27 17:32:05 2021 +0000
@@ -1,33 +1,75 @@
-/* mbed Microcontroller Library
- * Copyright (c) 2019 ARM Limited
- * SPDX-License-Identifier: Apache-2.0
- */
#include "mbed.h"
-//#include "EthernetInterface.h"
#include "platform/mbed_thread.h"
#include <MQTTClientMbedOs.h>
+#include "XNucleoIKS01A3.h"
+
// Blinking rate in milliseconds
#define BLINKING_RATE_MS 500
// Initialise the digital pin LED1 as an output
- DigitalOut led(LED1);
+DigitalOut led(LED1);
+/* Instantiate the expansion board */
+static XNucleoIKS01A3 *mems_expansion_board = XNucleoIKS01A3::instance(D14, D15, D4, D5, A3, D6, A4);
+
+/* Retrieve the composing elements of the expansion board */
+static STTS751Sensor *t_sensor = mems_expansion_board->t_sensor;
+
+
+volatile int mems_event = 0;
+uint32_t previous_tick = 0;
+uint32_t current_tick = 0;
+uint8_t high = 0, low = 0;
+float temperature = 0.0f;
+char buffer[32];
+
+void INT_cb();
+
+/* Helper function for printing floats & doubles */
+static char *print_double(char *str, double v, int decimalDigits = 2)
+{
+ int i = 1;
+ int intPart, fractPart;
+ int len;
+ char *ptr;
+
+ /* prepare decimal digits multiplicator */
+ for (; decimalDigits != 0; i *= 10, decimalDigits--);
+
+ /* calculate integer & fractinal parts */
+ intPart = (int)v;
+ fractPart = (int)((v - (double)(int)v) * i);
+
+ /* fill in integer part */
+ sprintf(str, "%i.", intPart);
+
+ /* prepare fill in of fractional part */
+ len = strlen(str);
+ ptr = &str[len];
+
+ /* fill in leading fractional zeros */
+ for (i /= 10; i > 1; i /= 10, ptr++) {
+ if (fractPart >= i) {
+ break;
+ }
+ *ptr = '0';
+ }
+
+ /* fill in (rest of) fractional part */
+ sprintf(ptr, "%i", fractPart);
+
+ return str;
+}
InterruptIn button(USER_BUTTON);
-
int arrivedcount = 0;
TCPSocket socket;
MQTTClient client(&socket);
MQTT::Message message;
-
int button_pressed=0;
-float version = 0.6;
- char* topic = "mbed-sample-pub";
- char* topic_sub = "mbed-sample-sub";
-
-
-
+char* topic_pub = "PMK-client-temperature";
+char* topic_sub = "PMK-client-topic_sub";
WiFiInterface *wifi;
@@ -53,34 +95,26 @@
int scan_demo(WiFiInterface *wifi)
{
WiFiAccessPoint *ap;
-
printf("Scan:\n");
-
int count = wifi->scan(NULL,0);
-
if (count <= 0) {
printf("scan() failed with return value: %d\n", count);
return 0;
}
-
/* Limit number of network arbitrary to 15 */
count = count < 15 ? count : 15;
-
ap = new WiFiAccessPoint[count];
count = wifi->scan(ap, count);
-
if (count <= 0) {
printf("scan() failed with return value: %d\n", count);
return 0;
}
-
for (int i = 0; i < count; i++) {
printf("Network: %s secured: %s BSSID: %hhX:%hhX:%hhX:%hhx:%hhx:%hhx RSSI: %hhd Ch: %hhd\n", ap[i].get_ssid(),
sec2str(ap[i].get_security()), ap[i].get_bssid()[0], ap[i].get_bssid()[1], ap[i].get_bssid()[2],
ap[i].get_bssid()[3], ap[i].get_bssid()[4], ap[i].get_bssid()[5], ap[i].get_rssi(), ap[i].get_channel());
}
printf("%d networks available.\n", count);
-
delete[] ap;
return count;
}
@@ -93,10 +127,8 @@
++arrivedcount;
}
-void buttonFunction() {
-
- button_pressed=1;
-
+void buttonFunction() {
+ button_pressed=1;
}
@@ -104,12 +136,25 @@
int main()
{
+ /* Attach callback to STTS751 INT */
+ t_sensor->attach_int_irq(&INT_cb);
+ /* Enable STTS751 temperature sensor */
+ t_sensor->enable();
+ /* Set ODR to 4Hz */
+ t_sensor->set_odr(4.0f);
+ /* Set Low Temperature Threshold */
+ t_sensor->set_low_temp_thr(22.0f);
+ /* Set High Temperature Threshold */
+ t_sensor->set_high_temp_thr(28.0f);
+ /* Enable Event pin */
+ t_sensor->set_event_pin(1);
+ /* Get beginning status */
+ t_sensor->get_temp_limit_status(NULL, NULL, NULL);
+
button.rise(&buttonFunction); // attach the address of the flip function to the rising edge
const char* hostname = "broker.mqttdashboard.com";
int port = 1883;
- // Network interface
- //EthernetInterface net;
wifi = WiFiInterface::get_default_instance();
if (!wifi) {
@@ -135,12 +180,8 @@
printf("IP: %s\n", wifi->get_ip_address());
printf("Netmask: %s\n", wifi->get_netmask());
printf("Gateway: %s\n", wifi->get_gateway());
- printf("RSSI: %d\n\n", wifi->get_rssi());
-
-
-
-
- //client = MQTTClient::(&socket);
+ printf("RSSI: %d\n\n", wifi->get_rssi());
+
socket.open(wifi);
socket.connect(hostname, port);
@@ -148,7 +189,7 @@
MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
data.MQTTVersion = 3;
- data.clientID.cstring = "mbed-sample";
+ data.clientID.cstring = "PMK-client";
//data.username.cstring = "testuser";
//data.password.cstring = "testpassword";
if ((rc = client.connect(data)) != 0)
@@ -157,34 +198,32 @@
if ((rc = client.subscribe(topic_sub, MQTT::QOS2, messageArrived)) != 0)
printf("rc from MQTT subscribe is %d\r\n", rc);
-
-
-
-
-
-
while (true) {
+ t_sensor->get_temperature(&temperature);
+ printf("Temp[C]: %7s C\r\n", print_double(buffer, temperature));
led = !led;
thread_sleep_for(BLINKING_RATE_MS);
if (button_pressed==1) {
button_pressed=0;
- printf("sace publish\r\n");
+ printf("Publishing data\r\n");
// QoS 0
char buf[100];
- sprintf(buf, "Hello World! QoS 0 message from app version %f\r\n", version);
+ sprintf(buf, "Temp[C]: %7s C\r\n", print_double(buffer, temperature));
message.qos = MQTT::QOS0;
message.retained = false;
message.dup = false;
message.payload = (void*)buf;
message.payloadlen = strlen(buf)+1;
- client.publish(topic, message);
- //pc.printf("sace jild\r\n");
- //client.yield(1000);
- //pc.printf("izjildovo\r\n");
+ client.publish(topic_pub, message);
}
- printf("sace jild\r\n");
+ printf("Yielding");
client.yield(1000);
- printf("izjildovo\r\n");
+ printf(" -> Yielded\r\n");
}
}
+
+void INT_cb()
+{
+ mems_event = 1;
+}
\ No newline at end of file