未验证 提交 77065bff 编写于 作者: S Sanket Wadekar 提交者: GitHub

Fixed espressif/esp-rainmaker#152 (#7171)

上级 01e22291
...@@ -31,12 +31,12 @@ LightSwitch switch_ch1 = {gpio_switch1, false}; ...@@ -31,12 +31,12 @@ LightSwitch switch_ch1 = {gpio_switch1, false};
LightSwitch switch_ch2 = {gpio_switch2, false}; LightSwitch switch_ch2 = {gpio_switch2, false};
//The framework provides some standard device types like switch, lightbulb, fan, temperature sensor. //The framework provides some standard device types like switch, lightbulb, fan, temperature sensor.
static Switch my_switch1("Switch_ch1", &gpio_relay1); static Switch my_switch1;
static Switch my_switch2("Switch_ch2", &gpio_relay2); static Switch my_switch2;
void sysProvEvent(arduino_event_t *sys_event) void sysProvEvent(arduino_event_t *sys_event)
{ {
switch (sys_event->event_id) { switch (sys_event->event_id) {
case ARDUINO_EVENT_PROV_START: case ARDUINO_EVENT_PROV_START:
#if CONFIG_IDF_TARGET_ESP32 #if CONFIG_IDF_TARGET_ESP32
Serial.printf("\nProvisioning Started with name \"%s\" and PoP \"%s\" on BLE\n", service_name, pop); Serial.printf("\nProvisioning Started with name \"%s\" and PoP \"%s\" on BLE\n", service_name, pop);
...@@ -44,7 +44,7 @@ void sysProvEvent(arduino_event_t *sys_event) ...@@ -44,7 +44,7 @@ void sysProvEvent(arduino_event_t *sys_event)
#else #else
Serial.printf("\nProvisioning Started with name \"%s\" and PoP \"%s\" on SoftAP\n", service_name, pop); Serial.printf("\nProvisioning Started with name \"%s\" and PoP \"%s\" on SoftAP\n", service_name, pop);
printQR(service_name, pop, "softap"); printQR(service_name, pop, "softap");
#endif #endif
break; break;
case ARDUINO_EVENT_WIFI_STA_CONNECTED: case ARDUINO_EVENT_WIFI_STA_CONNECTED:
Serial.printf("\nConnected to Wi-Fi!\n"); Serial.printf("\nConnected to Wi-Fi!\n");
...@@ -60,18 +60,18 @@ void write_callback(Device *device, Param *param, const param_val_t val, void *p ...@@ -60,18 +60,18 @@ void write_callback(Device *device, Param *param, const param_val_t val, void *p
const char *param_name = param->getParamName(); const char *param_name = param->getParamName();
if(strcmp(device_name, "Switch_ch1") == 0) { if(strcmp(device_name, "Switch_ch1") == 0) {
Serial.printf("Lightbulb = %s\n", val.val.b? "true" : "false"); Serial.printf("Lightbulb = %s\n", val.val.b? "true" : "false");
if(strcmp(param_name, "Power") == 0) { if(strcmp(param_name, "Power") == 0) {
Serial.printf("Received value = %s for %s - %s\n", val.val.b? "true" : "false", device_name, param_name); Serial.printf("Received value = %s for %s - %s\n", val.val.b? "true" : "false", device_name, param_name);
switch_state_ch1 = val.val.b; switch_state_ch1 = val.val.b;
(switch_state_ch1 == false) ? digitalWrite(gpio_relay1, LOW) : digitalWrite(gpio_relay1, HIGH); (switch_state_ch1 == false) ? digitalWrite(gpio_relay1, LOW) : digitalWrite(gpio_relay1, HIGH);
param->updateAndReport(val); param->updateAndReport(val);
} }
} else if(strcmp(device_name, "Switch_ch2") == 0) { } else if(strcmp(device_name, "Switch_ch2") == 0) {
Serial.printf("Switch value = %s\n", val.val.b? "true" : "false"); Serial.printf("Switch value = %s\n", val.val.b? "true" : "false");
if(strcmp(param_name, "Power") == 0) { if(strcmp(param_name, "Power") == 0) {
...@@ -80,9 +80,9 @@ void write_callback(Device *device, Param *param, const param_val_t val, void *p ...@@ -80,9 +80,9 @@ void write_callback(Device *device, Param *param, const param_val_t val, void *p
(switch_state_ch2 == false) ? digitalWrite(gpio_relay2, LOW) : digitalWrite(gpio_relay2, HIGH); (switch_state_ch2 == false) ? digitalWrite(gpio_relay2, LOW) : digitalWrite(gpio_relay2, HIGH);
param->updateAndReport(val); param->updateAndReport(val);
} }
} }
} }
void ARDUINO_ISR_ATTR isr(void* arg) { void ARDUINO_ISR_ATTR isr(void* arg) {
...@@ -92,8 +92,9 @@ void ARDUINO_ISR_ATTR isr(void* arg) { ...@@ -92,8 +92,9 @@ void ARDUINO_ISR_ATTR isr(void* arg) {
void setup() void setup()
{ {
uint32_t chipId = 0; uint32_t chipId = 0;
Serial.begin(115200); Serial.begin(115200);
// Configure the input GPIOs // Configure the input GPIOs
...@@ -102,7 +103,7 @@ void setup() ...@@ -102,7 +103,7 @@ void setup()
attachInterruptArg(switch_ch1.pin, isr, &switch_ch1, CHANGE); attachInterruptArg(switch_ch1.pin, isr, &switch_ch1, CHANGE);
pinMode(switch_ch2.pin, INPUT_PULLUP); pinMode(switch_ch2.pin, INPUT_PULLUP);
attachInterruptArg(switch_ch2.pin, isr, &switch_ch2, CHANGE); attachInterruptArg(switch_ch2.pin, isr, &switch_ch2, CHANGE);
// Set the Relays GPIOs as output mode // Set the Relays GPIOs as output mode
pinMode(gpio_relay1, OUTPUT); pinMode(gpio_relay1, OUTPUT);
pinMode(gpio_relay2, OUTPUT); pinMode(gpio_relay2, OUTPUT);
...@@ -112,20 +113,24 @@ void setup() ...@@ -112,20 +113,24 @@ void setup()
digitalWrite(gpio_relay2, DEFAULT_POWER_MODE); digitalWrite(gpio_relay2, DEFAULT_POWER_MODE);
digitalWrite(gpio_led, false); digitalWrite(gpio_led, false);
Node my_node; Node my_node;
my_node = RMaker.initNode("Sonoff Dual R3"); my_node = RMaker.initNode("Sonoff Dual R3");
//Initialize switch device
my_switch1 = Switch("Switch_ch1", &gpio_relay1);
my_switch2 = Switch("Switch_ch2", &gpio_relay2);
//Standard switch device //Standard switch device
my_switch1.addCb(write_callback); my_switch1.addCb(write_callback);
my_switch2.addCb(write_callback); my_switch2.addCb(write_callback);
//Add switch device to the node //Add switch device to the node
my_node.addDevice(my_switch1); my_node.addDevice(my_switch1);
my_node.addDevice(my_switch2); my_node.addDevice(my_switch2);
//This is optional //This is optional
RMaker.enableOTA(OTA_USING_PARAMS); RMaker.enableOTA(OTA_USING_PARAMS);
//If you want to enable scheduling, set time zone for your region using setTimeZone(). //If you want to enable scheduling, set time zone for your region using setTimeZone().
//The list of available values are provided here https://rainmaker.espressif.com/docs/time-service.html //The list of available values are provided here https://rainmaker.espressif.com/docs/time-service.html
// RMaker.setTimeZone("Asia/Shanghai"); // RMaker.setTimeZone("Asia/Shanghai");
// Alternatively, enable the Timezone service and let the phone apps set the appropriate timezone // Alternatively, enable the Timezone service and let the phone apps set the appropriate timezone
......
...@@ -21,11 +21,11 @@ static int gpio_switch = 16; ...@@ -21,11 +21,11 @@ static int gpio_switch = 16;
bool switch_state = true; bool switch_state = true;
//The framework provides some standard device types like switch, lightbulb, fan, temperaturesensor. //The framework provides some standard device types like switch, lightbulb, fan, temperaturesensor.
static Switch my_switch("Switch", &gpio_switch); static Switch my_switch;
void sysProvEvent(arduino_event_t *sys_event) void sysProvEvent(arduino_event_t *sys_event)
{ {
switch (sys_event->event_id) { switch (sys_event->event_id) {
case ARDUINO_EVENT_PROV_START: case ARDUINO_EVENT_PROV_START:
#if CONFIG_IDF_TARGET_ESP32S2 #if CONFIG_IDF_TARGET_ESP32S2
Serial.printf("\nProvisioning Started with name \"%s\" and PoP \"%s\" on SoftAP\n", service_name, pop); Serial.printf("\nProvisioning Started with name \"%s\" and PoP \"%s\" on SoftAP\n", service_name, pop);
...@@ -33,7 +33,7 @@ void sysProvEvent(arduino_event_t *sys_event) ...@@ -33,7 +33,7 @@ void sysProvEvent(arduino_event_t *sys_event)
#else #else
Serial.printf("\nProvisioning Started with name \"%s\" and PoP \"%s\" on BLE\n", service_name, pop); Serial.printf("\nProvisioning Started with name \"%s\" and PoP \"%s\" on BLE\n", service_name, pop);
printQR(service_name, pop, "ble"); printQR(service_name, pop, "ble");
#endif #endif
break; break;
default:; default:;
} }
...@@ -59,18 +59,21 @@ void setup() ...@@ -59,18 +59,21 @@ void setup()
pinMode(gpio_switch, OUTPUT); pinMode(gpio_switch, OUTPUT);
digitalWrite(gpio_switch, DEFAULT_POWER_MODE); digitalWrite(gpio_switch, DEFAULT_POWER_MODE);
Node my_node; Node my_node;
my_node = RMaker.initNode("ESP RainMaker Node"); my_node = RMaker.initNode("ESP RainMaker Node");
//Initialize switch device
my_switch = Switch("Switch", &gpio_switch);
//Standard switch device //Standard switch device
my_switch.addCb(write_callback); my_switch.addCb(write_callback);
//Add switch device to the node //Add switch device to the node
my_node.addDevice(my_switch); my_node.addDevice(my_switch);
//This is optional //This is optional
RMaker.enableOTA(OTA_USING_PARAMS); RMaker.enableOTA(OTA_USING_PARAMS);
//If you want to enable scheduling, set time zone for your region using setTimeZone(). //If you want to enable scheduling, set time zone for your region using setTimeZone().
//The list of available values are provided here https://rainmaker.espressif.com/docs/time-service.html //The list of available values are provided here https://rainmaker.espressif.com/docs/time-service.html
// RMaker.setTimeZone("Asia/Shanghai"); // RMaker.setTimeZone("Asia/Shanghai");
// Alternatively, enable the Timezone service and let the phone apps set the appropriate timezone // Alternatively, enable the Timezone service and let the phone apps set the appropriate timezone
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册