使用 Arduino,我将如何使用 MultiWii 串行协议从飞行控制器中的陀螺仪获取姿态?
我希望能够将数据从网页发送到 esp8266 并控制引脚,每当我按下网页上的按钮时,它都会返回 ESP 的 i 地址和CONNECTION REFUSED. 我究竟做错了什么?
ESP代码:
void setup() {
Serial.begin(115200);
delay(100);
Serial.println();
Serial.println();
Serial.print("Connecting to: ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while( WiFi.status() != WL_CONNECTED){
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
Serial.print("Netmask: ");
Serial.println(WiFi.subnetMask());
Serial.print("Gateway: ");
Serial.println(WiFi.gatewayIP());
}
int value = 0;
void loop() {
HTTPClient http;
http.begin("192.168.0.24:80");
int httpCode = http.GET();
if(httpCode > 0){
String payload = http.getString();
Serial.println(payload);
}
http.end();
delay(3000);
}
Run Code Online (Sandbox Code Playgroud)
HTML网站:
<html>
<head>
<title>ESP8266 toggle page</title>
</head>
<body> …Run Code Online (Sandbox Code Playgroud) 我需要将十六进制字符串转换为十进制值。我使用了以下方法。但有时它返回错误的十进制值。
十六进制字符串的格式为“00 00 0C 6E”
unsigned long hexToDec(String hexString) {
unsigned long decValue = 0;
int nextInt;
for (long i = 0; i < hexString.length(); i++) {
nextInt = long(hexString.charAt(i));
if (nextInt >= 48 && nextInt <= 57) nextInt = map(nextInt, 48, 57, 0, 9);
if (nextInt >= 65 && nextInt <= 70) nextInt = map(nextInt, 65, 70, 10, 15);
if (nextInt >= 97 && nextInt <= 102) nextInt = map(nextInt, 97, 102, 10, 15);
nextInt = constrain(nextInt, 0, 15); …Run Code Online (Sandbox Code Playgroud) 我正在研究由 Pratik Desai(令人讨厌的聪明人)编写的“Python Programming for Arduino”一书。
我陷入了练习,学生正在学习实现一个滑块,该滑块可以改变连接到引脚的 LED 的强度。我标记了代码不能正常工作的地方。
代码是:
import tkinter
from pyfirmata import ArduinoMega
from time import sleep
port = '/dev/ttyACM0'
board = ArduinoMega(port)
sleep(5)
lenPin = board.get_pin('d:11:o')
top = tkinter.Tk()
top.title('Specify time using Entry')
top.minsize(300, 30)
timePeriodEntry = tkinter.Entry(top, bd=5, width=25)
brightnessScale = tkinter.Scale(top, from_=0, to=100,
orient=tkinter.HORIZONTAL)
brightnessScale.grid(column=2, row=2)
tkinter.Label(top, text='Time (seconds)').grid(column=1, row=1)
tkinter.Label(top, text='Brightness (%)').grid(column=1, row=2)
def onStartPress():
time_period = timePeriodEntry.get()
time_period = float(time_period)
ledBrightness = brightnessScale.get()
ledBrightness = float(ledBrightness)
startButton.config(state=tkinter.DISABLED)
lenPin.write(ledBrightness / 100.0) # this …Run Code Online (Sandbox Code Playgroud) 在SPIFFS中的文件内,我将以"XX:XX:XX:XX:XX:XX"的形式保存有关mac地址的信息.当我读取文件时,我需要将它从STRING切换为十六进制值数组.
uint8_t* str2mac(char* mac){
uint8_t bytes[6];
int values[6];
int i;
if( 6 == sscanf( mac, "%x:%x:%x:%x:%x:%x%*c",&values[0], &values[1], &values[2],&values[3], &values[4], &values[5] ) ){
/* convert to uint8_t */
for( i = 0; i < 6; ++i )bytes[i] = (uint8_t) values[i];
}else{
/* invalid mac */
}
return bytes;
}
wifi_set_macaddr(STATION_IF, str2mac((char*)readFileSPIFFS("/mac.txt").c_str()));
Run Code Online (Sandbox Code Playgroud)
但我在某处的代码中错了
当我AA:00:00:00:00:01输入文件时,我的ESP8266设置29:D5:23:40:00:00
我需要帮助,谢谢
问题出在数组上。声明数组的那行没有问题,是它后面的那一行。似乎我无法像在 C 中那样编码。为了清晰起见,我如何在需要这种格式时修复它。我不想写很长的一行,因为这个数组中有 15 个变量。
这是代码
#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
#define SERVOMIN 150 // this is the 'minimum' pulse length count (out of 4096)
#define SERVOMAX 600 // this is the 'maximum' pulse length count (out of 4096)
///// void setup /////
void setup() {
Serial.begin(9600);
pwm.begin();
pwm.setPWMFreq(60); // Analog servos run at ~60 Hz updates
delay(10);
}
///// Coordonnees /////
int angles[5][3]; //angles de chaque moteur pour chaque cube
angles[0][0]=60;
angles[0][1]=120;
angles[0][2]=100;
Run Code Online (Sandbox Code Playgroud)
基本上它angles[0][0]=;是导致问题的和与它相似的线。
我有一个小问题,希望有一个简单的答案。在 C/C++ 中对 Arduino 进行编程时,行“DDRB |= 0b00101000;” 发生。虽然我知道 DDRB 是端口 B 的数据方向寄存器以及“0b00”(即插槽 13 到 9)之后数字的含义,但我仍然不知道“0b00”是什么意思。
在定义中,我只读它意味着高(而 0b11 意味着低)但这意味着什么?
完整代码:
#include <avr/io.h>
#include <util/delay.h>
int main (void) {
float seconds = 0.5;
int time = 1000 * seconds;
DDRB |= 0b00101000;
while (1) {
PORTB |= 0b00001000;
_delay_ms(time);
PORTB &= 0b11110111;
PORTB |= 0b00100000;
_delay_ms(time);
PORTB &= 0b11011111;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我想在静态类中有一个静态(常量)向量。向量不会填充新元素,它只会包含在 class configuration: 中定义的元素{1,2,3,4,5}。
我怎样才能使向量debugLevels静态?
#include <ArduinoSTL.h>
class configuration {
public:
static std::vector<int> const debugLevels = {1,2,3,4,5}; // throws error: in-class initialization of static data member 'const std::vector<int> configuration::debugLevels' of incomplete type
};
void setup() {
for(int i=0; i<configuration::debugLevels.size(); i++ {
// do some stuff here...
}
}
void loop() {
}
Run Code Online (Sandbox Code Playgroud) 所以我知道millis() 返回自程序开始运行以来经过的时间,对吗?
现在我遇到了这样的情况,在延迟中使用了 millis():
long dly = millis();
while (millis() - dly < 250) {
yield(); // enough time to send response
}
Run Code Online (Sandbox Code Playgroud)
的值怎么可能millis() - dly大于0?如果关键是无限期让步,为什么有人会使用这样的延迟?
arduino ×10
c++ ×6
esp8266 ×2
android ×1
arduino-c++ ×1
arduino-ide ×1
arduino-uno ×1
arrays ×1
bluetooth ×1
decimal ×1
esp32 ×1
firmata ×1
get ×1
hex ×1
kotlin ×1
python ×1
python-3.x ×1
send ×1
types ×1