手把手教你使用GoKit和GC211(4G模块)来实现4G网络的开发。包括模组接入、设备控制、数据采集,云端部署,APP开发等方面内容。难度较低,适合新手入门。
教程内容:
1. 将gokit2和gokit3实现4G网络模块GC211的接入2. STM32作为主控实现GOKIT全功能控制和采集3. arduino作为主控实现GOKIT全功能控制和采集4. 新版本APP快速开发
5. 网页控制GOKIT开发板
材料准备:1,Gokit2 或 GoKit3一块
2,GC211 一块(基于ML103的4G模组)
3,杜邦线若干4,电脑一台5,机智云开发平台账号(点击注册)
可根据自己需求选择对应的功能板和底板,其中gokit3支持SOC模式。
板子电路连接: 参考下图(如图看不清,参考附件中的原图):

云端部署
1.进入新版本开发者中心https://developer.gizwits.com/menu/dashboard,点击左上角创建,选择园艺农林,养殖监控(如果是wifi模式,请选择其他,微信宠物屋gokit,gokit3)。

2.如图创建一个产品,产品名称可以根据自己定义

3.将附件中的《数据点.xlsx》文件中的数据点导入产品之中。如果右上角没有管理按钮,那么先随便创建一个数据点之后就有了,之后就可以
管理→导入模板
4.在MCU开发菜单分别生成Arduino代码和STM32代码并进行下载。可按照个人需求和手里面的开发板选择要生成的代码,此处我生成2种代码。




STM32代码移植编写
1.下载STM32驱动文件,机智云下载中心下载http://goms-1251025085.cosgz.myq ... 8-1513076127294.rar(也可以在附件中获取)

2.复制STM32驱动文件里面的STM32F103C8x.ioc到下载的代码文件中替换该文件。

3.复制驱动库代码_CubeMX版文件夹里面的驱动文件到下载代码的\Hal文件夹。

4.打开下载代码里面的STM32F103C8x.ioc,需要在电脑上安装CUBEMX,安装方法自行百度。打开之后如下图所示。(此处个人提出建议,任何软件创建项目,不要包含中文路径,否则将会导致很多不可控问题,包括此软件。)

5.点击右上角generate code生成keil代码。

6.生成代码之后用keil打开代码,添加驱动文件到项目

7.gizwits_product.c文件修改
添加相关的头文件

gizwitsEventProcess函数修改,包含各个传感器的驱动。对RGB LED开启红色的控制。 if(0x01 ==currentDataPoint.valueLED_OnOff) { ledRgbControl(254,0,0); } else { ledRgbControl(0,0,0); }
break;

其他颜色和电机的修改 case EVENT_LED_Color: currentDataPoint.valueLED_Color =dataPointPtr->valueLED_Color; GIZWITS_LOG("Evt: EVENT_LED_Color%d\n", currentDataPoint.valueLED_Color); switch(currentDataPoint.valueLED_Color) { case LED_Color_VALUE0: ledRgbControl(currentDataPoint.valueLED_R,currentDataPoint.valueLED_G,currentDataPoint.valueLED_B); break; case LED_Color_VALUE1: ledRgbControl(254, 254, 0); break; case LED_Color_VALUE2: ledRgbControl(254,0, 70); break; case LED_Color_VALUE3: ledRgbControl(238,30, 30); break; default: break; } break;
case EVENT_LED_R: currentDataPoint.valueLED_R =dataPointPtr->valueLED_R; GIZWITS_LOG("Evt:EVENT_LED_R%d\n",currentDataPoint.valueLED_R); ledRgbControl(currentDataPoint.valueLED_R,currentDataPoint.valueLED_G,currentDataPoint.valueLED_B); break; case EVENT_LED_G: currentDataPoint.valueLED_G =dataPointPtr->valueLED_G; GIZWITS_LOG("Evt:EVENT_LED_G%d\n",currentDataPoint.valueLED_G); ledRgbControl(currentDataPoint.valueLED_R,currentDataPoint.valueLED_G,currentDataPoint.valueLED_B); break; case EVENT_LED_B: currentDataPoint.valueLED_B =dataPointPtr->valueLED_B; GIZWITS_LOG("Evt:EVENT_LED_B%d\n",currentDataPoint.valueLED_B); ledRgbControl(currentDataPoint.valueLED_R,currentDataPoint.valueLED_G,currentDataPoint.valueLED_B); break; case EVENT_Motor_Speed: currentDataPoint.valueMotor_Speed =dataPointPtr->valueMotor_Speed; GIZWITS_LOG("Evt:EVENT_Motor_Speed%d\n",currentDataPoint.valueMotor_Speed); motorStatus(currentDataPoint.valueMotor_Speed); break;

如使用wifi方案,在WIFI_CON_ROUTER加上关灯操作。case WIFI_CON_ROUTER:
ledRgbControl(0, 250, 0);

userInit函数修改,增加各个驱动函数的初始化,数据点初始化memset((uint8_t*)¤tDataPoint, 0,sizeof(dataPoint_t)); delay_init(72); // 延时 初始化 rgbLedInit(); // RGB LED 初始化 dht11Init(); // 温湿度初始化 irInit(); // 红外初始化 motorInit(); // 电机初始化 motorStatus(0); // 电机转速初始化 /** Warning !!! DataPoint Variables Init ,Must Within The Data Range **/
currentDataPoint.valueLED_OnOff =0; currentDataPoint.valueInfrared =0; currentDataPoint.valueAlert_1 = 0; currentDataPoint.valueAlert_2 = 0; currentDataPoint.valueFault_LED =0; currentDataPoint.valueFault_Motor= 0; currentDataPoint.valueFault_TemHum= 0; currentDataPoint.valueFault_IR =0; currentDataPoint.valueLED_Color =0; currentDataPoint.valueLED_R = 0; currentDataPoint.valueLED_G = 0; currentDataPoint.valueLED_B = 0; currentDataPoint.valueMotor_Speed= 0; currentDataPoint.valueTemperature= 0; currentDataPoint.valueHumidity =0;

userHandle函数的修改,各个数据点的上传,传感器采集。 uint8_t ret = 0; static uint32_t thLastTimer = 0; ///< 新添加代码: 红外传感器数据获取 currentDataPoint.valueInfrared =irHandle(); ///< 新添加代码: 温湿度传感器数据获取 if((gizGetTimerCount()-thLastTimer) > 2000) //上报间隔2S { ret = dht11Read((uint8_t*)¤tDataPoint.valueTemperature,(uint8_t*)¤tDataPoint.valueHumidity); if(ret != 0) { GIZWITS_LOG("Failedtoread DHT11 [%d] \n", ret); } thLastTimer =gizGetTimerCount(); }

8.将#include "main.h" 添加到hal下面的各个sensor驱动之中,否则sensor将会提示没有引脚定义报错。





9. main.c修改,增加头文件#include"hal_temp_hum.h"#include"hal_rgb_led.h"#include"hal_motor.h"#include"Hal_infrared.h"
#include"delay.h"

main.c 文件的 key2ShortPress( ) 函数与 key2LongPress( ) 函数中添加长/短按key2时的LED点亮代码。voidkey2ShortPress(void){GIZWITS_LOG("KEY2PRESS ,Soft APmode\n");#if!MODULE_TYPE gizwitsSetMode(WIFI_SOFTAP_MODE); #endif //Soft AP mode,RGB 红灯 ledRgbControl(250,0, 0);}voidkey2LongPress(void){ //AirLink mode GIZWITS_LOG("KEY2PRESS LONG,AirLink mode\n");#if!MODULE_TYPE gizwitsSetMode(WIFI_AIRLINK_MODE); #endif //AirLink mode,RGB 绿灯 ledRgbControl(0,250, 0);},

10.生成bin文件。魔术棒→user→$K\ARM\ARMCC\bin\fromelf.exe--bin --output=@L.bin !L

11.编译项目

12.STM32开发板集成串口下载,打开串口烧录软件mcuisp.exe,附件中可下载。

选择开发板串口

将如图所示开关拔到system

选择生成的固件文件

点击开始编程

烧录完成

烧录完成之后将开关拔回flash,复位模组,就可以正常运行程序。

13.打开串口调试助手.exe,附件中可以下载。监听串口出现如下图所示日志代表程序烧录成功。

14.接上功能板和4G模组(插上普通4G卡)。云端设备上线。

15.打开机智云串口调试助手.exe,附件中或者官网下载中心可以下载。工具,二设备二维码,按照如图生成二维码。


16.使用机智云App扫码即可绑定设备,官网下载中心可以下载APP



到此STM32移植完成
Arduino代码移植编写
1.下载Arduino驱动文件,机智云下载中心下载http://goms-1251025085.cosgz.myq ... 6-1502093919120.zip(也可以在附件中获取)

2.将下载的驱动文件解压,将芯片驱动文件复制到安装好的arduino软件的..\libraries目录下。

将下载的代码进行解压,复制Gizwits到arduino 软件的.. \libraries目录下.

3. 使用arduino软件打开下载自动生成代码下载的文件中MCU_ArduinoUNOR3_source\examples\simpleTry\simpleTry.ino ,然后添加各驱动库的头文件,以及全局变量。#include<DHT.h>#include<ChainableLED.h>#include<MsTimer2.h>/***************************HAL define ***************************/#defineInfrared_PIN 2 ///< 红外IO管脚#define DHTPIN3 ///< 温湿度IO管脚#defineMOTOR_PINA 4 ///< 电机IO管脚#defineMOTOR_PINB 5 ///< 电机IO管脚#define KEY1 6///< 按键IO管脚#define KEY2 7///< 按键IO管脚//温湿度功能值定义#define DHTTYPEDHT11//电机功能值定义#defineMOTOR_MAX 100#defineMOTOR_MAX1 ‐100#defineMOTOR_MIN 0#defineMOTOR_16//按键功能值定义#defineKEY1_SHORT_PRESS 1#defineKEY1_LONG_PRESS 2#defineKEY2_SHORT_PRESS 4#defineKEY2_LONG_PRESS 8#define NO_KEY0#defineKEY_LONG_TIMER 3unsigned longLast_KeyTime = 0;uint8_tNetConfigureFlag = 0;uint8_tgaterSensorFlag = 0;typedef enum{LED_Costom =0x00,LED_Yellow =0x01,LED_Purple =0x02,LED_Pink =0x03,}LED_ColorTypeDef;DHTdht(DHTPIN, DHTTYPE);ChainableLEDleds(A5, A4, 1)
STM32代码移植的教程:https://mp.weixin.qq.com/s/Q-9wkRTlaQi_ra8HLsbS5g
下载代码以及相关文档:
链接:https://pan.baidu.com/s/17UMSLF3mqhy_V5VboIFDhw
提取码:z5ty