Created
January 11, 2024 18:06
-
-
Save celso/7422e9d6680f3e033bfbb26434d99ca2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void wifiInitialize() { | |
#if DEBUG_WIFI == 1 | |
SERIAL_PRINTLN(F("Initializing Wifi")); | |
#endif | |
pinMode(WIFI_RESET_PIN, OUTPUT); | |
digitalWrite(WIFI_RESET_PIN,HIGH); | |
wifiReset(); | |
Serial1.begin(9600); // used with the wifi | |
SimpleWifi.setUart(&Serial1); // Tell the wifi library that we are not using the SPIUart | |
SimpleWifi.begin(); | |
} | |
void wifiReset() { | |
// hardware reset wifly module | |
LEDBlink(LED_BUTTON_ID,CRGB::DeepPink); | |
digitalWrite(WIFI_RESET_PIN,LOW); | |
delay(1000); | |
LEDBlink(LED_BUTTON_ID,CRGB::DeepPink); | |
digitalWrite(WIFI_RESET_PIN,HIGH); | |
delay(2500); | |
} | |
// Configuration commands, starts with "=" means they're sent litterally, otherwise are prefixed by "set " | |
char *wifiInitCommands[]={ "sys iofunc 0x01", // turns off green light annoying blinking | |
"sys output 0x10", // turns off green light annoying blinking | |
"ip protocol 8", // TCP client only. | |
"comm remote 0", // This command sets the ASCII string that is sent to the remote TCP client when the TCP port is opened | |
"comm close 0", // This command sets the ASCII string that is sent to the local UART when the TCP port is closed | |
"wlan auth "WIFI_AUTH_TYPE // 0=open, 1=wep-128, 2=wpa1, 3=mixed wpa1&wpa2-psk, 4=wpa2-psk, 5=not used, 6=adhoc | |
}; | |
void wifiOff() { | |
wifiInitState=-1; // -1 state turns off wifi | |
wifiConnected=0; | |
wifiReset(); | |
} | |
void wifiOn () { | |
wifiConnected=0; | |
wifiInitState=0; | |
} | |
void wifiLoop() { | |
char c; | |
int i; | |
char* sn; | |
if(wifiConnected==1) { | |
return; | |
} | |
switch(wifiInitState) { | |
case -1: // do nothing, use this state to turn off wifi completely | |
break; | |
case 0: | |
wifiReset(); | |
wifiInitCommandsIndex=0; | |
wifiInitState++; | |
break; | |
case 1: | |
Serial1.print("$$$"); | |
delay(500); | |
wifiInitState++; | |
break; | |
case 2: | |
Serial1.println(""); | |
delay(500); | |
wifiInitState++; | |
// SimpleWifi.sendCommand("factory RESET"); // we can't do this, we lose command mode, to fix later | |
break; | |
case 3: | |
LEDBlink(LED_BUTTON_ID,CRGB::DeepPink); | |
strcpy(buffer,"set "); | |
strcat(buffer,wifiInitCommands[wifiInitCommandsIndex++]); | |
#if DEBUG_WIFI == 1 | |
SERIAL_PRINT(F("Wifi sending: ")); | |
SERIAL_PRINTLN(buffer); | |
#endif | |
SimpleWifi.sendCommand(buffer); | |
if(wifiInitCommandsIndex>=sizeof(wifiInitCommands)/sizeof(char *)) wifiInitState++; | |
break; | |
case 4: | |
EEPROM_readAnything(EE_WIFI_LOGIN,(byte *)buffer, 32); | |
EEPROM_readAnything(EE_WIFI_PASSWORD,(byte *)&buffer[32], 32); | |
#if DEBUG_WIFI == 1 | |
SERIAL_PRINT(F("SSID:")); | |
SERIAL_PRINTLN(buffer); | |
SERIAL_PRINT(F("Password:")); | |
SERIAL_PRINTLN(&buffer[32]); | |
#endif | |
if (SimpleWifi.join(buffer, &buffer[32], true)) { // Joining Wifi | |
#if DEBUG_WIFI == 1 | |
SERIAL_PRINTLN(F("Joined wifi")); | |
#endif | |
if(wifiNeedsRegistration==1) { // we need to complete this http://krops.io/pages/api/registration | |
strcpy(buffer,"/api/redeemtoken/"); | |
strcat(buffer,krops_username); | |
if(http.getInit(KROPS_API_ADDRESS,KROPS_API_PORT,buffer)) { | |
http.getSubmit(); | |
http.readHeader(); | |
i=0; | |
while((c = http.readResponse()) && i<MAX_STRING_LEN) { | |
buffer[i++]=c; | |
SERIAL_PRINT(c); | |
} | |
buffer[i]=0; | |
http.getClose(); | |
JsonHashTable root = parser.parseHashTable(buffer); | |
if (root.success()) { | |
sn = root.getString("sn"); | |
if(sn) { | |
#if DEBUG_WIFI == 1 | |
SERIAL_PRINT(F("Serial number is: ")); | |
SERIAL_PRINTLN(sn); | |
#endif | |
EEPROM_writeAnything(EE_UNIQUE_ID, (byte *)sn, 40); | |
wifiNeedsRegistration=0; | |
} | |
} | |
if (wifiNeedsRegistration == 1) { // Parsing failed or invalid token: could be an invalid JSON, or too many tokens | |
#if DEBUG_WIFI == 1 | |
SERIAL_PRINTLN(F("JSON parsing failed or invalid token")); | |
SERIAL_PRINTLN(buffer); | |
#endif | |
wifiInitState=0; // Start over | |
wifiFailCount++; | |
return; | |
} | |
} | |
} | |
EEPROM_readAnything(EE_UNIQUE_ID,(byte *)krops_username, 40); | |
krops_username[40]=0; // end string | |
#if DEBUG_WIFI == 1 | |
SERIAL_PRINT(F("Krops UID:")); | |
SERIAL_PRINTLN(krops_username); | |
#endif | |
wifiInitState++; | |
wifiFailCount=0; // success | |
} | |
else | |
{ | |
wifiInitState=0; // Start over | |
wifiFailCount++; | |
/* This might not be a good idea after all | |
if(wifiFailCount>=1) { // We have a problem, go into wireless config mode | |
wifiOff(); | |
machine.action=HS_SETUP; | |
} | |
*/ | |
#if DEBUG_WIFI == 1 | |
SERIAL_PRINTLN(F("Joining wifi failed")); | |
#endif | |
} | |
break; | |
case 5: | |
wifiConnected=1; // we're done! | |
machine.action=HS_RECONNECTED; // enter the HS_RECONNECTED routine | |
wifiInitState=0; // start again next time | |
break; | |
} | |
} | |
void wifiRegistrationStart(char *ssid,char *password,char *token) { | |
EEPROM_writeAnything(EE_WIFI_LOGIN, (byte *)ssid, 32); | |
EEPROM_writeAnything(EE_WIFI_PASSWORD, (byte *)password, 32); | |
wifiNeedsRegistration=1; // request /redeemtoken/ | |
strcpy(krops_username,token); // use krops_username temporarily with the redeem token (small hack to save memory) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment