2021-6-22 21:05:51 [显示全部楼层]
4208浏览
查看: 4208|回复: 1

[入门] 求大佬看看,帮帮孩子,invalid conversion from 'const char*'to uint...

[复制链接]
  1. #include <WiFi.h>
  2. #include <PubSubClient.h>
  3. #include <ArduinoJson.h>
  4. #define PIN_AO 34
  5. int BUILTIN_LED = 12;
  6. const char ssid[] = "wang";
  7. const char password[] = "wanggnaw";
  8. const char mqtt_server[] = "183.230.40.39";
  9. WiFiClient espClient;
  10. PubSubClient client(espClient);
  11. long lastMsg = 0;
  12. char msg_buf[200];
  13. int value = 0;
  14. char dataTemplete[]="{"shumtest":%d}";
  15. char msgJson[75];
  16. char debug_buf[200];
  17. int i;
  18. unsigned short json_len=0;
  19. uint8_t* packet_p;
  20. uint8_t debug_buffer_start_index = 0;
  21. void setup_wifi() {
  22. delay(10);
  23. Serial.println();
  24. Serial.print("Connecting to ");
  25. Serial.println(ssid);
  26. WiFi.begin(ssid, password);
  27. while (WiFi.status() != WL_CONNECTED) {
  28. delay(500);
  29. Serial.print(".");
  30. }
  31. randomSeed(micros());
  32. Serial.println("");
  33. Serial.println("WiFi connected");
  34. Serial.println("IP address: ");
  35. Serial.println(WiFi.localIP());
  36. }
  37. void callback(char* topic, byte* payload, unsigned int length) {
  38. Serial.print("Message arrived [");
  39. Serial.print(topic);
  40. Serial.print("] ");
  41. for (int i = 0; i < length; i++) {
  42. Serial.print((char)payload<i>);
  43. }
  44. Serial.println();
  45. if ((char)payload[0] == '1') { // 收到字节为 1
  46. digitalWrite(BUILTIN_LED, HIGH); // 将继电器引脚电平拉高
  47. //delay(1000);
  48. // digitalWrite(BUILTIN_LED, LOW); // 将继电器引脚电平拉低
  49. } else {
  50. // digitalWrite(BUILTIN_LED, HIGH); // 将继电器引脚电平拉高
  51. // delay(1000);
  52. digitalWrite(BUILTIN_LED, LOW); // 将继电器引脚电平拉低
  53. } }
  54. void reconnect() {
  55. // Loop until we're reconnected
  56. while (!client.connected()) {
  57. Serial.print("Attempting MQTT connection...");
  58. if (client.connect("736391491","441729","w")) {
  59. Serial.println("connected");
  60. client.publish("outTopic", "hello world");
  61. client.subscribe("inTopic");
  62. } else {
  63. Serial.print("failed, rc=");
  64. Serial.print(client.state());
  65. Serial.println(" try again in 5 seconds");
  66. delay(5000);
  67.    }
  68.   }
  69. }
  70. void setup() {
  71. pinMode(BUILTIN_LED, OUTPUT);
  72. pinMode(PIN_AO, INPUT);
  73. Serial.begin(115200);
  74. setup_wifi();
  75. client.setServer(mqtt_server, 6002);
  76. client.connect("736391491","441729","w");
  77. client.setCallback(callback);
  78. }
  79. void loop() {
  80. if (!client.connected()) {
  81. reconnect();
  82. }
  83. client.loop();
  84. int shum = analogRead(PIN_AO);
  85. Serial.println(shum);
  86. delay(500);
  87. long now = millis();
  88. if (now - lastMsg > 2000){
  89. lastMsg = now;
  90. ++value;
  91. snprintf(msgJson,40,dataTemplete,shum);
  92. json_len=strlen(msgJson);
  93. msg_buf[0]=char(0x03);
  94. msg_buf[1]=char(json_len>>8);
  95. msg_buf[2]=char(json_len&0xff);
  96. memcpy(msg_buf+3,msgJson,strlen(msgJson));
  97. msg_buf[3+strlen(msgJson)] = 0;
  98. client.publish("$dp",msg_buf,3+strlen(msgJson),false);
  99. }
  100. }</i>
复制代码

这是所有的代码


微信图片_20210622210515.jpg

hockel  中级技匠

发表于 2021-6-24 09:46:54

如果是上传数据到onenet物联网平台,可以用这串代码
ThuJune-202106248661..png
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4 备案 沪公网安备31011502402448

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail