void loop() { if (tick >= 5) // publish to topic every 5seconds { tick = 0; const size_t bufferSize = JSON_OBJECT_SIZE(2) + 20; DynamicJsonBuffer jsonBuffer(bufferSize); JsonObject &root = jsonBuffer.createObject(); root["payload"] = msgCount++; root["thing_id"] = thing_id; String json_output; root.printTo(json_output); char payload[bufferSize]; json_output.toCharArray(payload, bufferSize); sprintf(payload, json_output.c_str()); if (aws_iot.publish(aws_mqtt_thing_topic_pub, payload) == 0) { Serial.print("Publish Message:"); Serial.println(payload); } else { Serial.println("Publish failed"); } } vTaskDelay(1000 / portTICK_RATE_MS); tick++; }
//*********************************************************************** bool ICACHE_FLASH_ATTR timeSync::processTimeStamp( String &str ) { staticThis->debugMsg( SYNC, "processTimeStamp(): str=%s\n", str.c_str()); DynamicJsonBuffer jsonBuffer(50 ); JsonObject& timeStampObj = jsonBuffer.parseObject(str); if ( !timeStampObj.success() ) { staticThis->debugMsg( ERROR, "processTimeStamp(): out of memory1?\n" ); return false; } num = timeStampObj.get<uint32_t>("num"); times[num] = timeStampObj.get<uint32_t>("time"); adopt = timeStampObj.get<bool>("adopt"); num++; if ( num < TIME_SYNC_CYCLES ) { str = buildTimeStamp(); return true; } else { return false; } }
bool parseCommand(char* command) { dbgf(debug, F(":parse cmd:%s\n"), command); if (strstr(command, "AT") == command) { // this is AT command for ESP8266 dbgf(debug, F(":ESP8266:%s\n"), command); esp8266.write(command); return true; } DynamicJsonBuffer jsonBuffer(JSON_MAX_SIZE * 2); JsonObject& root = jsonBuffer.parseObject(command); if (root.success()) { if (root[F("m")] == F("csr")) { // CSR tsLastStatusReport = tsCurr - STATUS_REPORTING_PERIOD_SEC; } else if (root[F("m")] == F("cfg")) { // CFG JsonObject& sensor = root[F("s")].asObject(); if (sensor.containsKey(F("id")) && sensor.containsKey(F("cf"))) { uint8_t id = sensor[F("id")].as<uint8_t>(); double cf = sensor[F("cf")].as<double>(); saveSensorCF(id, cf); } else if (root.containsKey(F("rap"))) { sprintf(WIFI_REMOTE_AP, "%s", root[F("rap")].asString()); eepromWriteCount += EEPROM.updateBlock(WIFI_REMOTE_AP_EEPROM_ADDR, WIFI_REMOTE_AP, sizeof(WIFI_REMOTE_AP)); } else if (root.containsKey(F("rpw"))) { sprintf(WIFI_REMOTE_PW, "%s", root[F("rpw")].asString()); eepromWriteCount += EEPROM.updateBlock(WIFI_REMOTE_PW_EEPROM_ADDR, WIFI_REMOTE_PW, sizeof(WIFI_REMOTE_PW)); } else if (root.containsKey(F("sip"))) { sscanf(root[F("sip")], "%d.%d.%d.%d", (int*) &SERVER_IP[0], (int*) &SERVER_IP[1], (int*) &SERVER_IP[2], (int*) &SERVER_IP[3]); eepromWriteCount += EEPROM.updateBlock(SERVER_IP_EEPROM_ADDR, SERVER_IP, sizeof(SERVER_IP)); } else if (root.containsKey(F("sp"))) { SERVER_PORT = root[F("sp")].as<int>(); eepromWriteCount += EEPROM.updateInt(SERVER_PORT_EEPROM_ADDR, SERVER_PORT); } else if (root.containsKey(F("dsp"))) { int sp = root[F("dsp")].as<int>(); if (sp == -1) { debug = NULL; } else if (sp == 1) { debug = serial; } esp8266.setDebug(debug); } else { reportConfiguration(); } } } else { return false; } return true; }