我一直试图通过UDP将读取的ADC值中的UDP数据包发送到任何终端。不幸的是,我无法从ESP直接在UDP终端上获得任何输入。这是代码:
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <string>
using namespace std;
//////////DEFINITIONS/////////////
#define ADC A0 //macrodef for reading the adc
///////////WiFi&UDP CONFIG///////////////////
WiFiUDP UdpPilot; // UDP Object for pilot
char udpSendingPacket[255] = "Hello there!"; //tab to send packet to board
unsigned int Port = 4210; // UDP port to send to
IPAddress destin_IP(192, 168, 0, 197); //IP Adress to send to
const char *softApName = "xxxxxxxx";
const char * pass = "xxxxxxxx";
/////////////////VARIABLES////////////////
int adcValueReaded = 0; //val to read from ADC
char adcToString[255];
void setup() {
WiFi.mode(WIFI_STA); //Station mode on WiFiUdp
Serial.begin(9600); //Begin serial
Serial.println(WiFi.begin( softApName, pass ) ? "WiFi Connection Ready!" : "WiFi Connection Failed!");
Serial.println(UdpPilot.begin(Port) ? "Port listening Ready!" : "Port listening Failed!");
}
void loop() {
adcValueReaded = analogRead(ADC);
if(adcValueReaded >= 500){
itoa(adcValueReaded, adcToString, 10);
UdpPilot.beginPacket(destin_IP, Port);
UdpPilot.write(adcToString);
UdpPilot.endPacket();
Serial.println(adcToString);
Serial.println(adcValueReaded);
}
yield();
}
期待您的帮助。