I'm trying to work with tasks on a ESP32;
loop(){
xTaskCreatePinnedToCore(SendCanBusMessageToCharger, "SendCanBusMessageToCharger", 1000, NULL, 3, NULL, 0);
}
void SendCanBusMessageToCharger(void *pvParameters) {
CAN_frame_t tx_frame;
tx_frame.FIR.B.FF = CAN_frame_ext;
tx_frame.MsgID = 0x1806E5F4;
tx_frame.FIR.B.DLC = 8;
tx_frame.data.u8[4] = 0x00;
tx_frame.data.u8[5] = 0x00;
tx_frame.data.u8[6] = 0x00;
tx_frame.data.u8[7] = 0x00;
while(true) {
tx_frame.data.u8[0] = highByte(outputvoltage);
tx_frame.data.u8[1] = lowByte(outputvoltage);
tx_frame.data.u8[2] = highByte(outputcurrent);
tx_frame.data.u8[3] = lowByte(outputcurrent);
ESP32Can.CANWriteFrame(&tx_frame);
Serial.println("CAN message sent to charger");
vTaskDelay(1000);
}
I expected to get one "CAN message sent to charger" message on the serial port each second, but I'm getting:
12:11:15.866 -> CAN message sent to charger
12:11:15.866 -> CAN message sent to charger
12:11:15.866 -> CAN message sent to charger
12:11:15.866 -> CAN message sent to charger
12:11:15.866 -> CAN message sent to charger
12:11:15.866 -> CAN message sent to charger
12:11:15.866 -> CAN message sent to charger
12:11:15.866 -> CAN message sent to charger
12:11:15.899 -> CAN message sent to charger
12:11:15.899 -> CAN message sent to charger
12:11:15.899 -> CAN message sent to charger
12:11:15.899 -> CAN message sent to charger
12:11:15.899 -> CAN message sent to charger
12:11:15.899 -> CAN message sent to charger
12:11:15.899 -> CAN message sent to charger
12:11:15.899 -> CAN message sent to charger
12:11:15.899 -> CAN message sent to charger
12:11:15.899 -> CAN message sent to charger
12:11:15.899 -> CAN message sent to charger
Around 8 messages each second. Also tried vTaskDelay(pdMS_TO_TICKS( 1000 )) but result is the same. I don't really see what I'm doing wrong here... Building with the latest Arduino IDE and ESP board definition. Thanks!
Yep well that was stupid.... I put the task creation in the loop instead of the setup and was staring at the code for so long I didn't see it anymore.
Your problem is, that the vTaskDelay waits for ticks, not ms, and you does not convert it properly.
Solution:
vTaskDelay(1000/portTICK_PERIOD_MS);
Related
I'm doing mini messenger. I have list of friends. And I want to when somebody send me some message, that person who send me message, go at the top of list of friends. How to do this? Below I'll show you me method which called when I send message.
func publishMessageAsync(message: String) {
let history = MessageHistory()
print("message is: \(message)")
history.message = message//text sms
history.user1 = Backendless.sharedInstance().userService.currentUser
history.user2 = self.friend.user2!
self.messageHistories.append(history)
self.chatTableView.reloadData()
MessageHistory is my class, which store history of messages))
Backendless.sharedInstance().messaging.publish(history.user2!.objectId, message: message,
response:{ ( messageStatus : MessageStatus!) -> () in
print("MessageStatus = \(messageStatus.status) ['\(messageStatus.messageId)']")
self.messageTextField.text = ""
self.messageTextField.resignFirstResponder()
// call function for saving
self.saveMessageHistory(message)
},
error: { ( fault : Fault!) -> () in
print("Server reported an error: \(fault)")
}
)
}
I'm trying to send a UDP packet with CocoaAsyncSocket. I've implemented the socket instance creation and the send method as follows:
let IP = "192.168.2.255"
let PORT:UInt16 = 6454
var socket:GCDAsyncUdpSocket!
func sendPacket(){
socket = GCDAsyncUdpSocket(delegate: self, delegateQueue: dispatch_get_main_queue())
let message = "test"
let data = message.dataUsingEncoding(NSUTF8StringEncoding)
socket.sendData(data, toHost: IP, port: PORT, withTimeout: 2, tag: 0)
}
I'm testing it with an iPad connected on my local Wi-Fi network. On a computer connected on the same network, I've installed Wireshark and I'm listening to the UDP packets received. No packet corresponding to mine is received.
Any idea why? :/
After successfully creating a group QBChatDialog, I try to sent invitation messages to all occupants by
if self.chatDialog.join() == true {
for occupantID in self.chatDialog.occupantIDs {
var inviteMessage = self.createChatNotificationForGroupChatCreation(self.chatDialog)
var timestamp = NSDate().timeIntervalSince1970
inviteMessage.customParameters["date_sent"] = timestamp
// send notification
//
inviteMessage.recipientID = occupantID as! UInt
//QBChat.instance().sendMessage(inviteMessage)
//QBChatDialog. sendMessage(inviteMessage)
if self.chatDialog.sendMessage(inviteMessage) == true {
println("################# Send ok ######################")
} else {
println("!!!!!!!!!!!!!!!! error sending !!!!!!!!!!!!!!!!!!")
}
}
}
but I always get the error. It keeps telling me that "Room is not joined". I did join the room using "self.chatDialog.join()" command. I even check that the process is ok before I send the message. Please help.
In my iOS App i want to send an message to the watch like this:
NSMutableDictionary *message = #{#(1): #(1),
#(2): #"The String"}
[_watch appMessagesPushUpdate:message onSent:^(PBWatch *watch, NSDictionary *update, NSError *error) {
if (error != nil)
NSLog(#"Error sending message: %#", error);
}];
If i am sending it like this it works. But if my string is longer or if i add more than 3 or 4 keys to the dictionary the message will not be delivered. Error is "the app did not acknowledge the message in time".
In my pebble App i do the following:
static void message_handler(DictionaryIterator *iter, void *context) {
APP_LOG(APP_LOG_LEVEL_DEBUG, "Received message.");
Tuple *msg_type_tuple = dict_find(iter, PebbleMessageKeyType);
Tuple *msg_value_tuple = dict_find(iter, PebbleMessageKeyValue);
write_line_on_screen(msg_value_tuple->value->cstring);
}
...
// Set sniff interval.
app_comm_set_sniff_interval(SNIFF_INTERVAL_NORMAL);
// Register message handlers
app_message_register_inbox_received(message_handler);
app_message_register_inbox_dropped(message_dropped);
// Init buffers
app_message_open(app_message_inbox_size_maximum(), app_message_outbox_size_maximum());
APP_LOG(APP_LOG_LEVEL_DEBUG, "App Message set up.");
My thought is that it has something to do with the message size. But i canĀ“t imagine that messages can only be so small?
In the ToDo list example i saw that they used app_message_open with values 64 for inbox and 16 for outbox parameter. What units are meant with this? 64 characters? How do i know on the iOS side how big my message will be when it arrives on the pebble?
First thing you should do when debugging this type of problem is adding a message_dropped handler and printing the failure reason:
void init() {
// ...
app_message_register_inbox_dropped(appmsg_in_dropped);
app_message_open(...);
// ...
}
static void appmsg_in_dropped(AppMessageResult reason, void *context) {
APP_LOG(APP_LOG_LEVEL_DEBUG, "In dropped: %i", reason);
}
You will find a list of reason code in the documentation.
The two most common problems are:
APP_MSG_BUFFER_OVERFLOW: The message is too big (see below)
APP_MSG_BUSY: You are sending messages too fast. You cannot send a new message until the previous one has been acknowledged.
The size of the message is equal to the size of the dictionary. The documentation of dict_calc_buffer_size() explains how to calculate it:
1 byte + 7 bytes for each key + the sum of the sizes of the values
Finally, the values passed to app_message_open() are the buffer sizes in bytes.
I am developing an iOS app that communicates with a WT11i by bluegiga via Bluetooth 4.0
The iOS is running as the central while WT11i is acting as the peripheral.
The characteristics of the service broadcast by WT11i is Write/Notify
After my app is connected to the peripheral (WT11i), the peripheral will continuously send the value of a counter to my app.
NSLog:
2013-09-18 14:22:58.843 IOS_Central[412:907] Receive -> 1
2013-09-18 14:22:58.904 IOS_Central[412:907] Receive -> 2
2013-09-18 14:22:58.963 IOS_Central[412:907] Receive -> 3
2013-09-18 14:22:59.023 IOS_Central[412:907] Receive -> 4
Code:
- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error
{
receivingThings =true;
if (error) {
NSLog(#"Error discovering characteristics: %#", [error localizedDescription]);
return;
}
NSString *newString = [[NSString alloc] initWithData:characteristic.value encoding:NSUTF8StringEncoding];
NSLog(#"Receive -> %#",newString);
}
I can send a short message from my app to my peripheral using
-(void) sendToPeripheral:(NSString*)message{
if(self.ConnectionState == CONNECTIONSTATE_NOTCONNECTED){
return;
}
NSData *mainData1= [message dataUsingEncoding:NSUTF8StringEncoding];
[self.connectedPeripheral writeValue:mainData1 forCharacteristic:self.connectedCharacteristic type:CBCharacteristicWriteWithResponse];
NSLog(#"Send Message");
}
Result of the writeValue: to Peripheral will be reflected here
- (void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error{
if(error == nil){
NSLog(#"Sent successfully");
}
else{
NSLog(#"Send Fail");
}
}
When my peripheral received the message, it will acknowledge by sending an acknowledgement "Message Received" back to my app.
Most of the time the communication is fine. However once in a while my app will miss an update right after I sent a message to the peripheral.
2013-09-18 14:22:58.843 IOS_Central[412:907] Receive -> 1
2013-09-18 14:22:58.904 IOS_Central[412:907] Receive -> 2
2013-09-18 14:22:58.963 IOS_Central[412:907] Receive -> 3
2013-09-18 14:22:59.023 IOS_Central[412:907] Receive -> 4
2013-09-18 14:22:59.050 IOS_Central[412:907] Send Message
2013-09-18 14:22:59.083 IOS_Central[412:907] Receive -> 5
2013-09-18 14:22:59.113 IOS_Central[412:907] Sent successfully
2013-09-18 14:22:59.143 IOS_Central[412:907] Receive -> 7
2013-09-18 14:22:59.203 IOS_Central[412:907] Receive -> 8
2013-09-18 14:22:59.263 IOS_Central[412:907] Receive -> Message Received
2013-09-18 14:22:59.322 IOS_Central[412:907] Receive -> 9
Notice I did not receive number "6" from my peripheral?
I have checked the logs on the peripheral, it says that it has sent out "6" to my app. My biggest suspicion lies in the iOS corebluetooth, where writeValue: clashes with didUpdateValueForCharacteristic:, therefore causing my "6" to be lost.
Has anyone encountered this problem before too? Any suggestion?
It can be the connection interval issue. You can send some amount data in single connection interval. Once received successful acknowledgment from Peripherals, send next some data. By doing this, there will be less chance of missing packets.
Please refer this:
Connection Interval Core Bluetooth