怎么样用索引去找数据“{}” 以及怎么样用App Inventor 去做一个app去发送信号控制
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <Ticker.h>
#include <ArduinoJson.h>
Ticker flipper;
int count = 0;
String led1, led2;
String led_shuju;
String shuju;
const char *ssid = "adan";
const char *password = "88888888";
const char *host = "183.230.40.33";
byte mark, a, b;
WiFiClient client;
const int tcpPort = 80;
uint8_t LED = 2;
int dakuohaodeweizhi;
int shujuchangdu;
void setup() {
Serial.begin(115200);
pinMode(LED, OUTPUT);
delay(10);
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.println(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
WiFi.setSleepMode(WIFI_LIGHT_SLEEP);
delay(2000);
flipper.attach(0.5, flip);
}
void loop() {
jian_Cha();
get_Shu_Ju();
post_Shu_Ju();
print_Shu_Ju();
}
void post_Shu_Ju()
{ a++;
if (client.connected()) {
b++;
String url = "http://api.heclouds.com/devices/736615010/datapoints?type=3";
client.print(String("POST ") + url + " HTTP/1.1\r\n");
client.print(String("api-key: ") + "Y3B9HobS89bRKlC9R4LAm6PnYaM=\r\n");
client.print(String("Host: ") + "api.heclouds.com\r\n");
String data = "{'led':" + String(b) + ",'led0':" + String(a) + "}";
client.print("Content-Length:" + String(data.length()) + "\r\n\r\n");
client.print(data);
}
delay(400);
}
void get_Shu_Ju()
{
if (client.connected()) {
String url = "http://api.heclouds.com/devices/736615010/datastreams/led1?type=3";
client.print(String("GET ") + url + " HTTP/1.1\r\n");
client.print(String("api-key: ") + "Y3B9HobS89bRKlC9R4LAm6PnYaM=\r\n");
client.print(String("Host: ") + "api.heclouds.com\r\n");
delay(400);
}
}
void print_Shu_Ju()
{
while (client.available())
{
shuju += char(client.read());
delay(2);
mark = 1;
}
if (mark == 1)
{
dakuohaodeweizhi = shuju.indexOf('{');
shuju.length();
led1 = String(shuju).substring(dakuohaodeweizhi, shuju.length());
delay(500);
jieXiJson();
shuju = "";
mark = 0;
}
}
void jian_Cha()
{
while (!client.connected())
{
if (!client.connect(host, tcpPort))
{
Serial.print("connection....");
delay(500);
}
}
}
void jieXiJson()
{
const size_t capacity = JSON_OBJECT_SIZE(3) + JSON_OBJECT_SIZE(4) + 250;
DynamicJsonDocument doc(capacity);
deserializeJson(doc, led1);
JsonObject data = doc["data"];
led_shuju = data["current_value"].as<String>();
Serial.println(led_shuju);
}
中断:
void flip() {
if (led_shuju == "1") {
digitalWrite(LED, LOW);
}
if (led_shuju == "0") {
digitalWrite(LED, HIGH);
}
}
|