Commit de748d9b authored by 957dd's avatar 957dd

apt code

parent 67c0da26
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#include <time.h>
#include "gps.h" #include "gps.h"
#include "delay.h" #include "delay.h"
......
#ifndef __GPS_H__ #ifndef __GPS_H__
#define __GPS_H__ #define __GPS_H__
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#include <time.h>
extern char glat[20], glon[20]; extern char glat[20], glon[20];
......
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <ifaddrs.h>
#include "ip.h" #include "ip.h"
struct ifaddrs *ifap, *ifa; struct ifaddrs *ifap, *ifa;
struct sockaddr_in *sa; struct sockaddr_in *sa;
char ip_address[INET_ADDRSTRLEN]; char ip_address[INET_ADDRSTRLEN];
int ipaddr() { int ipaddr() {
if (getifaddrs(&ifap) == -1) { if (getifaddrs(&ifap) == -1) {
perror("getifaddrs() error"); perror("getifaddrs() error");
return -1; return -1;
......
#ifndef __IP_H__ #ifndef __IP_H__
#define __IP_H__ #define __IP_H__
#include <stdio.h> extern char ip_address[];//INET_ADDRSTRLEN
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <ifaddrs.h>
int ipaddr(); int ipaddr();
extern char ip_address[INET_ADDRSTRLEN];
#endif #endif
No preview for this file type
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <fcntl.h>
#include <pthread.h>
#include <unistd.h>
#include <termios.h>
#include <mosquitto.h>
#include <cjson/cJSON.h>
#include <time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <ifaddrs.h>
#include "gps.h"
#include "main.h" #include "main.h"
#include "ip.h" #include "ip.h"
#include "mqtt.h" #include "mqtt.h"
#include "serial.h" #include "serial.h"
#include "delay.h" #include "delay.h"
#include "opensh.h" #include "opensh.h"
//#include "mac.h"
void *serial_usart1(void *arg) void *serial_usart1(void *arg)
...@@ -25,16 +40,11 @@ void *AppExit(void *arg)//2s程序,出现意外2s自动停止 ...@@ -25,16 +40,11 @@ void *AppExit(void *arg)//2s程序,出现意外2s自动停止
unsigned char bufpwm2[3]={gtypeTemp,2,0}; unsigned char bufpwm2[3]={gtypeTemp,2,0};
unsigned char bufpwm3[3]={gtypeTemp,3,0}; unsigned char bufpwm3[3]={gtypeTemp,3,0};
unsigned char bufpwm4[3]={gtypeTemp,4,0}; unsigned char bufpwm4[3]={gtypeTemp,4,0};
//unsigned char bufpwm5[3]={gtypeTemp,5,15};
Delay_Ms(0,100); Delay_Ms(0,100);
gPwmCount++; gPwmCount++;
//printf( "%d\n",gtypeTemp);
if(gPwmCount>20) if(gPwmCount>20)
{ {
//printf( "%d\n",gtypeTemp);
gPwmCount=21; gPwmCount=21;
//serial_Write(bufpwm5);
serial_Write(bufpwm1); serial_Write(bufpwm1);
serial_Write(bufpwm2); serial_Write(bufpwm2);
serial_Write(bufpwm3); serial_Write(bufpwm3);
...@@ -55,6 +65,7 @@ void *Mqttbeat(void *arg) ...@@ -55,6 +65,7 @@ void *Mqttbeat(void *arg)
refresh_cam(); refresh_cam();
Delay_Ms(20,0); Delay_Ms(20,0);
refresh_cam(); refresh_cam();
Delay_Ms(10,0);
while(1) while(1)
{ {
ipaddr();//获取ip ipaddr();//获取ip
...@@ -89,7 +100,7 @@ void *Mqtt_onnect(void *arg) ...@@ -89,7 +100,7 @@ void *Mqtt_onnect(void *arg)
Delay_Ms(5,0); Delay_Ms(5,0);
mqtt_init();//mqtt初始化 mqtt_init();//mqtt初始化
mqtt_create(mosq);//创建mqtt客户端 mqtt_create(mosq);//创建mqtt客户端
//mqtt_wirte();//mqtt心跳首次发送 mqtt_wirte();//mqtt心跳首次发送
while(1) while(1)
{ {
mqtt_create(mosq); mqtt_create(mosq);
...@@ -107,7 +118,7 @@ int main(int argc, char *argv[]) { ...@@ -107,7 +118,7 @@ int main(int argc, char *argv[]) {
TOPIC=argv[1]; TOPIC=argv[1];
TOPIC2=argv[2]; TOPIC2=argv[2];
TOPIC3=argv[3]; TOPIC3=argv[3];
printf("%s,%s,%s\n",TOPIC,TOPIC2,TOPIC3); printf("1:%s,2:%s,3:%s\n",TOPIC,TOPIC2,TOPIC3);
} }
system("pkill firefox"); system("pkill firefox");
...@@ -119,17 +130,10 @@ int main(int argc, char *argv[]) { ...@@ -119,17 +130,10 @@ int main(int argc, char *argv[]) {
serial_Init();//串口初始化 serial_Init();//串口初始化
// get_mac_address("eth0");//获取mac地址
ipaddr();//获取ip ipaddr();//获取ip
//mosquitto_publish(mosq, NULL, "macmqtt", strlen(TOPIC), TOPIC, 0, false);
//Delay_Ms(10,0);
//Delay_Ms(10,0);
thread_start(serial_usart1,AppExit,Mqttbeat,serial_usart2,opensh,Mqtt_onnect);
//mqtt_cycle(mosq);
thread_start(serial_usart1,AppExit,Mqttbeat,serial_usart2,opensh,Mqtt_onnect);
thread_end(); thread_end();
......
#ifndef __MAIN_H__ #ifndef __MAIN_H__
#define __MAIN_H__ #define __MAIN_H__
#include "ip.h"
#include "mqtt.h"
#include "serial.h"
#include "delay.h"
pthread_t thread[5]; pthread_t thread[5];
int thread_start(void *serial_usart1(void *arg),void *AppExit(void *arg), int thread_start(void *serial_usart1(void *arg),void *AppExit(void *arg),
void *Mqttbeat(void *arg),void *serial_usart2(void *arg),void *opensh(void *arg),void *Mqtt_onnect(void *arg)) void *Mqttbeat(void *arg),void *serial_usart2(void *arg),void *opensh(void *arg),void *Mqtt_onnect(void *arg))
{ {
if(pthread_create(&thread[0],NULL,serial_usart1,NULL)!=0) if(pthread_create(&thread[0],NULL,serial_usart1,NULL)!=0)
{ {
perror("Failed to create thread 1"); perror("Failed to create thread 1");
...@@ -52,15 +44,14 @@ void *Mqttbeat(void *arg),void *serial_usart2(void *arg),void *opensh(void *arg) ...@@ -52,15 +44,14 @@ void *Mqttbeat(void *arg),void *serial_usart2(void *arg),void *opensh(void *arg)
} }
} }
// 等待线程结束
void thread_end() void thread_end()
{ {
// 等待线程1结束
pthread_join(thread[0], NULL); pthread_join(thread[0], NULL);
// 等待线程2结束
pthread_join(thread[1], NULL); pthread_join(thread[1], NULL);
// 等待线程1结束
pthread_join(thread[2], NULL); pthread_join(thread[2], NULL);
pthread_join(thread[3], NULL); pthread_join(thread[3], NULL);
......
#include <mosquitto.h>
#include <cjson/cJSON.h>
#include <string.h>
#include<time.h>
#include <pthread.h>
#include "ip.h"
#include "serial.h"
#include "mqtt.h" #include "mqtt.h"
#include "gps.h" #include "gps.h"
#include "opensh.h" #include "opensh.h"
char* TOPIC =NULL;//="app2dev/controlcar0004"
char* TOPIC2 =NULL ;//="dev2app/controlcar0004"
char* TOPIC3 =NULL;//= "controlcar0004"
time_t gStart;
int gPwmCount = 0; // 计数 int gPwmCount = 0; // 计数
int gmessage_type=10086; int gmessage_type=10086;
int rc=0; int rc=0;
struct mosquitto *mosq;
unsigned char gmodeTemp=0; unsigned char gmodeTemp=0;
unsigned char gtypeTemp=0; unsigned char gtypeTemp=0;
...@@ -21,6 +20,12 @@ unsigned char gpinTemp=0; ...@@ -21,6 +20,12 @@ unsigned char gpinTemp=0;
unsigned char gvalTemp=0; unsigned char gvalTemp=0;
unsigned char gvalt[4]; unsigned char gvalt[4];
struct mosquitto *mosq;
time_t gStart;
char* TOPIC=NULL;
char* TOPIC2=NULL;
char* TOPIC3= NULL;
int mqtt_init() int mqtt_init()
{ {
// 初始化 mosquitto 库 // 初始化 mosquitto 库
...@@ -49,25 +54,11 @@ void on_connect(struct mosquitto *mosq, void *obj, int rc) //回调函数 ...@@ -49,25 +54,11 @@ void on_connect(struct mosquitto *mosq, void *obj, int rc) //回调函数
} }
} }
//int init_mqtt(struct mosquitto *mosq)
/*
void mqtt_MAC_wirte()
{
cJSON *root = cJSON_CreateObject();
cJSON *body = cJSON_CreateObject();
cJSON_AddStringToObject(body, "TOPIC", TOPIC);
cJSON_AddStringToObject(body, "device", "controlcar0001");
cJSON_AddStringToObject(body, "http", "https://jywy.yd-ss.com?dev=controlcar0001");//gsite
cJSON_AddItemToObject(root, "body", body);
char *payload = cJSON_PrintUnformatted(root);
mosquitto_publish(mosq, NULL, "macmqtt", strlen(payload), payload, 0, false);
}*/
void mqtt_wirte() void mqtt_wirte()
{ {
int message=1; int message=1;
cJSON *root = cJSON_CreateObject(); cJSON *root = cJSON_CreateObject();
cJSON *body = cJSON_CreateObject(); cJSON *body = cJSON_CreateObject();
cJSON *head = cJSON_CreateObject(); cJSON *head = cJSON_CreateObject();
...@@ -149,9 +140,7 @@ void message_0()//message_type为0时候要改变的值 ...@@ -149,9 +140,7 @@ void message_0()//message_type为0时候要改变的值
void message_3(cJSON *body,cJSON *pwm_ctrl)//message_type为3,控制pwm void message_3(cJSON *body,cJSON *pwm_ctrl)//message_type为3,控制pwm
{ {
//pwm控制
// 提取 pin_ctrl_req 对象
//cJSON *pwm_ctrl = cJSON_GetObjectItem(body, "pwm_ctrl");
if (!cJSON_IsObject(pwm_ctrl)) if (!cJSON_IsObject(pwm_ctrl))
{ {
return; return;
...@@ -180,7 +169,6 @@ void message_3(cJSON *body,cJSON *pwm_ctrl)//message_type为3,控制pwm ...@@ -180,7 +169,6 @@ void message_3(cJSON *body,cJSON *pwm_ctrl)//message_type为3,控制pwm
void message_4(cJSON *body, cJSON *pin_setctrl) void message_4(cJSON *body, cJSON *pin_setctrl)
{ {
//cJSON *pin_setctrl = cJSON_GetObjectItem(body, "pin_setctrl");
if (!cJSON_IsObject(pin_setctrl)) if (!cJSON_IsObject(pin_setctrl))
{ {
return; return;
...@@ -217,7 +205,7 @@ void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_messag ...@@ -217,7 +205,7 @@ void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_messag
{ {
return; return;
} }
memcpy(payload_str, message->payload, message->payloadlen); memcpy(payload_str, message->payload, message->payloadlen);
payload_str[message->payloadlen] = '\0'; payload_str[message->payloadlen] = '\0';
...@@ -226,6 +214,7 @@ void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_messag ...@@ -226,6 +214,7 @@ void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_messag
if (json == NULL) if (json == NULL)
{ {
fprintf(stderr, "Error before: [%s]\n", cJSON_GetErrorPtr()); fprintf(stderr, "Error before: [%s]\n", cJSON_GetErrorPtr());
free(payload_str);
} }
else else
{ {
...@@ -233,12 +222,14 @@ void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_messag ...@@ -233,12 +222,14 @@ void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_messag
cJSON *head = cJSON_GetObjectItem(json, "head"); cJSON *head = cJSON_GetObjectItem(json, "head");
if (!cJSON_IsObject(head)) if (!cJSON_IsObject(head))
{ {
free(payload_str);
return; return;
} }
// 提取 message_type // 提取 message_type
cJSON *message_type = cJSON_GetObjectItem(head, "message_type"); cJSON *message_type = cJSON_GetObjectItem(head, "message_type");
if (!cJSON_IsNumber(message_type)) if (!cJSON_IsNumber(message_type))
{ {
free(payload_str);
return; return;
} }
cJSON *body = cJSON_GetObjectItem(json, "body"); cJSON *body = cJSON_GetObjectItem(json, "body");
...@@ -315,9 +306,7 @@ int mqtt_cycle(struct mosquitto *mosq) ...@@ -315,9 +306,7 @@ int mqtt_cycle(struct mosquitto *mosq)
//mosquitto_message_callback_set(mosq, NULL); //mosquitto_message_callback_set(mosq, NULL);
// 开始循环,处理网络流量 // 开始循环,处理网络流量
//mosquitto_loop_start(mosq);//后台处理 //mosquitto_loop_start(mosq);//后台处理
//mosquitto_subscribe_with_options(mosq, NULL, TOPIC, 0, MQTT_SUB_OPT_NO_LOCAL);
mosquitto_loop_forever(mosq, -1, 1); // -1 表示无限等待,1 表示处理消息的最大数量 mosquitto_loop_forever(mosq, -1, 1); // -1 表示无限等待,1 表示处理消息的最大数量
//mosquitto_message_retry_set(mosq, 10);
} }
int mqtt_clean(struct mosquitto *mosq) int mqtt_clean(struct mosquitto *mosq)
......
#ifndef __MQTT_H__ #ifndef __MQTT_H__
#define __MQTT_H__ #define __MQTT_H__
#include <mosquitto.h>
#include <cjson/cJSON.h>
#include<time.h>
#include <pthread.h>
#include "ip.h"
#include "serial.h"
#define CLIENT_ID "carorship123" #define CLIENT_ID "carorship123"
#define BROKER_ADDRESS "119.45.167.177" #define BROKER_ADDRESS "119.45.167.177"
#define BROKER_PORT 1883 #define BROKER_PORT 1883
extern char* TOPIC ;//="app2dev/controlcar0004" extern char* TOPIC ;//="app2dev/controlcar0004"
extern char* TOPIC2 ;//="dev2app/controlcar0004" extern char* TOPIC2 ;//="dev2app/controlcar0004"
extern char* TOPIC3;//= "controlcar0004" extern char* TOPIC3;//= "controlcar0004"
#define USERNAME "admin" // 替换为你的用户名 #define USERNAME "admin" // 替换为你的用户名
#define PASSWORD "admin" // 替换为你的密码 #define PASSWORD "admin" // 替换为你的密码
......
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include "opensh.h"
#include <string.h> #include <string.h>
#include <mosquitto.h>
#include <cjson/cJSON.h>
#include<time.h>
#include <pthread.h>
#include "ip.h"
#include "serial.h"
#include "mqtt.h" #include "mqtt.h"
#include "opensh.h"
char gwebcam[100]; char gwebcam[100];
......
#include <mosquitto.h>
#include <cjson/cJSON.h>
#include<time.h>
#include "ip.h"
#include "serial.h" #include "serial.h"
#include "mqtt.h" #include "mqtt.h"
#include "delay.h" #include "delay.h"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment