LoRaSender.ino 8.28 KB
Newer Older
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
1
/*
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";

13 14 15
byte localAddress = 0xAA;     // address of this device
byte destination = 0xBB;      // destination to send to

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 29 30 31 32 33 34 35 36 37 38
  // 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);
  WiFi.begin("Thomas","viapiccardi47!!thomas");
  delay(100);

  byte count = 0;
  while(WiFi.status() != WL_CONNECTED && count < 10)
  {
    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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
  Heltec.display -> clear();
  if(WiFi.status() == WL_CONNECTED)
  {
    Heltec.display -> drawString(0, 0, "Connecting...OK.");
    Heltec.display -> display();
//    delay(500);
  }
  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

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

69 70 71 72 73 74
  LoRa.setTxPowerMax(10);
  //LoRa.enableCrc();
  LoRa.setPreambleLength(4);
  
  LoRa.onReceive(onReceive);
  LoRa.receive();
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
75

76 77 78
  //-------------------------------------
    
  WIFISetUp();
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
79 80
  // Start TCP (HTTP) server
  server.begin();
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
81

82
  //SETUP PIN
83 84 85 86
  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
87 88
}

89 90 91 92
String s;
unsigned long previousMillis = 0;
const long interval = 10000;

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

98
  if (WiFi.status() != WL_CONNECTED) {
99
    ESP.restart();
100 101
  }
  
102 103 104 105 106 107
  long rssi = WiFi.RSSI();

  if(rssi == 0)
  {
    ESP.restart();
  }
108
  
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
109 110 111 112 113
  // Check if a client has connected
  WiFiClient client = server.available();
  if (!client) {
    return;
  }
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
114

Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
115 116 117 118
  // Wait for data from client to become available
  while (client.connected() && !client.available()) {
    delay(1);
  }
119
  
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
120 121 122 123 124 125 126 127 128 129 130
  // 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
131

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

  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
138
    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
139
    s += ipStr;
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
140
    s += "";
141
    
142
  } else if (req.indexOf("/pin") >= 0) {
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
143
    pinRequest(req);
144
    lastCommandReceived = "";
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
145
    s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n";
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
146
    s += req;
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
147
    s += "";
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
148 149 150

    previousMillis = currentMillis;

151 152
  } else if (req.indexOf("/brightness") >= 0) {
    setBrightness(req);
153
    lastCommandReceived = "";
154 155 156 157 158 159 160
    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) {
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 238 239 240
  // 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();
  
  if (incoming.indexOf("r : ") == 0) {
    lastCommandReceived = incoming;
241 242
  }

243 244 245 246 247 248
  if (incoming.indexOf("p : ") == 0) {
    lastCommandReceived = incoming;
  }

  if (incoming.indexOf("b : ") == 0) {
    lastCommandReceived = incoming;
249
  }
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
250 251
}

252 253 254 255 256 257
//----------------------------------------------------------------------------------------------
//------------------------------------------ REQUEST -------------------------------------------
//----------------------------------------------------------------------------------------------

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

260
  String outgoing = "p : " + req;
261

262 263 264 265 266 267 268 269 270 271
  // 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();
    
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
272 273 274 275
  // put the radio into receive mode
  LoRa.receive();
}

276
void setBrightness(String req)
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
277 278
{
  req = req.substring(req.lastIndexOf('/') + 1);
279 280 281 282 283 284 285 286 287 288

  String outgoing = "b : " + req;

  // 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
289
  
290 291
  LoRa.endPacket();
    
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
292 293
  // put the radio into receive mode
  LoRa.receive();
294 295
}

296
void pinRequestRead(String req)
297 298
{
  req = req.substring(req.lastIndexOf('/') + 1);
299 300 301 302 303 304 305 306 307 308

  String outgoing = "r : " + req;

  // 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
309
  
310 311
  LoRa.endPacket();
    
312 313
  // put the radio into receive mode
  LoRa.receive();
Giacomo Lavermicocca's avatar
Giacomo Lavermicocca committed
314
}