/*
   Copyright (c) 2019 Boot&Work Corp., S.L. All rights reserved

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU Lesser General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <SimpleComm.h>
#include <WifiModule.h>

typedef struct {
  char name[15];
  uint8_t input;
  uint16_t value;
} data_t;

SimplePacket packet;
data_t data;

////////////////////////////////////////////////////////////////////////////////////////////////////
void setup() {
  Serial.begin(9600L);
  WifiModule.begin(115200UL);
  SimpleComm.begin();

  Serial.println("Wifi Module started");
}

////////////////////////////////////////////////////////////////////////////////////////////////////
void loop() {
  static uint32_t lastSent = 0UL;
  if (millis() - lastSent > 1000) {
    lastSent = millis();

    // Init data
    sprintf(data.name, "ABCD");
    data.input = 7;
    data.value = analogRead(I0_7);

    // Create packet from data
    packet.setData(&data, sizeof(data));

    // Send packet
    if (!SimpleComm.send(WifiModule, packet, 0)) {
      Serial.println("Send packet error");
    }
  }
}
