AC-SOCKET.ino 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. Description:Click button B to turn on the power. Click button A to turn off the power.
  3. */
  4. #include <Arduino.h>
  5. #include <M5Stack.h>
  6. #include "protocol.h"
  7. #include "modbus.h"
  8. volatile uint32_t tim = 0;
  9. uint32_t time_now = 0;
  10. uint8_t ucTestFlag = 0;
  11. bool ubCoilState = false;
  12. void setup() {
  13. M5.begin(true, false, true, false);
  14. M5.Power.begin();
  15. mb_init(0xac, 9600, &tim);
  16. Serial1.begin(9600, SERIAL_8N1, 16, 17);
  17. time_now = millis();
  18. // put your setup code here, to run once:
  19. }
  20. void loop() {
  21. M5.update();
  22. if(millis() - time_now > 60000UL) {
  23. time_now = millis();
  24. if(ucTestFlag) {
  25. if(ubCoilState) {
  26. char data_str[] = {0xAA, 5, 0x00, 0x00, 0x00, 0x00};
  27. mb_send_frame((uint8_t *)data_str, 6);
  28. } else {
  29. char data_str[] = {0xAA, 5, 0x00, 0x00, 0xff, 0x00};
  30. mb_send_frame((uint8_t *)data_str, 6);
  31. }
  32. ubCoilState = 1 - ubCoilState;
  33. }
  34. }
  35. if(M5.BtnA.wasPressed()) {
  36. char data_str[] = {0xAA, 5, 0x00, 0x00, 0x00, 0x00};
  37. mb_send_frame((uint8_t *)data_str, 6);
  38. }
  39. if(M5.BtnB.wasPressed()) {
  40. char data_str[] = {0xAA, 5, 0x00, 0x00, 0xff, 0x00};
  41. mb_send_frame((uint8_t *)data_str, 6);
  42. }
  43. if(M5.BtnC.wasPressed()) {
  44. ucTestFlag = 1 - ucTestFlag;
  45. }
  46. vTaskDelay(pdMS_TO_TICKS(10));
  47. // while(Serial1.available()) {
  48. // protocol_rec_put(Serial1.read());
  49. // micros();
  50. // }
  51. // put your main code here, to run repeatedly:
  52. }
  53. void mb_send_one_byte(uint8_t data) {
  54. Serial1.write(data);
  55. }
  56. void protocol_callback(CmdData cmd) {
  57. Serial.printf("got ... \r\n");
  58. }