LoRaSender.ino 8.2 KB
Newer Older
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
1
/*
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
2

3 4
  LoRa Sender commands & store the response
  Http comunications with outside
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
5

Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
6
*/
7
#include <heltec.h>
8
#include <WiFiClientSecure.h>
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
9 10 11 12

const char* ssid = "Thomas";
const char* password = "viapiccardi47!!thomas";

Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
13 14
byte localAddress = 0xDE;     // address of this device
byte destination = 0xDD;      // destination to send to
15

Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
16 17 18
// TCP server at port 80 will respond to HTTP requests
WiFiServer server(80);

19
String lastCommandReceived = "";
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
20
String signalLevel = "";
21

22
void WIFISetUp(void)
23
{
24 25 26 27 28
  // Set WiFi to station mode and disconnect from an AP if it was previously connected
  WiFi.disconnect(true);
  delay(1000);
  WiFi.mode(WIFI_STA);
  WiFi.setAutoConnect(true);
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
29
  WiFi.begin("Thomas", "viapiccardi47!!thomas");
30 31 32
  delay(100);

  byte count = 0;
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
33
  while (WiFi.status() != WL_CONNECTED && count < 10)
34 35 36 37 38
  {
    count ++;
    delay(500);
    Heltec.display -> drawString(0, 0, "Connecting...");
    Heltec.display -> display();
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
39
  }
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
40

41
  Heltec.display -> clear();
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
42
  if (WiFi.status() == WL_CONNECTED)
43 44 45
  {
    Heltec.display -> drawString(0, 0, "Connecting...OK.");
    Heltec.display -> display();
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
46
    //    delay(500);
47 48 49 50 51 52 53 54 55 56 57 58 59 60
  }
  else
  {
    Heltec.display -> clear();
    Heltec.display -> drawString(0, 0, "Connecting...Failed");
    Heltec.display -> display();
    //w                       hile(1);
  }
  Heltec.display -> drawString(0, 10, "WIFI Setup done");
  String ipAddr = WiFi.localIP().toString();
  Heltec.display -> drawString(0, 20, ipAddr);
  Heltec.display -> display();
  delay(500);
}
61

Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
62 63
#define BAND 868E6

64 65
void setup(void)
{
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
66
  Heltec.begin(true /*DisplayEnable Enable*/, true /*LoRa Enable*/, false /*Serial Enable*/, true /*LoRa use PABOOST*/, BAND /*LoRa RF working band*/);
67
  Heltec.display -> clear();
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
68

69
  Serial.println("LoRa Sender");
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
70

Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
71
  LoRa.setTxPowerMax(15);
72 73
  //LoRa.enableCrc();
  LoRa.setPreambleLength(4);
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
74

75 76
  LoRa.onReceive(onReceive);
  LoRa.receive();
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
77

78
  //-------------------------------------
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
79

80
  WIFISetUp();
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
81 82
  // Start TCP (HTTP) server
  server.begin();
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
83

84
  //SETUP PIN
85 86 87 88
  pinMode(17, INPUT);             // set pin to input
  digitalWrite(17, INPUT_PULLUP); // turn on pullup resistors
  pinMode(21, INPUT);             // set pin to input
  digitalWrite(21, INPUT_PULLUP); // turn on pullup resistors
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
89 90
}

91 92
String s;
unsigned long previousMillis = 0;
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
93
const long interval = 25000;
94

95
int val = 0;
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
96
void loop(void)
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
97
{
98
  unsigned long currentMillis = millis();
99

100
  if (WiFi.status() != WL_CONNECTED) {
101
    ESP.restart();
102
  }
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
103

104 105
  long rssi = WiFi.RSSI();

Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
106
  if (rssi == 0)
107 108 109
  {
    ESP.restart();
  }
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
110

Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
111 112 113 114 115
  // Check if a client has connected
  WiFiClient client = server.available();
  if (!client) {
    return;
  }
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
116

Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
117 118 119 120
  // Wait for data from client to become available
  while (client.connected() && !client.available()) {
    delay(1);
  }
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
121

Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
122 123 124 125 126 127 128 129 130 131 132
  // Read the first line of HTTP request
  String req = client.readStringUntil('\r');

  // First line of HTTP request looks like "GET /path HTTP/1.1"
  // Retrieve the "/path" part by finding the spaces
  int addr_start = req.indexOf(' ');
  int addr_end = req.indexOf(' ', addr_start + 1);
  if (addr_start == -1 || addr_end == -1) {
    return;
  }
  req = req.substring(addr_start + 1, addr_end);
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
133

134
  currentMillis = millis();
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
135 136 137 138 139

  if (req == "/")
  {
    IPAddress ip = WiFi.localIP();
    String ipStr = String(ip[0]) + '.' + String(ip[1]) + '.' + String(ip[2]) + '.' + String(ip[3]);
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
140
    s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\nHello from ESP32 at ";
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
141
    s += ipStr;
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
142
    s += "";
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
143

144
  } else if (req.indexOf("/pin") >= 0) {
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
145
    pinRequest(req);
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
146
    s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n";
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
147
    s += req;
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
148
    s += "";
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
149 150 151

    previousMillis = currentMillis;

152 153 154 155 156 157 158 159 160
  } else if (req.indexOf("/brightness") >= 0) {
    setBrightness(req);
    s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n";
    s += req;
    s += "";

    previousMillis = currentMillis;

  } else if (req.indexOf("/read") >= 0) {
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
161

Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
162
    pinRequestRead(req);
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
163
    s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n";
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
164
    s += req;
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
165
    s += "";
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
166 167 168

    previousMillis = currentMillis;

169
  } else if (req.indexOf("/check") >= 0) {
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
170 171 172
    //OK send s to browser

    if (currentMillis - previousMillis >= interval) {
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
173
      s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n";
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
174
      s += "Timeout command";
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
175
      s += "";
176
    } else {
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
177
      s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\nL.C. : ";
178
      s += lastCommandReceived;
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
179
      s += "";
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
180
    }
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
181 182 183
  } else {
    s = "HTTP/1.1 404 Not Found\r\n\r\n";
  }
184

Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
185
  client.print(s);
186
  client.stop();
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
187 188
}

189 190 191
//----------------------------------------------------------------------------------------------
//--------------------------------------- LORA RECEIVE -----------------------------------------
//----------------------------------------------------------------------------------------------
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
192

193 194 195 196 197 198 199
// code = 0 --> null
// code = 11 --> pinInput 1 --> read sensor
// code = 12 --> pinInput 2
// code = 13 --> pinInput 3
// code = 21 --> pinOutput 1 --> write to
// code = 22 --> pinOutput 2
// code = 23 --> pinOutput 3
200
String str;
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
201 202
void onReceive(int packetSize)
{
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
  if (packetSize == 0) return;          // if there's no packet, return

  // read packet header bytes:
  int recipient = LoRa.read();          // recipient address
  byte sender = LoRa.read();            // sender address
  byte incomingLength = LoRa.read();    // incoming msg length

  String incoming = "";                 // payload of packet

  while (LoRa.available())             // can't use readString() in callback
  {
    incoming += (char)LoRa.read();      // add bytes one by one
  }

  if (incomingLength != incoming.length())   // check length for error
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
218
  {
219 220
    Serial.println("error: message length does not match length");
    return;                             // skip rest of function
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
221 222
  }

223 224 225 226 227
  // if the recipient isn't this device or broadcast,
  if (recipient != localAddress)
  {
    Serial.println("This message is not for me.");
    return;                             // skip rest of function
228
  }
229

230 231 232 233 234 235 236 237
  // if message is for this device, or broadcast, print details:
  Serial.println("Received from: 0x" + String(sender, HEX));
  Serial.println("Sent to: 0x" + String(recipient, HEX));
  Serial.println("Message length: " + String(incomingLength));
  Serial.println("Message: " + incoming);
  Serial.println("RSSI: " + String(LoRa.packetRssi()));
  Serial.println("Snr: " + String(LoRa.packetSnr()));
  Serial.println();
238

Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
239
  if (incoming.indexOf("r : ") == 0) {
240
    lastCommandReceived = incoming;
241
  }
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
242 243
}

244 245 246 247 248 249
//----------------------------------------------------------------------------------------------
//------------------------------------------ REQUEST -------------------------------------------
//----------------------------------------------------------------------------------------------

void pinRequest(String req)
{
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
250 251
  req = req.substring(req.lastIndexOf('/') + 1);

252
  String outgoing = "p : " + req;
253

Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
254 255 256 257 258 259 260 261 262 263 264 265 266 267
  for (int i = 0; i < 5; i++) {
    // send packet
    LoRa.beginPacket();

    LoRa.write(destination);              // add destination address
    LoRa.write(localAddress);             // add sender address
    LoRa.write(outgoing.length());        // add payload length
    LoRa.print(outgoing);                 // add payload

    LoRa.endPacket();
    delay(2000);
  }

  lastCommandReceived = outgoing;
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
268 269
}

270
void setBrightness(String req)
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
271 272
{
  req = req.substring(req.lastIndexOf('/') + 1);
273 274 275

  String outgoing = "b : " + req;

Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
276 277 278 279 280 281 282 283 284 285 286 287 288 289
  for (int i = 0; i < 5; i++) {
    // send packet
    LoRa.beginPacket();

    LoRa.write(destination);              // add destination address
    LoRa.write(localAddress);             // add sender address
    LoRa.write(outgoing.length());        // add payload length
    LoRa.print(outgoing);                 // add payload

    LoRa.endPacket();
    delay(2000);
  }

  lastCommandReceived = outgoing;
290 291
}

292
void pinRequestRead(String req)
293 294
{
  req = req.substring(req.lastIndexOf('/') + 1);
295 296 297

  String outgoing = "r : " + req;

Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
298 299 300 301 302 303 304 305 306 307 308 309 310
  for (int i = 0; i < 5; i++) {
    // send packet
    LoRa.beginPacket();

    LoRa.write(destination);              // add destination address
    LoRa.write(localAddress);             // add sender address
    LoRa.write(outgoing.length());        // add payload length
    LoRa.print(outgoing);                 // add payload

    LoRa.endPacket();
    delay(2000);
  }

311 312
  // put the radio into receive mode
  LoRa.receive();
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
313
}