Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
- Committer:
- timilrlan
- Date:
- 2022-05-26
- Revision:
- 102:2591f689e242
- Parent:
- 101:bea6b4cfedac
File content as of revision 102:2591f689e242:
/* EXAMPLE */
#include "mbed.h"
#include "hcsr04.h"
//D12 TRIGGER D11 ECHO
HCSR04 sensor(D12, D11);
DigitalOut rele(D7);
if (distance <= 0) {
printf("scan() failed with return value: %d\n", count);
return 0;
}
#include "MQTTmbed.h"
#include <MQTTClientMbedOs.h>
#include <cstdio>
#include "mbed.h"
#include <LiquidCrystal_I2C.h>
// Set the LCD address to 0x27 for a 16 chars and 2 line display
//BufferedSerial pc(USBTX, USBRX);
//RawSerial pc(USBTX, USBRX);
// Serial pc2(USBTX, USBRX);
// RawSerial pc(USBTX, USBRX);
EventQueue eventQueue(/* event count */ 50 * EVENTS_EVENT_SIZE);
LowPowerTicker lpTicker;
LiquidCrystal_I2C lcd(0x4E, 16, 2);
void read_datchik_write_lcd_consol_publish_mqtt()
{
/* EXAMPLE */
long distance = sensor.distance();
printf("distance %d \n",distance);
// initialize the LCD
lcd.begin();
// initialize the LCD
lcd.begin();
// Turn on the blacklight and print a message.
lcd.backlight();
// lcd.printstr("distance %d \n",distance);
char displayText[1000];
sprintf(displayText,"rasstyanie: %d",distance);
lcd.print(displayText);
//lcd.printf("Hi mbed World!\n");
printf("Starting IBM MQTT demo:\n");
TCPSocket socket;
NetworkInterface *net = NetworkInterface::get_default_instance();
if (!net) {
printf("Error! No network inteface found.\n");
//return 0;
}
printf("Connecting to the network...\r\n");
nsapi_size_or_error_t rc = net->connect();
if (rc != 0) {
printf("Error! _net->connect() returned: %d\r\n", rc);
// return -1;
}
rc = socket.open(net);
if (rc != 0) {
printf("Error! _socket.open() returned: %d\r\n", rc);
//return -1;
}
SocketAddress address;
net->gethostbyname("dev.rightech.io",
&address);
address.set_port(1883);
printf("Opening connection to remote %s port %d\r\n",
address.get_ip_address(), address.get_port());
rc = socket.connect(address);
if (rc != 0) {
printf("Error! _socket.connect() returned: %d\r\n", rc);
// return -1;
}
printf("Connected socket\n");
MQTTClient client(&socket);
MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
data.MQTTVersion = 3;
data.clientID.cstring = "mqtt-timirlan_davletshin-6wtths";
data.username.cstring = "111";
data.password.cstring = "111";
if ((rc = client.connect(data)) != 0)
printf("rc from MQTT connect is %d\r\n", rc);
//{clientId:"vali_client_id",userName:"vali_user_name",password:"fkQiW3AJFcM38ED"}
MQTT::Message message;
char *topic = "distance";
char buf[100];
int temp = 33;
int press = 2;
int hum = 3;
// sprintf(buf,
// "{\"d\":{\"ST\":\"Nucleo-IoT-mbed\",\"Temp\":%d,\"Pressure\":"
// "%d,\"Humidity\":%d}}", temp, press, hum);
sprintf(buf,
"\%d", distance);
//sprintf(buf,
// "{\"distance\": %d}", distance);
printf("Sending message: \n%s\n", buf);
message.qos = MQTT::QOS0;
message.retained = false;
message.dup = false;
message.payload = (void *)buf;
message.payloadlen = strlen(buf);
rc = client.publish(topic, message);
printf("Message sent\n");
printf("Demo concluded successfully \r\n");
}
void tickerIRQ (void)
{
eventQueue.call(read_datchik_write_lcd_consol_publish_mqtt);
}
int main(int argc, char *argv[])
{
// pc.baud(115200);
lpTicker.attach(tickerIRQ, 5); // every 5 second
while(1)
{
eventQueue.dispatch(0);
sleep();
}
return 0;
}