How to add custom interface function to Arduino library - c++

I want to add event method to my arduino library something similar to cayenne or blynk framework.I have no C or C++ programming background.
CAYENNE_IN_DEFAULT() {
//to-do
}
Here is Fusi.h
#ifndef Fusi_h
#define Fusi_h
#define DEBUG true
#define MQTT_SERVER "192.168.31.10"
#define IOT_ENDPOINT "http://192.168.31.10:8069/iot"
class Fusi
{
public:
Fusi(String clientID,String clientSecret);
void setup();
void loop();
int digitalPinRead(int channel);
void addDigitalSwitch(String name, int channel);
private:
String clientID;
String clientSecret;
void digitalPinWrite(int channel, int value);
void virtualSwitchWrite(String name, int channel, int value);
};
#endif
Here is Fusi.cpp
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <EEPROM.h>
#include <ESP8266WebServer.h>
#include <ESP8266HTTPClient.h>
#include <PubSubClient.h>
#include "fusi.h"
#include <ArduinoJson.h>
HTTPClient http;
WiFiClient wifiClient;
ESP8266WebServer server(80);
PubSubClient mqtt(MQTT_SERVER, 1883, wifiClient);
Fusion::Fusion(String deviceID,String deviceSecret){
clientID = deviceID;
clientSecret = deviceSecret;
}
void Fusion::setup(){
//to-do
}
void Fusion::addDigitalSwitch(String name, int channel){
//to-do
}
int Fusion::digitalPinRead(int channel){
return 0;
}
void Fusion::loop(){
mqtt.loop();
//FUSI_IN_CHANNEL(5);
}
Now I want to notify on main sketch if some certain event occurred.
#include <Fusi.h>
Fusi fusi("4151b719-09bd-448e-9fc4-873807a66ba2","LSXJPBLW");
void setup() {
fusi.setup();
fusi.addDigitalSwitch("RELAY_0",D0);
fusi.addDigitalSwitch("RELAY_1",D1);
fusi.addDigitalSwitch("RELAY_2",D2);
fusi.addDigitalSwitch("RELAY_3",D3);
fusi.addDigitalSwitch("ONBOARD_LED",D4);
pinMode(D0,OUTPUT);
pinMode(D1,OUTPUT);
pinMode(D2,OUTPUT);
pinMode(D3,OUTPUT);
pinMode(D4,OUTPUT);
digitalWrite(D0,fusi.digitalPinRead(D0));
digitalWrite(D1,fusi.digitalPinRead(D1));
digitalWrite(D2,fusi.digitalPinRead(D2));
digitalWrite(D3,fusi.digitalPinRead(D3));
digitalWrite(D4,fusi.digitalPinRead(D4));
}
void loop() {
fusi.loop();
delay(200);
}
//This should notify from library
void FUSI_IN_CHANNEL(int channel){
//do update pins
}
}

Related

Why the Arduino Servo can't be reset in the middle after loop stage?

(excuse my poor english as a chinese high school student)
I don't really know that what's the reason of this problem.
When I set an angle in the "setup" section using "Servo.h" in Arduino IDE, I find that it will go back to last angle. However, I didn't set anything for this.
The following is my programme. Can you help me?
The following is main.ino :
#include <Arduino.h>
#include "HC_SR04.h"
#include "DCMotor.h"
#include "MyServo.h"
#include "Husky.h"
int servoPin = 2;
MyServo servo;
int left_ang = 60, right_ang = 175, def_ang = 120;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
// disleft.init(disPin[0], disPin[1]);
// disright.init(disPin[2], disPin[3]);
// mot.init(motPin);
servo.init(servoPin);
Serial.println("Init successfully! ");
// cam.init(camPin);
// prepare();
}
void loop() {
// put your main code here, to run repeatedly:
servo.set_ang(def_ang); // only this line matters that the servo changed back into angle:175 after a while, but I didn't set this at all.
/*mot.on();
while (disleft.read_dis() < 150 || disright.read_dis() < 150) {
warn();
if (disleft.read_dis() < 150) servo.set_ang(right_ang);
else if (disright.read_dis() < 150) servo.set_ang(left_ang);
}
if (cam.fd_tra()) {
if (cam.tra_posx() < 150) servo.set_ang(left_ang);
else if (cam.tra_posx() > 170) servo.set_ang(right_ang);
else servo.set_ang(def_ang);
} else {
servo.set_ang(def_ang);
}*/
}
This is my MyServo.h :
#ifndef _MyServo_H_
#define _MyServo_H_
#include <Arduino.h>
#include "Servo.h"
class MyServo {
public:
void reset();
void init(int pin);
void set_ang(int ang);
private:
Servo mys;
};
#endif
This is my MyServo.cpp :
#include <Arduino.h>
#include "MyServo.h"
void MyServo::reset() {
mys.write(120);
}
void MyServo::init(int pin) {
pinMode(pin, OUTPUT);
mys.attach(pin, 500, 2500);
mys.write(60);
delay(1000);
mys.write(175);
delay(1000);
mys.write(120);
}
void MyServo::set_ang(int ang) {
mys.write(ang);
}
However, this won't happen when I customize it in seperate program.
test.ino :
#include <Servo.h>
Servo mys;
int ang;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(2, OUTPUT);
mys.attach(2);
Serial.println("Initialize successfully! ");
}
void loop() {
// put your main code here, to run repeatedly:
mys.write(60);
delay(1000);
// mys.write(120);
// delay(00);
mys.write(175);
delay(1000);
mys.write(120);
delay(1000);
exit(0);
}

QT Multithread application crashing when I try to start threads

I am having issues when I try to create an application that simulates a restaurant. It needs to have 50 clients, 3 cooks, 1 waiter and 30 tables. When I try to start the threads of the clients, cooks and waiter it crashes and doesnt let me do anything, nor it gives me an error message or warning. I really don't know what to do. I hope you guys can help me with the answer.
Cook class.h:
`
#ifndef COOK_H
#define COOK_H
#include <QObject>
#include <QThread>
#include <QSemaphore>
class Cook : public QThread
{
Q_OBJECT
public:
Cook(int id, QString name);
void makeFood(int queueTotal);
void startCook();
private:
int id;
QString name;
int orderId;
QSemaphore semaphore;
protected:
void run(); //makeFood
};
#endif // COOK_H
cook.cpp
#include "cook.h"
#include <QTextStream>
Cook::Cook(int id, QString name)
{
this->id = id;
this->name = name;
}
void Cook::startCook(){
run();
}
void Cook::run(){
QTextStream(stdout)<< "hello im a cook";
}
`
client.h
`
#ifndef CLIENT_H
#define CLIENT_H
#include <QObject>
#include <QThread>
#include <QSemaphore>
class Order;
class Client : public QThread
{
Q_OBJECT
public:
Client(int id, QString name);
void setOrder(Order* order);
int getId();
void startClient();
private:
int id;
QString name;
QSemaphore semaphore;
Order* order;
protected:
void run();
};
#endif // CLIENT_H
`
client.cpp
`
#include "client.h"
#include <QTextStream>
Client::Client(int id, QString name)
{
this->id = id;
this->name = name;
}
void Client::startClient(){
run();
}
void Client::run(){
QTextStream(stdout)<< "hello im a client";
}
void Client::setOrder(Order* order){
this->order = order;
}
int Client::getId(){
return this->id;
}
`
order.cpp and .h
`
#ifndef ORDER_H
#define ORDER_H
class Order
{
public:
Order(int orderId, bool orderType);
private:
int orderId;
bool orderType; //true carne - false vegano
};
#endif // ORDER_H
`
order.cpp
`
#include "order.h"
Order::Order(int orderId, bool orderType)
{
this->orderId = orderId;
this->orderType = orderType;
}
table.h and cpp
#ifndef TABLE_H
#define TABLE_H
#include <QObject>
class Client;
class Table
{
public:
Table(int id, QString name, bool seat);
void clearClient();
void setClient(Client newClient);
private:
int id;
QString name;
bool seat;
int clientId;
};
#endif // TABLE_H
table.cpp
#include "table.h"
#include "client.h"
Table::Table(int id, QString name, bool seat)
{
this->seat = seat;
this->clientId = 0;
this->id = id;
this->name = name;
}
void Table::clearClient(){
this->seat = false;
this->clientId = -1;
}
void Table::setClient(Client newClient){
this->clientId = newClient.getId();
this->seat = true;
}
`
waiter.h
`
#ifndef WAITER_H
#define WAITER_H
#include <QObject>
#include <QThread>
#include <QSemaphore>
class Waiter : public QThread
{
Q_OBJECT
public:
Waiter(int id, QString name);
void startWaiter();
private:
int id;
QString name;
int orderId;
int tableId;
QSemaphore semaphore;
protected:
void run();
};
#endif // WAITER_H
`
waiter.cpp
`
#include "waiter.h"
#include <QTextStream>
Waiter::Waiter(int id, QString name)
{
this->id = id;
this->name = name;
}
void Waiter::startWaiter(){
run();
}
void Waiter::run(){
QTextStream(stdout)<< "hello im a waiter";
}
`
restaurant.h
`
#ifndef RESTAURANT_H
#define RESTAURANT_H
class Restaurant
{
public:
Restaurant();
void startRestaurant(int totalChefs, int totalTables, int totalWaiters, int totalClients);
};
#endif // RESTAURANT_H
`
restaurant.cpp
`
#include "restaurant.h"
#include <QThread>
#include "cook.h"
#include "client.h"
#include "waiter.h"
#include "table.h"
Restaurant::Restaurant()
{
}
void Restaurant::startRestaurant(int totalChefs, int totalTables, int totalWaiters, int totalClients){
QList <Cook*> cookList;
QList <Waiter*> waiterList;
QList <Client*> clientList;
QList <Table*> tables;
//start
for(int i=0; i<totalChefs; i++){
QString output = QStringLiteral("Cook %1").arg(i);
Cook cook(i, output);
cookList.append(&cook);
}
for(int i=0; i<totalWaiters; i++){
QString output = QStringLiteral("Waiter %1").arg(i);
Waiter waiter(i, output);
waiterList.append(&waiter);
}
for(int i=0; i<totalClients; i++){
QString output = QStringLiteral("Client %1").arg(i);
Client client(i, output);
clientList.append(&client);
}
for(int i=0; i<totalTables; i++){
QString output = QStringLiteral("Table %1").arg(i);
Table table(i, output, false);
tables.append(&table);
}
cookList.value(1)->startCook();
waiterList.value(1)->startWaiter();
clientList.value(1)->startClient();
}
`
I am new to QT so please bear that in mind, I hope you guys can help me!

c2653 no class or namespace .hpp/.cpp

Hey I'm a C++ beginner and trying to learn the basics. I tried to compile my code but it keeps telling me
error. (c2653) "Framework": no class or namespace in line 4.
Another error is
"sf":no class or namespace
but if I add #include <SFML\Graphics.hpp> to my Framework.cpp code it solve the problem. As I understand the .hpp headers include all the code to the cpp files. So why should I include the SFML\Graphics.hpp twice?
Here is my Code:
#ifndef FRAMEWORKK_H
#define FRAMEWORKK_H
#include <iostream>
#include <SFML\Graphics.hpp>
#include "stdafx.h"
class Framework
{
public:
Framework();
~Framework();
void run();
private:
void quit();
void update();
void handleEvent();
void render();
sf::RenderWindow * pRenderWindow;
sf::Event * pMainEvent;
bool mRun;
};#endif
and here is my .cpp code
#include "Frameworkk.h"
#include "stdafx.h"
Framework::Framework(){
pRenderWindow = new sf::RenderWindow(sf::VideoMode(800, 600, 32),
"Spiel");
pMainEvent = new sf::Event;
mRun = true;
}
Framework::~Framework()
{
}
void Framework::run()
{
while (mRun)
{
update();
handleEvent();
render();
quit();
}
}
void Framework::quit()
{
if (!mRun)
{
pRenderWindow->close();
}
}
void Framework::update()
{
}
void Framework::handleEvent()
{
while (pRenderWindow->pollEvent(*pMainEvent))
{
if (pMainEvent->type == sf::Event::Closed)
{
mRun = false;
}
}
}
void Framework::render()
{
pRenderWindow->clear(sf::Color(120, 120, 120));
pRenderWindow->display();
}

'drv' does not name a type

Been looking for awhile and I cant find an answer. All paths are included. I'm referencing a class in another namespace in the class I'm creating.
I'm getting the following error:
src/app/Application.h:30:9: error: 'drv' does not name a type
Code Below. Any help is appreciated!
Main.cpp
int main(int argc, char** argv) {
app::Application program;
program.run();
return 0;
}
LEDs.h
#ifndef LEDS_H
#define LEDS_H
namespace drv {
class LEDs {
public:
LEDs();
void InitLEDs(void);
void SetLEDs(const uint8_t value);
private:
static const uint8_t NUM_LEDs = 5;
};
}
#endif /* LEDS_H */
Application.h
#ifndef APPLICATION_H
#define APPLICATION_H
namespace app {
enum State {
NORMAL = 0,
ZONE,
PAIRING,
STUCK,
BATT,
OFF
};
class Application {
public:
Application();
void run(void);
void execute_loop(void);
private:
bool IDLE;
State STATE;
drv::LEDs Leds; // LINE 30
};
}
#endif // APPLICATION_H
Application.cpp
#include "stdint.h"
#include "stdbool.h"
#include "../drv/LEDs.h"
#include "Application.h"
namespace app {
Application::Application() {
IDLE = false;
STATE = NORMAL;
}
void Application::run(void) {
Leds.InitLEDs();
while(1)
{
if(IDLE) {
PowerSaveIdle();
}
else {
execute_loop();
}
}
}
void Application::execute_loop(void)
{
}
}
LEDs.cpp
#include <stdint.h>
#include "LEDs.h"
namespace drv {
LEDs::LEDs() {
}
void LEDs::InitLEDs() {
SetLEDs(0xff);
}
void LEDs::SetLEDs(const uint8_t value) {
//Removed for readability
}
}
You're missing an #include ... line in Application.h. At the top of that file (or just after the inclusion guards) add the line
#include "LEDs.h"

Qt client programme

I wrote a program for client on Qt to receive data from server but it is not receiving data and showing received bytes as zero,following is my program:
//client.h
#ifndef CLIENT_H
#define CLIENT_H
#include <QObject>
#include <QString>
#include <QtNetwork/QTcpSocket>
class Client: public QObject
{
Q_OBJECT
public:
Client(QObject* parent = 0);
~Client();
void start(QString address, quint16 port);
void send(const char*);
void receive();
public slots:
void startTransfer();
private:
QTcpSocket client;
};
#endif // CLIENT_H
//client.cpp
#include "client.h"
#include <QtNetwork/QHostAddress>
#include<QIODevice>
Client::Client(QObject* parent): QObject(parent)
{
connect(&client, SIGNAL(connected()),
this, SLOT(startTransfer()));
//connect(&client, SIGNAL(waitForBytesWritten()),
// this, SLOT(receive()));
}
Client::~Client()
{
client.close();
}
void Client::start(QString address, quint16 port)
{
QHostAddress addr(address);
client.connectToHost(addr, port);
}
void Client::startTransfer()
{
client.write("Connection Established", 22);
}
void Client::send(const char *buffer)
{
client.write(buffer,sizeof(buffer));
}
void Client::receive()
{
char temp[1024] = {0};
int len = client.read(temp,client.bytesAvailable());
printf("\tData recieved from server :: %s\n",temp);
printf("\tSize of data received is :: %d\n",client.bytesAvailable());
printf("\tBytes read is :: %d\n",len);
}
//main.cpp
#include <QCoreApplication>
#include "client.h"
//#include <QApplication>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Client client;
client.start("192.168.1.2", 9602);
char buff[] = "Send operation performed from main";
client.send(buff);
// while(1)
client.receive();
return a.exec();
}
Here my program function executes and then stops receiving(may be),when I send any thing from server it doesn't take anything.Any suggestions?
Plz don't be rude if I have done any silly programming mistake because I'm newbie.
You are not getting #Merlin069's answer....you should use readyRead in signal's place and your receieve function as slot...it will work.I hope this much easy language is understandable to you.
You can try using while(1) and inside it write your receive function