String funken, die Tausendste.

OP #7635243
Lesenswert?

Hallo zusammen:

Folgender Sketch wird zwar ohne Fehler kompiliert, funkt aber nur Nullen und CR/LF. Und zwar genau soviel Nullen, wie der String Buchstaben hat.

1
/* Generic ESP8266 Modul
2

3
   aus BasicHTTPClient.ino
4
   Created on: 27.03.2024
5
   liest den Status aus Shelly 3EM, filtert nach "POWER",ermittelt den Strom eines Moduls,
6
   schaltet die notwendige/zulässige Anzahl Module zu oder ab,
7
   und funkt das Ergebnis an einen ESP01 zur Anzeige am Monitor.
8
*/
9

10
#include <ESP8266WiFi.h>
11
#include <ESP8266WiFiMulti.h>
12
#include <ESP8266HTTPClient.h>
13
#include <WiFiClient.h>
14
#include <espnow.h>
15

16
ESP8266WiFiMulti WiFiMulti;
17

18
// REPLACE WITH RECEIVER MAC Address (ESP01:E1)
19
//uint8_t broadcastAddress[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
20
//uint8_t broadcastAddress[] = { 0xDC, 0x4F, 0x22, 0x26, 0x28, 0xA1 };//E2
21
//uint8_t broadcastAddress[] = { 0xC8, 0x2B, 0x96, 0x20, 0x85, 0x33 };//E5
22
uint8_t broadcastAddress[] = { 0x48, 0x3F, 0xDA, 0x4A, 0x27, 0x27 };  //Eb
23

24

25
//********
26
//01 11 21 22
27
String Verteil[11] = { "0000", "1000", "0100", "0010", "0001", "0110", "1110", "0011", "1011", "0111", "1111" };
28
//  00      10      11      21      22      23      33      34      44      54      55
29
//   0       1       1       2       2       3       3       4       4       5       5
30
//********
31

32
// Structure example to send data
33
// Must match the receiver structure
34
typedef struct struct_message 
35
{
36
  String d;
37
} struct_message;
38
struct_message myData;  //Create a struct_message called myData
39

40
String payload = "";
41
String x1;
42
String Ausgabe;
43
String Strom = "";
44
int Summe;
45
int Schwelle;
46
int Laststufe = 0;
47
int Limit;
48
float Ampere;
49
int s;
50
String inputString;
51

52
//*******************************
53

54
void setup() 
55
{
56
  pinMode(12, OUTPUT);
57
  pinMode(13, OUTPUT);
58
  pinMode(14, OUTPUT);
59
  pinMode(16, OUTPUT);
60
  WiFi.mode(WIFI_STA);
61
  WiFiMulti.addAP("shellyem3-3494547B8D61", "");
62

63
  Serial.begin(115200);  // Init Serial Monitor
64

65
  if (esp_now_init() != 0)  // Init ESP-NOW
66
  {Serial.println("Error initializing ESP-NOW"); return;}
67

68
  esp_now_set_self_role(ESP_NOW_ROLE_CONTROLLER);                      // Once ESPNow is successfully Init,
69
  esp_now_register_send_cb(OnDataSent);                                //  we will register for Send CB
70
                                                                       //toget the status of Trasnmitted packet
71
  esp_now_add_peer(broadcastAddress, ESP_NOW_ROLE_SLAVE, 1, NULL, 0);  // Register peer
72
}
73

74

75
//*******************************
76

77
void loop() {
78
  if ((WiFiMulti.run() == WL_CONNECTED)) {
79
    WiFiClient client;
80
    HTTPClient http;
81
    if (http.begin(client, "http://192.168.33.1/status")) {
82
      int httpCode = http.GET();  // start connection and send HTTP header
83
      if (httpCode > 0) {
84
        if (httpCode == HTTP_CODE_OK || httpCode == HTTP_CODE_MOVED_PERMANENTLY) {
85
          String payload = http.getString();
86
          Summe = 0;
87
          Ausgabe = "";
88
          for (s = 400; s < 750; s++)  // zwischen Stelle 400 und 750 stehen die Werte für die 3 Phasen
89
          {
90
            String A = payload.substring(s, s + 5);
91
            if (A == "power") {
92
              s = s + 7;
93
              //**************************
94
              x1 = payload.substring(s, s + 10);
95
              Ausgabe += "   ";
96
              Ausgabe += x1.toInt();
97
              Summe = Summe + x1.toInt();
98
              s = s + 10;
99
              //**************************
100
            }
101
          }
102
          Auswertung();
103
          Ausgabe = Verteil[Laststufe] + " " + Strom + " " + Ampere + " " + Limit + "       " + Summe + " " + Schwelle + Ausgabe;
104
          
105

106

107
          //myData.d = Ausgabe;
108
          //strcpy(myData.d,  Ausgabe);    // Set values to send
109
           //strcpy(myData.d, (char*) &Ausgabe);
110

111

112
          Serial.println(Ausgabe);
113
          
114
          esp_now_send(broadcastAddress, (uint8_t *)&Ausgabe, sizeof(Ausgabe));
115

116

117
          delay(1000);
118
        }
119
      }
120
    }
121
  }
122
}
123

124
//***************************************
125

126
void Auswertung() {
127
  Strom = analogRead(A0);
128
  Ampere = ((Strom.toInt() - 512) / 512.000 * 60);  // ADC-Ausgabe umgerechnet in Ampere
129
  if (Ampere < .01) { Ampere = .01; }
130
  Limit = 2 * 16 / Ampere;
131
  if (Limit > 10) { Limit = 10; }
132
  Schwelle = (Schwelle * 3 + Summe) / 4;
133
  if (Schwelle <= 0 and Laststufe > 0) { Laststufe -= 1; }
134
  if (Schwelle >= 200 and Laststufe < 10) { Laststufe += 1; }
135
  if (Laststufe > Limit) { Laststufe = Limit; }
136

137

138
  //Schalten: (LOW: MODUL abgeklemmt, HIGH: MODUL zugeschaltet)
139
  if (Verteil[Laststufe].substring(0, 1) == "0") { digitalWrite(12, LOW); }
140
  if (Verteil[Laststufe].substring(1, 2) == "0") { digitalWrite(13, LOW); }
141
  if (Verteil[Laststufe].substring(2, 3) == "0") { digitalWrite(14, LOW); }
142
  if (Verteil[Laststufe].substring(3, 4) == "0") { digitalWrite(16, LOW); }
143

144
  if (Verteil[Laststufe].substring(0, 1) != "0") { digitalWrite(12, HIGH); }
145
  if (Verteil[Laststufe].substring(1, 2) != "0") { digitalWrite(13, HIGH); }
146
  if (Verteil[Laststufe].substring(2, 3) != "0") { digitalWrite(14, HIGH); }
147
  if (Verteil[Laststufe].substring(3, 4) != "0") { digitalWrite(16, HIGH); }
148
}
149

150
//*************************************
151

152
void OnDataSent(uint8_t *mac_addr, uint8_t sendStatus)  // Callback when data is sent
153
{
154
  if (sendStatus == 0) 
155
  { 
156
    Serial.println("Last Packet Send Status: Delivery success");
157
  } 
158
  else                 
159
  {
160
    Serial.println("Last Packet Send Status: Delivery fail");
161
  }
162
}

Das Problem ist wohl wieder ab Zeile 113, einen String so zuzubereiten, daß ESP_NOW den akzeptiert zum Funken. Ich kapier das nicht. Wer kann mir helfen?

Gast #7635248
Lesenswert?

Ein String ist ein komplexes C++ Objekt, kein Array von Bytes. Deine Typumwandlung bei der Ausgabe ist sinnlos. Du zeigst damit auf das Objekt, erzählst dem Compiler aber, dass es ein Zeiger auf ein Byte-Array sei.

Siehe https://www.arduino.cc/reference/en/language/variables/data-types/string/functions/getbytes/

Interessant ist auch String::begin() und due identische c_str() Methode, aber diese sind auf arduino.cc nicht dokumentiert, warum auch immer.

Aktiviere in den Einstellungen alle Compiler Warnungen. Oft sind sie hilfreich. Wenn mann sauber programmiert gat, bekommt man keine einzige Warnung. Insofern lohnt es sich, jeder einzelnen Warnung nach zu gehen.

Antwort schreiben

Bitte melde dich an, um einen Beitrag zu schreiben.

oder

Mit Google-Account einloggen

Die Registrierung ist kostenlos und dauert nur eine Minute.

Jetzt registrieren