This repository has been archived by the owner on Dec 29, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCommunicationThread.c
154 lines (128 loc) · 4.4 KB
/
CommunicationThread.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include "cmsis_os.h" // CMSIS RTOS header file
#include "rl_net.h" // Keil.MDK-Pro::Network:CORE
#include <stdio.h>
#include <stdlib.h>
#ifdef RTE_Network_Interface_PPP
/*----------------------------------------------------------------------------
* Thread 1 'CommunicationThread': Thread that handles communications
*---------------------------------------------------------------------------*/
static void CommunicationThread (void const *argument); // thread function
osThreadId tid_CommunicationThread; // thread id
osThreadDef (CommunicationThread, osPriorityNormal, 1, 768); // thread object
int Init_CommunicationThread (void) {
tid_CommunicationThread = osThreadCreate (osThread(CommunicationThread), NULL);
if (!tid_CommunicationThread) return(-1);
return(0);
}
#define CONNECTION_WAITS 10
#define SERVER_NAME "maxwellweru.azurewebsites.net"
static int SendReport(void);
static void CommunicationThread (void const *argument) {
osEvent evt;
netStatus stat;
uint8_t connection_waits;
HOSTENT* hostentry;
int err, j;
IN_ADDR *addr;
while (1) {
// Wait forever until there is a signal sent to this thread
evt = osSignalWait(0, osWaitForever);
// Ensure that the result received is from a signal
if (evt.status != osEventSignal)
continue;
stat = netPPP_Connect("*99#", "", "");
if (stat != netOK) {
printf("PPP connection failed. ret=%d\r\n", stat);
continue;
}
// Wait for the PP link to be established
connection_waits = CONNECTION_WAITS;
while (connection_waits--) {
if (netPPP_LinkUp())
break;
osDelay(800);
}
// Do not proceed if the PPP link did not work
if (!netPPP_LinkUp()) {
printf("Could not estalish a PPP connection after %dms\r\n", CONNECTION_WAITS*800);
continue;
}
// At this point we have a connection now we can do somthing
// Let us resolve the IP address of the blog
hostentry = gethostbyname(SERVER_NAME, &err);
if (hostentry == NULL) {
if (err == BSD_ERROR_NONAME)
printf("The host name \'%s\' does not exist\r\n", SERVER_NAME);
else
printf("The host name \'%s\' could not be resolved. ret=%d", SERVER_NAME, err);
} else {
printf("Hostname \'%s\' resolved to:\r\n", SERVER_NAME);
for (j = 0; hostentry->h_addr_list[j]; j++) {
addr = (IN_ADDR *)hostentry->h_addr_list[j];
printf("IPv4: %d.%d.%d.%d\r\n", addr->s_b1, addr->s_b2, addr->s_b3, addr->s_b4);
}
free(hostentry);
}
// Test MQTT
SendReport();
// We must close the PP link after using it
printf("CLosing the PPP link\r\n");
stat = netPPP_Close();
if (stat != netOK)
printf("There was a problem closing the PPP link\r\n");
// Wait for the PP link to be established
connection_waits = CONNECTION_WAITS;
while (connection_waits--) {
if (!netPPP_LinkUp())
break;
osDelay(400);
}
if (netPPP_LinkUp())
printf("Could not close the PPP connection after %dms\r\n", CONNECTION_WAITS*400);
else
printf("PPP link was closed\r\n");
}
}
#include <string.h>
#include "mqtt/MQTTPacket.h"
#include "mqtt_keilds_transport.h"
char *host = "m2m.eclipse.org";
int port = 1883;
char* username = "testuser";
char* password = "testpassword";
MQTTPacket_connectData data = MQTTPacket_connectData_initializer;
int rc = 0;
unsigned char buf[200];
int buflen = sizeof(buf);
int mysock = 0;
MQTTString topicString = MQTTString_initializer;
char* payload = "{\"temp1\":22.3546,\"temp2\":54.1287,\"weight\":4578.125}";
int payloadlen;
int len = 0;
char* topic = "devices/64F7295EA8C/telemetry";
static int SendReport(void) {
payloadlen = strlen(payload);
mysock = transport_open(host,port);
if(mysock < 0)
return mysock;
printf("Sending to hostname %s port %d\n", host, port);
data.clientID.cstring = "th2gv1";
data.keepAliveInterval = 20;
data.cleansession = 1;
data.username.cstring = username;
data.password.cstring = password;
data.MQTTVersion = 4;
len = MQTTSerialize_connect((unsigned char *)buf, buflen, &data);
topicString.cstring = topic;
len += MQTTSerialize_publish((unsigned char *)(buf + len), buflen - len, 0, 0, 0, 0, topicString, (unsigned char *)payload, payloadlen);
len += MQTTSerialize_disconnect((unsigned char *)(buf + len), buflen - len);
rc = transport_sendPacketBuffer(mysock, buf, len);
if (rc == len)
printf("Successfully published\n");
else
printf("Publish failed\n");
//exit:
transport_close(mysock);
return 0;
}
#endif