Example Code for STM32
This article describes how to use the WiFi IoT module with the STM32 development board, including sample code and library file addresses. It covers the configuration methods for MQTT, IFTTT, and ThingSpeak, providing practical references for developers looking to integrate wireless communication functions into STM32 applications.
Use with STM32
The WiFi IOT module can also be used with STM32 development board, and the functions achieved are the same. Here the details will be omitted. You can try it if you are interested.
WiFi IoT Module Library Address for STM32: https://github.com/DFRobot/DFRobot_WIFI_I0T_Module_STM32
Sample Code
#include "system.h"
#include "SysTick.h"
#include "led.h"
#include "usart.h"
#include "DFRobot_wifi_iot.h"
//wifi
#define WIFISSID "hitest" //WiFi name
#define WIFIPWS "12345678" //wifi password
//MQTT
//#define MQTT
#ifdef MQTT
#define SERVER "iot.dfrobot.com.cn" //server address
#define PORT "1883" //Port number
#define DEVICENAME "rHpr0RcWR" //User name
#define DEVICESECRET "9NtrAg5ZRz" //User login password
#define TOPIC "OSpwrHHMg" //Subscribe channel
#endif
//IFTTT
#define IFTTT
#ifdef IFTTT
#define IFTTTKEY "dtpfTlU3Wqa8y0HRh77xXE"
#define IFTTTEVENT "BBB"
#endif
//ThingSpeak
//#define THINGSPEAK
#ifdef THINGSPEAK
#define THINGSPEAKKEY "U01NPZTC2G9WTDNY"
#endif
int main()
{
u8 i=0;
SysTick_Init(72);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
LED_Init();
//USB print port, corresponding pin: RX to PA10, TX to PA9
USART1_Init(9600);
//IOT communication port, corresponding pin: RX to PB11, TX to PB10
USART3_Init(9600);
//Connect to wifi
connectWifi(WIFISSID,WIFIPWS);
//Access EASYIOT
#ifdef MQTT
mqtt(SERVER,PORT,DEVICENAME,DEVICESECRET,TOPIC);
#endif
#ifdef IFTTT
configIFTTT(IFTTTEVENT,IFTTTKEY);
#endif
#ifdef THINGSPEAK
configThingSpeak(THINGSPEAKKEY);
#endif
while(1){
#ifdef MQTT
//send data shen using MQTT
publish(TOPIC,"HI TANG");
#endif
#ifdef IFTTT
//Access IFTTT, send messages to IFTTT registered event
IFTTTSendMasage("100","78","78");
#endif
#ifdef THINGSPEAK
//Access thingSpeak, send messages to ThingSpeak
thingSpeakSendMasage("5000", "100");
#endif
i+=10;
if(i%20==0)
{
i=0;
led1=!led1;
}
delay_ms(100);
loop();
}
}
Was this article helpful?
