发送传感器数据有困难

Difficulty with sending sensor data

我尝试通过 arduino 以太网屏蔽向客户端发送数据(python 在 PC 上) 我遇到的问题是,当我像例子一样阅读 arduino 中的引脚 A0 时,我得到 1023,但是当我将这个值发送到 python 时,我得到 49152...

arduino代码

#include <SPI.h>         
#include <Ethernet.h>
#include <EthernetUdp.h>

byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };   // Enter a MAC    address for your controller below.
IPAddress ip(192,168,0,101);
IPAddress gateway(192,168,0,254);
IPAddress subnet(255,255,255,0);
unsigned int UDPport = 5000;// local port to listen for UDP packets
IPAddress UDPServer(192,168,0,100); // destination device server

const int UDP_PACKET_SIZE= 48;
byte packetBuffer[ UDP_PACKET_SIZE]; //buffer to hold incoming and outgoing  packets 
unsigned int noChange = 0;
int UDPCount = 0;
EthernetUDP Udp;

unsigned long currentTime;
unsigned long secondTime;
unsigned long msPerSecond = 100UL;

float temperature;
float vitesse;
float charge;
float current;

void setup() 
{
Serial.begin(9600);
Ethernet.begin(mac,ip,gateway,gateway,subnet);
Udp.begin(UDPport);
delay(1500);
currentTime=millis();
secondTime = currentTime;
}



void loop()
{
currentTime = millis();
getUDPpacket();

if(currentTime - secondTime > msPerSecond) {

temperature = analogRead(0); //read analog input on pin A0
vitesse = analogRead(1); //read analog input on pin A1
charge = analogRead(2); //read analog input on pin A2
current = analogRead(3); //read analog input on pin A3
Serial.println(temperature);
sendUDPpacket(UDPServer); // send an NTP packet to a time server
secondTime += msPerSecond;
}
}

unsigned int udpCount = 0;
unsigned long sendUDPpacket(IPAddress& address)
{
udpCount++;
memset(packetBuffer, 0, UDP_PACKET_SIZE);            sprintf((char*)packetBuffer,"%u,%u,%u,%u",temperature,vitesse,charge,current);
Udp.beginPacket(address, UDPport);
Udp.write(packetBuffer,UDP_PACKET_SIZE);
Udp.endPacket(); 
}

void getUDPpacket() {
if ( Udp.parsePacket() ) {  

if(Udp.remoteIP() == UDPServer) {
  Serial.print(F("UDP IP OK  "));
}
else {
  Serial.println(F("UDP IP Bad"));
  return;
}
if(Udp.remotePort() == UDPport) {
  Serial.println(F("Port OK"));
}
else {
Serial.println(F("Port Bad"));
  return;
}

Udp.read(packetBuffer,UDP_PACKET_SIZE);  // read the packet into the buffer
Serial.print(F("Received: "));
Serial.println((char*)packetBuffer);
}
}

python代码

import socket
import time


UDP_IP = "192.168.0.100"
UDP_PORT = 5000


sock = socket.socket(socket.AF_INET, # Internet
                 socket.SOCK_DGRAM) # UDP

sock.bind((UDP_IP, UDP_PORT))

while True:
data, addr = sock.recvfrom(48)
print data
time.sleep(5)

我认为问题出在这一行

  sprintf((char*)packetBuffer,"%u,%u,%u,%u",temperature,vitesse,charge,current);

但是我不知道该怎么办

编译 C 代码时应启用所有警告(例如在 gcc 中使用 -Wall)。这样,当您尝试使用 %u 无符号整数格式说明符打印浮点数时,它会警告您,因为这会导致未定义的行为。

您应该 a) 将 temperaturevitessechargecurrent 转换为无符号整数,然后再将它们传递给 sprintf(),或者 b ) 将格式说明符更改为 %f 之类的内容,以便 sprintf() 知道需要 floating-point 数据。

您还应该包括 <stdio.h> 以便您的程序具有 sprintf() 的原型;我 假设 你所包含的那些 headers 包含与以太网和 Arduino 端口 IO 功能相关的原型和常量。