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 "delay.h"
......
#ifndef __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];
......
#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"
struct ifaddrs *ifap, *ifa;
struct sockaddr_in *sa;
char ip_address[INET_ADDRSTRLEN];
struct ifaddrs *ifap, *ifa;
struct sockaddr_in *sa;
char ip_address[INET_ADDRSTRLEN];
int ipaddr() {
if (getifaddrs(&ifap) == -1) {
perror("getifaddrs() error");
return -1;
......
#ifndef __IP_H__
#define __IP_H__
#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>
extern char ip_address[];//INET_ADDRSTRLEN
int ipaddr();
extern char ip_address[INET_ADDRSTRLEN];
#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 "ip.h"
#include "mqtt.h"
#include "serial.h"
#include "delay.h"
#include "opensh.h"
//#include "mac.h"
void *serial_usart1(void *arg)
......@@ -25,16 +40,11 @@ void *AppExit(void *arg)//2s程序,出现意外2s自动停止
unsigned char bufpwm2[3]={gtypeTemp,2,0};
unsigned char bufpwm3[3]={gtypeTemp,3,0};
unsigned char bufpwm4[3]={gtypeTemp,4,0};
//unsigned char bufpwm5[3]={gtypeTemp,5,15};
Delay_Ms(0,100);
gPwmCount++;
//printf( "%d\n",gtypeTemp);
if(gPwmCount>20)
{
//printf( "%d\n",gtypeTemp);
gPwmCount=21;
//serial_Write(bufpwm5);
serial_Write(bufpwm1);
serial_Write(bufpwm2);
serial_Write(bufpwm3);
......@@ -55,6 +65,7 @@ void *Mqttbeat(void *arg)
refresh_cam();
Delay_Ms(20,0);
refresh_cam();
Delay_Ms(10,0);
while(1)
{
ipaddr();//获取ip
......@@ -89,7 +100,7 @@ void *Mqtt_onnect(void *arg)
Delay_Ms(5,0);
mqtt_init();//mqtt初始化
mqtt_create(mosq);//创建mqtt客户端
//mqtt_wirte();//mqtt心跳首次发送
mqtt_wirte();//mqtt心跳首次发送
while(1)
{
mqtt_create(mosq);
......@@ -107,7 +118,7 @@ int main(int argc, char *argv[]) {
TOPIC=argv[1];
TOPIC2=argv[2];
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");
......@@ -119,17 +130,10 @@ int main(int argc, char *argv[]) {
serial_Init();//串口初始化
// get_mac_address("eth0");//获取mac地址
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_end();
......
#ifndef __MAIN_H__
#define __MAIN_H__
#include "ip.h"
#include "mqtt.h"
#include "serial.h"
#include "delay.h"
pthread_t thread[5];
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))
{
if(pthread_create(&thread[0],NULL,serial_usart1,NULL)!=0)
{
perror("Failed to create thread 1");
......@@ -52,15 +44,14 @@ void *Mqttbeat(void *arg),void *serial_usart2(void *arg),void *opensh(void *arg)
}
}
// 等待线程结束
void thread_end()
{
// 等待线程1结束
pthread_join(thread[0], NULL);
// 等待线程2结束
pthread_join(thread[1], NULL);
// 等待线程1结束
pthread_join(thread[2], 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 "gps.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 gmessage_type=10086;
int rc=0;
struct mosquitto *mosq;
unsigned char gmodeTemp=0;
unsigned char gtypeTemp=0;
......@@ -21,6 +20,12 @@ unsigned char gpinTemp=0;
unsigned char gvalTemp=0;
unsigned char gvalt[4];
struct mosquitto *mosq;
time_t gStart;
char* TOPIC=NULL;
char* TOPIC2=NULL;
char* TOPIC3= NULL;
int mqtt_init()
{
// 初始化 mosquitto 库
......@@ -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()
{
int message=1;
cJSON *root = cJSON_CreateObject();
cJSON *body = cJSON_CreateObject();
cJSON *head = cJSON_CreateObject();
......@@ -149,9 +140,7 @@ void message_0()//message_type为0时候要改变的值
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))
{
return;
......@@ -180,7 +169,6 @@ void message_3(cJSON *body,cJSON *pwm_ctrl)//message_type为3,控制pwm
void message_4(cJSON *body, cJSON *pin_setctrl)
{
//cJSON *pin_setctrl = cJSON_GetObjectItem(body, "pin_setctrl");
if (!cJSON_IsObject(pin_setctrl))
{
return;
......@@ -226,6 +214,7 @@ void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_messag
if (json == NULL)
{
fprintf(stderr, "Error before: [%s]\n", cJSON_GetErrorPtr());
free(payload_str);
}
else
{
......@@ -233,12 +222,14 @@ void on_message(struct mosquitto *mosq, void *obj, const struct mosquitto_messag
cJSON *head = cJSON_GetObjectItem(json, "head");
if (!cJSON_IsObject(head))
{
free(payload_str);
return;
}
// 提取 message_type
cJSON *message_type = cJSON_GetObjectItem(head, "message_type");
if (!cJSON_IsNumber(message_type))
{
free(payload_str);
return;
}
cJSON *body = cJSON_GetObjectItem(json, "body");
......@@ -315,9 +306,7 @@ int mqtt_cycle(struct mosquitto *mosq)
//mosquitto_message_callback_set(mosq, NULL);
// 开始循环,处理网络流量
//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_message_retry_set(mosq, 10);
}
int mqtt_clean(struct mosquitto *mosq)
......
#ifndef __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 BROKER_ADDRESS "119.45.167.177"
......
#include <stdio.h>
#include <stdlib.h>
#include "opensh.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 "opensh.h"
char gwebcam[100];
......
#include <mosquitto.h>
#include <cjson/cJSON.h>
#include<time.h>
#include "ip.h"
#include "serial.h"
#include "mqtt.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